[antlr-interest] Re: Custom error recovery

Alexey Demakov demakov at ispras.ru
Wed Jul 27 22:33:12 PDT 2005


----- Original Message ----- 
From: "Martin Olsson" <mnemo at minimum.se>
To: <antlr-interest at antlr.org>
Sent: Thursday, July 28, 2005 5:14 AM
Subject: [antlr-interest] Re: Custom error recovery


> In particular I'm not really sure I understand the default syntax error
> recovery strategy used by ANTLR. Maybe someone could explain it in plain
> english, that would be great.

http://www.antlr.org/doc/err.html#ParserExceptionHandling

"The default exception handler will report an error, 
sync to the follow set of the rule, and return from that rule."

It means, when syntax error occurs, ANTLR will skip all tokens
until one from the follow set of the current rule. For your example:

> int a;
> print "hello";
> int var1, var2, var3;
> int x = 1+1;
> nondatatype whatever not valid tokens;
> int c;
> int more, vars;

in line 5 parser expects statement. After error message it will skip
all tokens until 'int', 'print' or something else that can start
next statement. Not until ';' because it is part of statement rule.
May be it is not bad - what if ';' is missed? Whole next statement will be skipped.

> public void reportError(RecognitionException e) {
>    try {
>      Token errorToken = LT(0);
>    } catch (TokenStreamException e) { /* ignored for now */ }
>    editor.displayError(e.getMessage(), e.getLine(),
>                        e.getColumn(), errorToken.getLength());
> }
> 
> The problem with this approach was that the Token class apparently does
> not have a getLength() method -- thus I didn't get this experiment very
> far.

Please read ANTLR generate? and library code - it is the main source
of information.

LT is antlr.Parser method:

public Token LT(int i) throws TokenStreamException

antlr.Token has method 

public String getText()

so you can use getText().length()

> I'm not really sure what token LT(0) returns so I don't know if it even
> could have worked had there been a getLength() method. The idea was of
> course to have Eclipse place its red underlining starting at the column
> (on the specified line) and extending for length-chars ahead.
> 
> Have anyone else does something like this before? i.e. investigating the
> length of the token or word of whatever that appeared instead of the
> expected token?

Use 'token' field of caught exception instead of LT(0).
It is defined in inheritors of RecognitionException that are
assotioated with some token. For example, in NoViableAltException.

Regards,
Alexey

-----
Alexey Demakov
TreeDL: Tree Description Language: http://treedl.sourceforge.net
RedVerst Group: http://www.unitesk.com




More information about the antlr-interest mailing list