forked from KevinDuarte/COP3402-Module-3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.c
520 lines (470 loc) · 11.2 KB
/
parser.c
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SYMBOL_TABLE_SIZE 100
typedef struct symbol
{
int kind; // const = 1, var = 2, proc = 3
char name[12]; // name up to 11 chars
int val; // number (ASCII value)
int level; // L level
int addr; // M address
} symbol;
symbol symbol_table[MAX_SYMBOL_TABLE_SIZE];
//the lexemelist fp
FILE * ifp;
//Most recent token
int TOKEN;
//most recent identifier or number
char IDENTIFIER[12];
int NUMBER;
//number of symbols in symbol_table
int numOfSymbols = 0;
//number of variables in symbol_table
int numOfVariables = 0;
//used for the branching in conditionals
int inConditional = 0;
int jumpLine = 0;
//counts the amount of lines of assembly that have been written
int lines = 0;
//number symbol values
typedef enum
{
nulsym = 1, identsym = 2, numbersym = 3, plussym = 4, minussym = 5, multsym = 6, slashsym = 7, oddsym = 8,
eqlsym = 9, neqsym = 10, lessym = 11, leqsym = 12, gtrsym = 13, geqsym = 14, lparentsym = 15, rparentsym = 16,
commasym = 17, semicolonsym = 18, periodsym = 19, becomessym = 20, beginsym = 21, endsym = 22, ifsym = 23,
thensym = 24, whilesym = 25, dosym = 26, callsym = 27, constsym = 28, varsym = 29, procsym = 30, writesym = 31,
readsym = 32, elsesym = 33
}token_type;
//returns that an error has occured
void ERROR(char error[])
{
printf("%s\n", error);
exit(1);
}
//gets the next token GET(TOKEN) in the psuedo
void GETTOKEN()
{
//scans in the next token, if there is no token, it is given the value -1
if(fscanf(ifp, "%d", &TOKEN) == EOF)
{
TOKEN = -1;
}
//gets the identifier
if(TOKEN == identsym)
{
fscanf(ifp, "%s", IDENTIFIER);
}
//gets the number
if(TOKEN == numbersym)
{
fscanf(ifp, "%d", &NUMBER);
}
}
//enters a new symbol into the symbol table
// kind: const = 1, var = 2, proc = 3
void ENTER(int kind)
{
//ensures a symbol is in the symbol table once
int i;
for(i = 0; i < numOfSymbols; i++)
{
if(strcmp(symbol_table[i].name, IDENTIFIER) == 0)
{
ERROR("Error number 27, an identifier has been declared multiple times.");
}
}
symbol_table[numOfSymbols].kind = kind;
strcpy(symbol_table[numOfSymbols].name, IDENTIFIER);
if(kind == 1)
{
symbol_table[numOfSymbols].val = NUMBER;
//level is 0 since this is tiny PL/0
symbol_table[numOfSymbols].level = 0;
symbol_table[numOfSymbols].addr = 0;
}
if(kind == 2)
{
//level is 0 since this is tiny PL/0
symbol_table[numOfSymbols].level = 0;
//TODO find what the base pointer is (0 or 1)
int basePointer = 0;
symbol_table[numOfSymbols].addr = basePointer + 4 + numOfVariables;
numOfVariables++;
}
numOfSymbols++;
}
//checks if the token is a relational operator ( = | <> | < | <= | > | >= )
int isRelationalOperator(int token)
{
return(token == eqlsym || token == neqsym || token == lessym || token == leqsym || token == gtrsym || token == geqsym);
}
//returns the symbol/indentifier with the given name
symbol getSymbol(char identifier[])
{
int i, found = 0;
for(i = 0; i < numOfVariables; i++)
{
if(strcmp(symbol_table[i].name, identifier))
return symbol_table[i];
}
if(found == 0)
ERROR("Error number 11, undeclared identifier.");
}
//used to print the assembly code to the output file
void printToFile(char op[], int L, int M)
{
//if you are in a conditional, it does not print
if(inConditional != 0)
return;
//print to the file here
}
void FACTOR();
void TERM();
void EXPRESSION();
void CONDITION();
void STATEMENT();
void BLOCK();
void PROGRAM();
void FACTOR()
{
if(TOKEN == identsym)
{
/*
if getSymbol() == variable
possible ERROR(variable has not been initialized)
LOD getSymbol().L getSymbol().M
printToFile()
lines++;
else if getSymbol() == constant
INC 0 1
LIT 0 getSymbol().val
printToFile()
lines += 2;
*/
GETTOKEN();
}
else if(TOKEN == numbersym)
{
/*
INC 0 1
LIT 0 NUMBER
printToFile()
lines += 2;
*/
GETTOKEN();
}
else if(TOKEN == lparentsym)
{
GETTOKEN();
EXPRESSION();
if(TOKEN != rparentsym)
{
ERROR("Error number 22, right parenthesis missing.");
}
GETTOKEN();
}
else
{
printf("%d\n", TOKEN);
ERROR("Error number 23, the preceding factor cannot begin with this symbol.");
}
}
void TERM()
{
FACTOR();
while(TOKEN == multsym || TOKEN == slashsym)
{
GETTOKEN();
FACTOR();
/*
OPR 0 4 (for mult)
OPR 0 5 (for div)
printToFile()
lines +=2;
*/
}
}
void EXPRESSION()
{
if(TOKEN == plussym || TOKEN == minussym)
{
/*
if(minussym)
OPR 0 1
printToFile()
lines++;
*/
GETTOKEN();
}
TERM();
while(TOKEN == plussym || TOKEN == minussym)
{
GETTOKEN();
TERM();
/*
OPR 0 2 (for add)
OPR 0 3 (for sub)
printToFile()
lines +=2;
*/
}
}
void CONDITION()
{
if(TOKEN == oddsym)
{
GETTOKEN();
EXPRESSION();
/*
OPR 0 6
printToFile()
lines ++;
*/
}
else
{
EXPRESSION();
if(!isRelationalOperator(TOKEN))
{
ERROR("Error number 20, relational operator expected.");
}
GETTOKEN();
EXPRESSION();
/*
OPR 0 [conditionValue
printToFile()
lines ++;
*/
}
}
void STATEMENT()
{
//initialization
if(TOKEN == identsym)
{
//stores the name of the identifier that will be initialized
char name[12];
strcpy(name, IDENTIFIER);
//if identifier is not a variable, produce error
if(getSymbol(IDENTIFIER).kind != 2)
{
ERROR("Error number 12, assignment to constant or procedure not allowed.");
}
GETTOKEN();
if(TOKEN != becomessym)
{
ERROR("Error number 13, assignment operator expected.");
}
GETTOKEN();
EXPRESSION();
/*
STO getSymbol(name).L getSymbol(name).M
printToFile()
lines++;
*/
}
//procedure call (not in tiny PL/0)
else if(TOKEN == callsym)
{
GETTOKEN();
if(TOKEN != identsym)
{
ERROR("Error number 14, call must be followed by an identifier.");
}
//if the identifier is not a procedure, produce an error
if(getSymbol(IDENTIFIER).kind != 3)
{
ERROR("Error number 15, call of a constant or variable is meaningless.");
}
GETTOKEN();
}
//a group of statements
else if(TOKEN == beginsym)
{
GETTOKEN();
STATEMENT();
while(TOKEN == semicolonsym)
{
GETTOKEN();
STATEMENT();
}
if(TOKEN != endsym)
{
ERROR("Error number 26, end is expected.");
}
GETTOKEN();
}
else if(TOKEN == ifsym)
{
GETTOKEN();
CONDITION();
//top of the stack has whether it is true or false
if(TOKEN != thensym)
{
ERROR("Error number 16, then expected.");
}
//after the condition, count how many instructions are written
int currentLines = lines;
inConditional++;
fpos_t filePos;
fgetpos(ifp, &filePos);
//loop ensures this is done twice
int i;
for(i = 0; i < 2; i++)
{
if(i == 1)
{
inConditional--;
//make branch here (lines contains the line that you jump to if the condition is not met)
//printToFile()
//returns the file to the previous position
fsetpos(ifp, &filePos);
lines = currentLines;
}
//the line for the branch is added
//lines++;
GETTOKEN();
STATEMENT();
}
}
else if(TOKEN == whilesym)
{
GETTOKEN();
CONDITION();
//top of the stack has whether it is true or false
if(TOKEN != dosym)
{
ERROR("Error number 18, do expected.");
}
//after the condition, count how many instructions are written
int currentLines = lines;
inConditional++;
fpos_t filePos;
fgetpos(ifp, &filePos);
//loop ensures this is done twice
int i;
for(i = 0; i < 2; i++)
{
if(i == 1)
{
inConditional--;
//make branch here (lines contains the line that you jump to if the condition is not met)
//printToFile()
//returns the file to the previous position
fsetpos(ifp, &filePos);
lines = currentLines;
}
//the line for the branch is added
//lines++;
GETTOKEN();
STATEMENT();
//Jump back to condition (currentLines is where you jump to)
//printToFile()
//lines++
}
}
}
void BLOCK()
{
if(TOKEN == constsym)
{
do
{
GETTOKEN();
if(TOKEN != identsym)
{
ERROR("Error number 4, const must be followed by identifier.");
}
GETTOKEN();
if(TOKEN == becomessym)
{
ERROR("Error number 1, use = instead of :=");
}
if(TOKEN != eqlsym)
{
ERROR("Error number 2, identifier must be followed by =");
}
GETTOKEN();
if(TOKEN != numbersym)
{
ERROR("Error number 3, = must be followed by a number.");
}
ENTER(1);
GETTOKEN();
} while(TOKEN == commasym);
if(TOKEN != semicolonsym)
{
ERROR("Error number 5, semicolon or comma missing.");
}
GETTOKEN();
}
if(TOKEN == varsym)
{
do
{
GETTOKEN();
if(TOKEN != identsym)
{
ERROR("Error number 4, var must be followed by identifier.");
}
ENTER(2);
GETTOKEN();
} while(TOKEN == commasym);
if(TOKEN != semicolonsym)
{
ERROR("Error number 5, semicolon or comma missing.");
}
GETTOKEN();
}
//procedure section (will not be used in module 3)
while(TOKEN == procsym)
{
GETTOKEN();
if(TOKEN != identsym)
{
ERROR("Error number 4, procedure must be followed by identifier.");
}
ENTER(3);
GETTOKEN();
if(TOKEN != semicolonsym)
{
ERROR("Error number 6, incorrect symbol after procedure declaration.");
}
GETTOKEN();
BLOCK();
if(TOKEN != semicolonsym)
{
//may need to be a different error message
ERROR("Error number 5, semicolon or comma missing.");
}
GETTOKEN();
}
/*
INC 0 (4 + numOfVariables)
printToFile()
lines++
*/
STATEMENT();
}
void PROGRAM()
{
GETTOKEN();
BLOCK();
if(TOKEN != periodsym)
{
ERROR("Error number 9, period expected.");
}
}
int main()
{
ifp = fopen("lexemelist.txt", "r");
PROGRAM();
printf("parsing finished\n");
int i;
for(i = 0; i < numOfSymbols; i++)
{
printf("%d %s\n", symbol_table[i].kind, symbol_table[i].name);
}
fclose(ifp);
}