yacc错误没有类型声明,但是我已经在tiger.grm中声明了这些?

时间:2019-05-24 06:32:19

标签: compiler-errors bison yacc

我正在关注Apple的书,但是当我为第4章“抽象语法”编写程序时,这让我感到困惑,我已经声明了type。如何解决它?

当我运行该命令时:

$ yacc -dv tiger.grm

yacc给我:

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

我的发电机和环境:

ubuntu 18.04 LTS

野牛(GNU野牛)3.0.4 由Robert Corbett和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);}

1 个答案:

答案 0 :(得分:0)

所有这些错误是由于右侧符号错误计数造成的。例如,在

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

$2LBRACK(第二个符号),而$4RBRACK。也许您想要两个exp符号($3$6)的值,但是忽略ID$1的值有点奇怪

也许您正在考虑只计算带有值的符号。事实并非如此; $n指向右侧符号n的值,因此,如果该符号没有值,则会出错。

请注意,yacc / bison不能真正判断特定终端是否具有值;它所知道的就是您是否告诉过该值的类型。