help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Optional rule throws shift/reduce conflict


From: John P. Hartmann
Subject: Re: Optional rule throws shift/reduce conflict
Date: Sun, 19 Sep 2010 13:22:39 +0200

While Bison has only one token lookahead, it has infinite memory.  If
something is optional it is best to write two reduction rules.  Though
the combinations do grow exponentially.  In your case:

syntax1 : decl_spec ';'
  | decl_spec init_spec ';'
  | decl_spec init_spec decl_attrib ';'
  | decl_spec decl_attrib init_spec ';'
  | decl_spec init_spec decl_attrib init_spec ';'
  ;

Depending on the rest of your grammar, you might get away with:

syntax1:  dclspec ';'
   | dclspec initspec ';'
   ;

dclspec:  decl_spec
   | decl_spec decl_attrib;
   ;

initspec:  init_spec
   | init_spec decl_attrib
   ;

You might also find it less work to sketch out just the rules and see
what conflicts you have before fleshing them out with semantic code.

Remember:  Most people think top-down, and that is also how one often
writes out the rules, but Bison is strictly bottom-up (and cares not
at all what sequence you write the rules, except for the first).  You
might find it easier while building the grammar to pass it through
Bison often, so that you know which little trivial change caused 64
shift/reduce conflicts all of a sudden.

   j.


On 19 September 2010 12:10, anandvn <address@hidden> wrote:
>
> Hi,
>
> I'm new to bison and need help to acheive my following syntax,
>
> syntax1: decl_spec semicolon
>
> syntax2: decl_spec decl_attrib(optional) init_spec decl_attrib(optional)
> semicolon
>
>
> To achieve this i wrote bison grammar as,
>
>
> declaration: decl_spec semicolon
>           |decl_spec decl_attrib init_spec decl_attrib semicolon
>
>
> decl_attrib:  /* Since this is optional i kept blank rule */
>           | '__attribute__'
>
> But am getting shift/reduce conflicts. Also my grammar is not parsing the
> input successfully, throws parser syntax error.
>
> Please correct me where i did mistake. Also please let me know if any other
> information required to fix this.
>
> Thanks,
> Anand V
> --
> View this message in context: 
> http://old.nabble.com/Optional-rule-throws-shift-reduce-conflict-tp29751078p29751078.html
> Sent from the Gnu - Bison - Help mailing list archive at Nabble.com.
>
>
> _______________________________________________
> address@hidden http://lists.gnu.org/mailman/listinfo/help-bison
>



reply via email to

[Prev in Thread] Current Thread [Next in Thread]