[antlr-interest] [C++ Target] Error recovery and rule actions
Jim Idle
jimi at temporal-wave.com
Sun Sep 9 08:31:37 PDT 2012
Well your build<n>fixOp should be checking for NULL parameters as a matter
of good programming. At some point in error recovery, the parser will be
back on track but some of the values won't be set to anything. You could
set a flag in your error handler and just have all your methods return 0
if it is set.
Jim
> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of kjam
> Sent: Sunday, September 09, 2012 5:27 AM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] [C++ Target] Error recovery and rule actions
>
> 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.
>
> 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