我正在编写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中嵌入代码是否有限制?我觉得这是可能的。
感谢。
答案 0 :(得分: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); }