如何获取“预期令牌”在 bison/yacc GLR 解析器中?

发布于 2024-11-19 08:11:18 字数 250 浏览 10 评论 0原文

如何在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴梦一场 2024-11-26 08:11:18

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文