[antlr-interest] OR alternatives
    g4 at novadsp.com 
    g4 at novadsp.com
       
    Mon Feb 28 14:35:17 PST 2011
    
    
  
The CoCo grammar expression spec is shown below [2].  My problem is with 
constructions such as these:
CS = { ExternAliasDirective }
         { UsingDirective }
         EOF .
CS results in an AST with a OR_EXPR node although no '|' character 
actually appears. I'm sure this is due to my simplistic definition of 
expression but I cannot see any other way to write the rules.
I did experiment with [1] to resolve the ambiguity. It does work but the 
hack seems only to reinforce the fact that the basic construction is wrong.
Any tips much appreciated.
Jerry.
[1] expression
@init { bool ored = false; }
     : term {ored = (input.LT(1).Type == OR); } (OR term)*
         ->  {ored}? ^(OR_EXPR term term*)
         ->            ^(LIST term term*)
[2] rule '=' expression '.' ;
expression
     : term ('|' term)*
         -> ^( OR_EXPR term term* )
     ;
term
     : (factor (factor)*)? ;
factor
     : symbol
     | '(' expression ')'
         -> ^( GROUPED_EXPR expression )
     | '[' expression']'
         -> ^( OPTIONAL_EXPR expression)
     | '{' expression '}'
         -> ^( SEQUENCE_EXPR expression)
     ;
symbol
     : IF_ACTION
     | ID (ATTRIBUTES)?
     | STRINGLITERAL
     ;
    
    
More information about the antlr-interest
mailing list