[antlr-interest] How to force a TreeWalker to match exactly?

Koehne Kai Kai.Koehne at student.hpi.uni-potsdam.de
Mon Mar 20 10:43:24 PST 2006

I have used AntLR for a while now, and thought I knew most of the traps. I was wrong :-(
I am working on a compiler that started as a multi-person project. We decided to separate the different compiler stages in different grammars: A lexer generates tokens, a parser constructs an AST, which all the following stages consume. These stages are mostly written as TreeWalkers, which all inherit from an abstract TreeWalker grammar describing the output of the parser ... I think this is the standard way to do it with AntLR.
However, I just detected that the compiler fails to reject a lot of incorrect programs! This happens because the parser sometimes generates AST nodes that the TreeWalker grammar just silently ignores ... First I thought that there is something wrong with AntLR or with my error-handling code. But then I reread the documentation, and there it is:
"An important thing to remember when specifying tree patterns and tree grammars in general is that sufficient matches are done not exact matches. As long as the tree satistfies the pattern, a match is reported, regardless of how much is left unparsed."
This can be very annoying! If there is an inconsistency between the Parser and the TreeWalker grammar, there is very little chance to detect it. Instead, unknown nodes are sometimes silently ignored. Is there a way to turn off this feature? How do you ensure that the TreeWalker grammars really correspond to the AST generated by the parser?
Kai Koehne

More information about the antlr-interest mailing list