gcc在编译lex输出时在printf上给出错误 [英] gcc giving error on printf while compiling lex output

查看:241
本文介绍了gcc在编译lex输出时在printf上给出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于example.l lex文件,我得到以下错误.如果我注释掉printf,它就会消失.尽管我在lex规范的顶部可以包含%{%}之间的任意C代码.我需要能够在lex匹配任何内容之前打印一些输出.我所做的事情有什么问题以及如何解决?

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

推荐答案

如果在这里查看代码,您会发现这里发生了两件事之一……或者您正在函数内部执行#include正文没有意义,或者您在任何函数外部调用printf(),这同样是错误的.

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.

现在,当您考虑到这是flex时,它就是后者.您可能正在拍摄更像这样的东西:

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;
}

这篇关于gcc在编译lex输出时在printf上给出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆