/* C++ interface test of Parser Exception Handling * * Given input: * * if a+ then a=b+b; * * the program should respond with * * invalid conditional in 'if' statement * found assignment to a */ << #include #include "DLGLexer.h" #include "PBlackBox.h" typedef ANTLRCommonToken ANTLRToken; int main() { ParserBlackBox p(stdin); int retsignal; p.parser()->rule(&retsignal); return 0; } >> /* Uncommenting this will make ANTLR think you put these handlers at the end of each rule: exception catch MismatchedToken : <> default : <> */ #token "[\ \t]+" <> #token "\n" <> #token THEN "then" #tokclass DIE { "@" "if" ID "else" } class PEHTest { rule: ( stat )+ ; stat: "if" t:expr THEN stat { "else" stat } | id:ID "=" expr ";" <getText());>> ; exception[t] default : << printf("invalid conditional in 'if' statement\n"); consumeUntilToken(THEN); >> exception catch MismatchedToken : catch NoViableAlt : catch NoSemViableAlt : << printf("stat:caught predefined signal\n"); consumeUntil(DIE_set); >> expr: expr1 ("\+" expr1)* ; expr1 : expr2 ("\*" expr2)* ; expr2: ID ; } #token ID "[a-z]+"