[antlr-interest] Parameters in fragment lexer rules

Jean-Christophe Bach jeanchristophe.bach at inria.fr
Tue Aug 3 05:49:59 PDT 2010


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


More information about the antlr-interest mailing list