[antlr-interest] @rulecatch not working with antlr 3.0.1; Help for catching parser exceptions

Johannes Luber jaluber at gmx.de
Sat Nov 29 04:04:18 PST 2008


SSG JJV schrieb:
> Greetings,
>  
> I am trying to write a sample antlr program to catch a parser exception.
> Below is the program that i use.
> I want to catch the mismatch exceptions raised from parser. Used
> @rulecatch syntax to print a message for exceptions.
> But antlr does not seem to honor '@rulecatch' <mailto:'@rulecatch'>
> syntax. The following line inside @rulecatch block was not generated in
> the parser.
> " System.out.println("Exception occured");" i tried 'catch [
> exception]'  and @exception syntax. Nothing worked.
>  
> How can I throw custom messages when an parser rule exception is raised?
> I am using antlr 3.0.1. <http://3.0.1.>

IIRC, @rulecatch isn't implemented yet (even in 3.1). The only
alternative is to manually throw exceptions derived from an exception
class which ANTLR normally throws.

Johannes
> 
> Thanks,
> 
> Munna
> 
> ========================================
> 
> grammar testANTLR;
> 
> options {
>  output=Java;
> }@header <mailto:}@header> {
>  package sampleTest;
> }
> @lexer::header {
>  package sampleTest;
> }
> 
> /* to match 'var = 10;' line */
> 
> assign 
> @rulecatch {
>  System.out.println("Exception occured");
> }
>  : NAME EQUAL INTEGER SEMICOLON EOF {
>   System.out.println("assign matched on line :"+$EQUAL.line);
>  }
>  ;
> 
> NAME : ('a' .. 'z')+
>  ;
> 
> SEMICOLON : ';'
>  ;
> 
> EQUAL : '='
>  ;
>  
> INTEGER : ('0'..'9')+
>  ;
> 
> WS : ( ' ' | '\t' | '\r' | '\n' )+ { $channel=HIDDEN;}
>  ;
> 
> =======================
> 
> Generated assign function inside parser:
> 
> *
> 
> public
> 
> * *final* *void* assign() *throws* RecognitionException {
> 
> Token EQUAL1=
> 
> *null*;
> 
> *try* {
> 
> *//* testANTLR.g:18:2: ( NAME EQUAL INTEGER SEMICOLON EOF )
> 
>  
> 
> // testANTLR.g:18:4: NAME EQUAL INTEGER SEMICOLON EOF
> 
>  
> 
> {
> 
> match(
> 
> input,/NAME/,/FOLLOW_NAME_in_assign43/);
> 
> EQUAL1=(Token)
> 
> input.LT(1);
> 
> match(
> 
> input,/EQUAL/,/FOLLOW_EQUAL_in_assign45/);
> 
> match(
> 
> input,/INTEGER/,/FOLLOW_INTEGER_in_assign47/);
> 
> match(
> 
> input,/SEMICOLON/,/FOLLOW_SEMICOLON_in_assign49/);
> 
> match(
> 
> input,/EOF/,/FOLLOW_EOF_in_assign51/);
> 
> System.
> 
> /out/.println("assign matched on line :"+EQUAL1.getLine());
> 
> }
> 
> }
> 
> *catch* (RecognitionException re) {
> 
> reportError(re);
> 
> recover(
> 
> input,re);
> 
> }
> 
> *finally* {
> 
> }
> 
> *return* ;
> 
> }
> 
> 
> ------------------------------------------------------------------------
> 
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
> 



More information about the antlr-interest mailing list