[antlr-interest] Option backtrack=true

Johannes Luber jaluber at gmx.de
Tue Jan 15 16:03:38 PST 2008


Stephanie Balzer schrieb:
> Hi all,
> 
> I am new to ANTLR and use ANTLRWorks to develop a compiler for my 
> programming language. I based my grammar on the Java 1.5 grammar for 
> ANTLR v3. Thus, I took over the options
> 
> k=2; backtrack=true; memoize=true
> 
> With above options, I was able to generate the parser successfully. 
> However, when I started debugging some programming language 
> statements/expressions the parser seemed to simply ignore some of the 
> rules and didn't produce the result I expected. I guess this has to do 
> with the syntactic predicates that are assigned to productions.
> 
> Now my questions:
> 
> - What does this option actually mean?

<http://en.wikipedia.org/wiki/Backtracking> explains what backtracking 
is doing in general.

> - Is it advisable not use the backtracking option?

It is slower because every rule reference is guarded by a predicate. It 
also hides errors, so I wouldn't use it, if I can help it.

> - If I use the backtracking option, how can I influence these predicates?

I believe that putting an own predicate in front of a rule makes ANTLR 
leaving it untouched.
> 
> I also tried to remove the backtracking option. In this case, however, I 
> get a lot of warnings when generating the parser. To resolve these 
> warnings I started to left factor my rules. But this seems to be a 
> endless story!
> 
> So, another question:
> 
> - Is left factoring the only solution to deal with the warnings (besides 
> enabling backtracking)?

No. You can use also syntactic predicates. I've attached a draft of a 
tutorial dealing with this issue. I'm very interested in feedback before 
I put it into the wiki. I'm not confident that I've got all the rough 
edges yet.

Johannes


-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: NoBacktrackingTutorial.txt
Url: http://www.antlr.org/pipermail/antlr-interest/attachments/20080116/2ed400d5/attachment-0001.txt 


More information about the antlr-interest mailing list