[antlr-interest] Couldn't catch the exception object at the last token in the rule

Ha Luong haluongvn at gmail.com
Tue Aug 18 20:33:24 PDT 2009


Dear all,

I'd like to catch the exception at the *last token* in the rule. I managed
to test some rules to catch the last token mismatch but I couldn't do.
Here is my test:

1. In "T.g" :

grammar T;
options {
    language=Java;
}
@members {
String s;
}

// Alter code generation so catch-clauses get replace with
// this action.
@rulecatch {

catch (RecognitionException e) {
    throw e;

}

}
// END:override

r : ID '#' {s = $ID.text; System.out.println("found "+s);} ;
ID: 'a'..'z'+ ;
WS: (' '|'\n'|'\r')+ {skip();} ; // ignore whitespace

2. In "input.txt":
b

3. In "Test.java":
import org.antlr.runtime.*;

public class Test {
    public static void main(String[] args) throws Exception {
        //ANTLRInputStream input = new ANTLRInputStream(System.in);
        //TLexer lexer = new TLexer(input);
        TLexer lexer = new TLexer(new ANTLRFileStream("input.txt"));
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        TParser parser = new TParser(tokens);
        try{
            parser.r();
        }
        catch (RecognitionException e) {
            System.out.println(e.getClass().toString());
            //e.printStackTrace();
        }
    }
}

4. The output in console when I run Test:
$ java Test
line 0:-1 missing '#' at '<EOF>'
found b

I don't know why the last token mismatch couldn't catch in the main function
of Test. Could you please help me?

Thank you very much,
Ha Luong
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090818/275ceca6/attachment.html 
-------------- next part --------------
b 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: T.g
Type: application/octet-stream
Size: 380 bytes
Desc: not available
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20090818/275ceca6/attachment.obj 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Test.java
Type: application/octet-stream
Size: 955 bytes
Desc: not available
Url : http://www.antlr.org/pipermail/antlr-interest/attachments/20090818/275ceca6/attachment-0001.obj 


More information about the antlr-interest mailing list