Вопрос

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.

Это было полезно?

Решение

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top