-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.g
253 lines (221 loc) · 4.84 KB
/
reader.g
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#header
<<
#include <string>
#include <iostream>
using namespace std;
// struct to store information about tokens
typedef struct {
string kind;
string text;
} Attrib;
// function to fill token information (predeclaration)
void zzcr_attr(Attrib *attr, int type, char *text);
// fields for AST nodes
#define AST_FIELDS string kind; string text;
#include "ast.h"
// macro to create a new AST node (and function predeclaration)
#define zzcr_ast(as,attr,ttype,textt) as=createASTnode(attr,ttype,textt)
AST* createASTnode(Attrib* attr,int ttype, char *textt);
>>
<<
#include <cstdlib>
#include <cmath>
//global structures
//AST *root;
// function to fill token information
void zzcr_attr(Attrib *attr, int type, char *text) {
if (type == ID) {
attr->kind = "id";
attr->text = text;
}
else if(type == ASSIGN) {
attr->kind = "Assign";
attr->text = "";
}
else if(type == OR || type == AND || type == NOT) {
attr->kind = text;
attr->text = "";
}
else if(type == EQ) {
attr->kind = "Eq";
attr->text = "";
}
else if(type == GT) {
attr->kind = "Gt";
attr->text = "";
}
else if(type == PLUS) {
attr->kind = "Plus";
attr->text = "";
}
else if(type == MINUS) {
attr->kind = "Minus";
attr->text = "";
}
else if(type == TIMES) {
attr->kind = "Times";
attr->text = "";
}
else if(type == COND) {
attr->kind = "Cond";
attr->text = "";
}
else if(type == LOOP) {
attr->kind = "Loop";
attr->text = "";
}
else {
for(int i = 1; text[i]; ++i) text[i] = tolower(text[i]);
attr->kind = text;
attr->text = "";
}
}
// function to create a new AST node
AST* createASTnode(Attrib* attr, int type, char* text) {
AST* as = new AST;
as->kind = attr->kind;
as->text = attr->text;
as->right = NULL;
as->down = NULL;
return as;
}
/// create a new "list" AST node with one element
AST* createASTlist(AST *child) {
AST *as=new AST;
as->kind="Seq";
as->right=NULL;
as->down=child;
return as;
}
AST* createASTvar(AST *child) {
AST *as=new AST;
as->kind="Var";
as->right=NULL;
as->down=child;
return as;
}
AST* createASTconst(AST *child) {
AST *as=new AST;
as->kind="Const";
as->right=NULL;
as->down=child;
return as;
}
/// get nth child of a tree. Count starts at 0.
/// if no such child, returns NULL
AST* child(AST *a,int n) {
AST *c=a->down;
for (int i=0; c!=NULL && i<n; i++) c=c->right;
return c;
}
/// print AST, recursively, with indentation
void ASTPrintIndent(AST *a,string s)
{
if (a==NULL) return;
cout<<a->kind;
if (a->text!="") cout<<"("<<a->text<<")";
cout<<endl;
AST *i = a->down;
while (i!=NULL && i->right!=NULL) {
cout<<s+" \\__";
ASTPrintIndent(i,s+" |"+string(i->kind.size()+i->text.size(),' '));
i=i->right;
}
if (i!=NULL) {
cout<<s+" \\__";
ASTPrintIndent(i,s+" "+string(i->kind.size()+i->text.size(),' '));
i=i->right;
}
}
/// print AST
void ASTPrint(AST *a)
{
while (a!=NULL) {
cout<<" ";
ASTPrintIndent(a,"");
a=a->right;
}
}
void programPrint(AST* a);
void commandPrint(AST* a) {
if(a->kind == "Seq") {
cout << " (Seq [";
a = a->down;
commandPrint(a);
a = a->right;
while(a!=NULL) {
cout << ",";
commandPrint(a);
a = a->right;
}
cout << "])";
}
else if(a->kind == "id") {
cout << " \"" << a->text << "\" ";
}
else {
cout << "(" << a->kind;
a = a->down;
programPrint(a);
cout << ")";
}
}
void programPrint(AST *a) {
while(a != NULL) {
commandPrint(a);
a = a->right;
}
}
int main() {
AST *root = NULL;
ANTLR(program(&root), stdin);
//ASTPrint(root);
programPrint(root);
}
>>
#lexclass START
#token NUM "[0-9]+"
#token ASSIGN ":="
#token INPUT "INPUT"
#token PRINT "PRINT"
#token EMPTY "EMPTY"
#token PUSH "PUSH"
#token POP "POP"
#token SIZE "SIZE"
#token COND "IF"
#token THEN "THEN"
#token ELSE "ELSE"
#token END "END"
#token LOOP "WHILE"
#token DO "DO"
#token GT "\>"
#token EQ "="
#token PLUS "\+"
#token MINUS "\-"
#token TIMES "\*"
#token AND "AND"
#token OR "OR"
#token NOT "NOT"
#token ID "[a-zA-Z0-9]+"
#token SPACE "[\ \n]" << zzskip();>>
program: instr "@"!;
instr: (instruction)* <<#0=createASTlist(_sibling);>>;
instruction: assign | input | print | empty | push | pop | size | cond | loop;
assign: ID ASSIGN^ numexpr;
input: INPUT^ ID;
print: PRINT^ ID;
empty: EMPTY^ ID;
push: PUSH^ ID numexpr;
pop: POP^ ID ID;
size: SIZE^ ID ID;
cond: COND^ boolexpr THEN! instr ELSE! instr END!;
loop: LOOP^ boolexpr DO! instr END!;
numexpr: term (PLUS^ term | MINUS^ term)*;
term: varconst (TIMES^ varconst)*;
var: ID <<#0=createASTvar(_sibling);>>;
consta: NUM <<#0=createASTconst(_sibling);>>;
varconst: var | consta;
boolexpr: boole (OR^ boole)*;
boole: (neg | comp) (AND^ (neg | comp))*;
neg: NOT^ comp;
comp: numexpr (GT^ numexpr | EQ^ numexpr );