[antlr-interest] DIY AST error recovery problem

Ric Klaren klaren at cs.utwente.nl
Tue May 6 01:37:06 PDT 2003


On Sun, May 04, 2003 at 03:08:39PM +1000, Ross Bencina wrote:
> 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.

I'm assuming you are using the default error handling mechanism? If the
error over a IDENT is handled at the end of the ident rule by the default
error handler it will return a null pointer.

You might get away with turning of the default (or the complete)
errorhandlers for the ident rule. That way the parse error is escalated to
the name rule and the action for with the list will not be performed. Since
ident's error is handled at the end of the name rule.

ident returns [ std::auto_ptr<IdentifierNode> result ]
        options { defaultErrorHandler = false; }
: .......		  

Depends a bit on other rules if this works 100%. Look at the generated code
for ident to get a feel for the effect of this.

Cheers,

Ric
--
-----+++++*****************************************************+++++++++-------
    ---- Ric Klaren ----- j.klaren at utwente.nl ----- +31 53 4893722  ----
-----+++++*****************************************************+++++++++-------
  Wo das Chaos auf die Ordnung trifft, gewinnt meist das Chaos, weil es
  besser organisiert ist. --- Friedrich Nietzsche


 

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




More information about the antlr-interest mailing list