[antlr-interest] baffling grammar behavior
Tom Fiddaman
tom at metasd.com
Fri Feb 18 17:12:50 PST 2011
I've built a grammar for much of a declarative simulation language.
Everything works, except this bit that I've distilled as much as possible:
grammar test;
input : VARNAME IS LITERAL
;
VARNAME : VCHAR (SP? VCHAR)*
;
fragment
VCHAR : ('a'..'z'|'A'..'Z')
;
fragment
SP : (' ')
;
IS : ':IS:'
;
LITERAL: '\'' ( options {greedy=false;} : . )* '\''
;
WS : ( ' '
| '\t'
| '\n'
| '\r'
) {$channel=HIDDEN;}
;
When I feed the interpreter a typical string in ANTLRworks, like
y :IS: 'frank'
the first token matched (a VARNAME) is
y :IS
followed by a MismatchedTokenException for the rest.
The initial match doesn't make sense to me, because : isn't a possible
character of my VARNAME. I'm expecting my rules to first pick up the y as a
VARNAME, then consume the space as WS on the hidden channel, then grab the
:IS: operator.
This works fine if I eliminate the space before the :IS: in the input, or
simplify the VARNAME to just VARNAME : VCHAR ; or VARNAME : VCHAR (VCHAR)* ;
but unfortunately the target language permits spaces in variable names, so I
need the optional space SP?.
I'm hoping that I'm just making a dumb noob syntax error, but any
suggestions would be much appreciated.
Tom
More information about the antlr-interest
mailing list