[antlr-interest] [C++ Target] Error recovery and rule actions

kjam pohilets at gmail.com
Sun Sep 9 05:27:02 PDT 2012


Hi All,

My parser crashes in user action on invalid input. I do not care much about
error handling for now, but crashing is definitely not an option.

I'm trying to parse fragment 'a+*' according to the following rules
(simplified):

expr3 returns [DataPtr retVal]
    : lhs=expr2 { $retVal = lhs; }
      ( opToken=('+' | '-')
        rhs=expr2
        {
            $retVal = buildInfixOp($retVal, opToken, rhs);
        }
      )*
    ;
expr2 returns [DataPtr retVal]
    : opToken=('!' | '*') e=expr2
      {
         $retVal = buildPrefixOp(opToken, e); // <- the problem is here
      }
    | e=expr1
      {
         $retVal = e;
      }
    ;

expr1 returns [DataPtr retVal]
    : lit=literal
    {
        $retVal = ... ;
    }
    | idToken=ID
    {
        $retVal = ... ;
    }
    | '(' e=expr3 ')'
    {
        $retVal = e;
    }
    ;

Parser consumes 'a' as LHS, '+' as binary operator and '*' as prefix unary
operator and calls expr2 recursively. Inside second call to expr2, parser
encounters EOF, generates error, recovers and returns.

So, inside first invocation of expr2 there are no unhandled errors, for
parsing continues and rule action is executed. But DataPtr (a typedef for
boost::shared_ptr<>) returned from second invocation of expr2 is NULL, so
rule action crashes.

I expected expr2 to either return non-null DataPtr or raise an error. How
can I achieve this? Or should I use different strategy? Checking manually
validity of data returned from nested rule is tedious and error prone, I'd
like to avoid this.



--
View this message in context: http://antlr.1301665.n2.nabble.com/C-Target-Error-recovery-and-rule-actions-tp7578694.html
Sent from the ANTLR mailing list archive at Nabble.com.


More information about the antlr-interest mailing list