flex有问题请教
%x CCOMMENT
%x LCOMMENT
W [ \t\b\f\r]+
%%
^"#line"[ ]+\"[^\"]*\"[ ]++.* { line_directive(); }
^"`line"[ ]++[ ]+\"[^\"]*\".* { line_directive2(); }
[ \t\b\f\r] { ; }
\n { yylloc.first_line += 1; }
/* C++ style comments start with / / and run to the end of the
current line. These are very easy to handle. */
"//".* { comment_enter = YY_START; BEGIN(LCOMMENT); }
<LCOMMENT>. { yymore(); }
<LCOMMENT>\n { yylloc.first_line += 1; BEGIN(comment_enter); }
/* The contents of C-style comments are ignored, like white space. */
"/*" { comment_enter = YY_START; BEGIN(CCOMMENT); }
<CCOMMENT>. { yymore(); }
<CCOMMENT>\n { yylloc.first_line += 1; yymore(); }
<CCOMMENT>"*/" { BEGIN(comment_enter); }
请问这里标志的CCOMMENT什么意思,如何理解上面所识别的语句,请高手解释下?? %x表示该状态下,只匹配该状态的内容,默认下(前面没有状态名的任何匹配)的任何东西不计入匹配.
例如,
如果前面有个
%x STAT1
当前状态为STAT1
有个通用的匹配为
xxxxx {.....}
则这一段匹配不起作用
当前只认识
<STAT1>xxxxx{.....}
这样的匹配 看来版主在这方面很有造诣,小弟最近正在学这方面的知识,希望能得到指点一二:lol: 另问一下:
1.flex与lex,bison和yacc其用法是否是兼容的。
2.下面语句片断,执行是如何进行的?
^"#line"[ ]+\"[^\"]*\"[ ]++.* { line_directive(); }
^"`line"[ ]++[ ]+\"[^\"]*\".* { line_directive2(); }
[ \t\b\f\r] { ; }
\n { yylloc.first_line += 1; }
"//".* { comment_enter = YY_START; BEGIN(LCOMMENT); }
<LCOMMENT>. { yymore(); }
<LCOMMENT>\n { yylloc.first_line += 1; BEGIN(comment_enter); }
一个疑问是,上面语句片断的执行是否是这样的:第一步
1.匹配]^"#line"[ ]+\"[^\"]*\"[ ]++.* ,
得到匹配时执行动作 line_directive();
第二步
2.匹配^"`line"[ ]++[ ]+\"[^\"]*\".*
得到匹配时执行动作 line_directive2();
第三步
3.匹配[ \t\b\f\r]
得到匹配时执行动作 ;
4.匹配\n,
得到匹配时执行动作 yylloc.first_line += 1;
5.匹配"//".*
得到匹配时执行动作 comment_enter = YY_START; BEGIN(LCOMMENT);
6.匹配<LCOMMENT>.
得到匹配时执行动作 yymore();
7..匹配<LCOMMENT>\n
得到匹配时执行动作yylloc.first_line += 1; BEGIN(comment_enter); }
其中,
"//".* { comment_enter = YY_START; BEGIN(LCOMMENT); }
<LCOMMENT>. { yymore(); }
<LCOMMENT>\n { yylloc.first_line += 1; BEGIN(comment_enter); }
是否等价于匹配注释串,
"//".+\n
但是用条件LCOMMENT的作用在于可以将动作分解。 最大程度的匹配谁就用谁 遵循自顶至低最大匹配原则的
楼上两位
说的最大程度的匹配是不是指的"贪吃蛇"的算法! 我还是说错了。LEX语言应该是自顶向下匹配。
越上优先级越高。
页:
[1]