-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paths4.c
676 lines (583 loc) · 19.1 KB
/
s4.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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
/*
==================================================
EECS3301 Project 1 Version A
Jun Lin Chen chen256@my.yorku.ca
Vishal Malik vishal27@my.yorku.ca
Tong Wu malan52.82@gmail.com
==================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
/*====================================================*/
/* Data Type and Variable Declaration */
/*====================================================*/
#define MAX_LEXEME_LENGTH 100
#define CLASS_OTHERS 0
#define CLASS_LETTER 1
#define CLASS_DIGIT 2
#define CLASS_SYMBOL 3
struct Lexeme{
int tokenClass;
char * value;
size_t length;
struct Lexeme * next;
};
struct Lexeme * lexemeList;
struct Label {
struct Lexeme * labelName;
struct Lexeme * nextCommand;
struct Label * next;
};
struct Label * labelList;
struct Variable {
char * variableName;
long variableValue;
struct Variable * next;
};
struct Variable * variableList;
// RDP functions
struct Lexeme * symbolFactor(struct Lexeme * pointer, long * writeBack);
struct Lexeme * symbolTerm(struct Lexeme * pointer, long * writeBack);
struct Lexeme * symbolExpr(struct Lexeme * pointer, long * writeBack);
struct Lexeme * symbolStatementPrint(struct Lexeme * pointer);
struct Lexeme * symbolStatementIfPositive(struct Lexeme * pointer);
struct Lexeme * symbolStatementAssign(struct Lexeme * pointer);
struct Lexeme * symbolStatementLabel(struct Lexeme * pointer);
struct Lexeme * symbolStatementComment(struct Lexeme * pointer);
struct Lexeme * symbolStatement(struct Lexeme * pointer);
/*========================================*/
/* Utility */
/*========================================*/
/*
Print Error Function
Print out error message (including syntax error messages) and exit the program.
Argument:
error - error message
*/
void printError(char * error, char * symbol, char * tokenValue){
printf("Error:\n");
if (symbol != NULL){
printf("\tIn %s lexeme \"%s\".\n",symbol, tokenValue == NULL ? "NULL" : tokenValue);
}
printf("\t%s\n", error);
exit(1);
}
/*
Initialize Function
Initialize memory for storing labels and variables.
*/
void initialize(){
labelList = malloc(sizeof(struct Label));
variableList = malloc(sizeof(struct Variable));
if (labelList == NULL || variableList == NULL){
printError("Not Enough Memory", NULL, NULL);
}
labelList->next = NULL;
variableList->next = NULL;
}
/*========================================*/
/* Buffer & Lexical Analysis */
/*========================================*/
/*
Create New Lexeme String Function
Allocate memory space for Lexeme. The lexeme will be stored as a string (char[]).
Maximum size is defined by MAX_LEXEME_LENGTH.
Return:
pointer to char[]. the string of lexeme
*/
char * createNewLexemeString(){
char * temp = malloc(MAX_LEXEME_LENGTH);
if (temp == NULL){
printError("Not Enough Memory", NULL, NULL);
}
return temp;
}
/*
Create New Lexeme Structure Function
Allocate memory space for lexeme structure. One lexeme structure contains one lexeme and its character class.
Return:
pointer to a empty and new lexeme structure
*/
struct Lexeme * createNewLexeme(){
struct Lexeme * temp = malloc(sizeof(struct Lexeme));
if (temp == NULL){
printError("Not Enough Memory", NULL, NULL);
}
temp->length = 0;
temp->value = createNewLexemeString();
return temp;
}
/*
Read Source Function
Read script from standard input. And store it in buffer (lexemeList).
This is like a light compiling process.
Argument:
pointer - allows you to continue writing on the buffer.
*/
void readSource(struct Lexeme * pointer){
if (pointer == NULL){
pointer = createNewLexeme();
lexemeList = pointer;
}
bool appendLexeme = false;
char c;
while ((c = getchar()) != EOF){
if (isdigit(c)) {
if (!appendLexeme){
pointer->next = createNewLexeme();
pointer = pointer->next;
pointer->tokenClass = CLASS_DIGIT;
appendLexeme = true;
}
if (pointer->length < MAX_LEXEME_LENGTH){ // avoid out of index problem
pointer->value[pointer->length ++] = c;
pointer->value[pointer->length] = '\0';
}
}else if (isalpha(c)) {
if (!appendLexeme){
pointer->next = createNewLexeme();
pointer = pointer->next;
pointer->tokenClass = CLASS_LETTER;
appendLexeme = true;
}
if (pointer->length < MAX_LEXEME_LENGTH){ // avoid out of index problem
pointer->value[pointer->length ++] = c;
pointer->value[pointer->length] = '\0';
}
}else if (ispunct(c)){
pointer->next = createNewLexeme();
pointer = pointer->next;
pointer->tokenClass = CLASS_SYMBOL;
pointer->value[pointer->length ++] = c;
pointer->value[pointer->length] = '\0';
appendLexeme = false;
}else{
appendLexeme = false;
continue;
}
}
}
/*========================================*/
/* Label List */
/*========================================*/
/*
Create New Label Function
Allocates memory for label structure.
Return:
Label structure
*/
struct Label * createNewLabel(){
struct Label * temp = malloc(sizeof(struct Label *));
if (temp == NULL){
printError("Not Enough Memory", NULL, NULL);
}
temp->next = NULL;
return temp;
}
/*
Add Label Function
Add label to list. Over write if it exists.
Argument:
labelName - the lexeme structure which stores the label name
nextComment - the lexeme structure which stores the semicolon for the label statement
*/
void addLabel(struct Lexeme * labelName, struct Lexeme * nextComment){
//second declaration for the same label name will over write the previous one
struct Label * p = labelList;
while (p->next != NULL){
p = p->next;
if (strcmp(labelName->value, p->labelName->value) == 0){
p->nextCommand = nextComment;
return;
}
}
p->next = createNewLabel();
p = p->next;
p->labelName = labelName;
p->nextCommand = nextComment;
}
/*
Go To Label Function
return the pointer to the semicolon of the label statement. Therefore the RDP can execute next command.
Argument:
name - of the label
Return:
pointer to the semicolon.
*/
struct Lexeme * gotoLabel(char * name){
struct Label * p = labelList;
while ((p = p->next) != NULL){
if (strcmp(name, p->labelName->value) == 0){
return p->nextCommand;
}
}
printError("Runtime: Lable Not Found", "label name searching process, for", name);
return NULL;
}
/*========================================*/
/* Variable List */
/*========================================*/
/*
Create New Variable Function
Allocate memory for Variable structure.
Return:
Variable structure.
*/
struct Variable * createNewVariable(){
struct Variable * temp = malloc(sizeof(struct Variable));
if (temp == NULL){
printError("Runtime: Not Enough Memory", NULL, NULL);
}
temp->next = NULL;
return temp;
}
/*
Set Variable Function
Set the value of a variable. If the variable exists, change its value.
Argument:
name - variable name
value - value of the variable
*/
void setVariable(char * name, long value){
struct Variable * p = variableList;
while (p->next != NULL){
p = p->next;
if (strcmp(name, p->variableName) == 0){
p->variableValue = value;
return;
}
}
p->next = createNewVariable();
p = p->next;
p->variableName = name;
p->variableValue = value;
}
/*
Get Variable Function
Get the value of a variable.
Argument:
name - variable name
Return:
value of the variable
*/
long getVariable(char * name){
struct Variable * p = variableList;
while ((p = p->next) != NULL){
if (strcmp(name, p->variableName) == 0) {
return p->variableValue;
}
}
printError("Variable Not Defined", "variable searching process, for", name);
return 0;
}
/*========================================*/
/* lexicaly Operation */
/*========================================*/
/*
Check Lexeme Function
Confirm if the lexeme is what we want.
Argument:
lexeme - lexeme structure
compareTo - lexeme
Return:
True if same
*/
bool checkLexeme(struct Lexeme * lexeme, char * compareTo){
if (lexeme == NULL || lexeme->value == NULL || compareTo == NULL){
return false;
}
return strcmp(lexeme->value, compareTo) == 0;
}
/*
Get Lexeme Function
Return:
lexeme itself as string (char[])
*/
char * getLexeme(struct Lexeme * lexeme){
return lexeme->value;
}
/*========================================*/
/* Recursive-Descent Parsing Functions */
/*========================================*/
/*
There is a double semicolon issue in Prof's PDF. So after discussing with Prof we are following these EBNF
<letter> -> A | B | … | Z | a | b | … | z
<digit> -> 0 | 1 | 2 | 3 | 4 | 5| 6 | 7 | 8 | 9
<int_constant> -> <digit> { <digit> }
<id> -> <letter> { <letter> | <digit> }
<factor> -> id | int_constant | (<expr>)
<term> -> <factor> { * <factor>}
<expr> -> <term> { ( + | - ) <term> }
<s_print> -> print <expr>
<s_goto> -> goto <id>
<s_ifpos> -> ifpos <expr> goto <id>
<s_assign> -> <id> = <expr>
<s_label> -> label <id>
<statement> -> <s_print> |<s_assign> |<s_label> |<s_goto> |<s_ifpos>
<program> -> <statement> { {;} <statement> } [;]
We also add a comment feature.
<s_comment> -> COMMENT: <anything except semicolon>
<statement> -> <s_print> |<s_assign> |<s_label> |<s_goto> |<s_ifpos> |<s_comment>
In some situation, there are too many options that can be chosen. In each recursive-descent function, there are two parts.
1. "SOFT" means the RDP has NOT YET confirmed the statement exists in the functions' statements.
If it cannot recognize the statement, it will return the pointer back to upper level without moving the pointer.
2. "HARD" means the RDP has confirmed that the statement exists in the functions' statement.It cannot be anything other than this option.
If it didn't recognize the statement, it will produce a syntax error.
Although the professor said we can have all the keywords reserved, however in this implementation,
all the keywords can be <id>. That means you can have a variable name same as any keyword.
*/
struct Lexeme * symbolFactor(struct Lexeme * pointer, long * writeBack){
// <factor> -> id | int_constant | (<expr>)
// HARD
long value = 0;
if (checkLexeme(pointer, "(")){
long cb = 0;
pointer = symbolExpr(pointer->next, &cb);
value = value + cb;
if (!checkLexeme(pointer, ")")){
printError("Syntax: Expecting Right Parentheses for '(<expr>).'", "<factor>", pointer->value);
}
* writeBack = value;
}else if (pointer->tokenClass == CLASS_LETTER){
value = getVariable(pointer->value);
}else if (pointer->tokenClass == CLASS_DIGIT) {
value = atol(pointer->value);
}else{
printError("Syntax: CANNOT Find a valid 'id | int_constant | (<expr>)'", "<factor>", pointer->value);
}
*writeBack = value;
return pointer->next;
}
struct Lexeme * symbolTerm(struct Lexeme * pointer, long * writeBack){
// <term> -> <factor> { * <factor>}
// HARD
long value = 0;
pointer = symbolFactor(pointer, &value);
for (;;){
if (checkLexeme(pointer, "*")) {
long cb = 0;
pointer = symbolTerm(pointer->next, &cb);
value = value * cb;
}else {
break;
}
}
*writeBack = value;
return pointer;
}
struct Lexeme * symbolExpr(struct Lexeme * pointer, long * writeBack){
// <expr> -> <term> { ( + | - ) <term> }
// HARD
long value = 0;
pointer = symbolTerm(pointer, &value);
for (;;){
if (checkLexeme(pointer, "+")){
long cb = 0;
pointer = symbolTerm(pointer->next, &cb);
value = value + cb;
}else if (checkLexeme(pointer, "-")){
long cb = 0;
pointer = symbolTerm(pointer->next, &cb);
value = value - cb;
}else {
break;
}
}
*writeBack = value;
return pointer;
}
struct Lexeme * symbolStatementPrint(struct Lexeme * pointer){
// <s_print> -> print <expr>
// SOFT
if (!checkLexeme(pointer, "print")){
return pointer;
}
if (pointer->next == NULL || (pointer->next->tokenClass != CLASS_DIGIT && pointer->next->tokenClass != CLASS_LETTER && !checkLexeme(pointer->next, "("))) {
return pointer;
}
// HARD
long value = 0;
pointer = symbolExpr(pointer->next, &value);
printf("%ld\n", value);
return pointer;
}
struct Lexeme * symbolStatementGoTo(struct Lexeme * pointer){
// <s_goto> -> goto <id>
// SOFT
if (!checkLexeme(pointer, "goto")){
return pointer;
}
if (pointer->next == NULL || pointer->next->tokenClass != CLASS_LETTER) {
return pointer;
}
// HARD
// We need to check the semicolon here rather than in computeParse() function
// Because the reading pointer will jump away after return.
if (pointer->next->next != NULL && !checkLexeme(pointer->next->next, ";")){
printError("Syntax: '<s_goto> -> goto <id> ;' Expecting ';'.", "<s_goto>", pointer->value);
}
return gotoLabel(pointer->next->value);
}
struct Lexeme * symbolStatementIfPositive(struct Lexeme * pointer){
// <s_ifpos> -> ifpos <expr> goto <id>
// SOFT
if (!checkLexeme(pointer, "ifpos")){
return pointer;
}
// FIRST(<expr>) = {a-zA-Z0-9\(}
if (pointer->next == NULL || checkLexeme(pointer->next, "=")) {
return pointer;
}
// HARD
long value = 0;
pointer = symbolExpr(pointer->next, &value);
if (!checkLexeme(pointer, "goto")){
printError("Syntax: '<s_ifpos> -> ifpos <expr> goto <id> ;' Expecting 'goto'.", "<s_goto>", pointer->value);
}
if (pointer->next == NULL || pointer->next->tokenClass != CLASS_LETTER) {
printError("Syntax: '<s_ifpos> -> ifpos <expr> goto <id> ;' Expecting '<id>'.", "<s_goto>", pointer->value);
}
// We need to check the semicolon here rather than in computeParse() function
// Because the reading pointer will jump away after return.
if (pointer->next->next != NULL && !checkLexeme(pointer->next->next, ";")){
printError("Syntax: '<s_ifpos> -> ifpos <expr> goto <id> ;' Expecting ';'.", "<s_goto>", pointer->value);
}
if (value > 0){
return gotoLabel(pointer->next->value);
}
return pointer->next->next;
}
struct Lexeme * symbolStatementAssign(struct Lexeme * pointer){
// <s_assign> -> <id> = <expr>
// SOFT
if (pointer->tokenClass != CLASS_LETTER){
return pointer;
}
char * name = pointer->value;
if (pointer->next == NULL || !checkLexeme(pointer->next, "=")){
return pointer;
}
// HARD
long value = 0;
pointer = symbolExpr(pointer->next->next, &value);
setVariable(name, value);
return pointer;
}
struct Lexeme * symbolStatementLabel(struct Lexeme * pointer){
// <s_label> -> label <id>
// We don't do anything here. Because label parsing is in a separate function.
// SOFT
if (!checkLexeme(pointer, "label")){
return pointer;
}
if (pointer->next == NULL || pointer->next->tokenClass != CLASS_LETTER){
return pointer;
}
// HARD
return pointer->next->next;
}
struct Lexeme * symbolStatementComment(struct Lexeme * pointer){
// <s_comment> -> COMMENT: <anything except semicolon>
// SOFT
if (!checkLexeme(pointer, "COMMENT")){
return pointer;
}
if (pointer->next == NULL || !checkLexeme(pointer->next, ":")){
return pointer;
}
// HARD
while ((pointer = pointer->next) != NULL){
if (checkLexeme(pointer, ";")){
return pointer;
}
}
return pointer;
}
struct Lexeme * symbolStatement(struct Lexeme * pointer){
// <statement> -> <s_print> |<s_assign> |<s_label> |<s_goto> |<s_ifpos> |<s_comment>
struct Lexeme * onHold = pointer;
if ((pointer = symbolStatementPrint(pointer)) != onHold){
return pointer;
}
if ((pointer = symbolStatementGoTo(pointer)) != onHold){
return pointer;
}
if ((pointer = symbolStatementIfPositive(pointer)) != onHold){
return pointer;
}
if ((pointer = symbolStatementAssign(pointer)) != onHold){
return pointer;
}
if ((pointer = symbolStatementLabel(pointer)) != onHold){
return pointer;
}
if ((pointer = symbolStatementComment(pointer)) != onHold){
return pointer;
}
// HARD
return pointer;
}
/*========================================*/
/* Parsing Process */
/*========================================*/
/*
Label Parse Function
Find all the labels and save them in the label list.
Argument:
pointer - the starting point to find
*/
void labelParse(struct Lexeme * pointer){
bool checkLabelToken = true;
while ((pointer = pointer->next) != NULL){
if (checkLexeme(pointer, "label") && checkLabelToken){
// Check the next two Tokens
// <id> [;]
if (pointer->next == NULL){
printError("Syntax: '<s_label> -> label <id> ;' Expecting <id>", "<s_label>", pointer->next->value);
}
if (pointer->next->tokenClass != CLASS_LETTER){
checkLabelToken = false;
continue;
}
if (pointer->next->next != NULL && !checkLexeme(pointer->next->next, ";")){
printError("Syntax: '<s_label> -> label <id> ;' Expecting ';'", "<s_label>", pointer->next->next->value);
}
addLabel(pointer->next,pointer->next->next);
}else if (checkLexeme(pointer, ";")){
checkLabelToken = true;
}else{
checkLabelToken = false;
}
}
}
/*
Compute Parse Function
This parsing will compute the result.
Argument:
pointer - the starting point
*/
void computeParse(struct Lexeme * pointer){
// <program> -> <statement> { ; <statement> } [;]
while ((pointer->next) != NULL){
pointer = symbolStatement(pointer->next);
if (pointer == NULL){
break;
}else if (!checkLexeme(pointer, ";")){
printError("Syntax: Statement CANNOT Be Resolved.", "<s_label>", pointer->value);
}
}
}
/*========================================*/
/* MAIN */
/*========================================*/
//Main Function
int main(){
initialize();
readSource(NULL);
labelParse(lexemeList);
computeParse(lexemeList);
return 0;
}