0
votes

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

1
Sounds like it's just that none of those rules are actually used anywhere else. Is defn actually used elsewhere? If you do add something using it and related to the top-level symbol, do any warnings remain? - aschepler
The choice of alternatives for procname is odd. I don't think we can help much without an MCVE (Minimal, Complete, Verifiable Example — or MRE or whatever name SO now uses) or an SSCCE (Short, Self-Contained, Correct Example). That means removing most of the lines from line 1 through 157 — but leaving enough that your problem still occurs, albeit with different line numbers. Given the useless rule warnings, though, it is fairly clear that you don't have defn in use on the RHS of a definition — some_non_terminal: … defn … - Jonathan Leffler

1 Answers

0
votes

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.