]> pd.if.org Git - pccts/blob - testcpp/5/test.g
auto commit for import
[pccts] / testcpp / 5 / test.g
1 /* This is test.g which tests multiple scanners/parsers; DLG-based scanner */
2 <<
3 #include "Lexer.h"
4 typedef ANTLRCommonToken ANTLRToken;
5
6 int main()
7 {
8         ANTLRTokenPtr aToken = new ANTLRToken;
9         DLGFileInput in(stdin);
10         Lexer scan(&in);
11         scan.setToken(mytoken(aToken));
12         ANTLRTokenBuffer pipe(&scan);
13         Include parser(&pipe);
14         parser.init();
15
16         parser.input();
17         return 0;
18 }
19 >>
20
21 #token "[\ \t\n]+"      <<skip();>>
22 #token Eof "@"
23
24 class Include {
25
26 <<
27 /* this is automatically defined to be a member function of Include::
28  * since it is within the "class {...}" boundaries.
29  */
30 private:
31 char *stripquotes(ANTLRChar *s)
32 {
33         s[strlen(s)-1] = '\0';
34         return &s[1];
35 }
36 >>
37
38 input
39         :       ( cmd | include )* Eof
40         ;
41
42 cmd     :       "print"
43                 (       NUMBER          <<printf("%s\n", $1->getText());>>
44                 |       STRING          <<printf("%s\n", $1->getText());>>
45                 )
46         ;
47
48 include
49         :       "#include" STRING
50                 <<{
51                 FILE *f;
52                 f = fopen(stripquotes($2->getText()), "r");
53                 if ( f==NULL ) {fprintf(stderr, "can't open %s\n", $2->getText()+1);}
54                 else {
55                         ANTLRTokenPtr aToken = new ANTLRToken;
56                         DLGFileInput in(f);
57                         Lexer scan(&in);
58                         scan.setToken(mytoken(aToken));
59                         ANTLRTokenBuffer pipe(&scan);
60                         Include parser(&pipe);
61                         parser.init();
62                         parser.input();
63                 }
64                 }>>
65         ;
66
67 }
68
69 #token STRING   "\" [a-zA-Z0-9_.,\ \t]+ \""
70 #token NUMBER   "[0-9]+"