]> pd.if.org Git - pccts/blob - testcpp/13/test.g
auto commit for import
[pccts] / testcpp / 13 / test.g
1 /* C++ interface test of Parser Exception Handling
2  *
3  * Given input:
4  *
5  *              if a+ then a=b+b;
6  *
7  * the program should respond with
8  *
9  *              invalid conditional in 'if' statement
10  *              found assignment to a
11  */
12 <<
13 #include <stream.h>
14 #include "DLGLexer.h"
15 #include "PBlackBox.h"
16 typedef ANTLRCommonToken ANTLRToken;
17
18 int main()
19 {
20         ParserBlackBox<DLGLexer, PEHTest, ANTLRToken> p(stdin);
21         int retsignal;
22         p.parser()->rule(&retsignal);
23         return 0;
24 }
25 >>
26
27 /*
28 Uncommenting this will make ANTLR think you put these handlers at the
29 end of each rule:
30  
31 exception
32         catch MismatchedToken   : <<printf("dflt:MismatchedToken\n");>>
33         default : <<printf("dflt:dflt\n");>>
34 */
35
36 #token "[\ \t]+"        <<skip();>>
37 #token "\n"                     <<skip(); newline();>>
38 #token THEN     "then"
39 #tokclass DIE { "@" "if" ID "else" }
40
41 class PEHTest {
42
43 rule:   ( stat )+
44         ;
45
46 stat:   "if" t:expr THEN stat { "else" stat }
47         |       id:ID "=" expr ";"
48                 <<printf("found assignment to %s\n", $id->getText());>>
49         ;
50         exception[t]
51                 default :
52                         <<
53                         printf("invalid conditional in 'if' statement\n");
54                         consumeUntilToken(THEN);
55                         >>
56         exception
57                 catch MismatchedToken   :
58                 catch NoViableAlt               :
59                 catch NoSemViableAlt    :
60                         <<
61                         printf("stat:caught predefined signal\n");
62                         consumeUntil(DIE_set);
63                         >>
64
65 expr:   expr1 ("\+" expr1)*
66         ;
67
68 expr1
69         :       expr2 ("\*" expr2)*
70         ;
71
72 expr2:  ID
73         ;
74
75 }
76
77 #token ID "[a-z]+"