you can define in your members section a method public String getErrorMessage(RecognitionException e, String[] tokenNames) { String msg = e.getMessage(); if (e instanceof MissingTokenException) { MissingTokenException mte = (MissingTokenException) e; String tokenName = ""; if (mte.expecting == Token.EOF) { tokenName = "EOF"; } else { tokenName = tokenNames[mte.expecting]; } ........
Or u can use @rulecatch. cheers, 2009/8/21 Marwan Ajraoui <marwan.ale...@gmail.com>: > r : ID '#' {s = $ID.text; System.out.println("found "+s);} ; > > Tow alternatives > > 1. take out '#' from ur file.g > > or > > 2. Put it in ur input file. so it look like: b # > > > U should read about how it works. Jim did send u a link. > > cheers, > > > > > 2009/8/19 Ha Luong <haluon...@gmail.com>: >> 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 >> >> >> >> >> >> List: http://www.antlr.org/mailman/listinfo/antlr-interest >> Unsubscribe: >> http://www.antlr.org/mailman/options/antlr-interest/your-email-address >> >> > > > > -- > Marouane > -- Marouane List: http://www.antlr.org/mailman/listinfo/antlr-interest Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "il-antlr-interest" group. To post to this group, send email to il-antlr-interest@googlegroups.com To unsubscribe from this group, send email to il-antlr-interest+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/il-antlr-interest?hl=en -~----------~----~----~----~------~----~------~--~---