Posted on 2013-05-11 23:58 
S.l.e!ep.¢% 阅读(3175) 
评论(2)  编辑 收藏 引用  所属分类: 
yacc 
			 
			
		 
		没接触过语法解析, 今天在网上看到的一个simple demo,随手试了一下
一开始在 sourceforge 下载了 flex, 结果发现在里面没有找到 vc 的工程 
结果另外找了 flex+gcc+tools 直接拿了 flex.exe 使用
simple.l 文件

 %
% {
{
 #include <stdio.h>
#include <stdio.h>
 %}
%}

 %%
%%
 begin printf("started\n");
begin printf("started\n");
 hello printf("hello yourself\n");
hello printf("hello yourself\n");
 thanks printf("your welcome\n");
thanks printf("your welcome\n");
 end printf("stopped\n");
end printf("stopped\n");
 %%
%%然后用VC建了个工程,编译之
结果出现这个错了
lex.yy.obj : error LNK2019: 无法解析的外部符号 _yywrap,该符号在函数 _yylex 中被引用
x:\Debug\test.exe : fatal error LNK1120: 1 个无法解析的外部命令
难道是需要加入lib?
结果在 simple.l 最前面加上
%option noyywrap    
表示不连接  yywrap 库, 再生成一次
结果是另一个错
error LNK2019: 无法解析的外部符号 _main,该符号在函数 ___tmainCRTStartup 中被引用
看了它生成的  lex.yy.c 
#if YY_MAIN    <---- 它这里有个开关
int main()
 {
 yylex();
 return 0;
 }
#endif
于是在 %option noyywrap    的基础上猜想,应该有另一个 option 的开关让他生成  main() 函数吧, 度娘一搜果然找到了
%option main 
结果还惊奇的发现 
%option main 是默认打开  %option noyywrap    的
完整的 simple.l 如下
 %option main
%option main 


 %
% {
{
 #include <stdio.h>
#include <stdio.h>
 %}
%}

 %%
%%
 begin printf("started\n");
begin printf("started\n");
 hello printf("hello yourself\n");
hello printf("hello yourself\n");
 thanks printf("your welcome\n");
thanks printf("your welcome\n");
 end printf("stopped\n");
end printf("stopped\n");
 %%
%%完美的编译通过
后记:
一开始,出现 error LNK2019: 无法解析的外部符号 _yywrap,该符号在函数 _yylex 中被引用 这个错误时,一直在找 flex 的 windows 版本的lib但一直没找着
后来在网上看了别人写的.l 文件有%option noyywrap ,并且没有链接 flex的lib, 果断试了下.