[antlr-interest] Concatenating variable number of tokens into a single string in an action?

Dejas Ninethousand dejas9000 at gmail.com
Mon Jul 18 14:25:52 PDT 2011


Thanks John, that worked.

On Mon, Jul 18, 2011 at 3:52 PM, John B. Brodie <jbb at acm.org> wrote:

> Greetings!
>
> On Mon, 2011-07-18 at 15:35 -0500, Dejas Ninethousand wrote:
> > Hello,
> >
> > If I have the rule:
> >
> >         text : t=(UNQUOTED_ALPHA_TEXT | DECIMAL_NUMBER)+ {
> > stack.pushUnquotedText(...); };
> >
> > Is there any way for me to gather the text of all the ALPHA_TEXT and
> > DECIMAL_NUMBER tokens in this production into a single string and shove
> that
> > string as an argument in my action?
> >
>
> Basically accumulate the text into a variable using an action inside the
> (...)+ loop and then push the accumulation.  Something like (UNTESTED):
>
> text
>    @init{ String str = ""; } :
>    ( t=(UNQUOTED_ALPHA_TEXT | DECIMAL_NUMBER) { str+=$t.text; } )+
>    { stack.pushUnquotedText(str); }
>  ;
>
> Note that the t=(...) only works because all of the stuff inside the
> (...) are Tokens (i think).
>
> Note that any tokens that were put on the HIDDEN channel (or skip();d)
> by the Lexer will not be accumulated/pushed.
>
> Hope this helps...
>    -jbb
>
>
>


More information about the antlr-interest mailing list