[antlr-interest] How to get and use AST after parse error?

Graham Wideman gwlist at grahamwideman.com
Wed Jun 24 14:24:16 PDT 2009


Folks -- 

I'd appreciate if someone could straighten me out on what is The Intended Way on the following issue:

I'm working on a PHP grammar that produces an AST, testing it against various source code.  

When a parse succeeds, I get a nice AST returned from PhpParser.prog() via prog_return.getTree(). However, if the parse fails, then what's returned via prog_return.getTree() is a tree with just an error node (and this is readily understood from the generated source in PhpParser.prog()).

So:

a) In case of error, is there an intended way of getting access to the actual AST?  I hacked an extra public field into PhpParser to get access to the AST root_0 node, and it seems to be useful, but I would not have expected to need to do that.

b) Is there some reason why I should not want to do that?

c) Why isn't prog() designed to just return the actual AST as usual and provide error info by separate means?

d) I note that prior to normal exits, the PhpParser.prog() refines the completed AST using rulePostProcessing, and setTokenBoundaries. For an AST that's partially complete due to error exit, these still make sense to apply, right?

Thanks,

-- Graham

-------------------
Grammar info:
The PHP grammar is similar to that listed at antlr.org (kuruvila) -- pretty vanilla:
---------------
options {
    backtrack = true; 
    memoize = true;
    k=2;
    output = AST;
    ASTLabelType = CommonTree;
}
---------------



More information about the antlr-interest mailing list