0

I know that most of the shift/reduce conflicts can be solved by using %left or %right directives. But even with that, I am getting conflicts. Following is a block of my grammar:

expression:    variable '=' expression
          |    expression operator expression
          |    '-' expression    %prec UMINUS
          |    '(' expression ')'
          |    value_holder
;

operator:    relational_operator
        |    arithmetic_operator
;

relational_operator:    LESS
                   |    GREATER
                   |    LESS_OR_EQUAL
                   |    GREATER_OR_EQUAL
                   |    EQUAL
                   |    NOT_EQUAL
;

arithmetic_operator:    '+'
                   |    '-'
                   |    '*'
                   |    '/'
                   |    '%'
;

I have precedence and associativity directives like following:

%right '='

%left EQUAL NOT_EQUAL
%left LESS LESS_OR_EQUAL GREATER GREATER_OR_EQUAL

%left '+' '-'
%left '*' '/'
%left '%'

%right UMINUS

Still it's giving 11 shift-reduce errors. Here is the state 84 which is giving these errors.

State 84

33 expression: expression . operator expression
33           | expression operator expression .

EQUAL             shift, and go to state 56
NOT_EQUAL         shift, and go to state 57
LESS              shift, and go to state 58
LESS_OR_EQUAL     shift, and go to state 59
GREATER           shift, and go to state 60
GREATER_OR_EQUAL  shift, and go to state 61
'+'               shift, and go to state 62
'-'               shift, and go to state 63
'*'               shift, and go to state 64
'/'               shift, and go to state 65
'%'               shift, and go to state 66

EQUAL             [reduce using rule 33 (expression)]
NOT_EQUAL         [reduce using rule 33 (expression)]
LESS              [reduce using rule 33 (expression)]
LESS_OR_EQUAL     [reduce using rule 33 (expression)]
GREATER           [reduce using rule 33 (expression)]
GREATER_OR_EQUAL  [reduce using rule 33 (expression)]
'+'               [reduce using rule 33 (expression)]
'-'               [reduce using rule 33 (expression)]
'*'               [reduce using rule 33 (expression)]
'/'               [reduce using rule 33 (expression)]
'%'               [reduce using rule 33 (expression)]
$default          reduce using rule 33 (expression)

operator             go to state 68
relational_operator  go to state 69
arithmetic_operator  go to state 70

I am unable to figure out why the directives are not working.

If needed, this is the parser file : parser.y

Edit : file parser.y has been removed.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Chaitanya Patel
  • 390
  • 1
  • 4
  • 15
  • What happens if you use the operators directly in the `expression operator expression` rule? Something like `expression '+' expression | expression '-' expression | ...` – Henry Oct 18 '17 at 07:21
  • @Henry, If I use as you suggest, it's not giving any conflict. But I want to know why above grammar design is giving conflicts. And one more doubt : in design perspective, how good it is to line up 11 rules for 11 operators, rather than having one more non-terminal. What are the performance consequences of these two methods ? – Chaitanya Patel Oct 18 '17 at 07:54
  • @rici, thanks for pointing out. I understood the reason behind the conflicts. I want to know, how good it is in a designing perspective, to list out 11 rules for 11 operators ? Also should I delete my question as it is quite a perfect duplicate of other one ? – Chaitanya Patel Oct 18 '17 at 07:58
  • 1
    The problem is, that all precedence info is lost after the reduction to `operator` is made. In order to resolve the conflict the parser would have to look into the `operator` which it simply does not do. Performace wise there is little difference, The version with fewer reductions should be even slightly faster. But performace does not really play a role if one version works and the other doesn't. – Henry Oct 18 '17 at 07:59

0 Answers0