[antlr-interest] Catch errors and stop token stream creation

Hélène Chagnot helene.chagnot at gmail.com
Thu Jun 26 02:19:02 PDT 2008


Hi,

Working with ANTLR 3.1b1, we would like to capture unrecognised tokens
passed to the lexer to immediately stop parsing when incorrect text is
entered.
We tried to implement recoverFromMismatchedSet() method but it is never
called. Here is an example grammar:

grammar Example;

options {
    output = AST;
}

@header {
    package main.antlr;
}
@lexer::header {
    package main.antlr;
}

@lexer::rulecatch {

    catch (RecognitionException re) {
        System.out.println("RecognitionException");
        reportError(re);
    }

}
@lexer::members {

    protected void mismatch(IntStream input, int ttype, BitSet follow)
throws RecognitionException {
        System.out.println("mismatch");
        throw new MismatchedTokenException(ttype, input);
    }

    public Object recoverFromMismatchedSet(IntStream input,
RecognitionException e, BitSet follow) throws RecognitionException {
        System.out.println("recoverFromMismatchedSet");
        throw e;
    }
}

b : a*;
a : LETTER | NUMBER;

LETTER : 'A'..'Z';
NUMBER : '0'..'9';


When running this grammar with "ABCDEFghiJKL123", here is the output that we
get:

line 1:6 no viable alternative at character 'g'
line 1:7 no viable alternative at character 'h'
line 1:8 no viable alternative at character 'i'

When printing the token stream sent to the parser, we get the following:

ABCDEFJKL123

How can we stop parsing when text is not recognised ?

Thanks
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080626/0dfa9a67/attachment.html 


More information about the antlr-interest mailing list