[antlr-interest] Customizing errors in antlr3
Benoit Miller
fulg at iname.com
Sun Jan 7 07:58:01 PST 2007
What is the best way to customize reported errors in antlr3?
For example, in the following snippet:
start
: forwardDecl* objectDecl*
;
forwardDecl
: classOrStruct IDENTIFIER ';'
-> ^(classOrStruct IDENTIFIER)
;
objectDecl
: attrList? classOrStruct IDENTIFIER '{' property* '}' ';'
-> ^(classOrStruct IDENTIFIER attrList? property*)
;
[unedited snippet, so this time it should be representative :)]
Given an input such as "[foo] class Bar;", I want to report to the tool
user something like "cannot specify attributes on forward declarations",
but it's unclear to me how to do this.
I tried using a syntactic predicate to throw my error from the parser:
objectDecl
: attrList? classOrStruct IDENTIFIER
( {input.LA(1)==';'}? { throw new Exception("foo"); }
| '{' property* '}' ';'
-> ^(classOrStruct IDENTIFIER attrList? property*)
)
;
..but that didn't work, the parser still throws a NoViableAlt exception
instead of my own:
[objectDecl]: line 1:20 state 0 (decision=5) no viable alt;
token=[@7,20:20=';',<30>,1:20]
Any suggestions?
Thanks,
Ben.
More information about the antlr-interest
mailing list