[antlr-interest] syntactic predicates vs. backtrack=true

Loring Craymer lgcraymer at yahoo.com
Tue Feb 5 16:55:37 PST 2008


--- Mark Volkmann <r.mark.volkmann at gmail.com> wrote:

> On Feb 5, 2008 10:53 AM, Jim Idle
> <jimi at temporal-wave.com> wrote:
> > In my opinion you should never use backtracking
> unless performance is
> > not a big deal for you. It is good for prototyping
> and as you say it can
> > make the specification of your grammar
> easier/neater.
> 
> I snipped out a lot of your post which was
> excellent. However, I'm
> still confused by this statement in the book.
> 
> "ANTLR also supports an auto-backtracking feature
> whereby ANTLR
> inserts syntactic predicates on the left edge of
> every alternative."
> 
> To me this statement implies that the backtrack
> option is just a
> shortcut for inserting syntactic predicates. Doesn't
> that mean that
> these rules are equivalent in both behavior and
> performance? If not, can someone explain how they
> differ at runtime?
> Note that I'm specifying the backtrack option on a
> specific rule, not
> on the entire grammar.
> 
Well, almost.  Backtracking inserts synpreds at all
decision points, and the analysis engine tries to
remove unnecessary ones.  More importantly, though,
the use of backtracking masks the cases where rules
can be refactored.  Manually inserted synpreds (or
rule/subrule backtracking) are the better approach for
production grammars because the effort of fine-tuning
leads to significant performance optimizations.  If
you are doing something quick and dirty,
backtrack=true can lessen development time.  For
production grammars, though, Jim's "never use the
backtrack option at the grammar level" is right on
target.

--Loring


      ____________________________________________________________________________________
Looking for last minute shopping deals?  
Find them fast with Yahoo! Search.  http://tools.search.yahoo.com/newsearch/category.php?category=shopping


More information about the antlr-interest mailing list