Compiler construction 解决语法歧义
我将把有问题的语法规则贴到开始处Compiler construction 解决语法歧义,compiler-construction,programming-languages,grammar,shift-reduce-conflict,Compiler Construction,Programming Languages,Grammar,Shift Reduce Conflict,我将把有问题的语法规则贴到开始处 interface_sections : main_interface bind_buttons bind_functions bind_panel_items ; /* Components of a gui program */ bind_buttons : T_BEGIN T_BIND T_BUTTONS T_SEMIC component_list
interface_sections : main_interface bind_buttons bind_functions bind_panel_items
; /* Components of a gui program */
bind_buttons : T_BEGIN T_BIND T_BUTTONS T_SEMIC component_list
T_END T_BIND T_BUTTONS T_SEMIC
| epsilon
; /* Bind the buttons for GUI */
bind_functions : T_BEGIN T_BIND T_FUNCTIONS T_SEMIC component_list
T_END T_BIND T_FUNCTIONS T_SEMIC
| epsilon
; /* Bind the graphical drawing functions for GUI */
bind_panel_items : T_BEGIN T_BIND T_PANEL T_ITEMS T_SEMIC component_list
T_END T_BIND T_PANEL T_ITEMS T_SEMIC
| epsilon
; /* Bind the panel items or menus for GUI */
注意,在main_接口之后,如果编译器看到令牌T_BEGIN,它将不知道要转到哪个绑定规则。它可能意味着开始绑定按钮,也可能意味着您想跳过绑定按钮,而T\u begin是启动绑定功能
如何更改此语法以避免此问题
要求:我不允许添加/删除端子。我不能告诉用户他们必须改变他们编写代码的方式,我必须改变规则来处理它
我被难住了,有什么想法吗
更新:
界面部分:主界面绑定按钮绑定功能绑定面板项
; /* gui程序的组件*/
prefix_stuff : T_BEGIN T_BIND
bind_buttons : prefix_stuff T_BUTTONS T_SEMIC component_list
T_END T_BIND T_BUTTONS T_SEMIC
| epsilon
; /* Bind the buttons for GUI */
bind_functions : prefix_stuff T_FUNCTIONS T_SEMIC component_list
T_END T_BIND T_FUNCTIONS T_SEMIC
| epsilon
; /* Bind the graphical drawing functions for GUI */
bind_panel_items : prefix_stuff T_PANEL T_ITEMS T_SEMIC component_list
T_END T_BIND T_PANEL T_ITEMS T_SEMIC
| epsilon
; /* Bind the panel items or menus for GUI */
在bison中运行时,这给了我相同的移位/减少错误
然而,我认为它在正确的轨道上,我认为我需要将T_按钮、T_功能和T_面板放在规则的前面
其他信息:
component_list : component_list valid_components
| valid_components
; /* For the four bind blocks - a list of components */
valid_components : dialog_box_spec
| browser_box_spec
| pull_down_or_right
; /* Possible components for the list */
这并没有产生一个减少错误的转换,而且对我来说似乎应该是可行的
有人看到问题了吗?我开始编辑我的答案,但现在我很好奇——你认为这里为什么会有冲突?解析器是一个移位-减少解析器,这意味着它将令牌转移到堆栈上,然后尽可能减少为非终结符。当解析器看到T_开始时,它将被迫移位,因为它不能减少。你似乎在用自上而下的方式思考。我正在使用flex/bison来进行解析。它告诉我有两个换档减少错误。作为作业的一部分,我还被告知有两个sift减少错误。我还被告知,这个问题涉及到能够包含0-3个绑定部分。我将尝试将T-BEGINT T_绑定移动到prefix_stuff规则中,看看bison给了meBummer什么。我刚刚做了更改,仍然有2个shift/reduce冲突。我想我需要以某种方式将T_按钮、T_函数和T_面板移到规则的前面
interface_sections : main_interface bind_sections_one
; /* Components of a gui program */
bind_sections_one : epsilon | T_BEGIN T_BIND bind_first ;
bind_first : T_BUTTONS T_SEMIC component_list
T_END T_BIND T_BUTTONS T_SEMIC bind_sections_two
| T_FUNCTIONS T_SEMIC component_list T_END T_BIND T_FUNCTIONS T_SEMIC bind_sections_three | T_PANEL T_ITEMS T_SEMIC component_list T_END T_BIND T_PANEL T_ITEMS T_SEMIC
;
bind_sections_two : epsilon | T_BEGIN T_BIND bind_second ;
bind_second : T_FUNCTIONS T_SEMIC component_list T_END T_BIND T_FUNCTIONS T_SEMIC bind_sections_three | T_PANEL T_ITEMS T_SEMIC component_list T_END T_BIND T_PANEL T_ITEMS T_SEMIC ;
bind_sections_three : epsilon | T_BEGIN T_BIND bind_third;
bind_third : T_PANEL T_ITEMS T_SEMIC component_list T_END T_BIND T_PANEL T_ITEMS T_SEMIC ;