[antlr-interest] syntax predicate strange behavior

Jim Idle jimi at temporal-wave.com
Tue May 1 08:34:23 PDT 2007



-----Original Message-----
From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of David Holroyd
Sent: Tuesday, May 01, 2007 12:54 AM
To: antlr-interest at antlr.org
Subject: Re: [antlr-interest] syntax predicate strange behavior

On Tue, May 01, 2007 at 02:03:37AM +0400, Ilia Kantor wrote:
> command_user_body:	
> 	 (LCURL) => LCURL command_arguments? RCURL -> ^(USER_COMMAND
command_arguments?) |		 			
> 	 -> USER_COMMAND
> ;
> 
> On input LCURL WS WORD WS MINUS GT...
> By the way, this works fine:
> 
> command_user_body
> options {backtrack=true;}:	
> 	LCURL command_arguments? RCURL -> ^(USER_COMMAND
command_arguments?) |		 			
> 	 -> USER_COMMAND
> ;

The backtracking option is excellent for prototyping and getting your
grammar in a state where it performs a logically correct parse. This is
because it effectively tries every possible option until it finds one
that works with the input. This is of course a huge over head. However,
for an individual rule, where you can easily see the backtracking
consequences for yourself (but be careful with your analysis ;-), it can
be a useful option.

The fact that backtracking mode works makes me guess [pun intended] that
your syntactic predicates are not quite enough. You can probably try:

: (LCURL) => LCURL ( (WORD)=>command_arguments) ? RCURL
|
;

For the shortest predicate that indicates command_argument is present.
If that does not work then you will have to predicate the whole of
command_arguments perhaps, but in that case you are effectively doing
what backtracking mode does.

The reason you need to predicate command_arguments is unclear from the
context you gave us, but is it possible that that rule itself has an
'empty' option so that you are confusing things by both making it option
in command_user_body AND optional in and of itself?

Jim



More information about the antlr-interest mailing list