Flex BEGIN 条件激活规则

test.l:

%option noyywrap noline
%{
#include <iostream>
%}

%x textSpan str
%%

"$"               { BEGIN(textSpan); }
\"                { printf("string:");  BEGIN(str); }

<textSpan>\"      {  BEGIN(INITIAL); printf("textspan:"); BEGIN(str); }
<str>[^"]*\"      { BEGIN(INITIAL); printf("%s\n", std::string(yytext, yyleng-1).c_str()); }
%%

int main()
{
  yylex();
}
> .\test.exe
"xxx"
string:xxx

$"xxx"
textspan:xxx

"<*>"匹配任何模式

<*>"//".* { /* eat comment */ }

在任何开始条件下,都吃掉注释

See also:

posted @ 2021-05-17 16:40  Ajanuw  阅读(213)  评论(0编辑  收藏  举报