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

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: 
http://www.antlr.org/mailman/options/antlr-interest/your-email-address

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"il-antlr-interest" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to