如何获取“预期令牌”在 bison/yacc GLR 解析器中?
如何在 bison/yacc GLR 解析器中获取“预期标记”?
你好,
在我正在做的项目中,有一些不明确的语法。所以我尝试使用 %glr-parser 来解决移位/减少冲突。
当我使用非 GLR 解析器时,我可以使用 yystate(全局变量)在检测到语法错误时获取“预期标记”。但是切换到 GLR 解析器后,我发现它不再是全局变量了。
所以我的问题是,当出现语法错误时,是否有办法在 GLR 解析器中获取“预期标记”?
How to get 'expected token' in bison/yacc GLR-parser?
Hi,
In the project i am doing, there'er a few ambiguous gramar. So i am trying to use %glr-parser to solve the shift/reduce confilicts.
When i was using non-GLR parser, i can use the yystate(global variable) to get the "expected token" when detect syntax error.But after switch to GLR parser, i find it's not global variable anymore.
So my question is, is there anyway to get the "expected token" in GLR-parser when there's a syntax error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
GLR 解析器中没有有效的 yystate 信息是正确的,因此不可能有“预期令牌”的简单解决方案。
要了解为什么必须了解默认 LALR 解析器和 GLR 解析算法之间的差异。这里有详细记录: http://www.delorie.com/gnu/ docs/bison/bison_11.html。基本上,GLR(通用 LR)执行多个并发线程,每个线程尝试不同的模糊替代方案来搜索匹配项。当它们都不匹配时,解析就会失败,并且您处于语法错误的情况。因此,不可能有单个预期令牌,但可能有许多预期令牌。这就是为什么它是模棱两可的。解释 bison 解析器如何工作的好文档在这里: http://www.cs .uic.edu/~spopuri/cparser.html,它解释了非 GLR 解析中 yystate 变量的功能。
@rici对此问题的回答最好地介绍了为 GLR 解析器生成更好的错误消息的方法:语法不明确时 GLR 解析器上的附加语法错误消息;从很多方面来说,这个问题实际上与那个问题重复。
它只是没有作为重复项被关闭,因为对于为什么他们都询问同一问题相当模糊。
It is correct that there is no valid
yystate
information in a GLR parser, and so there cannot be a simple solution of "expected token".To see why one has to understand the difference between the default LALR parser and the GLR parsing algorithms. This is well documented here: http://www.delorie.com/gnu/docs/bison/bison_11.html. Basically, the GLR (Generalised LR) executes multiple concurrent threads, each one trying the different ambiguous alternatives to search for a match. When none of them match the parse fails and you are in a syntax error situation. Therefore there cannot be a single expected token, but there might be many expected tokens. That is why it is ambiguous. A good document explaining how the bison parser works is here: http://www.cs.uic.edu/~spopuri/cparser.html, which explains the function of the yystate variable in a non-GLR parse.
The method of generating better error message for a GLR parser is best covered in @rici's answer to this question: Additional syntax error message on GLR parser when syntax is ambiguous; and in many ways this question is really a duplicate of that one.
It just hasn't been closed as a duplicate because it's rather obscure as to why they are both asking about the same problem.