I've written a bison parser using flex and c. The parser is compiled without error, but when I run the executable file the main function is not executed (It doesn't print out the first line after the main declaration that is actually a print instruction)
我用flex和c编写了一个野牛解析器。解析器编译时没有错误,但是当我运行可执行文件时,不执行main函数(它不会在主声明之后打印出第一行,实际上是打印指令)
main( int argc, char *argv[] )
{
printf("*** C2P version 1.0 @2015***\n");
extern FILE *yyin;
++argv; --argc;
printf("Open C file %s...",argv[0]);
yyin = fopen( argv[0], "r" );
if (yyin==NULL) {
printf("ERROR file not found %s", argv[0]);
exit(1);
}
yydebug = 1; //enable debug
yyparse();
exit(0);
}
I've used the following commands for compiling:
我使用以下命令进行编译:
bison -d c_def.y
flex c_def.l
gcc c_def.tab.h lex.yy.c -o c2p -lfl
1 个解决方案
#1
2
This is wrong:
这是错的:
gcc c_def.tab.h lex.yy.c -o c2p -lfl
The bison parser is in the file c_def.tab.c
. c_def.tab.h
is just the header file containing the token definitions.
野牛解析器位于文件c_def.tab.c中。 c_def.tab.h只是包含令牌定义的头文件。
So there wouldn't be a main()
at all in c2p
if it were not for your inclusion of the flex library (-lfl
). That library includes a main
function which calls the lexer until it returns an end-of-file indicator. (It does not call the parser, which is why your parser is not being called.)
因此,如果不包含flex库(-lfl),则c2p中根本不会有main()。该库包含一个main函数,它调用词法分析器直到它返回文件结束指示符。 (它不会调用解析器,这就是为什么不调用解析器的原因。)
You probably shouldn't use -lfl
. Aside from the main()
function which you don't need, the only other thing it contains is a fake implementation of yywrap
which always returns 1; instead of relying on that, just include the option
你可能不应该使用-lfl。除了你不需要的main()函数之外,它包含的唯一另一个东西是yywrap的伪实现,它总是返回1;而不是依赖于它,只需包括选项
%option noyywrap
in your flex definition, and then your lexer won't depend on yywrap
at all.
在你的flex定义中,然后你的词法分析器根本不依赖于yywrap。
#1
2
This is wrong:
这是错的:
gcc c_def.tab.h lex.yy.c -o c2p -lfl
The bison parser is in the file c_def.tab.c
. c_def.tab.h
is just the header file containing the token definitions.
野牛解析器位于文件c_def.tab.c中。 c_def.tab.h只是包含令牌定义的头文件。
So there wouldn't be a main()
at all in c2p
if it were not for your inclusion of the flex library (-lfl
). That library includes a main
function which calls the lexer until it returns an end-of-file indicator. (It does not call the parser, which is why your parser is not being called.)
因此,如果不包含flex库(-lfl),则c2p中根本不会有main()。该库包含一个main函数,它调用词法分析器直到它返回文件结束指示符。 (它不会调用解析器,这就是为什么不调用解析器的原因。)
You probably shouldn't use -lfl
. Aside from the main()
function which you don't need, the only other thing it contains is a fake implementation of yywrap
which always returns 1; instead of relying on that, just include the option
你可能不应该使用-lfl。除了你不需要的main()函数之外,它包含的唯一另一个东西是yywrap的伪实现,它总是返回1;而不是依赖于它,只需包括选项
%option noyywrap
in your flex definition, and then your lexer won't depend on yywrap
at all.
在你的flex定义中,然后你的词法分析器根本不依赖于yywrap。