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

SSG JJV virtuoso.coders at gmail.com
Tue Nov 25 20:32:55 PST 2008


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' 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.

Thanks,

Munna

========================================

grammar testANTLR;

options {
 output=Java;
}@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* ;

}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20081125/85fcb5ce/attachment.html 


More information about the antlr-interest mailing list