Reputation: 3
I'm new to Bison. I wrote some rules but I get a lot of 'rule useless in grammar'.
I think 'expr' cause this problem. Please help me.
File :
| class {printf("accepted");}
;
class : CLASS IDENT '{' function '}'
| CLASS IDENT '{' global '}'
;
global : data_type IDENT
;
function :
|data_type IDENT'('Params')' '{'statement'}'
|VOID IDENT'('Params')' '{'statement'}'
;
Params : data_type IDENT
| data_type IDENT',' Params
;
data_type : INT_T
|DOUBLE_T
|BOOL_T
|VOID;
;
statement : WHILE '(' expr')' statement
| FOR'('data_type IDENT '=' expr ';' expr ';' expr')' statement
;
expr: expr COMP expr
|expr '=' expr
|INT_T
|BOOL_T
;
Upvotes: 0
Views: 83
Reputation: 241691
The problem is that statement
has only recursive rules. That makes statement
impossible to use in a derivation because any derivation starting with statement
never terminates.
Because statement
cannot be used to parse any finite string, bison removes it and all its rules from the grammar, as well as any rules which use statement
. With those rules removed, expr
and Params
are no longer referred to in the grammar, so they are also marked useless.
You probably intended statement
to have other, non-recursive alternatives, such as expr
.
Upvotes: 1