Embedding Code in Yacc
I'm writing a yacc file as part of a compiler. I have the following error:
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开发者_StackOverflow社区 no declared type
occurring on this line in my .y file:
CLASS { /* code will be embedded here */ } ID EXTENDS ID '{' ClassBody '}'
{ $$.classDeclaration = new ClassDeclaration($2.identifier, $4.identifier, $6.classBody); }
When I remove the inner embedded code:
CLASS ID EXTENDS ID '{' ClassBody '}'
{ $$.classDeclaration = new ClassDeclaration($2.identifier, $4.identifier, $6.classBody); }
It works just fine.
Are there limitations to embedding code within yacc? I was under the impression that this was possible.
Thanks.
I think you have used wrong indexes. In previous way, embedded codes are also indexed, say
CLASS { /* code will be embedded here */ } ID EXTENDS ID '{' ClassBody '}'
$1 $2 $3 $4 $5 $6 $7 $8
So the action codes should be
{ $$.classDeclaration = new ClassDeclaration($3.identifier, $5.identifier, $7.classBody); }
精彩评论