[antlr-interest] ids+=ID question

John Connett jrc at skylon.demon.co.uk
Wed Jul 25 12:06:33 PDT 2007


On Wed, 2007-07-25 at 17:00 +0200, Johannes Luber wrote:
> jrc at skylon.demon.co.uk wrote:
> > I am using the lexer to split lines into fields using PIPE, SPACE and
> > EQUALS.  After identifying the fields at the start of the line, I want
> > to reassemble the remaining text at the end of the line.  The 'jText'
> > rule recognizes the fields to be reassembled (see below).
> > 
> > However, I am having a problem figuring out how to obtain the '.text'
> > token attributes from the '$ids'.
> > 
> > Please can someone show me the way?
> > ======================================================================
> > jText returns [String text]
> >         :       (ids+=STRING | ids+=EQUALS | ids+=PIPE | ids+=SPACE)*
> >                 {
> >                     $text = "";
> >                     for (int i=0; i<$ids.size(); i++)
> >                         $text += $ids.get(i).text;
> >                 }
> >         ;
> > EQUALS  :       '=' ;
> > PIPE    :       '|' ;
> > SPACE   :       ' ' ;
> > // STRING is printable ASCII excluding SPACE, EQUALS, PIPE, CR and NL
> > STRING  : (~(SPACE | EQUALS | PIPE | '\r' | '\n'))+ ;
> > ======================================================================
> > --
> > John Connett
> 
> I'm not sure which language you are using. In C# you would use the
> "Text" property, in Java (and probably C) the "GetText()" function.
> 
> Best regards,
> Johannes Luber

Many thanks.  I am using Java (but I'm not an experienced Java
programmer).  Here is what I found worked:

        $text = "";
        if ($ids!=null)
        {
            for (int i=0; i<$ids.size(); i++)
            {
                $text += ((Token)$ids.get(i)).getText();
            }
        }

Perhaps there is a neater solution?
--
John Connett




More information about the antlr-interest mailing list