2011-05-20 64 views
0

我正在编写一个yacc文件作为编译器的一部分。 我有以下错误:在Yacc中嵌入代码

lang_grammar.y:143.54-55: $2 of `ClassDeclaration' has no declared type 
lang_grammar.y:143.69-70: $4 of `ClassDeclaration' has no declared type 
lang_grammar.y:143.84-85: $6 of `ClassDeclaration' has no declared type 

发生在此行中我.Y文件:

CLASS { /* code will be embedded here */ } ID EXTENDS ID '{' ClassBody '}' 
    { $$.classDeclaration = new ClassDeclaration($2.identifier, $4.identifier, $6.classBody); } 

当我删除内嵌入代码:

CLASS ID EXTENDS ID '{' ClassBody '}' 
    { $$.classDeclaration = new ClassDeclaration($2.identifier, $4.identifier, $6.classBody); } 

它工作得很好。

在yacc中嵌入代码有限制吗?我的印象是这是可能的。

谢谢。

回答

1

我想你已经使用了错误的索引。在以前的方式,嵌入的代码也收录,说

CLASS { /* code will be embedded here */ } ID EXTENDS ID '{' ClassBody '}' 
$1 $2         $3 $4  $5 $6 $7  $8 

所以操作代码应该是

{ $$.classDeclaration = new ClassDeclaration($3.identifier, $5.identifier, $7.classBody); } 
+0

啊,所以整个嵌入部分(从{}到)一个指数,然后计算 – pseudosudo 2011-05-20 09:15:17