[antlr-interest] grammar guessing.

Ric Klaren ric.klaren at gmail.com
Tue Apr 5 09:40:12 PDT 2005


Bharath Sundararaman wrote:
> I have a rule of the form:
> 
> var: v:IDENT{Get type of "v" and store it in AST #v}(subscripts[#v])?;
> 
> Before the 'subscripts' rule is called, I have to store the type of 'v'.
> However, the compiler is in GUESSING MODE because of (subscripts[#v])?.
> This causes the execution of 'subscripts' rule without performing the
> actions related to the IDENT.

A rule like this cannot result in guessing mode. It must come from an 
encapsulating rule. Only a syntactic predicate ( foo ) => bar switches 
the parser to guessing mode.

If you *really* *really* want to do things during guessing mode you can 
do things in init actions.

var {
	Token v = LT(1);
	// do stuff for v regardless off guessing mode.
	// ...
}:
	IDENT (subscripts[v])?;

Check the antlr grammar/docs on where init actions are possible.

Most probable an extra pass over the AST would be nicer in the long run 
YMMV.

Cheers,

Ric


More information about the antlr-interest mailing list