2011-05-20 69 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