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 开发者_Go百科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?
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.
精彩评论