[antlr-interest] Token Mismatch
Brisard, Fred D
Fred.Brisard at ca.com
Thu Jul 24 13:28:18 PDT 2008
Thanks Matt,
I better go reread that section.
Fred Brisard
________________________________
From: Matt Palmer [mailto:mattpalms at gmail.com]
Sent: Thursday, July 24, 2008 4:02 PM
To: Brisard, Fred D
Cc: antlr-interest at antlr.org
Subject: Re: [antlr-interest] Token Mismatch
Hi Fred,
I am also a novice, but I have a copy of the antlr reference to hand,
and this situation is mentioned. The suggestion is to use semantic
predicates to resolve whether the rule is an ID or a keyword.
e.g.
r : keyCALL ID ';' {System.out.println("invoke "+$ID.text);} ;
keyCALL : {input.LT(1).getText().equals("call")}? ID ;
ID: 'a'..'z'+ ;
WS: (' '|'\n'|'\r')+ {$channel=HIDDEN;} ;
The idea is that the keyCALL rule is also just an ID token (so both
match as IDs), but with a semantic predicate that only matches if the
text is explicitly "call". Note: I haven't tested this grammar - I just
modified the example in the book to match your example.
Regards,
Matt Palmer.
On Thu, Jul 24, 2008 at 6:33 PM, Brisard, Fred D <Fred.Brisard at ca.com>
wrote:
I'm sure this is a novice question but I can't seem to find an answer
that I feel comfortable with.
It seems like any literal specified in the parser rules becomes an
implied token.
Using the basic example T.g -
grammar T;
/** Match things like "call foo;" */
r : 'call' ID ';' {System.out.println("invoke "+$ID.text);} ;
ID: 'a'..'z'+ ;
WS: (' '|'\n'|'\r')+ {$channel=HIDDEN;} ;
I get a MismatchedTokenException I run this in the ANTLRWorks debugger
for the input
call call;
I would like the second call to be identified as a token of type ID.
Any help is appreciated
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080724/f59d0e31/attachment-0001.html
More information about the antlr-interest
mailing list