[antlr-interest] error recovery in top level repeated rules?

Juancarlo Añez apalala at gmail.com
Sat Sep 8 04:37:15 PDT 2012


Jesus,

The traditional way to write a grammar that can parse invalid input is to
augment it with rules that skip input until the next reasonable follow up
token.

For example, when parsing an expression within parentheses, an addtional:

   | e=(.*) RPAREN { parse_error($e); } ;

will match anything until the next right parenthesis, report the error, and
allow previously invoked rules to try to continue parsing the input.

Cheers,

-- Juancarlo



On Fri, Sep 7, 2012 at 9:28 PM, JesusFreke <jesusfreke at jesusfreke.com>wrote:

> Is there any way to recover from an error (say, an extra unwanted token),
> in a top level repeated rule?
>
> For example, consider the following simple grammar
>
> top : (('a' 'c') | ('b' 'c'))*;
>
> For input acabc", it parses 'ac', produces an error on 'a', and then
> contiues on with 'bc'. This is perfect.
>
> But for input "accbc", it parses 'ac', and then fails at 'c' and stops
> parsing. Is there any way to get it to produce an error for the extra 'c',
> and then continue on parsing the remaining 'bc'?
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe:
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>



-- 
Juancarlo *Añez*


More information about the antlr-interest mailing list