语法不明确时,GLR解析器上的其他语法错误消息 [英] Additional syntax error message on GLR parser when syntax is ambiguous

查看:162
本文介绍了语法不明确时,GLR解析器上的其他语法错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Bison 2.7编写GLR解析器,还打开了%error-verbose选项.当我运行解析器时,它给了我语法不明确"的错误. Bison有办法给我更多有关语法在哪里/如何模棱两可的详细信息吗?

I am using Bison 2.7 to write a GLR parser and also turn on %error-verbose option. When I ran the parser, it gave me "syntax is ambiguous" error. Is there a way that Bison can give me more details on where/how the syntax is ambiguous?

推荐答案

如果您希望产生有意义的错误消息,则可能需要构建自己的函数来报告歧义. bison确实为您提供了一个基本工具:自定义%merge函数.

If you are looking to produce meaningful error messages, you will probably need to craft your own function to report ambiguities. bison does give you a basic tool for this: the custom %merge function.

如手册中所述(请参见上面链接末尾的段落),您可以在生产中使用%merge子句指定自定义合并功能,这可能会导致歧义.合并功能可以很好地完成任何工作,包括信令错误,但是有一些限制:

As indicated in the manual (see the paragraphs at the end of the link above), you can specify a custom merge function using %merge clauses on productions which can lead to ambiguities. A merge function can do pretty well anything, including signaling errors, but there are some limitations:

  1. 合并功能的参数是语义值(YYSTYPE),该语义值将是歧义生成的子类型.歧义右侧的语义动作将在调用合并功能之前已执行,因此,如果语义动作使全局解析器状态发生变化,则该状态可能会不一致,并且必须清理合并功能. (因此,建议这种语法中的语义动作不要修改全局状态.)merge函数必须返回适当类型的语义值.

  1. The arguments to the merge function are semantic values (YYSTYPE), which will be of the subtype of the ambiguous production. The semantic actions of the ambiguous right-hand sides will have been executed before the merge function is called, so if the semantic actions mutate global parser state, that state might be inconsistent and the merge function will have to clean up. (Consequently, it is recommended that semantic actions in such a grammar not modify global state.) The merge function must return a semantic value of the appropriate type.

如果模棱两可的产生式具有不同的类型,则可能需要为每种类型创建不同的合并函数,因为除非知道如何编码,否则无法知道语义联合的哪个元素适合于特定的联合值进入YYSTYPE本身.一种简单的方法是通过将枚举标记作为每个工会成员的第一个元素来创建歧视的工会". (C/C++允许您使用任何联合成员访问此类标签,但必须在开始时才能使用此标签.)

If possibly ambiguous productions have different types, you will probably need to create a different merge function for each type, since is no way to know which element of a semantic union is appropriate for a specific union value unless you code that into the YYSTYPE itself. A simple way of doing that is to create a "discriminated union" by including an enumerated tag as the first element of every union member. (C/C++ allow you to access such a tag using any union member, but it must be at the beginning for this to work.)

使用正好两个语义值调用合并函数.因此,如果有两个以上可能的解析,它将被多次调用.

The merge function is called with exactly two semantic values. So if there are more than two possible parses, it will be called multiple times.

合并功能无法访问位置信息,除非该信息包含在语义类型中(这反而使具有单独的位置堆栈恕我直言,但是您只需要包括位置可能会导致歧义的信息类型.)

The merge function does not have access to location information, unless that information is included in the semantic type (which rather defeats the point of having a separate location stack IMHO, but of course you only need to include the location information in types which might participate in an ambiguity.)

这篇关于语法不明确时,GLR解析器上的其他语法错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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