1

I have a problem during my project(making mini C compiler).

There is no error and I can scan and parse mini C source code file.

But in *.output file I found

State 17 conflicts: 2 shift/reduce
State 19 conflicts: 2 shift/reduce
State 57 conflicts: 8 shift/reduce
State 60 conflicts: 8 shift/reduce
State 61 conflicts: 8 shift/reduce
State 62 conflicts: 8 shift/reduce
State 63 conflicts: 8 shift/reduce
State 96 conflicts: 8 shift/reduce
State 103 conflicts: 10 shift/reduce
State 126 conflicts: 8 shift/reduce
State 130 conflicts: 10 shift/reduce
State 131 conflicts: 10 shift/reduce
State 132 conflicts: 10 shift/reduce
State 133 conflicts: 10 shift/reduce
State 134 conflicts: 10 shift/reduce
State 135 conflicts: 10 shift/reduce
State 136 conflicts: 10 shift/reduce
State 137 conflicts: 10 shift/reduce
State 138 conflicts: 10 shift/reduce
State 139 conflicts: 10 shift/reduce

State 154 conflicts: 1 shift/reduce

and at state 17

4 DECLARATION: INT IDENTIFIER ';' . DECLARATION
6            | INT IDENTIFIER ';' .

INT    shift, and go to state 29
FLOAT  shift, and go to state 30

INT       [reduce using rule 6 (DECLARATION)]
FLOAT     [reduce using rule 6 (DECLARATION)]
$default  reduce using rule 6 (DECLARATION)

DECLARATION  go to state 31

here is my yacc file for Declaration

DECLARATION : 
INT IDENTIFIER ';' DECLARATION %prec p10 {
    struct DECLARATION *decl = (struct DECLARATION *) malloc (sizeof (struct DECLARATION));
    decl->t = eInt;
    decl->id = $2;
    decl->prev = $4;
    $$ = decl;
}
|
FLOAT IDENTIFIER ';' DECLARATION {
    struct DECLARATION *decl = (struct DECLARATION *) malloc (sizeof (struct DECLARATION));
    decl->t = eFloat;
    decl->id = $2;
    decl->prev = $4;
    $$ = decl;
}
|
INT IDENTIFIER ';' {
    struct DECLARATION *decl = (struct DECLARATION *) malloc (sizeof (struct DECLARATION));
    decl->t = eInt;
    decl->id = $2;
    decl->prev = NULL;
    $$ = decl;
}
|
FLOAT IDENTIFIER ';' {
    struct DECLARATION *decl = (struct DECLARATION *) malloc (sizeof (struct DECLARATION));
    decl->t = eFloat;
    decl->id = $2;
    decl->prev = NULL;
    $$ = decl;
}
;
melpomene
  • 84,125
  • 8
  • 85
  • 148
김종우
  • 11
  • 1
  • Shouldn't it be something like: `DECLARATION_LIST : DECLARATION | DECLARATION_LIST DECLARATION; DECLARATION : INT IDENTIFIER ';' { ... } | FLOAT IDENTIFIER { ... } ';'`? – melpomene Sep 30 '16 at 13:36
  • You need to show more of the grammar if you want a solution. A [mcve] is always good. And the usual convention with bison is that tokens (terminals) are upper-case, while non-terminals are lower-case. You don't need to follow that convention unless you want someone else to read your code. But you wouldn't be here unless you wanted someone else to read your code :) – rici Sep 30 '16 at 23:27

0 Answers0