2

I'll post the rules of the grammar in question to start.

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

Notice that after main_interface if the compiler sees the token T_BEGIN it wont know which of the bind rules to go to. It could mean begin bind_buttons or it could mean you want to skip bind_buttons and the T_BEGIN is to start bind_functions.

How can I change this grammar to not have this issue?

Requirement: I am not allowed to add/remove terminals. I can't tell the user they have to change the way they write the code, I have to change the rules to handle it.

I'm stumped, any ideas?

Update: interface_sections : main_interface bind_buttons bind_functions bind_panel_items ; /* Components of a gui program */

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

This gives me the same shift/reduce errors when running it through bison.

However, I think it's on the right track, I think I need to get the T_BUTTONS and T_FUNCTIONS and T_PANEL to the front of the rule

Additional info:

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 */
kralco626
  • 8,456
  • 38
  • 112
  • 169
  • I started to edit my answer, but now I'm curious--why do you think there are shift-reduce conflicts here? The parser is a shift-reduce parser, meaning it shifts tokens onto a stack, then reduces to a nonterminal if possible. When the parser sees T_BEGIN, it will be forced to shift because it cannot reduce. It seems you are thinking in a top-down manner. – Kizaru Nov 03 '10 at 03:53
  • I am using flex/bison to do the parsing. It tells me there are 2 shift reduce errors. I also was told as part of the assignment that there are 2 sift reduce errors. I was also told the issue involved being able to include 0-3 of these bind sections. – kralco626 Nov 03 '10 at 03:57
  • I'm going to try moving just T-BEGIN T_BIND into the prefix_stuff rule and see what bison gives me – kralco626 Nov 03 '10 at 03:58
  • Bummer I just made the changes and still got 2 shift/reduce conflicts – kralco626 Nov 03 '10 at 04:01
  • I think I need to get the T_Buttons and T_functions and T_Panel to the front of the rule somehow – kralco626 Nov 03 '10 at 04:01
  • What is your component_list rule? – Kizaru Nov 03 '10 at 04:02
  • 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 */ but i'll add to question so i can format – kralco626 Nov 03 '10 at 04:07

1 Answers1

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 ;

This did not produce an shift-reduce errors and seems like it should work to me.

Anyone see an issue?

kralco626
  • 8,456
  • 38
  • 112
  • 169