Search code examples
compiler-errorsbisonyacc

The yacc error have no type declare, but I had declare these in my tiger.grm?


I'm following Apple's book,but when I do the program for chapter 4 Abstract Syntax it's confuse me ,I had declared type.how to fix it?

When i Run that:

$ yacc -dv tiger.grm

the yacc give me :

tiger.grm:150.62-63: error: $2 of ‘arrayExp’ has no declared type
     : ID LBRACK exp RBRACK OF exp {$$ = A_arrayExp(EM_tokPos,$2,$4);}
                                                              ^^
tiger.grm:150.65-66: error: $4 of ‘arrayExp’ has no declared type
     : ID LBRACK exp RBRACK OF exp {$$ = A_arrayExp(EM_tokPos,$2,$4);}
                                                                 ^^
tiger.grm:175.51-52: error: $1 of ‘efieldList_’ has no declared type
     | COMMA efield efieldList_ {$$ = A_EfieldList($1,$3);}
                                                   ^^
tiger.grm:226.48-49: error: $1 of ‘fieldList_’ has no declared type
     | COMMA field fieldList_ {$$ = A_FieldList($1,$3);}
                                                ^^
makefile:11: recipe for target 'y.tab.c' failed
make: *** [y.tab.c] Error 1

My generator and environment:

ubuntu 18.04 LTS

bison (GNU Bison) 3.0.4 Written by Robert Corbett and Richard Stallman.

%{
#include <stdio.h>
.......
%union {
    int pos;
    ......
    A_efieldList efieldList;
}

%token <sval> ID STRING
%token <ival> INT
%token
COMMA COLON SEMICOLON LPAREN RPAREN LBRACK RBRACK
......
FUNCTION VAR TYPE

%type <exp> exp varExp nilExp intExp stringExp callExp opExp recordExp seqExp assignExp ifExp whileExp forExp breakExp letExp arrayExp
%type <var> lvalue
%type <explist> argList argList_ seqList
%type <declist> decList funcDecList
%type <dec> dec varDec funcDec funcDec_
%type <efield> efield
%type <efieldlist> efieldList efieldList_
%type <namtylist> typeDec nametyList
%type <namty> namety
%type <field> field
%type <fieldlist> fieldList fieldList_

%nonassoc LOWER
......
%nonassoc UMINUS

%start program
%%
program 
    : exp  {absyn_root = $1;}

...... # these are so much code ,so i don't post it
       # but if you want the orignal code you can got it from
       # https://paste.ubuntu.com/p/KRQCDCftr6/

fieldList_ 
    : %empty                 {$$ = NULL;}
    | COMMA field fieldList_ {$$ = A_FieldList($1,$3);}

Solution

  • All of these errors are the result of miscounting right-hand side symbols. For example, in

     : ID LBRACK exp RBRACK OF exp {$$ = A_arrayExp(EM_tokPos,$2,$4);}
    

    $2 is LBRACK (the second symbol) and $4 is RBRACK. Maybe you wanted the values of the two exp symbols ($3 and $6), but it's a bit odd to ignore the value of the ID at $1.

    Perhaps you are thinking that only symbols with values are counted. That's not the case; $n refers to the value of the symbol n in the right-hand side, and is therefore an error if that symbol doesn't have a value.

    Note that yacc/bison can't really tell whether or not a particular terminal has a value or not; all it knows is whether you have told it what the type of that value is.