[antlr-interest] How to force error recovery?

Edson Tirelli tirelli at post.com
Mon Oct 4 15:27:28 PDT 2010


   Hi all,

   Look at this simple grammar:

grammar testGrammar;
options {
	output=AST;
}

compilationUnit
	: statement* EOF
	;
	
statement
	:	A^
	|	B^ C
	;	

A   :   'a';

B   :	'b';	

C   :   'c';	

WS  :   ( ' '
        | '\t'
        | '\r'
        | '\n'
        ) {$channel=HIDDEN;}
    ;


    Using the above grammar, it will successfully parse an input like:

a b c a

    Now, if the input is:

a c a

    The generated parser will parse "a", and will fail at "c", as it
is not a valid statement. Reading the error recovery chapter on the
ANTLR book, I would imagine ANTLR would delete/skip the "c" token and
try to recover, successfully parsing the second "a", as that is a
valid statement again. But it is not working like this. It is aborting
the parsing with an error at "c".

    Question: how do I force it to recover from the error and continue parsing?

    The actual scenario is that the parser I am working on is used by
an IDE environment (eclipse), so we need it to continue parsing and
presenting the users with all the errors found in the file, not just
the first one. The error recovery seems to work on some rules, but not
on the top rule (compilationUnit).

    Thanks,
       Edson

-- 
  Edson Tirelli
  JBoss Drools Core Development
  JBoss by Red Hat @ www.jboss.com


More information about the antlr-interest mailing list