-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselfcomp.c
More file actions
3105 lines (2860 loc) · 84.8 KB
/
selfcomp.c
File metadata and controls
3105 lines (2860 loc) · 84.8 KB
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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* PSEUDOCODE (SELFCOMPILING) C COMPILER
* authors: thomas huetter 1120239, mario preishuber 1120643
*
*/
struct object_t;
struct type_t;
struct token_t {
int id;
int digitValue;
int lineNr;
int colNr;
char *valueStr;
};
struct type_t {
int form;
int size;
struct object_t *fields;
struct type_t *base;
};
struct object_t {
char *name;
int class;
int offset;
int scope;
struct type_t *type;
struct object_t *next;
struct object_t *params;
int value;
};
struct item_t {
int mode;
struct type_t *type;
int reg;
int offset;
int value;
int op;
int fls;
int tru;
};
struct cmd_t {
int op;
int a;
int b;
int c;
};
/******************************************************************************************/
/************************************** targetMachine Commands ****************************/
/******************************************************************************************/
void initTMCmd();
char *getCmdName(int cmd_id);
/* immediate addressing */
int CMD_ADDI; //= 10;
int CMD_SUBI; //= 11;
int CMD_MULI; //= 12;
int CMD_DIVI; //= 13;
int CMD_MODI; //= 14;
int CMD_CMPI; //= 15;
/* register addressing */
int CMD_ADD; //= 20;
int CMD_SUB; //= 21;
int CMD_MUL; //= 22;
int CMD_DIV; //= 23;
int CMD_MOD; //= 24;
int CMD_CMP; //= 25;
/* memory: load and store */
int CMD_LDW; //= 30;
int CMD_STW; //= 31;
/* stack operations */
int CMD_POP; //= 40;
int CMD_PSH; //= 41;
/* conditional branching */
int CMD_BEQ; //= 50;
int CMD_BGE; //= 51;
int CMD_BGT; //= 52;
int CMD_BLE; //= 53;
int CMD_BLT; //= 54;
int CMD_BNE; //= 55;
/* unconditional branching */
int CMD_BR ; //= 56;
int CMD_BSR; //= 57;
int CMD_JSR; //= 58;
int CMD_RET; //= 59;
/* i/o operations */
int CMD_FLO; //= 60;
int CMD_FLC; //= 61;
int CMD_RDC; //= 62;
int CMD_WRC; //= 63;
/* end of code */
int CMD_TRAP; //= 1;
/* meta data */
int CMD_GP; // = 1; /* global pointer */
int CMD_SP; // = 3; /* string pointer */
int CMD_CS; // = 2; /* code size */
int CMD_MAL; // = 4; /* malloc */
int CMD_PRN; // = 5; /* print given value as integer */
int CMD_PRC; // = 6; /* print given value as char */
/******************************************************************************************/
/************************************** tokenMapping **************************************/
/******************************************************************************************/
void initTokenMapping();
int ERROR ; /* = -1; invalid token */
int INIT ; /* = 0; */
int LSQBR ; /* = 1; [ */
int RSQBR ; /* = 2; ] */
int LPAR ; /* = 3; ( */
int RPAR ; /* = 4; ) */
int LCUBR ; /* = 5; { */
int RCUBR ; /* = 6; } */
int SEMCOL ; /* = 7; ; */
int COMMA ; /* = 8; , */
int DQUOTE ; /* = 9; " */
int QUOTE ; /* = 10; ' */
int EQSIGN ; /* = 11; = */
int PLUS ; /* = 12; + */
int MINUS ; /* = 13; - */
int TIMES ; /* = 14; * */
int DIV ; /* = 15; / */
int LT ; /* = 16; < */
int GT ; /* = 17; > */
int EQ ; /* = 18; == */
int NEQ ; /* = 19; != */
int LET ; /* = 20; <= */
int GET ; /* = 21; >= */
int AND ; /* = 22; && */
int OR ; /* = 23; || */
int INT ; /* = 30; key-word: int */
int CHAR ; /* = 31; key-word: char */
int VOID ; /* = 32; key-word: void */
int STRUCT ; /* = 33; key-word: struct */
int TYPEDEF ; /* = 34; key-word: typedef */
int BOOL ; /* = 35; boolean value */
int IDENT ; /* = 36; identifier */
int NUMBER ; /* = 37; number value */
int STRING ; /* = 38; string value */
int CHARACTER ; /* = 39; character value */
int IF ; /* = 40; key-word: if */
int ELSE ; /* = 41; key-word: else */
int WHILE ; /* = 42; key-word: while */
int RETURN ; /* = 43; key-word: return */
int COMMENT ; /* = 50; comment (this) */
int DOT ; /* = 51; . */
int INCLUDE ; /* = 52; #include */
int LF ; /* = 53; \n */
int END ; /* = 54; last token */
int ARROW ; /* = 55; -> */
int SIZEOF ; /* = 56; size of type */
int MALLOC ; /* = 57; allocate memory */
int PRINTF ; /* = 58; write to output */
int OPEN ; /* = 60; open file */
int CLOSE ; /* = 61; close file */
int READ ; /* = 62; read file */
int WRITE ; /* = 63; write file */
/******************************************************************************************/
/**************************************** scanner *****************************************/
/******************************************************************************************/
int fd;
int lineNr;
int colNr;
char ungetc;
struct token_t *symbol;
void initScanner(char *file);
void closeScanner();
char getChar();
void ungetChar(char c);
bool hasMoreChars();
bool hasMoreTokens();
void getNextToken();
void initToken();
int getNumber(char pnr);
void getIdentifier(char *identifier, char c);
bool isLetter(char c);
bool isDigit(char c);
int strCmp(char *s1, char *s2);
int strnCpy(char *s1, char *s2, int n);
/******************************************************************************************/
/************************************** symboltable ***************************************/
/******************************************************************************************/
typedef char *string_t;
int GLOBAL_SCOPE;
int LOCAL_SCOPE;
int TYPE_FORM_INT;
int TYPE_FORM_CHAR;
int TYPE_FORM_VOID;
int TYPE_FORM_BOOL;
int TYPE_FORM_ARRAY;
int TYPE_FORM_RECORD;
int TYPE_FORM_UNKNOWN;
int OBJECT_CLASS_VAR;
int OBJECT_CLASS_TYPE;
int OBJECT_CLASS_FIELD;
int OBJECT_CLASS_PROC;
int globOffset;
int locOffset;
int paramOffset;
int nrOfGVar;
char *file;
struct object_t *globList;
struct object_t *locList;
struct object_t *lookUp(struct object_t *head, string_t name);
struct object_t *findProcedureObject(struct object_t *head, string_t name);
void insertName(struct object_t *ptr, string_t name);
int insert(struct object_t *head, struct object_t *obj);
struct object_t *delete(struct object_t *head, string_t name);
struct type_t *newType(int form);
void initSymbolTable();
/******************************************************************************************/
/**************************************** parser *****************************************/
/******************************************************************************************/
char *srcfile;
char *outfile;
int *out_cmd_op;
int *out_cmd_a;
int *out_cmd_b;
int *out_cmd_c;
int nrOfStrs;
int errorCounter;
int PC;
int returnFJumpAddress;
int mainPos;
int typeWA;
struct object_t *procedureContext;
int RR; /* return register */
int LINK; /* link pointer */
int FPTR; /* frame pointer */
int GPTR; /* global pointer */
int SPTR; /* stack pointer */
int HPTR; /* heap pointer */
int STRPTR; /* string pointer */
int *regs;
int nrOfRegs;
int ITEM_MODE_CONST;
int ITEM_MODE_VAR;
int ITEM_MODE_REF;
int ITEM_MODE_REG;
int ITEM_MODE_COND;
int ITEM_MODE_NONE;
int OP_NONE;
int OP_ADD;
int OP_SUB;
int OP_MUL;
int OP_DIV;
int OP_AND;
int OP_OR;
int OP_EQ;
int OP_NEQ;
int OP_GT;
int OP_GET;
int OP_LT;
int OP_LET;
void initOutputFile();
void finalizeOutputFile();
int cg_encode(int op, int a, int b, int c);
void cg_put(int op, int a, int b, int c);
void copyItem(struct item_t *copy, struct item_t *orig);
void initItemModes();
void initOperators();
void cg_initRegs();
int cg_requestReg();
void cg_releaseReg(int r);
void cg_const2Reg(struct item_t *item);
void cg_var2Reg(struct item_t *item);
void cg_ref2Reg(struct item_t *item);
void cg_load(struct item_t *item);
void cg_simpleExpOR(struct item_t* item);
void cg_termOpAND(struct item_t* item);
void cg_simpleExpBinOp(struct item_t *leftItem, struct item_t *rightItem, int op);
void cg_termOperator(struct item_t *leftItem, struct item_t *rightItem, int op);
void cg_expressionOperator(struct item_t *leftItem, struct item_t *rightItem, int op);
void cg_field(struct item_t *item, struct object_t *object);
void cg_index(struct item_t *item, struct item_t *indexItem);
void cg_assignment(struct item_t *leftItem, struct item_t *rightItem);
void cg_allocate(struct item_t *item);
void cg_cJump(struct item_t *item);
int cg_fJump();
void cg_bJump(int backAddress);
void cg_fixUp(int branchAddress);
void cg_fixLink(int branchAddress);
int cg_concatenate(int right, int left);
int cg_negateOperator(int op);
int cg_branch(int op);
void cg_encodeC(int branchAddress, int newC);
int cg_decodeC(int branchAddress);
void cg_loadBool(struct item_t *item);
void cg_unloadBool(struct item_t *item);
void printError(char *msg);
bool number();
bool compOp();
bool op();
bool identifier();
int typeSpec(struct item_t *item, struct object_t *head);
bool include();
bool storeString(struct item_t *item);
bool sizeOf(struct item_t *item, struct object_t *head);
bool selector(struct item_t *item);
bool factor(struct item_t *item);
bool term(struct item_t *item);
bool arithExp(struct item_t *item);
bool expression(struct item_t *item);
bool printMethod(struct item_t *item);
bool fileOpen(struct item_t *item);
bool fileClose(struct item_t *item);
bool fileWrite(struct item_t *item);
bool fileRead(struct item_t *item);
bool ifCmd(struct item_t *item);
bool whileLoop(struct item_t *item);
int variableDeclarationSequence(struct object_t *head, int isStruct);
bool typedefDec(struct object_t *head);
bool structDec();
struct object_t *createObject(string_t name);
struct type_t *basicArrayRecordType();
void prologue(int localSize);
void epilogue(int paramSize);
bool procedureImplementation(struct item_t* item, string_t identifier);
bool formalParameters(struct object_t* object);
struct object_t* createFormalParameter(struct object_t* object, struct type_t* type, string_t identifier);
struct object_t* formalParameter(struct object_t* object, struct object_t* formalParameter);
bool procedureReturn();
bool fJumpChain(int branchAddress);
bool pushUsedRegisters();
void popUsedRegisters(int counter);
bool isBSR(int offset);
void procedureCall(struct item_t* item, string_t procName);
bool sJump(int branchAddress);
void pushParameter(struct item_t* item);
bool actualParameters(struct object_t* object);
struct object_t* createAnonymousParameter(struct object_t* object, struct type_t *type);
struct object_t* actualParameter(struct object_t* object, struct object_t* formalParameter);
bool globalDec();
bool statementSeq();
bool block();
bool programm();
bool startParsing(char *sfile, char *ofile);
void printString(char *str);
/******************************************************************************************/
void initTokenMapping() {
ERROR = -1; /* invalid token */
INIT = 0;
LSQBR = 1; /* [ */
RSQBR = 2; /* ] */
LPAR = 3; /* ( */
RPAR = 4; /* ) */
LCUBR = 5; /* { */
RCUBR = 6; /* } */
SEMCOL = 7; /* ; */
COMMA = 8; /* , */
DQUOTE = 9; /* " */
QUOTE = 10; /* ' */
EQSIGN = 11; /* = */
PLUS = 12; /* + */
MINUS = 13; /* - */
TIMES = 14; /* * */
DIV = 15; /* / */
LT = 16; /* < */
GT = 17; /* > */
EQ = 18; /* == */
NEQ = 19; /* != */
LET = 20; /* <= */
GET = 21; /* >= */
AND = 22; /* && */
OR = 23; /* || */
INT = 30; /* key-word: */
CHAR = 31; /* key-word: char */
VOID = 32; /* key-word: void */
STRUCT = 33; /* key-word: struct */
TYPEDEF = 34; /* key-word: typedef*/
BOOL = 35; /* boolean value */
IDENT = 36; /* identifier */
NUMBER = 37; /* number value */
STRING = 38; /* string value */
CHARACTER = 39; /* character value */
IF = 40; /* key-word: if */
ELSE = 41; /* key-word: else */
WHILE = 42; /* key-word: while */
RETURN = 43; /* key-word: return */
COMMENT = 50; /* comment (this) */
DOT = 51; /* . */
INCLUDE = 52; /* #include */
LF = 53; /* \n */
END = 54; /* last token */
ARROW = 55; /* -> */
SIZEOF = 56; /* size of type */
MALLOC = 57; /* allocate memory */
PRINTF = 58; /* write to output */
OPEN = 60; /* open file */
CLOSE = 61; /* close file */
READ = 62; /* read file */
WRITE = 63; /* write file */
}
void initTMCmd() {
/* immediate addressing */
CMD_ADDI = 10;
CMD_SUBI = 11;
CMD_MULI = 12;
CMD_DIVI = 13;
CMD_MODI = 14;
CMD_CMPI = 15;
/* register addressing */
CMD_ADD = 20;
CMD_SUB = 21;
CMD_MUL = 22;
CMD_DIV = 23;
CMD_MOD = 24;
CMD_CMP = 25;
/* memory: load and store */
CMD_LDW = 30;
CMD_STW = 31;
/* stack operations */
CMD_POP = 40;
CMD_PSH = 41;
/* conditional branching */
CMD_BEQ = 50;
CMD_BGE = 51;
CMD_BGT = 52;
CMD_BLE = 53;
CMD_BLT = 54;
CMD_BNE = 55;
/* unconditional branching */
CMD_BR = 56;
CMD_BSR = 57;
CMD_JSR = 58;
CMD_RET = 59;
/* i/o operations */
CMD_FLO = 60;
CMD_FLC = 61;
CMD_RDC = 62;
CMD_WRC = 63;
/* end of code */
CMD_TRAP = 9;
/* meta data */
CMD_GP = 1; /* global pointer */
CMD_SP = 2; /* string pointer */
CMD_CS = 3; /* code size */
CMD_MAL = 4; /* malloc */
CMD_PRN = 5; /* print given value as integer */
CMD_PRC = 6; /* print given value as char */
}
char *getCmdName(int cmd_id) {
initTMCmd();
if(cmd_id == CMD_ADDI) return "addi";
if(cmd_id == CMD_SUBI) return "subi";
if(cmd_id == CMD_MULI) return "muli";
if(cmd_id == CMD_DIVI) return "divi";
if(cmd_id == CMD_CMPI) return "cmpi";
if(cmd_id == CMD_ADD ) return "add ";
if(cmd_id == CMD_SUB ) return "sub ";
if(cmd_id == CMD_MUL ) return "mul ";
if(cmd_id == CMD_DIV ) return "div ";
if(cmd_id == CMD_CMP ) return "cmp ";
if(cmd_id == CMD_LDW ) return "ldw ";
if(cmd_id == CMD_STW ) return "stw ";
if(cmd_id == CMD_POP ) return "pop ";
if(cmd_id == CMD_PSH ) return "psh ";
if(cmd_id == CMD_BEQ ) return "beq ";
if(cmd_id == CMD_BGE ) return "bge ";
if(cmd_id == CMD_BGT ) return "bgt ";
if(cmd_id == CMD_BLE ) return "ble ";
if(cmd_id == CMD_BLT ) return "blt ";
if(cmd_id == CMD_BNE ) return "bne ";
if(cmd_id == CMD_BR ) return "br ";
if(cmd_id == CMD_BSR ) return "bsr ";
if(cmd_id == CMD_JSR ) return "jsr ";
if(cmd_id == CMD_RET ) return "ret ";
if(cmd_id == CMD_TRAP) return "trap";
if(cmd_id == CMD_GP ) return "gp ";
if(cmd_id == CMD_SP ) return "sp ";
if(cmd_id == CMD_CS ) return "cs ";
if(cmd_id == CMD_MAL ) return "mal ";
if(cmd_id == CMD_PRN ) return "prn ";
if(cmd_id == CMD_PRC ) return "prc ";
if(cmd_id == CMD_FLO ) return "flo ";
if(cmd_id == CMD_FLC ) return "flc ";
if(cmd_id == CMD_WRC ) return "wrc ";
if(cmd_id == CMD_RDC ) return "rdc ";
return "unknown";
}
void main(){
char *sfile;
char *ofile;
sfile = "../selftest.c";
ofile = "../out_selftest";
initScanner(sfile);
if(fd == -1) { printError("can not open file"); }
else {
startParsing(sfile, ofile);
closeScanner();
}
}
/******************************************************************************************/
/**************************************** scanner *****************************************/
/******************************************************************************************/
void initScanner(char *file) {
fd = open(file, 0, 448); /* 0 ... read only 448 ... S_IWUSR | S_IRUSR | S_IXUSR */
ungetc = -1;
lineNr = 1;
colNr = 1;
initTokenMapping();
initToken();
}
void closeScanner() { close(fd); }
void initToken() {
char *str; str = "";
symbol = malloc(sizeof(struct token_t));
symbol->valueStr = malloc(64*sizeof(char));
symbol->id = INIT;
symbol->digitValue = -1;
strnCpy(symbol->valueStr, str, 0);
}
bool hasMoreTokens() {
char c; c = getChar();
while (c == '\n') {
c = getChar();
if (c == -1) { return 0; }
}
if (c == -1) { return 0; }
ungetChar(c);
return 1;
}
bool hasMoreChars() {
char c; c = getChar();
if (c == -1) { return 0; }
ungetChar(c);
return 1;
}
char getChar() {
char c;
char *buff;
int r;
buff = malloc(sizeof(char));
colNr = colNr + 1;
if(ungetc == -1) {
r = read(fd, buff, 1);
if(buff[0] == '\n') {
lineNr = lineNr + 1;
colNr = 0;
}
if(r == 0) { return -1; }
return buff[0];
}
c = ungetc;
ungetc = -1;
return c;
}
void ungetChar(char c) {
ungetc = c;
colNr = colNr - 1;
}
int strCmp(char *s1, char *s2) {
int i; i = 0;
while(s1[i] == s2[i]) {
if( (s1[i] == '\0') || (s1[i] == 0) ) { return 0; }
i = i + 1;
}
if(s1[i] < s2[i]) { return -1; }
return 1;
}
int strnCpy(char *s1, char *s2, int n) {
int i; i = 0;
while ((i < n) && (s2[i] != '\0') && (s2[i] != 0)) {
s1[i] = s2[i];
i = i+1;
}
s1[i] = 0;
return 0;
}
bool isLetter(char c) {
return (((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')));
}
bool isDigit(char c) { return ((c >= '0') && (c <= '9')); }
int getNumber(char pnr) {
int nr;
char actnr;
nr = pnr - '0';
actnr = getChar();
while(isDigit(actnr)) {
nr = nr * 10 + (actnr - '0');
actnr = getChar();
}
ungetChar(actnr);
return nr;
}
void getIdentifier(char *identifier, char c) {
char nc;
int i;
nc = getChar();
i = 1;
identifier[0] = c;
while((isLetter(nc) || isDigit(nc) || (nc == '_')) && hasMoreChars() && (i < 63)) {
identifier[i] = nc;
nc = getChar();
i = i + 1;
}
identifier[i] = 0;
ungetChar(nc);
}
/* fd ... filedescriptor */
void getNextToken() {
char c;
char nc;
char nnc;
char *token_ident;
char *buff;
char *str;
int i;
token_ident = malloc(64*sizeof(char));
buff = malloc(1000*sizeof(char));
c = getChar();
symbol->id = INIT; symbol->digitValue = -1; str = ""; strnCpy(symbol->valueStr, str, 0);
while(( (c == ' ') || (c == '\n') || (c == 10) || (c == '\r') || (c == '\t') || (c == 9) ) && hasMoreChars()) {
c = getChar();
}
if(isLetter(c)) {
getIdentifier(token_ident, c);
str = "if"; if(strCmp(token_ident, str) == 0) { symbol->id = IF; strnCpy(symbol->valueStr, token_ident, 2); }
str = "int"; if(strCmp(token_ident, str) == 0) { symbol->id = INT; strnCpy(symbol->valueStr, token_ident, 3); }
str = "char"; if(strCmp(token_ident, str) == 0) { symbol->id = CHAR; strnCpy(symbol->valueStr, token_ident, 4); }
str = "void"; if(strCmp(token_ident, str) == 0) { symbol->id = VOID; strnCpy(symbol->valueStr, token_ident, 4); }
str = "bool"; if(strCmp(token_ident, str) == 0) { symbol->id = BOOL; strnCpy(symbol->valueStr, token_ident, 4); }
str = "else"; if(strCmp(token_ident, str) == 0) { symbol->id = ELSE; strnCpy(symbol->valueStr, token_ident, 4); }
str = "open"; if(strCmp(token_ident, str) == 0) { symbol->id = OPEN; strnCpy(symbol->valueStr, token_ident, 4); }
str = "read"; if(strCmp(token_ident, str) == 0) { symbol->id = READ; strnCpy(symbol->valueStr, token_ident, 4); }
str = "write"; if(strCmp(token_ident, str) == 0) { symbol->id = WRITE; strnCpy(symbol->valueStr, token_ident, 5); }
str = "close"; if(strCmp(token_ident, str) == 0) { symbol->id = CLOSE; strnCpy(symbol->valueStr, token_ident, 5); }
str = "while"; if(strCmp(token_ident, str) == 0) { symbol->id = WHILE; strnCpy(symbol->valueStr, token_ident, 5); }
str = "return"; if(strCmp(token_ident, str) == 0) { symbol->id = RETURN; strnCpy(symbol->valueStr, token_ident, 6); }
str = "sizeof"; if(strCmp(token_ident, str) == 0) { symbol->id = SIZEOF; strnCpy(symbol->valueStr, token_ident, 6); }
str = "malloc"; if(strCmp(token_ident, str) == 0) { symbol->id = MALLOC; strnCpy(symbol->valueStr, token_ident, 6); }
str = "struct"; if(strCmp(token_ident, str) == 0) { symbol->id = STRUCT; strnCpy(symbol->valueStr, token_ident, 6); }
str = "printf"; if(strCmp(token_ident, str) == 0) { symbol->id = PRINTF; strnCpy(symbol->valueStr, token_ident, 6); }
str = "typedef"; if(strCmp(token_ident, str) == 0) { symbol->id = TYPEDEF; strnCpy(symbol->valueStr, token_ident, 7); }
if(symbol->id == INIT) { symbol->id = IDENT; strnCpy(symbol->valueStr, token_ident, 64); }
}
else {
if(isDigit(c)) { symbol->id = NUMBER; symbol->digitValue = getNumber(c); }
else {
if(c == '[') { symbol->id = LSQBR; }
if(c == ']') { symbol->id = RSQBR; }
if(c == '(') { symbol->id = LPAR; }
if(c == ')') { symbol->id = RPAR; }
if(c == '{') { symbol->id = LCUBR; }
if(c == '}') { symbol->id = RCUBR; }
if(c == ';') { symbol->id = SEMCOL; }
if(c == ',') { symbol->id = COMMA; }
if(c == '.') { symbol->id = DOT; }
if(c == '"') {
i = 0;
nc = getChar();
while( (nc != '"') && hasMoreTokens()) {
buff[i] = nc;
nc = getChar();
i = i + 1;
}
symbol->id = STRING; strnCpy(symbol->valueStr, buff, 1000);
symbol->digitValue = i;
}
if(c == '\'') {
buff[0] = getChar();
if(buff[0] == '\\') {
buff[1] = getChar();
buff[2] = '\0';
}
else {buff[1] = '\0';}
nc = getChar();
if(nc == '\'') { symbol->id = CHARACTER; strnCpy(symbol->valueStr, buff, 1); }
else {
symbol->id = QUOTE;
ungetChar(nc);
}
}
if(c == '=') {
nc = getChar();
if(nc == '=') {symbol->id = EQ;}
else {
symbol->id = EQSIGN;
ungetChar(nc);
}
}
if(c == '+') { symbol->id = PLUS; }
if(c == '-') {
nc = getChar();
if(nc == '>') {symbol->id = ARROW;}
else {
symbol->id = MINUS;
ungetChar(nc);
}
}
if(c == '*') { symbol->id = TIMES; }
if(c == '/') {
nc = getChar();
if(nc == '*') {
nc = getChar();
nnc = getChar();
while(( (nc != '*') || (nnc != '/') ) && hasMoreChars()) {
nc = nnc;
nnc = getChar();
}
symbol->id = COMMENT;
}
else {
if(nc == '/') {
nc = getChar();
while( (nc != '\n') && hasMoreChars() ) { nc = getChar(); }
symbol->id = COMMENT;
}
else {
symbol->id = DIV;
ungetChar(nc);
}
}
}
if(c == '!') {
nc = getChar();
if(nc == '=') { symbol->id = NEQ; }
else {
symbol->id = ERROR;
symbol->valueStr[0] = c; symbol->valueStr[1] = 0;
ungetChar(nc);
}
}
if(c == '<') {
nc = getChar();
if(nc == '=') { symbol->id = LET; }
else {
symbol->id = LT;
ungetChar(nc);
}
}
if(c == '>') {
nc = getChar();
if(nc == '=') { symbol->id = GET;}
else {
symbol->id = GT;
ungetChar(nc);
}
}
if(c == '&') {
nc = getChar();
if(nc == '&') { symbol->id = AND; }
else {
symbol->id = ERROR;
symbol->valueStr[0] = c; symbol->valueStr[1] = 0;
ungetChar(nc);
}
}
if(c == '|') {
nc = getChar();
if(nc == '|') { symbol->id = OR; }
else {
symbol->id = ERROR;
symbol->valueStr[0] = c; symbol->valueStr[1] = 0;
ungetChar(nc);
}
}
if(c == '#') {
nc = getChar();
str = "include ";
strnCpy(buff, str, 8);
i = 0;
while( (i < 8) && (nc == buff[i]) ) {
nc = getChar();
i = i + 1;
}
if(i == 8) {
if(nc == '"') {
nc = getChar();
i = 0;
buff[i] = nc;
while( (buff[i] != '\"') && (i < 1000) ) {
i = i + 1;
buff[i] = getChar();
}
buff[i] = 0;
symbol->id = INCLUDE;
strnCpy(symbol->valueStr, buff, 1000);
}
}
else { symbol->id = ERROR; strnCpy(symbol->valueStr, buff, 10); }
}
if(symbol->id == INIT) { symbol->id = ERROR; symbol->valueStr[0] = c; symbol->valueStr[1] = 0; }
}
}
if(symbol->id == COMMENT) { getNextToken(); }
symbol->lineNr = lineNr;
symbol->colNr = colNr;
}
/******************************************************************************************/
/************************************** symboltable ***************************************/
/******************************************************************************************/
struct object_t *lookUp(struct object_t *head, string_t name) {
struct object_t *ptr;
struct object_t *ptrParams;
if(head->name != 0) {
ptr = head;
while(ptr != 0) {
if(strCmp(ptr->name, name) == 0) {
return ptr;
}
ptr = ptr->next;
}
}
return 0;
}
struct object_t *findProcedureObject(struct object_t *head, string_t name) {
struct object_t *ptr;
if(head->name != 0) {
ptr = head;
while(ptr != 0) {
if((ptr->class == OBJECT_CLASS_PROC) && (strCmp(ptr->name, name) == 0)) {
return ptr;
}
ptr = ptr->next;
}
}
return 0;
}
void insertName(struct object_t *ptr, string_t name) {
ptr->name = malloc(64 * sizeof(char));
strnCpy(ptr->name, name, 64);
}
int insert(struct object_t *head, struct object_t *obj) {
struct object_t *ptr;
char *str; str = "--first entry--";
obj->next = 0;
if(head->name == 0) {
head->name = malloc(64 * sizeof(char));
strnCpy(head->name, str, 64);
head->class = 0;
head->offset = 0;
head->scope = 0;
head->type = 0;
head->next = obj;
} else {
if(lookUp(head, obj->name) != 0) {
errorCounter = errorCounter + 1;
printf(" ");
printString(srcfile); printf(":");
printf(symbol->lineNr);printf(":");
printf(symbol->colNr);printf(": ERROR: multible declaration of ");
printf(obj->name); printf("\n");
return 0;
}
ptr = head;
while(ptr->next != 0) {
ptr = ptr->next;
}
ptr->next = obj;
}
return 1;
}
struct object_t *delete(struct object_t *head, string_t name) {
struct object_t *ptr;
struct object_t *ptr1;
if(head != 0) {
if(strCmp(head->name, name) == 0) {
ptr = head->next;
head = ptr;
} else {
ptr = head;
while(ptr->next != 0) {
ptr1 = ptr->next;
if(strCmp(ptr1->name, name) == 0) {
ptr->next = ptr1->next;
}
ptr = ptr1;
}
}
}
return head;
}
struct type_t *newType(int form) {
struct type_t *type;
type = malloc (sizeof (struct type_t));
type->form = form;
return type;
}
void initSymbolTable() {
globList = malloc(sizeof(struct object_t));
GLOBAL_SCOPE = 1;
LOCAL_SCOPE = 2;
TYPE_FORM_INT = 1;
TYPE_FORM_CHAR = 2;
TYPE_FORM_VOID = 3;
TYPE_FORM_BOOL = 4;
TYPE_FORM_ARRAY = 5;
TYPE_FORM_RECORD = 6;
TYPE_FORM_UNKNOWN = 7;
OBJECT_CLASS_VAR = 1;
OBJECT_CLASS_TYPE = 2;
OBJECT_CLASS_FIELD = 3;
OBJECT_CLASS_PROC = 4;
nrOfGVar = 0;
globOffset = 0;
locOffset = 0;
paramOffset = 0;
}
/******************************************************************************************/
/************************************** parser ********************************************/
/******************************************************************************************/
/*************************************************************
* START PARSER
************************************************************/
bool startParsing(char *sfile, char *ofile){
int i;
srcfile = sfile;
outfile = ofile;
nrOfGVar = 0;
nrOfStrs = 0;
errorCounter = 0;
PC = 1;
initTMCmd();
cg_initRegs();
initItemModes();
initOperators();
initSymbolTable();
initOutputFile();
printf("\nstart parsing "); printString(srcfile); printf("...\n");
if ( hasMoreTokens() ) {