yacc 错误没有类型声明,但我已经在 tiger.grm?

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

我正在关注 Apple 的书,但是当我编写第 4 章抽象语法的程序时,它让我感到困惑,我已经声明 type.how 来修复它?

当我 运行 时:

$ yacc -dv tiger.grm

yacc 给我:

tiger.grm:150.62-63: error:  of ‘arrayExp’ has no declared type
     : ID LBRACK exp RBRACK OF exp {$$ = A_arrayExp(EM_tokPos,,);}
                                                              ^^
tiger.grm:150.65-66: error:  of ‘arrayExp’ has no declared type
     : ID LBRACK exp RBRACK OF exp {$$ = A_arrayExp(EM_tokPos,,);}
                                                                 ^^
tiger.grm:175.51-52: error:  of ‘efieldList_’ has no declared type
     | COMMA efield efieldList_ {$$ = A_EfieldList(,);}
                                                   ^^
tiger.grm:226.48-49: error:  of ‘fieldList_’ has no declared type
     | COMMA field fieldList_ {$$ = A_FieldList(,);}
                                                ^^
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 = ;}

...... # 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(,);}

所有这些错误都是错误计算 right-hand 个边符号的结果。例如,在

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

</code>是<code>LBRACK(第二个符号),</code>是<code>RBRACK。也许您想要两个 exp 符号(</code> 和 <code>)的值,但是忽略 </code> 处的 <code>ID 的值有点奇怪.

也许您认为只有具有值的符号才会被计算在内。事实并非如此; $n 指的是 right-hand 一侧的符号 n 的值,因此如果该符号没有值,则会出错。

请注意 yacc/bison 无法真正判断特定终端是否具有值;它所知道的只是你是否告诉它那个值的类型是什么。