4

I need help to solve this one and explanation how to deal with this SHIFT/REDUCE CONFLICTS in future.

I have some conflicts between few states in my cup file.

Grammer look like this:

I have conflicts between "(" [ActPars] ")" states.

1. Statement = Designator ("=" Expr | "++" | "‐‐" | "(" [ActPars] ")" ) ";"

2. Factor = number | charConst | Designator [ "(" [ActPars] ")" ].

I don't want to paste whole 700 lines of cup file. I will give you the relevant states and error output.

This is code for the line 1.)

Matched ::= Designator LPAREN ActParamsList RPAREN SEMI_COMMA

ActParamsList ::=  ActPars
               |
               /* EPS */
               ;

ActPars ::= Expr
        |
        Expr ActPComma
        ;

ActPComma ::= COMMA ActPars;    

This is for the line 2.)

Factor ::= Designator ActParamsOptional ;


ActParamsOptional ::= LPAREN ActParamsList2 RPAREN
              |
              /* EPS */
              ;

ActParamsList2 ::= ActPars
               |
               /* EPS */
               ;

Expr ::= SUBSTRACT Term RepOptionalExpression
         |
         Term RepOptionalExpression
         ;

The ERROR output looks like this:

Warning : *** Shift/Reduce conflict found in state #182
between ActParamsOptional ::= LPAREN ActParamsList RPAREN (*) 
and     Matched ::= Designator LPAREN ActParamsList RPAREN (*) SEMI_COMMA 
under symbol SEMI_COMMA
Resolved in favor of shifting.

Error : * More conflicts encountered than expected -- parser generation aborted

rds
  • 26,253
  • 19
  • 107
  • 134
Milan Bojovic
  • 199
  • 4
  • 10
  • I think `ActParamsList2` has unbalanced parentheses. An example for an expanded `ActParamsOptional` would look like this: `(Expr))`. – tehlexx Nov 27 '13 at 07:28
  • Yes you are right - I have changed that - that was mistake. I have changed that and updated error output as new shift reduce appeared. – Milan Bojovic Nov 27 '13 at 21:34

1 Answers1

1

I believe the problem is that your parser won't know if it should shift to the token:

SEMI_COMMA

or reduce to the token

ActParamsOptional

since the tokens defined in both ActParamsOptional and Matched are

LPAREN ActPars RPAREN
sdasdadas
  • 23,917
  • 20
  • 63
  • 148
  • And the solution is to give more of a lookahead, or switch up your grammar into something non-ambiguous. For example, replace the parens with square brackets for one of the tokens. – sdasdadas Nov 27 '13 at 22:31
  • Thank you for your answer - is there some other way - I have to keep this syntax - I have to keep parenthases "(" [ActPars] ")" - you can see how this needs to look like at the beginning of my post ( lines named 1. And 2.) And I can't use precedence keyword because they told us not to use it :-( – Milan Bojovic Nov 28 '13 at 07:39
  • @MilanBojovic I've tried to go through it and see the grammar but I'm still a bit confused as to what these items represent. Could you give a few lines of example showing what the language looks like? – sdasdadas Nov 28 '13 at 18:41
  • Ok I will give you the examples for the first and second line: 1.) int i = 0; a = 10; b = 5; i = add(a,b); Or: int i = 0; i = functionWithoutActualParameters(); – Milan Bojovic Nov 28 '13 at 22:59
  • BTW Language is Micro Java. And well if you follow first line 1.) Statement = Designator = Expr (Expression) and Expression can eventually guide us to the second one 2.) Example: Expr = Factor = number | charConst | Designator [ "(" [ActPars] ")" ]. Maybe that leads to the problem - but how can I change this lines which are conflicted so Parser can make difference between them ? Sorry if I am little confused – Milan Bojovic Nov 28 '13 at 23:16
  • @MilanBojovic I'm not sure if this is the same language (MiniJava), but take a look at how they represent [their grammar](http://www.cambridge.org/resources/052182060X/MCIIJ2e/grammar.htm). – sdasdadas Nov 29 '13 at 22:18