[antlr-interest] rule return value not declared
Kendall Shaw
kshaw at kendallshaw.com
Tue Jul 28 14:37:34 PDT 2009
Hi,
This is a toy example, but it shows a problem I'm having. Is it a bug,
or am I doing something wrong? I want to use the text matched by
expr_sub to modify objects created by actions in that rule, so I do
that in the expr rule ($e.text). The result is that the return
attribute "list" is not declared in the resulting parser:
grammar test;
tokens {
THING = 'thing' ;
LP = '(' ;
RP = ')' ;
OR = '|' ;
}
@header {
import java.util.ArrayList;
import java.util.List;
}
expr[List<List<String>> ll]
: e=expr_sub { $e.list.add($e.text); ll.add($e.list); }
;
expr_sub returns [List<String> list]
@init { list = new ArrayList<String>(); }
: (t=thingy { list.add($t.name); })+
;
thingy returns [String name]
@init { name = ""; }
: THING LP (NAME { name = $NAME.text; })? RP
;
NAME : ('a'..'z')+ ;
The code generated for expr_sub, see that "list" is not declared:
public static class expr_sub_return extends ParserRuleReturnScope {
public List<String> list;
};
// $ANTLR start "expr_sub"
// test.g:17:1: expr_sub returns [List<String> list] : (t= thingy )+ ;
public final testParser.expr_sub_return expr_sub() throws RecognitionException {
testParser.expr_sub_return retval = new testParser.expr_sub_return();
retval.start = input.LT(1);
String t = null;
list = new ArrayList<String>();
try {
// test.g:19:5: ( (t= thingy )+ )
// test.g:19:7: (t= thingy )+
{
// test.g:19:7: (t= thingy )+
int cnt1=0;
loop1:
do {
int alt1=2;
int LA1_0 = input.LA(1);
if ( (LA1_0==THING) ) {
alt1=1;
}
switch (alt1) {
case 1 :
// test.g:19:8: t= thingy
{
pushFollow(FOLLOW_thingy_in_expr_sub109);
t=thingy();
state._fsp--;
list.add(t);
}
break;
default :
if ( cnt1 >= 1 ) break loop1;
EarlyExitException eee =
new EarlyExitException(1, input);
throw eee;
}
cnt1++;
} while (true);
}
retval.stop = input.LT(-1);
}
catch (RecognitionException re) {
reportError(re);
recover(input,re);
}
finally {
}
return retval;
}
More information about the antlr-interest
mailing list