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
", std::string(yytext, yyleng-1).c_str()); }
%%

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

$"xxx"
textspan:xxx

"<*>"匹配任何模式

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

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

See also:

原文地址:https://www.cnblogs.com/ajanuw/p/14777528.html