[antlr-interest] Island grammars in Antlr 2?

Fiid Williams fiid at fiid.net
Thu Nov 17 09:55:24 PST 2005


I did something like this for a Cisco IOS parser I am working on for  
the banner keyword.  Used in the following way

banner &
freeform banner text 1
freeform banner text 2
&

(so & is the delimiter, which can be any character).  It's a little  
ugly but:

"banner"  delim=one_token  {
             // eat tokens until the end of the banner.
             String delimchar = delim.getText().substring(0,1);
             while(true) {
                 Token t = LT(1);
                 if(t.getText().contains(delimchar)) {
                     break;
                 }
                 consume();
             }
             consume();
         };

You could concatenate the strings you get back from token t to get  
the full text of what's between the braces.  one_token matches  
everything except a newline (which is significant in my grammar.).

This might be a terrible solution - just throwing it out to see what  
people think.

Fiid.




On Nov 17, 2005, at 9:16 AM, Pete Gonzalez wrote:

> I need to parse something like this:
>
>     block {
>       block {
>       }
>       exec {
>         freeform text
>       }
>     }
>
> That is, I want to collect the raw text inside the "exec" block  
> (i.e. [^}]*) and return it as a single token, which will be parsed  
> by separately by a different grammar.  I know that this is  
> addressed explicitly in Antlr 3, but is it possible with Antlr 2?
>
> Cheers,
> -Pete
>
>



More information about the antlr-interest mailing list