[antlr-interest] Parameters in fragment lexer rules

Jim Idle jimi at temporal-wave.com
Tue Aug 3 09:29:47 PDT 2010


You need to make the distinction in the lexer via island grammars. Your case
looks like it will be easy enough but you might need to formulate your lexer
rules to avoid ambiguous cases. Look at the example island grammar in the
downloadable example set.

For your lexer you will need something like this:

ARROWLBRACE
   : '->'
      (
             (WS* '{')=> WS* '{' SPECIFICPARSER { $type = SPECIFICBLOCK; }
          |
      )
   ;

COLON
  : ':'
       (
             (WS* '{')=> WS* '{' DIFFERENTPARSER { $type = DIFFERENTBLOCK; }
          |
      )
  ;

LBRACE 
  : '{' // Either the parser that calls this lexer knows what to do with
Java, or you call a java parser here
  ;

Jim
  
> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Jean-Christophe Bach
> Sent: Tuesday, August 03, 2010 5:50 AM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] Parameters in fragment lexer rules
> 
> Hi list,
> 
> I am rewriting our old parser and I use antlr3 for that.
> Since I have few problem to handle the '{' and to call the appropriate
parser
> depending on the context, I am wondering if fragment lexer rule + a
> parameter could help me.
> There are many situations, but I write here 3 cases :
> ... '->' '{' ... : I need to call a specific parser (#1) ... ':'  '{' ...
: I need to call
> ainother specific parser (#2)
> ...      '{' ... : I need to do a simple Java treatment
> 
> I read few articles and the antlr book, and I saw that it was possible to
do give
> parameters to a fragment lexer rule. I am wondering if something like that
is
> OK :
> 
> ARROWLBRACE : '->' LBRACE[2] ;
> ...
> <other rules with LBRACE[n]>
> ...
> fragment
> LBRACE[int lbtype] : '{'
>   {
>   switch(lbtype) {
>   case 1:
>     <Java code1>
>   case 2:
>     <Java code2>
>   case 3:
>     <Java code3>
>     ...
>   }
>   }
>   ;
> 
> But am I also allowed to write a parser rule containing a LBRACE[n] or is
it
> totally illegal ?
> e.g. :
> 
> myRule :
>  ... LBRACE[1] ... -> ...
>  |... ARROWLBRACE ... -> ...
>  ;
> 
> When attempting to do that, I have errors :
> "token reference LBRACE may not have parameters"
> 
> Is there any good way to solve this type of problem ?
> 
> Thanks in advance,
> 
> JC
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> email-address



More information about the antlr-interest mailing list