Search code examples
bisonyacc

Problem with yacc embedded actions when using bison


I am trying to implement a compiler from the book “The Unix Programming Environment” by Brian Kernighan and Rob Pike, 1984. The book assumes yacc, however I am using a Mac which has bison version 2.3. The code question is described on page 276 in the book.

I am getting warnings from a piece of the grammar/actions that make use of embedded actions (yacc lingo), which I think is the same as mid-rule actions in bison.

Below, is the piece of grammar that generates warnings (line numbers are from listing):

158: defn: FUNC procname         { $2->type=FUNCTION; indef=1; }
159:       '(' ')' stmt          { code(procret); define($2); indef=0; }
160:     | PROC procname         { $2->type=PROCEDURE; indef=1; }
161:       '(' ')' stmt          { code(procret); define($2); indef=0; }
162:     ;
163:
164:
165: procname: VAR
166:    | FUNCTION
167:    | PROCEDURE
168:    ;

Below are the warnings from Bison:

7 rules never reduced
hoc.y: warning: 4 useless nonterminals and 7 useless rules
hoc.y:158.1-4: warning: useless nonterminal: defn
hoc.y:158.29-59: warning: useless nonterminal: @1
hoc.y:160.29-60: warning: useless nonterminal: @2
hoc.y:47.17-24: warning: useless nonterminal: procname
hoc.y:158.29-59: warning: useless rule: @1: /* empty */
hoc.y:158.7-159.67: warning: useless rule: defn: FUNC procname @1 '(' ')' stmt
hoc.y:160.29-60: warning: useless rule: @2: /* empty */
hoc.y:160.7-161.67: warning: useless rule: defn: PROC procname @2 '(' ')' stmt
hoc.y:165.11-13: warning: useless rule: procname: VAR
hoc.y:166.7-14: warning: useless rule: procname: FUNCTION
hoc.y:167.7-15: warning: useless rule: procname: PROCEDURE

Is it possible that the grammar/actions are acceptable to yacc and not bison? If so, does bison have a ‘yacc mode’? If not, how should the grammar/actions be rewritten to be acceptable to bison? Thanks


Solution

  • These mid-rule actions are perfectly acceptable in bison. That is not your problem.

    As indicated in the comments, the most likely cause of this error message is that your grammar is missing the production which includes defn, which is the third production for list:

    list:     /* nothing */
        | list '\n'
        | list defn '\n'
        | list asgn '\n'  { code2(xpop, STOP); return 1; }
        | list stmt '\n'  { code(STOP); return 1; } 
        | list expr '\n'  { code2(printtop, STOP); return 1; }
        | list error '\n' { yyerrok; }
        ;
    

    You can download a tarball containing all of the code from the UPE book, using the Wayback Machine aka web.archive.org. I got that link from the Wikipedia entry for hoc, which also contains links to other hoc implementations.