]> pd.if.org Git - pccts/blob - testcpp/9/test.g
auto commit for import
[pccts] / testcpp / 9 / test.g
1 /* This is test.g which tests simple AST refs and construction */
2
3 <<
4 typedef ANTLRCommonToken ANTLRToken;
5 #include "DLGLexer.h"
6 #include "PBlackBox.h"
7
8 class AST : public ASTBase {
9 public:
10         ANTLRTokenPtr token;
11         AST(ANTLRTokenPtr t) { token = t; }
12         void preorder_action() {
13                 char *s = token->getText();
14                 printf(" %s", s);
15         }
16 };
17
18 int main()
19 {
20         ParserBlackBox<DLGLexer, Expr, ANTLRToken> p(stdin);
21         ASTBase *root = NULL;
22         p.parser()->e(&root);
23         root->preorder();
24         printf("\n");
25         root->destroy();
26         return 0;
27 }
28 >>
29
30 #token "[\ \t\n]+"      <<skip();>>
31 #token Eof "@"
32
33 class Expr {                            /* Define a grammar class */
34
35 e       :       mult_expr ( ("\+"^|"\-"^) mult_expr )*
36         ;
37
38 mult_expr
39         :       atom ( ("\*"^|"\/"^) atom )*
40         ;
41
42 atom:   IDENTIFIER
43         |       NUMBER
44         ;
45
46 }
47
48 #token IDENTIFIER       "[a-z]+"
49 #token NUMBER           "[0-9]+"