[antlr-interest] [3.1.1] ANTLR3_MIN_TOKEN_TYPE define possibly incorrect

Jim Idle jimi at temporal-wave.com
Thu Mar 26 07:58:43 PDT 2009


Sven Van Echelpoel wrote:
> +- A
> +- B
> +- C
> +- C
> +- ALTERNATION
> |  +- GROUP
> |  |  +- D
> |  +- GROUP
> |     +- E
> +- C
> +- ALTERNATION
> |  +- GROUP
> |     +- C
> |  +- GROUP
> |     +- C
> + C
>
> Simplified, I have expressed this like so:
>
> element_list : 
>   element+ 
> ;
>
> element : 
>   A
> | B
> | C
> | alternation
> |  // ... more elements and operators that look like 
>    // ^( OPERATOR element_list )
> ;
> 	
> alternation :
> | ^( ALTERNATION group+ )
> ;
>
> group :
>   ^( GROUP element_list )
> ;
>
> I have been unable to imagine a pattern that would single out those C's
> that need a P inbetween. I would guess that the compiler would complain
> about multiple alternatives for input... if I were to add special case
> for two consecutive C's, right? 
>   
It's difficult to see exactly from your examples of course, but I think 
that that is what you want to do. Assuming that you are only looking for 
C C, then a simple syntactic predicate shoudl suffice:

element : 
  A
| B
| C   (   (C)=> C  {special stuff; }
        |          {something else do nothing}
      )
| etc.

You can also set a flag to say whether you are in an alternation by using a scope at the element_list level like this:

element_list 
scope
{
  ANTLR3_BOOLEAN isAlternation;
}
@init
{
   $element_list::isAlternation = ANTLR3_FALSE;
}
: 


...

alternation

:  ^( ALTERNATION { $element_list:isAlternation = ANTLR3_TRUE; } group+ )

and then testing that flag.

This might not be exactly what you are looking for, but it is close I think.

Jim





More information about the antlr-interest mailing list