[antlr-interest] Pruning malformed ASTs

Kevin Carroll kcarroll at signmgmt.com
Fri Jun 4 09:57:22 PDT 2010


I've run into a similar situation.  Basically, I needed to do the same thing as you - i.e., completely remove a node from arbitrary places in the tree.  I had trouble coming up with a solution using Antlr tree grammars, so I just wrote a simple recursive method to strip out the unwanted nodes.  In my case, running this method is the last link in the chain, so I'm only interested in the Ast.  I believe you will have to create a new CommonTreeNodeStream using the pruned Ast prior to passing it on to your tree grammar parser.  I've quickly converted it here to use CommonTree nodes as the ast type.  Hope this helps (If any ANTLR guru's know of a better, more elegant solution, please chime in):

private static bool RemoveErrorNodes(CommonTree ast)
    {
    if (ast is CommonErrorNode)
        {
        ((CommonTree) ast.Parent).DeleteChild(ast.ChildIndex);
        return true;
        }

    for (int i = 0; i < ast.ChildCount; ++i)
        if (RemoveErrorNodes((CommonTree) ast.GetChild(i)))
            --i;

    return false;
    }



-----Original Message-----
From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-bounces at antlr.org] On Behalf Of Richard Thrippleton
Sent: Friday, June 04, 2010 11:30 AM
To: ANTLR Interest
Subject: [antlr-interest] Pruning malformed ASTs

I am in the slightly painful situation of wanting to apply a tree grammar 
(type and other constraints checking) after a parser grammar even in the 
case that there were parse errors.

At the moment I just have the tree grammar exit at the moment that it 
encounters a malformed part of the tree; as I've seen stated on here before, 
there's no other sensible thing to do.

Sometimes I'm fortunate to have simple parse errors that still result in a 
correct tree (e.g. standalone unexpected tokens just get removed in 
recovery). For the larger parse errors I'll get an AST containing 
CommonErrorNodes which obviously trip up the tree grammar.

What I'm looking for is some common pattern or part of the ANTLR API that 
will allow the parser to snip subtrees at a specific granularity that 
contain error nodes as I create them. For example:

procedure:
	command* -> ^(PROC command*)

needs to ignore any tree coming out of the 'command' rule that contains 
error nodes.

Thanks,
Richard
-- 
\o/

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address


More information about the antlr-interest mailing list