source: trunk/yao/share/antlr-2.7.7/examples/java/preserveWhiteSpace/instr.g @ 1

Last change on this file since 1 was 1, checked in by lnalod, 15 years ago

Initial import of YAO sources

File size: 2.4 KB
Line 
1class InstrParser extends Parser;
2options {
3        buildAST = true;
4        k=2;
5}
6
7tokens {
8        CALL; // define imaginary token CALL
9}
10
11slist
12        :       ( stat )+
13        ;
14
15stat:   LBRACE^ (stat)+ RBRACE
16        |       "if"^ expr "then" stat ("else" stat)?
17        |       ID ASSIGN^ expr SEMI
18        |       call
19        ;
20
21expr
22        :       mexpr (PLUS^ mexpr)*
23        ;
24
25mexpr
26        :       atom (STAR^ atom)*
27        ;
28
29atom:   INT
30        |       ID
31        ;
32
33call:   ID LPAREN (expr)? RPAREN SEMI
34                {#call = #(#[CALL,"CALL"], #call);}
35        ;
36
37class InstrLexer extends Lexer;
38options {
39        charVocabulary = '\3'..'\377';
40}
41
42WS      :       (' '
43                |       '\t'
44                |       ('\n'|'\r'('\n')?) {newline();}
45                )+
46        ;
47
48// Single-line comments
49SL_COMMENT
50        :       "//"
51                (~('\n'|'\r'))* ('\n'|'\r'('\n')?)
52                {newline();}
53        ;
54
55LBRACE: '{'
56        ;
57
58RBRACE: '}'
59        ;
60
61LPAREN: '('
62        ;
63
64RPAREN: ')'
65        ;
66
67STAR:   '*'
68        ;
69
70PLUS:   '+'
71        ;
72
73SEMI:   ';'
74        ;
75
76ASSIGN
77        :       '='
78        ;
79
80protected
81DIGIT
82        :       '0'..'9'
83        ;
84
85INT     :       (DIGIT)+
86        ;
87
88ID      :       ('a'..'z')+
89        ;
90
91class InstrTreeWalker extends TreeParser;
92{
93        /** walk list of hidden tokens in order, printing them out */
94        public static void dumpHidden(antlr.CommonHiddenStreamToken t) {
95          for ( ; t!=null ; t=InstrMain.filter.getHiddenAfter(t) ) {
96            System.out.print(t.getText());
97          }
98        }
99
100        private void pr(AST p) {
101                System.out.print(p.getText());
102                dumpHidden(
103                        ((antlr.CommonASTWithHiddenTokens)p).getHiddenAfter()
104                );
105        }
106}
107
108slist
109        :       {dumpHidden(InstrMain.filter.getInitialHiddenToken());}
110                (stat)+
111        ;
112
113stat:   #(LBRACE {pr(#LBRACE);} (stat)+ RBRACE {pr(#RBRACE);})
114        |       #(i:"if" {pr(i);} expr t:"then" {pr(t);} stat (e:"else" {pr(e);} stat)?)
115        |       #(ASSIGN ID {pr(#ID); pr(#ASSIGN);} expr SEMI {pr(#SEMI);} )
116        |       call
117        ;
118
119expr
120        :       #(PLUS expr {pr(#PLUS);} expr)
121        |       #(STAR expr {pr(#STAR);} expr)
122        |       INT {pr(#INT);}
123        |       ID  {pr(#ID);}
124        ;
125
126call:   {
127                // add instrumentation about call; manually call rule
128                callDumpInstrumentation(#call);
129                }
130                #(CALL ID {pr(#ID);}
131                  LPAREN {pr(#LPAREN);} (expr)? RPAREN {pr(#RPAREN);}
132                  SEMI
133                  {
134                  // print SEMI manually; need '}' between it and whitespace
135                  System.out.print(#SEMI.getText());
136                  System.out.print("}"); // close {...} of instrumentation
137                  dumpHidden(
138                        ((antlr.CommonASTWithHiddenTokens)#SEMI).getHiddenAfter()
139                  );
140                  }
141                )
142        ;
143
144/** Dump instrumentation for a call statement.
145 *  The reference to rule expr prints out the arg
146 *  and then at the end of this rule, we close the
147 *  generated called to dbg.invoke().
148 */
149callDumpInstrumentation
150        :       #(CALL id:ID
151                  {System.out.print("{dbg.invoke(\""+id.getText()+"\", \"");}
152                  LPAREN (e:expr)? RPAREN SEMI
153                  {System.out.print("\"); ");}
154                )
155        ;
156
Note: See TracBrowser for help on using the repository browser.