[antlr-interest] context information through rule parameters

Gerard van de Glind g.vandeglind at beinformed.nl
Fri Jul 4 00:53:31 PDT 2008


Hi Gavin and all,

Thanks Gavin for your reply!

At 20:04 3/07/2008, Gerard van de Glind wrote:
 >Because of this, my grammar contains two rules that use
 >backtracking. This also implies that it is impossible to
 >replace these by syntactic predicates.

You're mistaken about that -- all backtracking does is to insert 
automatic syntactic predicates, so anything you can do with 
backtracking you can do by writing your own synpreds.

Yes, you are right about that. I know that backtracking inserts
syntactic predicates under the hood. But is this the only thing it does?
I have seen that ANTLR also generates the following statement: if (
backtracking==0 ) So is the behavior of ANTLR with the backtrack option
compared to ANTLR with my own syntactic predicates really the same? And
is it always possible to replace the backtrack option with my own
syntactic predicates in such a way that it accepts the same language?

It might just be more work than what you want to do :)

Please explain, can you give me a hint of what I should do? Sofar, I
didn't succeed in being able to make my grammar to accept the same
language with my own syntactic predicates compared to the backtrack
option.

The grammar I presented is over simplified...in the original grammar I
also use actions and interrule communication through parameter passing.
I just learnt that it is possible to run into a situation where method
arguments result in noncompilable code if you use backtracking and
parameter passing a lot (paragraph 14.7 of The Definitive ANTLR
Reference). A solution to this problem is to use dynamic scopes.

 >	relationalExpression[boolean checkAmbiguity]
 >	  options {backtrack=true;}
 >    		:	formula[true]	(LET^ | GET^ | LT^ | GT^)
 >formula[true]
 >	   	|	dateAtom[true]	(LET^ | GET^ | LT^ | GT^)
 >dateAtom[true]
 >	    	|	booleanAtom[$checkAmbiguity]
 >		;
 >	
 >	formula[boolean bool]
 >		: IDENTIFIER;
 >
 >	dateAtom[boolean bool]
 >		: IDENTIFIER;

Even backtracking isn't going to help you disambiguate those first 
two alts of relationalExpression -- they're exactly identical.

I am aware of the fact that my grammar is ambiguous, but that's
something I have to live with.
I don't want to resolve the ambiguity, I want to recognize it and give a
warning to the end users.

Thanks again!

Cheers, Gerard


More information about the antlr-interest mailing list