[antlr-interest] Matching braces in grammar

Johannes Luber jaluber at gmx.de
Mon May 21 04:28:08 PDT 2007


Jukkis wrote:
> Hello all ANTLR fans!
> 
> I'm developing a small language with ANTLR. One feature is that my language can have BeanShell code written into the language in special BeanShell blocks.
> 
> Currently, I have a special kind of statement which takes the BeanShell code (which is essentially Java):
> 
> ${"print(\"Hey, I'm BeanShell code\");"}
> 
> defined in my grammar as:
> 
> beanshell_statement
>     : "$" LCURLY! STRING_LITERAL RCURLY!
>     ;
> 
> The problem is that BeanShell code may contain the symbol '}' which I use to terminate the statement. Currently, I use STRING_LITERAL to work around this fact.
> 
> Now, what I would want is that there would be no need to write the BeanShell code inside a string. How can I make ANTLR understand that it would consider any curlys found INSIDE the MATCHING '{' ... '}' pair as just ordinary text?
> 
> Thank you very much for any advice!

Basically you need to replace STRING_LITERAL with something like that:

beanshell_statement
     : "$" LCURLY! block+ RCURLY!
     ;

block
   :   text
   |   LCURLY text RCURLY
   ;

Best regards,
Johannes Luber


More information about the antlr-interest mailing list