问题
In lex, I can define the following starting condition for equations defined between $...$.
%x EQN1
\$ { BEGIN(EQN1); }
<EQN1>{
\$ { BEGIN(INITIAL); }
[^\$]* {}
}
For equations between $$...$$, how can I define the anything but $$ rule, such as in [^\$]*. I guess [^\$\$]* wouldn't work.
回答1:
I think you don't understand the way the patterns are matched, see flex manual
Flex always try to match longest input possible. You can understand it in way, that longer rules have higher priority.
Because "\$\$" match two characters and "." just one, the example below will work just fine.
%x EQN2
\$\$ { BEGIN(EQN2); }
<EQN2>{
\$\$ { BEGIN(INITIAL); }
. {}
}
You can also replace [^\$]* {} with . {} in your example, because when rules match same size of input, the first one in lex.l has higher priority.
来源:https://stackoverflow.com/questions/14843564/start-condition-for-tex-equations