[antlr-interest] Problem with Self 4.1 grammar

Gavin Lambert antlr at mirality.co.nz
Mon Mar 24 05:40:29 PDT 2008


At 01:14 25/03/2008, Carter Cheng wrote:
 >I have one question about backtracking though which hopefully 
can
 >fix the last remaining problem which I have at the moment sort 
of
 >hacked around by disabling an operator.
[...]
 >If I remove | from the operator list the code parses properly. I 
am
 >curious why ANTLR can't backtrack this. Is there a way to force 
the
 >parser to prefer the non-operator interpretation?

By default ANTLR generates code for performance, and so won't 
backtrack unless you specifically add syntactic predicates.  If 
you want it to do backtracking, then you'll either need to 
manually add predicates wherever needed or set 'backtrack=true' in 
the options (which will implicitly add predicates to every alt).

 >The relevant parse rules are as follows-
 >
 >block
 >	: '[' code? ']' -> ^(BLOCK code?)
 >	| '[' '|' slotList? '|' code? ']' -> ^(BLOCK slotList? code?)
 >	;
 >
 >slotList
 >	: ( unannotatedSlotList | annotatedSlotList )*
 >	;

Note: you're specifying optionality twice here (slotList is 
optional and the rule itself is allowed to match nothing).  This 
is generally a bad idea; you should either remove the ? or change 
the * to a +.



More information about the antlr-interest mailing list