[antlr-interest] Error nodes created upon syntax error

Johannes Luber jaluber at gmx.de
Mon Dec 3 05:05:57 PST 2007


Terence Parr wrote:
> hi,
> 
> Currently syntax errors cause invalid trees and possibly even runtime
> exceptions when building ASTs. What we really need I believe is to have
> rules that encounter syntax errors return an ERROR node of some sort
> that records where the error occurred and, with luck, the tokens
> consumed during recovery. I started an improvement request:
> 
> http://www.antlr.org:8888/browse/ANTLR-193
> 
> The basic idea is that ERROR nodes get used in place of ASTs that would
> normally be produced by rule indications.  For example, the following
> rule would return a valid AST except for the subtrees associated with
> rule refs in encountering syntax errors:
> 
> forDecl : 'for' '(' decl ';' expr ';' expr ')' stat -> ... ;
> 
> If there is an error inside decl, the tree would return
> 
> ^('for' ERROR subtree-expr subtree-expr)
> 
> This effectively means that I must turn off the single token insertion
> and deletion that occurs automatically within a single rule.  If a
> syntax error occurs, the immediately surrounding rule must terminate in
> return an error node.
> 
> Does this make sense? I would like to stick this into 3.1 release.

It makes sense, but what about single deletion/insertion in normal mode?
How do we deal with an ERROR node in the next grammar? Or is then there
a way to emit an error message during the construction of the AST?

Johannes


More information about the antlr-interest mailing list