开发者

bison/flex: Peek at the next letter or token

开发者 https://www.devze.com 2023-02-09 06:20 出处:网络
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 t开发者_如

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 t开发者_如何学Che 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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消