Bison / flex: look at the next letter or token

When you are dealing with strings (it has its own state, for example, comments), I need to find out if the next letter is “or not”. If this is not the end of the line. So what happens, I just don’t end the string in my state of the string (I use <STRING_STATE>.and process it letter by letter). So, what happens, I note that the last line was "and if the current does not exit the state and produces the last letter.

This has a weird effect. When I get errors in lines with lines, I see the letter twice (usually "," or ")"). and if it is at the end of the line, the side effect will be considered two lines! (even if there is no error).

How can i solve this? my only option is to create a global var and mark it when I left the state of the string and hacked YY_USer to fix myself? I kind of want to avoid this. To be cleaner, I could just look at the next letter or token, is this possible?

+3
source share
1 answer

In flex, you can look ahead into future tokens using the /lookahead operator . so the rule is like

ab/cd

"ab" , "cd" . , flex "cd" , , yytext "ab", , "cd"

+9

Source: https://habr.com/ru/post/1790973/


All Articles