[antlr-interest] Treewalking : non-determinism error

Ric Klaren klaren at cs.utwente.nl
Tue Apr 1 06:39:44 PST 2003


Hi,

On Tue, Apr 01, 2003 at 11:56:28AM +0100, Anthony Youngman wrote:
> I think I know what the problem is, I just don't know how to fix it ...
> 
> In my main parser, I have the following two rules:
> 
> printst : (pr:"PRINT"^ expr (COLONPRINT)? {System.out.println(pr+" print
> ");} );
> expr : ( catexpr | logicexpr ) ;
> 
> This seems to be fine - the traces are printing what I expect.
> 
> In my treewalker I then have the rule:
> 
> printst
> 	: #(PRINT expr COLONPRINT) {System.out.println("PRNT");}
> 	| #(PRINT expr) {System.out.println("OUTP");}
> 	;

Provided I don't have a coffee shortage or overdose ;)

Tree parsers only use a lookahead of 1 so the above rule will indeed give
nondeterminism. One way to fix it might be:

printst
 	: ( #(PRINT expr COLONPRINT) ) => #(PRINT expr COLONPRINT) {System.out.println("PRNT");}
 	| #(PRINT expr) {System.out.println("OUTP");}
 	;

Alternatively:

printst { bool colon = false; }
	: #(PRINT expr ( COLONPRINT { colon = true; } )? )
		{
			if( colon )
				System.out.println("PRNT");
			else
				System.out.println("OUTP");
		}
	;

Cheers,

Ric
-- 
-----+++++*****************************************************+++++++++-------
    ---- Ric Klaren ----- j.klaren at utwente.nl ----- +31 53 4893722  ----
-----+++++*****************************************************+++++++++-------
  "You know how to use that thing?" [pointing to the sword]
  "Sure.. The pointy end goes into the other guy."
  --- The Mask of Zorro


 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 



More information about the antlr-interest mailing list