野牛/弯曲:窥视下一个字母或标记 [英] bison/flex: Peek at the next letter or token

查看:91
本文介绍了野牛/弯曲:窥视下一个字母或标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在处理字符串时(它有自己的状态,例如注释),我需要找出下一个字母是否为.如果是,则不结束字符串状态.所以发生的是我只是不结束字符串状态处于我的字符串状态的字符串(我使用<STRING_STATE>.并逐个字母地对其进行处理.)然后发生的是,我标记了最后一个字符串是否为",并且如果当前不是,则退出该状态并取出最后一个字母.

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).

我该如何解决?创建全局变量并在我离开字符串状态并标记YY_USer进行自我修复时对其进行标记是我唯一的选择吗?我有点想避免这种情况.为了更清洁,我可以看看下一个字母或令牌,这可能吗?

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?

推荐答案

在flex中,您可以使用/ lookahead运算符前瞻"未来的令牌.这样的规则

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

ab/cd

仅当且仅当其后跟"cd"时,才会匹配"ab"输入.这意味着flex实际上与此规则匹配'cd',但是在调用该规则的动作之前将其推回输入缓冲区,因此yytext仅在看到它时包含'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

这篇关于野牛/弯曲:窥视下一个字母或标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆