Only part of matched string in flex in yytext

浪尽此生 提交于 2020-07-09 03:07:36

问题


I'm a newbie but I would like to know if I can, with flex, parse something with regex in a way yytext would only be part of the matched sequence. For example: @abcd{efgh,. I would like to match abcd once and use it then efgh, but I need to use the @ and the { to match them. Is this possible or do I have to process it entirely later in C?


回答1:


You can use following context with the '/' operator. For eaxmple,

abcd/efgh

will match the string "abcd" only if its followed by "efgh", with the latter string being left on the input, so it will be (part of) the next matched token.

I'm not sure exactly what you're asking about with the "@" and "{" however -- do you want to match them but just ignore them, or do you want to return them as seperate tokens? For the former you could use "@abcd"/"{efgh" and then just use yytext+1 to get "abcd". The latter is more complex, but it can be done using flex's states. You might do something like:

%x at

%%

"@"        { BEGIN(at); return *yytext; }
<at>"abcd" { BEGIN(INITIAL); return ABCD; }

to match an "abcd" only if its immediately after an "@" that was matched by itself.

You can do lots of complex things in flex with start states, but generally trying to parse a non-regular language is a bad idea -- you're better off using a parsing tool such as bison for that.



来源:https://stackoverflow.com/questions/5345008/only-part-of-matched-string-in-flex-in-yytext

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