Cómo resolver el conflicto SHIFT/REDUCE - en el generador del analizador

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

preguntado el 27 de noviembre de 13 a las 06:11

Creo que el ActParamsList2 has unbalanced parentheses. An example for an expanded ActParamsOptional se vería así: (Expr)). -

Yes you are right - I have changed that - that was mistake. I have changed that and updated error output as new shift reduce appeared. -

1 Respuestas

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 y Matched Fuck Cancer.

LPAREN ActPars RPAREN

respondido 27 nov., 13:22

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

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

@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

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

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

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.