1 | class InstrParser extends Parser; |
---|
2 | options { |
---|
3 | buildAST = true; |
---|
4 | k=2; |
---|
5 | } |
---|
6 | |
---|
7 | tokens { |
---|
8 | CALL; // define imaginary token CALL |
---|
9 | } |
---|
10 | |
---|
11 | slist |
---|
12 | : ( stat )+ |
---|
13 | ; |
---|
14 | |
---|
15 | stat: LBRACE^ (stat)+ RBRACE |
---|
16 | | "if"^ expr "then" stat ("else" stat)? |
---|
17 | | ID ASSIGN^ expr SEMI |
---|
18 | | call |
---|
19 | ; |
---|
20 | |
---|
21 | expr |
---|
22 | : mexpr (PLUS^ mexpr)* |
---|
23 | ; |
---|
24 | |
---|
25 | mexpr |
---|
26 | : atom (STAR^ atom)* |
---|
27 | ; |
---|
28 | |
---|
29 | atom: INT |
---|
30 | | ID |
---|
31 | ; |
---|
32 | |
---|
33 | call: ID LPAREN (expr)? RPAREN SEMI |
---|
34 | {#call = #(#[CALL,"CALL"], #call);} |
---|
35 | ; |
---|
36 | |
---|
37 | class InstrLexer extends Lexer; |
---|
38 | options { |
---|
39 | charVocabulary = '\3'..'\377'; |
---|
40 | } |
---|
41 | |
---|
42 | WS : (' ' |
---|
43 | | '\t' |
---|
44 | | ('\n'|'\r'('\n')?) {newline();} |
---|
45 | )+ |
---|
46 | ; |
---|
47 | |
---|
48 | // Single-line comments |
---|
49 | SL_COMMENT |
---|
50 | : "//" |
---|
51 | (~('\n'|'\r'))* ('\n'|'\r'('\n')?) |
---|
52 | {newline();} |
---|
53 | ; |
---|
54 | |
---|
55 | LBRACE: '{' |
---|
56 | ; |
---|
57 | |
---|
58 | RBRACE: '}' |
---|
59 | ; |
---|
60 | |
---|
61 | LPAREN: '(' |
---|
62 | ; |
---|
63 | |
---|
64 | RPAREN: ')' |
---|
65 | ; |
---|
66 | |
---|
67 | STAR: '*' |
---|
68 | ; |
---|
69 | |
---|
70 | PLUS: '+' |
---|
71 | ; |
---|
72 | |
---|
73 | SEMI: ';' |
---|
74 | ; |
---|
75 | |
---|
76 | ASSIGN |
---|
77 | : '=' |
---|
78 | ; |
---|
79 | |
---|
80 | protected |
---|
81 | DIGIT |
---|
82 | : '0'..'9' |
---|
83 | ; |
---|
84 | |
---|
85 | INT : (DIGIT)+ |
---|
86 | ; |
---|
87 | |
---|
88 | ID : ('a'..'z')+ |
---|
89 | ; |
---|
90 | |
---|
91 | class 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 | |
---|
108 | slist |
---|
109 | : {dumpHidden(InstrMain.filter.getInitialHiddenToken());} |
---|
110 | (stat)+ |
---|
111 | ; |
---|
112 | |
---|
113 | stat: #(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 | |
---|
119 | expr |
---|
120 | : #(PLUS expr {pr(#PLUS);} expr) |
---|
121 | | #(STAR expr {pr(#STAR);} expr) |
---|
122 | | INT {pr(#INT);} |
---|
123 | | ID {pr(#ID);} |
---|
124 | ; |
---|
125 | |
---|
126 | call: { |
---|
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 | */ |
---|
149 | callDumpInstrumentation |
---|
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 | |
---|