Lexer rule (regex) for TeX equation

ぐ巨炮叔叔 提交于 2020-01-16 09:03:09

问题


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?

I tried:

equation  \$[^\$]*\$

without a success.


回答1:


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 $ */
}        

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.



来源:https://stackoverflow.com/questions/14717920/lexer-rule-regex-for-tex-equation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!