bison/flex: Peek at the next letter or token

微笑、不失礼 提交于 2019-12-06 08:48:59

问题


When dealing with strings (it has its own state like comments) i need to find out if the next letter is a " or not. If it is i dont end the string state. So what happens is i just dont end the string in my string state (i use <STRING_STATE>. and process it letter by letter). So what happens is, i mark if the last string was " and if the current isnt i exit the state and unput the last letter.

This has a weird effect. When i get errors on lines with strings i see the letter (usually a ',' or ')') twice. and if it happens to be on the end of the line the side effect counts as two lines! (even if there isnt an error).

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


回答1:


In flex, you can "peek ahead" at future tokens by using the / lookahead operator. so a rule like

ab/cd

will match an 'ab' input if and only if its followed by a 'cd'. Which means that flex actually matches the 'cd' for this rule, but then pushes it back into the input buffer before calling your action for the rule, so yytext only contains 'ab' when you see it, and the 'cd' will be read again for the next token



来源:https://stackoverflow.com/questions/4946070/bison-flex-peek-at-the-next-letter-or-token

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