我正在关注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);}
最佳答案
所有这些错误是由于错误地计算了右侧符号而导致的。例如,在
: ID LBRACK exp RBRACK OF exp {$$ = A_arrayExp(EM_tokPos,$2,$4);}
$2
是LBRACK
(第二个符号),$4
是RBRACK
。也许您想要两个exp
符号($3
和$6
)的值,但是忽略ID
上$1
的值有点奇怪。也许您在考虑只计算带有值的符号。事实并非如此;
$n
指向右侧符号n
的值,因此,如果该符号没有值,则为错误。请注意,yacc/bison无法真正分辨出特定终端是否具有值。它所知道的只是您是否已告诉它该值的类型。