[antlr-interest] Token Stream Rewriting

Bart Kiers bkiers at gmail.com
Tue Jun 21 23:45:09 PDT 2011


Is it _really_ returning a String

*  private String merge(String s1, List lst, String s2) {*
*    ...*
*    return ...*
*  }*

?
Not an Object _you think_ is a String:

*  private Object merge(String s1, List lst, String s2) {*
*    ...*
*    return ...*
*  }*

?
I ask because the stack-trace you posted: "Caused by:
java.lang.ClassCastException: java.util.ArrayList" seems to suggest it is
an ArrayList.
Either way: a String or an ArrayList, both are wrong: that method needs to
return a Tree.

Bart.


On Wed, Jun 22, 2011 at 8:39 AM, Fabien Hermenier <hermenierfabien at gmail.com
> wrote:

>  merge(...) is returning a String.
>
> Fabien.
>
> Le 22/06/11 00:36, Bart Kiers a écrit :
>
> Fabien, but what is the return type of  this `merge(...)` method? Could you
> post the method? Or even better: post a SSCCE <http://sscce.org>that
> causes such an exception?
>
>  Regards,
>
>  Bart.
>
>
> On Wed, Jun 22, 2011 at 8:30 AM, Fabien Hermenier <
> hermenierfabien at gmail.com> wrote:
>
>> In fact, I've badly readed the help. So yet, it is running a String that
>> should be tokenized (then translated into tree I suppose) at run time.
>>
>> Le 22/06/11 00:28, Bart Kiers a écrit :
>> > Is your `merge(String, List, String)` method returning
>> > a java.util.ArrayList instead of a Tree?
>> >
>> > Regards,
>> >
>> > Bart.
>> >
>> >
>> > On Wed, Jun 22, 2011 at 8:04 AM, Fabien Hermenier
>>  > <hermenierfabien at gmail.com <mailto:hermenierfabien at gmail.com>> wrote:
>> >
>> >     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<http://www.antlr.org/wiki/display/%7Eadmin/2007/06/28/Token+stream+rewriting+with+rewrite+rules>
>>  >     <
>> http://www.antlr.org/wiki/display/%7Eadmin/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.
>> >
>> >     List: http://www.antlr.org/mailman/listinfo/antlr-interest
>> >     Unsubscribe:
>> >
>> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>> >
>> >
>>
>>
>>  --
>> Fabien Hermenier
>> Postdoctoral researcher at Flux
>> http://sites.google.com/site/hermenierfabien/home
>>
>>
>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
>> Unsubscribe:
>> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>>
>
>
>
> --
> Fabien Hermenier
> Postdoctoral researcher at Flux
> http://sites.google.com/site/hermenierfabien/home
>
>


More information about the antlr-interest mailing list