-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.cpp
326 lines (314 loc) · 8.55 KB
/
parser.cpp
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
//============================================================================
// Name : parser.cpp
// Author : Guneykan Ozgul.
// Description : Implementation of 'MyLang Parser' in C++.
//============================================================================
#include "parser.h"
//Constructor method.
Parser:: Parser(string infile,string outfile) {
counter=1;
label="LABL";
input = "EMPTY";
reader.open(infile.c_str());
writer.open(outfile.c_str());
if (reader.good())
reader >> input;
temp = input;
}
//Starts to parse input.
void Parser::parse(){
writer<<"code segment"<<endl;
stat();
// cout<<"stop"<<endl;
writer<<" int 20h ; exit to dos "<<endl;
writer<<endl;
printread();
writer<<endl;
printer();
writer<<endl;
while (!vars.empty()) {
writer << 'v' << *vars.begin()<<" "<<"dw (?)"<<endl;
vars.erase(vars.begin());
}
writer<<"code ends"<<endl;
}
//Gets the next token from input.
void Parser:: update() {
if (reader.good())
reader >> input;
else
input = " ";
}
//Checks if the given string is matched with the current token. If it is matched, updates the current token.
bool Parser::receive(string keyword) {
if (input.compare(keyword) == 0) {
temp = input;
update();
return true;
}
return false;
}
//Checks if the given string is matched with the current token. If it is matched, updates the current token.
//This function is used to detect errors.
bool Parser::check(string keyword) {
if (input.compare(keyword) == 0) {
// temp=input;
update();
return true;
}
cout << "Syntax error: "<<"Unexpected token:"<<input<<", "<<keyword<<" expected"<<endl;
return false;
}
//Represents the nonterminal '<stat>'.
void Parser::stat() {
if (isVar()) {
string tem=input;
update();
if (check("=")) {
//cout << "push-addr-var " << tem << " " << endl;
writer<<" push offset v"<<tem<<endl;
vars.insert(tem);
expr();
writer<<" pop ax"<<endl;
writer<<" pop bp"<<endl;
writer<<" mov [bp],ax"<<endl;
//writer<<" mov v"<<tem<<",ax"<<endl;
//cout << "assign" << endl;
} else {
return;
}
}
else if (receive("if")) {
//cout << "if \n" << endl;
expr();
int c=counter;
writer<<" pop ax"<<endl;
writer<<" cmp ax,0"<<endl;
writer<<" if z jmp "<<"LABL"<<counter<<endl;
counter++;
if (check("then")) {
// cout << "\n then \n" << endl;
stat();
writer<<"LABL"<<c<<" :"<<endl;
//cout << "\n";
}
else {
return;
}
}
else if (receive("print")) {
//cout << "print ";
// cout<<" push v"<<input<<" "<<endl;
expr();
writer<<" pop ax"<<endl;
writer<<" call myprint"<<endl;
}
else if (receive("read")) {
writer<<" call myread"<<endl;
writer<<" mov "<<"v"<<input<<",cx"<<endl;
//cout << "read ";
if(isVar()){
vars.insert(input);
update();
}
}
else if (receive("while")) {
// cout << "while \n" << endl;
int counter1=counter;
writer<<"LABL"<<counter1<<":"<<endl;
counter++;
expr();
writer<<" pop ax"<<endl;
writer<<" cmp ax,0"<<endl;
//cout<<"jump-if-false "<<"LABL"<<counter<<endl;
writer<<" if z jmp LABL"<<counter<<endl;
int counter2=counter;
counter++;
if (check("do")) {
// cout << "\ndo \n" << endl;
stat();
//cout<<"jump "<<"LABL"<<counter1<<endl;
writer<<" jmp "<<"LABL"<<counter1<<endl;
writer<<"LABL"<<counter2<<":"<<endl;
// cout << "\n";
}
else {
return;
}
}
else if (receive("begin")) {
// cout << "begin \n" << endl;
opt_stms();
if (check("end")) {
// cout << "\n end" << endl;
}else{
return;
}
}
else if(input.compare(";")!=0){
cout <<"Syntax Error";
}
}
//Represents the nonterminal '<expr>'.
void Parser::expr() {
term();
moreterm();
}
void Parser::term() {
factor();
morefactors();
}
void Parser::moreterm() {
if (receive("+")) {
term();
//cout << "+" << endl;
writer<<" mov dx,0"<<endl;
writer<<" pop cx"<<endl;
writer<<" pop ax"<<endl;
writer<<" add ax,cx"<<endl;
writer<<" push ax"<<endl;
moreterm();
}
else if (receive("-")) {
term();
//cout << "-" << endl;
writer<<" pop cx"<<endl;
writer<<" pop ax"<<endl;
writer<<" sub ax,cx"<<endl;
writer<<" push ax"<<endl;
moreterm();
}
else {
}
}
//Represents the nonterminal '<factor>'.
void Parser::factor() {
if (receive("(")) {
expr();
check(")");
}
else if (isVar()) {
// cout << "push-val-var " << input << endl;
writer<<" push v"<<input<<" w"<<endl;
vars.insert(input);
temp=input;
update();
}
else if (isNum()) {
//cout << "push-num " << input << endl;
writer<<" push "<<input<<endl;
temp=input;
update();
}
else {
cout << "Syntax error: Unexpected token:"<<input<< endl;
}
}
//Represents the nonterminal '<morefactors>'.
void Parser::morefactors() {
if (receive("*")) {
factor();
//cout << "*" << endl;
writer<<" pop ax"<<endl;
writer<<" pop cx"<<endl;
writer<<" mul cx"<<endl;
writer<<" push ax"<<endl;
morefactors();
}
else if (receive("/")) {
factor();
// cout << "div" << endl;
writer<<" xor dx,dx"<<endl;
writer<<" pop cx"<<endl;
writer<<" pop ax"<<endl;
writer<<" div cx"<<endl;
writer<<" push ax"<<endl;
morefactors();
}
else if (receive("%")) {
factor();
//cout << "%" << endl;
writer<<" xor dx,dx"<<endl;
writer<<" pop cx"<<endl;
writer<<" pop ax"<<endl;
writer<<" div cx"<<endl;
writer<<" push dx"<<endl;
morefactors();
}
}
//Represents the nonterminal '<opt_stms>'.
void Parser::opt_stms() {
if (input.compare("end") != 0) {
stmt_list();
}
}
//Represents the nonterminal '<stmt_list>'.
void Parser::stmt_list() {
stat();
if (input.compare(";") == 0) {
temp=input;
update();
stmt_list();
}
}
//Checks if the current token is a variable.
bool Parser::isVar() {
char first = input.at(0);
bool isLetter=('A' <= first && first <= 'Z') || ('a' <= first && first <= 'z');
bool isSymbol=(input.compare("if") == 0 || input.compare("then") == 0 || input.compare("while") == 0
|| input.compare("print") == 0 || input.compare("read") == 0 || input.compare("begin") == 0 || input.compare("end") == 0);
return (isLetter&&!isSymbol);
}
//Checks if the current token is a number.
bool Parser::isNum() {
int length = input.length();
for (unsigned int i = 0; i < length; i++) {
if (!isdigit(input.at(i)))
return false;
}
return true;
}
void Parser::printer(){
writer <<"myprint:"<<endl;
writer<<" mov si,10d"<<endl;
writer<<" xor dx,dx"<<endl;
writer<<" push 0Ah ; push newline"<<endl;
writer<<" mov cx,1d"<<endl;
writer<<"nonzero:"<<endl;
writer<<" div si"<<endl;
writer<<" add dx,48d"<<endl;
writer<<" push dx"<<endl;
writer<<" inc cx"<<endl;
writer<<" xor dx,dx"<<endl;
writer<<" cmp ax,0h"<<endl;
writer<<" jne nonzero"<<endl;
writer<<"writeloop:"<<endl;
writer<<" pop dx"<<endl;
writer<<" mov ah,02h"<<endl;
writer<<" int 21h"<<endl;
writer<<" dec cx"<<endl;
writer<<" jnz writeloop"<<endl;
writer<<" ret"<<endl;
}
void Parser::printread(){
writer <<"myread:"<<endl;
writer<<" MOV CX,0"<<endl;
writer<<"morechar:"<<endl;
writer<<" mov ah,01h "<<endl;
writer<<" int 21H"<<endl;
writer<<" mov dx,0"<<endl;
writer<<" mov dl,al"<<endl;
writer<<" mov ax,cx"<<endl;
writer<<" cmp dl,0D"<<endl;
writer<<" je myret"<<endl;
writer<<" sub dx,48d"<<endl;
writer<<" mov bp,dx"<<endl;
writer<<" mov ax,cx"<<endl;
writer<<" mov cx,10d"<<endl;
writer<<" mul cx"<<endl;
writer<<" add ax,bp"<<endl;
writer<<" mov cx,ax"<<endl;
writer<<" jmp morechar"<<endl;
writer<<" myret:"<<endl;
writer<<" ret "<<endl;
}