[antlr-interest] backtracking works :)

Terence Parr parrt at cs.usfca.edu
Fri Dec 2 20:27:22 PST 2005


Ok, 1.5 hours of work and I convinced antlr to generated backtracking  
code for syntactic predicates; naturally, I'll have to find the holes  
tomorrow.  The memoization shouldn't take more than an hour tomorrow  
or when I can get back to this.   I updated the blog as I went:

http://www.antlr.org/blog/antlr3/lookahead.tml

No exceptions used now.  Not even a return value. ;)  The new  
template stuff and v3 code base just kicks butt.

Here is my example:

grammar W;

a : (b)=> b {System.out.println("alt 1");}
   | c       {System.out.println("alt 2");}
   ;

b : '(' 'x' ')' ;

c : '(' c ')' | 'x' ;

WS : (' '|'\n')+ {channel=99;}
    ;

Upon "(x)" I get "alt 1" and with "((x))", I get "alt 2".

Jim and other code generators.  outputFile and headerFile now have  
two new arguments:  backtracking (boolean--somebody can backtrack in  
code), synpreds (names of preds).  See my genericParser template for  
needed output changes.  For all other changes, ANTLR's normal code  
gen facility makes it happen.  Soooo sweet.

I co-opted the semantic predicate mechanism and normal rule  
generation mechanism to make this work.  Yeah!

And all the "we don't want no stinking exceptions for backtracking"  
people can sleep soundly tonight!

Ter


More information about the antlr-interest mailing list