[antlr-interest] Mismatched token problem
Richard Wallace
rwallace at thewallacepack.net
Tue Jan 13 15:49:29 PST 2009
Hello,
I am trying to write a rule to match expressions in the following algebraic form
an+b
But, when the b term is negative it is only allowed to be written as
an-b
It seems easy enough, the problem is that identifiers can have the '-'
character in them. So I have the following in my grammar
expr
: DASH? NUMBER? 'n' S* ( PLUS | DASH ) S* NUMBER
;
DASH
: '-'
;
PLUS
: '+'
;
IDENT
: ('_' | 'a'..'z'| 'A'..'Z' | '\u0100'..'\ufffe' )
('_' | DASH | 'a'..'z'| 'A'..'Z' | '\u0100'..'\ufffe' |
'0'..'9')*
| DASH ('_' | 'a'..'z'| 'A'..'Z' | '\u0100'..'\ufffe' )
('_' | DASH | 'a'..'z'| 'A'..'Z' | '\u0100'..'\ufffe' |
'0'..'9')*
;
NUMBER
: '-' (('0'..'9')* '.')? ('0'..'9')+
| (('0'..'9')* '.')? ('0'..'9')+
;
S
: ( ' ' | '\t' | '\r' | '\n' | '\f' )
;
So, when I try this grammar against 4n+3 it works great. But, if I
try it against 4n-1 it fails with a MismatchedTokenException. This
seems to be because when evaluating 4n-1 antlr matches the expression
as NUMBER IDENT instead of NUMBER 'n' DASH NUMBER. I've tried
changing the lookahead and using backtracking all to no avail. I'm
out of ideas on how to make antlr stop seeing the n-1 as an IDENT and
instead see it as 'n' DASH NUMBER. Any suggestions?
Thanks,
Rich
More information about the antlr-interest
mailing list