0

I keep on running into reduce/reduce and reduce/shift problems with my grammar, but I'm not sure why or how to fix it.

Here is what I have

block ::=   
    block_statement
|   block_statement block
;
block_statement ::=
    type variable_declarators SEMICOLON
|   statement
;
statement ::=   sub_statement
|   IF LPAREN boolexp RPAREN statement FI
|   IF LPAREN boolexp RPAREN statement ELSE statement FI
|   WHILE LPAREN boolexp RPAREN statement ELIHW
|   FOR LPAREN assignment SEMICOLON boolexp SEMICOLON
        statement_expression RPAREN statement ROF
;
sub_statement ::=
    statement_expression SEMICOLON
|   RETURN Exp SEMICOLON
|   block
;
statement_expression ::=
    assignment
|   IDENTIFIER LPAREN argument_list RPAREN
|   IDENTIFIER PLUSPLUS
|   IDENTIFIER MINUSMINUS
;
Paul
  • 121
  • 1
  • 1
  • 4
  • I see this is CUP notation (not flex or bison). You are missing rules for argument_list, type, boolexp, Exp,variable_declarators and assignment. Can you make a smaller example to illustrate the problem? By simplifying you will learn where the conflicts are. – Brian Tompsett - 汤莱恩 Jul 05 '15 at 18:44
  • In CUP you can use the -dump_states and the -dump_grammar options to find specifically which rules are causing the ambiguity. With that detail you might be able to ask a more specific question that we can answer. – Brian Tompsett - 汤莱恩 Jul 05 '15 at 21:53

1 Answers1

1

In that grammar, a block is a block_statement is a statement is a sub_statement is a block and you can go around that particular mulberry bush as long as you like without ever resolving anything.

So naturally you have conflicts, since your grammar is ambiguous.

Perhaps you meant for a sub_statement to include some kind of delimited block? (BEGIN block NIGEB?) Otherwise, I'd suggest removing block from the list of sub_statement alternatives.

rici
  • 234,347
  • 28
  • 237
  • 341