[antlr-interest] Problem with Self 4.1 grammar

Carter Cheng carter_cheng at yahoo.com
Mon Mar 24 22:37:45 PDT 2008


Thanks for the input. The problem I am having is that it seems that ANTLR in this case with backtracking switched on still misses one of the alternatives and returns a no viable alt. error when the decision is nondeterministic. Whereas if I disable the '|' token as an operator it becomes deterministic and parses correctly. The decision branch in this case is obviously buried in different rules so the alternative is not manifest. I am curious if there is a simple way in this case to add the syntactic predicate back in without refactoring the rules.   

--- On Mon, 3/24/08, Gavin Lambert <antlr at mirality.co.nz> wrote:

> From: Gavin Lambert <antlr at mirality.co.nz>
> Subject: Re: [antlr-interest] Problem with Self 4.1 grammar
> To: carter_cheng at yahoo.com, antlr-interest at antlr.org
> Date: Monday, March 24, 2008, 5:40 AM
> 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 +.


      ____________________________________________________________________________________
Never miss a thing.  Make Yahoo your home page. 
http://www.yahoo.com/r/hs


More information about the antlr-interest mailing list