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;
[EMAIL PROTECTED] {
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* ;
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"il-antlr-interest" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/il-antlr-interest?hl=en
-~----------~----~----~----~------~----~------~--~---
List: http://www.antlr.org:8080/mailman/listinfo/antlr-interest
Unsubscribe:
http://www.antlr.org:8080/mailman/options/antlr-interest/your-email-address