gcc giving error on printf while compiling lex output

倾然丶 夕夏残阳落幕 提交于 2020-01-05 08:19:12

问题


For the example.l lex file I get the error below. If I comment out the printf it goes away. I though that the top section of the lex specification could contain any arbitrary C code between the %{ and %}. I need to be able to print some output before lex matches anything. What is wrong with what I have done and how do I fix it?

$ 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:


If you look at the code here, you can see that one of two things is happening here ... either you are doing a #include inside a function body which doesn't make sense, or you are calling printf() outside any function, which is equally wrong.

Now, when you take into account this is flex, it's the latter. You were probably shooting for something a little more like this:

%{
#include <stdio.h>
%}

%%

.       ECHO;

%%

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


来源:https://stackoverflow.com/questions/9166488/gcc-giving-error-on-printf-while-compiling-lex-output

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!