解决语法歧义

时间:2010-11-03 03:04:49

标签: 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
                      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_interface之后,如果编译器看到令牌T_BEGIN,它就不知道要去哪个绑定规则。它可能意味着开始bind_buttons或者它可能意味着你想要跳过bind_buttons而T_BEGIN是要启动bind_functions。

如何更改此语法以避免此问题?

要求:我不允许添加/删除终端。我无法告诉用户他们必须改变编写代码的方式,我必须更改规则来处理它。

我很难过,有什么想法吗?

更新 interface_sections:main_interface bind_buttons bind_functions bind_panel_items                        ; / * 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 */

在通过野牛运行时,这给了我相同的移位/减少错误。

但是,我认为它是在正确的轨道上,我想我需要将T_BUTTONS和T_FUNCTIONS以及T_PANEL放到规则的前面

其他信息:

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 */

1 个答案:

答案 0 :(得分:1)

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 ;

这并没有产生转移减少错误,似乎它应该对我有用。

有人看到了问题吗?