[antlr-interest] Creating tokens on error

Gavin Lambert antlr at mirality.co.nz
Mon Apr 7 13:45:24 PDT 2008


At 05:41 7/04/2008, Marko Simovic wrote:
 >If I have a parser rule for addition that looks like this:
 >
 >sum	:	something (Plus^ something)*;
 >
 >it works fine for input like:
 >
 >a+b
 >
 >however, if I have input such as:
 >
 >a+
 >
 >which is invalid syntax (and causes a NoViableAltException), I
 >would like the parser to generate a "placeholder" token for the
 >missing operand. Is this possible?

Untested, but something like this ought to do the trick:

sum
   : ( a=something -> $a )
     ( Plus
       ( b=something -> ^(Plus $sum $b)
       | -> ^(Plus $sum ERROR) )
     )*
   ;



More information about the antlr-interest mailing list