2012-02-06 65 views
1

對於example.l lex文件,我得到下面的錯誤。如果我註釋掉printf,它就會消失。我認爲lex規範的頂部部分可以包含%{%}之間的任意C代碼。我需要能夠在lex匹配任何東西之前打印一些輸出。我做了什麼錯,我該如何解決?gcc在編譯lex輸出時給出printf錯誤

$ cat example.l 

%{ 
#include <stdio.h> 
printf("foobar\n"); 
%} 

%% 

.  ECHO; 

$ lex example.l 
$ gcc -g -L/usr/lib/flex-2.5.4a -lfl -o example lex.yy.c 
example.l:3: error: expected declaration specifiers or '...' before string constant 
example.l:3: warning: data definition has no type or storage class 
example.l:3: error: conflicting types for 'printf' 
example.l:3: note: a parameter list with an ellipsis can't match an empty parameter name list declaration 

回答

1

如果你的代碼看這裏,你可以看到一兩件事情發生在這裏......無論你正在做一個#include的函數體沒有意義裏面,或者你在呼喚printf()以外的任何函數,這同樣是錯誤的。

現在,當你考慮到這是flex,它是後者。你可能是拍了一些更像這樣的東西:

%{ 
#include <stdio.h> 
%} 

%% 

.  ECHO; 

%% 

int main() { 
    printf("foobar\n"); 
    while (yylex() != 0); 
    return 0; 
} 
+0

啊,是的,這似乎是完全明顯的,現在你已經說了。謝謝! – 2012-02-06 21:47:05