Dodaaa
Dodaaa

Reputation: 3

Bison grammar (1 reduce/reduce 1 shift/reduce )

Updated ... i know, my grammar is ambiguous but how can I rewrite this grammar to eliminate this ambiguity? ..................

I have a grammar just like this:

Bison file

%left equal nEqual
%left gre less greOrEqual lessOrEqual
%left plus sub
%left DIV mult exp mod
%left not
%left leftB rightB
%%


S :
var "=" A ";"  
;


A:
 Aexp   
|Rexp   
;

Aexp :
Num                                     
| leftB Aexp  rightB               
| Aexp  plus    Aexp            
| Aexp  sub     Aexp            
| Aexp  DIV     Aexp            
| Aexp  mult    Aexp            
;


Rexp :
Aexp                            
| Rexp  gre Rexp                
| Rexp  less Rexp               
| Rexp  greOrEqual  Rexp                
| Rexp  lessOrEqual Rexp            
| Rexp  equal Rexp              
| Rexp  nEqual Rexp             
;

I get 1 shift/reduce and 1 reduce/reduce conflicts doing this, how can I change the grammar to eliminate Conflicts ?

Upvotes: 0

Views: 182

Answers (1)

Thomas Padron-McCarthy
Thomas Padron-McCarthy

Reputation: 27632

Your grammar is ambiguous. An A can be an Aexp or an Rexp. But an Rexp can also be an Aexp. This leads to a reduce/reduce conflict.

Let's say you give your parser this token sequence as input:

var = Num ;

The start symbol S expands to var = A ;

The non-terminal A has to match the token Num. But should that be an A that expands to and Aexp which then expands to Num, or should it be an A that expands to and Rexp, which then expands to Aexp, which then expands to Num?

Upvotes: 1

Related Questions