[antlr-interest] A little problem with DOT in numbers and as line terminators.

Eric Merritt cyberlync at gmail.com
Thu Mar 10 14:52:16 PST 2005


I am using a dot as both a decimal separator in numbers and a
statement terminator. I run into a problem when a number is at the end
of a statement. For example

 origin := 0 @ 0.

Gives me huge problems because the NUMBER rule consumes the '.' and
automatically expects another digit. When it doesn't find one it dies
because it doesn't find the expected digit. What I need to to do is
check the character after the '.' to see if it is a number. If a
number follows it should continue to consume normally. If not it
should unconsume the '.' and terminate the number rule. That would
allow the '.' to be consumed by the DOT rule and form the expected
statement terminator. Of course, I will still have problems with

 origin := 0 @ 0.
 0 + 33.

but I will cross that bridge when I come to it.

I played around with semantic predicates and these will allow me to
look ahead, but these will only allow the rule to continue or an
exception to be thrown. Throwing an exception isn't the right
solution. Following is the number rule, I will gladly provide the
entire grammar if there is a need.




// a numeric literal
NUMBER
  : (DIGIT DIGIT 'r')? (DIGIT)+ ('.' (DIGIT)+ EXPONENT)?
  ;

protected 
DIGIT
  : ('0'..'9')
  ;

protected
EXPONENT
   :  ('e'|'E') ('+'|'-')? ('0'..'9')+
   ;




-- 
I'm a programmer, I don't have to spell correctly; I just have to
spell consistently


More information about the antlr-interest mailing list