0

I am writing a c-like language, but for grammar shown below:

module: module declORFunc
    | declORFunc
    ;

declORFunc: decl
    | func

decl: CONST_opt btype varDef_list SEMICOLON ;

CONST_opt: /* empty */ | CONST ;

btype: INT | FLOAT ;

...

func: functype ID LPAREN funcFParams_opt RPAREN blockStmt ;

functype: INT | FLOAT | VOID ;

Because yacc is LALR(1), when processing token INT, it exhibits a shift-reduce conflict during grammar resolution for both sytanx start with INT ID. For more detail:

Sy.y: warning: shift/reduce conflict on token INT [-Wcounterexamples]
  First example: • INT ID LPAREN funcFParams_opt RPAREN blockStmt $end
  Shift derivation
    $accept
    ↳ 0: module                                                                 $end
         ↳ 2: declORFunc
              ↳ 4: func
                   ↳ 20: functype    ID LPAREN funcFParams_opt RPAREN blockStmt
                         ↳ 21: • INT
  Second example: • INT varDef_list SEMICOLON $end
  Reduce derivation
    $accept
    ↳ 0: module                                                  $end
         ↳ 2: declORFunc
              ↳ 3: decl
                   ↳ 5: CONST_opt btype    varDef_list SEMICOLON
                        ↳ 6: ε •  ↳ 8: INT

I trid:

  • replace btype and functype with type: INT | FLOAT | VOID;
  • add %proc to force yacc to shift into decl before reduce into func.

How can I resolve this conflict?

1
  • I don't think you can; C is not LALR(1), so no LALR(1) parser is going to be able to fully parse it. You can eliminate one of your shift/reduce errors by changing to "functype: btype | VOID;" but I don't think you can eliminate the ID problem. (You can make the bison grammar accept illegal C and test for the illegal bits in code and yyerror() out manually if you really want to use bison to parse your C-like programs) Commented Oct 6 at 0:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.