[antlr-interest] baffling grammar behavior

Jim Idle jimi at temporal-wave.com
Fri Feb 18 19:11:53 PST 2011


It is probably that the interpreter cannot deal with it to be honest. Use
the debugger rather than the interpreter.

Jim

> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Tom Fiddaman
> Sent: Friday, February 18, 2011 5:13 PM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] baffling grammar behavior
>
> 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
>
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> email-address


More information about the antlr-interest mailing list