[antlr-interest] DIY AST error recovery problem

Ross Bencina rbencina at iprimus.com.au
Sat May 3 22:08:39 PDT 2003


I'm constructing my own AST (not using ANTLR's built in tree building) and
have come accross a problem with my code when a parse error happens...

---
ident           returns [ std::auto_ptr<IdentifierNode> result ]
                : a:IDENT
                {
                    result.reset( new IdentifierNode(
                        SRCLOC(a),
                        Identifier( a->getText().c_str() );
                }
                ;

name            returns [ std::auto_ptr<NameDeclaration> result ]
                {
                    std::auto_ptr<IdentifierNode> a;
                    SaolConstants::Width b = SaolConstants::SINGLE_VALUE;
                }
                : a=ident ( LBRACK b=arrayLength RBRACK )?
                    {
                        SourceLocation srcLoc = a->sourceLocation; //
***crash
                        result.reset( new NameDeclaration( srcLoc, a, b ) );
                    }
                ;

The above code is for parsing declarations like the following:

ivar x, illegal, y[10];

The caller of name() looks like:

namelist        returns [ std::auto_ptr<NameList> result ]
                {
                    result.reset( new NameList() );
                    std::auto_ptr<NameDeclaration> a, b;
                }
                : a=name { result->push_back( a.release() ); }
                    ( COMMA b=name { result->push_back( b.release() ); }
                    )*
                {
                    result->sourceLocationBegin =
result->front()->sourceLocation;
                }
    ;

Ideally, if ident() or name() fails, then the illegal NameDeclaration simply
wouldn't be added to the NameList.
---

At the moment, if the ident rule fails, then ident() returns an empty (null)
auto_ptr and the a-> expression in name() crashes. I could explicitly check
return values for null before using them, but right now I don't understand
the problem enough to know which return values to check - I would prefer not
to have to check every rule's return value for NULL, although I have a
sinking feeling that's what I will have to do.

What would be better is if ANTLR's error recovery wound back further up the
stack - could anyone advise of the best way to do this, or any other
possible approaches?

Is there an explanation of what an ANTLR parser does if it encounters

Thanks in advance

Ross.





 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 




More information about the antlr-interest mailing list