本文介绍了TeX方程的Lexer规则(regex)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在TeX中,方程式在$...$
之间定义.我如何为lex定义词法分析器规则,例如两个美元符号之间的任意数量的任何字符?
In TeX, equations are defined in between $...$
. How can I define the lexer rule for lex, for the instance of any number of any characters between two dollar signs?
我尝试过:
equation \$[^\$]*\$
没有成功.
推荐答案
您可以尝试使用开始条件,如果您不希望将美元符号包括在等式中:
You can try using start conditions if you don't want the dollar signs to be included as part of the equation:
%x EQN
%%
\$ { BEGIN(EQN); } /* switch to EQN start condition upon seeing $ */
<EQN>{
\$ { BEGIN(INITIAL); } /* return to initial state upon seeing another $ */
[^\$]* { printf(yytext); } /* match everything that isn't a $ */
}
如果在词法分析器中定义了其他状态,则可以使用yy_push_state()
和yy_pop_state()
代替使用BEGIN(STATE)
.
Alternately instead of using BEGIN(STATE)
you can use yy_push_state()
and yy_pop_state()
if you have other states defined in your lexer.
这篇关于TeX方程的Lexer规则(regex)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!