[antlr-interest] Token Stream Rewriting
Fabien Hermenier
hermenierfabien at gmail.com
Tue Jun 21 23:04:35 PDT 2011
Hi
I have some troubles with token stream rewriting. Below is the piece of
ANTLR code. I have a grammar, with an AST as output and Java
as the target. I want to insert a sequence of token into the stream.
I have followed the page
http://www.antlr.org/wiki/display/~admin/2007/06/28/Token+stream+rewriting+with+rewrite+rules
and adapted the example that interest me. A piece of the code is
following. Basically, in the alternative of 'explodedSet', I get the
return values of other rules and do some stuff in the merge method. This
one returns a list of String as explained in the online example.
explodedSet: '{' (setContent (',' setContent)*)? '}' -> ^(EXPLODED_SET
setContent+)
| {List l = new LinkedList();}LEFTY
r1=brace_content{l.add($r1.ret);} (','
r2=brace_content{l.add($r2.ret);})* RIGHTY
-> {
merge($LEFTY.text,l,$RIGHTY.text)
};
brace_content returns [List ret]:
st=number ('..' ed=number)? {$ret = new LinkedList(); for (int
i = $st.val; i <= $ed.val; i++) {$ret.add(i);}}
| NAME {$ret = new LinkedList(); $ret.add($NAME.text);};
The code compiles well but at runtime, I've got this exception:
Caused by: java.lang.ClassCastException: java.util.ArrayList cannot be
cast to org.antlr.runtime.tree.Tree
at
org.antlr.runtime.tree.BaseTreeAdaptor.addChild(BaseTreeAdaptor.java:107)
at Parser.explodedSet(Parser.java:560)
So, the return value of merge does not seems to be converted into tokens
nor Tree. Does someone has an idea ?
Fabien.
More information about the antlr-interest
mailing list