Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bison nonterminal useless in grammar

Tags:

bison

When I compile the below grammar with bison test.y, it warns that the nonterminals "header_stms" and "forward" are useless. Why? They are used in the "program" rule. The nonterminal "stm" is not reported useless.

%%

program: /* empty */
    | stm
    | header_stms
    ;

header_stms: header_stms forward
    ;

/* forward proc declaration */
forward: "forward" TK_ID ";"
    ;

stm: TK_ID "+" TK_ID
    ;

%%

The rules that use those nonterminals are also deemed useless.

like image 871
Mike Avatar asked Oct 02 '22 22:10

Mike


1 Answers

That's because stm doesn't have how to reduce to the program rule, try this:

%%

program: /* empty */
       | stm
       | header_stms
       ;

header_stms: /* empty */ 
           | forward
           | header_stms forward
           ;

/* forward proc declaration */
forward: "forward" TK_ID ";"
       ;

stm: /* empty */ 
   | TK_ID "+" TK_ID
   ;

%%

Also, I solved your reduce problem in the header_stms, through left recursion.

like image 88
João Pinho Avatar answered Oct 14 '22 01:10

João Pinho