· 8 years ago · Jul 26, 2018, 01:00 PM
1
2%{
3
4#include <stdio.h>
5#include <stdbool.h>
6#include <stdlib.h> /* malloc */
7#include <string.h> /* strlen */
8#include <malloc.h>
9#include "katebisondefs.h"
10
11
12
13extern int yynewlines;
14extern char *yytext;
15
16
17 int yylex(void); /* function prototype */
18
19void yyerror(char * s)
20{
21 if ( *yytext == '\0' )
22 fprintf(stderr, "line %d: %s near end of file\n", yynewlines,s);
23 else{
24 fprintf(stderr, "line %d: %s near %s\n", yynewlines, s, yytext);
25 }
26 exit(0); //quits program
27}
28
29/* my code below P2 symbol tables for types, vars, and procs/function calls */
30
31
32
33/* ------------ GLOBAL VARIABLES ------------ */
34
35
36
37scopestruct *scopetop; //points to the top of the scope stack
38int scopelevel; // default 0 is global.
39 //increments with the scope push and decrements with scope pop functs
40
41int tmictr; // tm instruction counter - this is the first location in tm memory
42int tmdctr; // tm data counter - this is the first location in tm data
43int stringlength; //to record string lengths locally
44
45
46/* ---------------END DEFINITIONS OF STRUCTS AND GLOBALS------------- */
47
48
49
50/* --------------PROCEDURES/FUNCTIONS BELOW------------ */
51
52/* --------------TYPE PROCEDURES FUNCTIONS BELOW--------------- */
53
54
55bool equivtype(typesymrec *A, typesymrec *B){
56
57 if (A == 0 || B == 0) { yyerror("ERROR: equivtype was given null parameters"); }
58
59 //strcmp result, 0 means they match
60 //if strcmp result is (1), i.e. they don't match, return 0
61 if (strcmp(A->basetype,B->basetype) !=0) {
62 printf ("dont match\n");
63 return 0;
64 }
65
66 // if either is null
67 if ( A->undertype == 0 || B->undertype == 0) {
68
69 // if either undertype is null (prior) and both are null
70 if ( A->undertype == 0 && B->undertype == 0) {
71 return 1; //they match
72 }
73 // if either undertype is null and both are not null
74 } return 0; //return false
75 // do the same for the undertype
76 if (equivtype( A->undertype , B->undertype)) {return 1;}
77}
78
79/* ************** ADDTYPESYM FUNCTION *************** */
80/* Function: adds an entry to the type symbol table */
81/* STEPS */
82/* - Determine if the record already exists, handle scope etc */
83/* - Create a record assigned to an addptr */
84/* - Walk the symbol table with a walkptr and find the last record */
85/* - point the last entry in the symbol table to the addptr */
86/* - assigns the temporary pointer to LAST */
87
88/* INPUTS */
89/* char *sym_name = symbol table entry name */
90/* char * sym_basetype = base type, if an array, will have an underlying type */
91/* typesymrec *sym_undertype = underlying type, is a new record that can have another underlying type */
92/* int arraysize = this is the size of an array if this type is an array */
93/* ************************************************** */
94//returns 1 if successful, 0 if not - i.e. record is already added
95
96 typesymrec *addtypesym (char *sym_name, char *sym_basetype, typesymrec *sym_undertype, int sym_arraysize)
97{
98
99 // printf ("*FUNCTION ADDTYPESYM*\n");
100
101 /* DETERMINE IF THE NODE IS ALREADY ADDED / SCOPING */
102 // If the name is already in this scope, then it is a name clash
103 // If the name is in a prior scope, it is an override. the nature of scope does this
104
105 /* FIND THE LAST EMPTY RECORD IN THE SYMBOL TABLE AND ADD THE TEMP RECORD */
106 typesymrec *findwalkptr; /* temporary pointer to walk the symbol table */
107 findwalkptr = scopetop -> typesymtableroot; /* assign pointer to the root of current scope */
108 bool nextisnotnull;
109 nextisnotnull = true;
110
111 /* stops when the NEXT pointer is null */
112 if (findwalkptr != 0) {
113 /* printf ("findWalkptr != 0\n"); */ /* DEBUG */
114 while (nextisnotnull == true){
115 //check to see if the new name is in this record
116 //if so return a 0 to indicate the function was not successful
117
118 //printf ("findwalkptr name is %s\n",findwalkptr -> name);
119 //printf ("sym_name is %s\n",sym_name);
120
121 //strcmp compares strings and if they match it's 0
122 if (strcmp (sym_name, findwalkptr -> name) == 0){
123// printf ("it matched something\n");
124// printf ("Unsuccessful\n");
125 //will quit program so doesn't need to return really
126 yyerror("Error - Can only declare type once");
127 return 0;
128
129
130 }
131
132 //check if next is null, if so change bool to quit searching
133 if (findwalkptr -> next == 0){
134 nextisnotnull = false;
135 }
136 else findwalkptr = findwalkptr ->next;
137 } //end while
138 } //end if findwalkptr != 0
139
140 /* ----------END DETERMINE IF NODE IS ALREADY ADDED----- */
141
142 typesymrec *addptr; /* temporary pointer used to create temporary record*/
143
144 /* CREATE A NEW NODE */
145
146 /* debugging */
147
148
149
150 addptr = (typesymrec *) malloc (sizeof (typesymrec)); /* alloc space for temp addptr symrecord */
151
152 addptr->name = (char *) malloc (strlen (sym_name) + 1); /* allocates space */
153 strcpy (addptr->name,sym_name); /* puts value in there */
154
155 // printf("type symbol record name:\t%s\n", addptr->name); // DEBUG
156
157 addptr->basetype = (char *) malloc (strlen (sym_basetype) + 1); /* allocates space */
158 strcpy (addptr->basetype,sym_basetype); /* puts value in there */
159
160 /* printf("type symbol record basetype:\t%s\n", addptr->basetype); */ /* DEBUG */
161
162
163 addptr->undertype = (typesymrec *) malloc (sizeof (typesymrec)); /* allocates space */
164 addptr->undertype = sym_undertype; /* assigns the undertype */
165 addptr->arraysize = sym_arraysize; // assigns arraysize
166
167 /* printf("type symbol record array size:\t%d\n\n", addptr->arraysize); */ /* DEBUG */
168
169 /* set the NEXT pointer to null */
170 addptr->next = (typesymrec *)0;
171
172
173
174 /* FIND THE LAST EMPTY RECORD IN THE SYMBOL TABLE AND ADD THE TEMP RECORD */
175
176 typesymrec *walkptr; /* temporary pointer to walk the symbol table */
177 walkptr = scopetop -> typesymtableroot; /* assign pointer to the root */
178
179 /* stops when the NEXT pointer is null */
180 if (walkptr != 0) {
181 //printf ("Walkptr != 0\n"); // DEBUG
182 while (walkptr->next !=0){
183 walkptr = walkptr ->next;
184 }
185 /* at this point, the root is not null, and it is pointing to the last record */
186 /* add the record to the end of the TYPE symbol table root */
187
188 /* printf ("walkptr-> next = addptr \n"); */ /* DEBUG */
189 walkptr-> next = addptr;
190 }
191 /* else if the root is null then point the root to the new record*/
192 else {
193 scopetop -> typesymtableroot = addptr;
194 /* printf ("scopetop -> typesymtableroot = addptr \n"); */ /* DEBUG */
195 }
196
197
198
199 return addptr; //it was successful
200
201}
202// -----------------------SEARCH THE TYPE SYMBOL TABLE AND IF MATCHES A NAME, RETURN THE type -------
203// RETURN NULL IF DOESN"T EXIST
204
205
206/* *************** ---return pointer to type node--- ************** */
207typesymrec *symreturntype(char *nameinput){
208 // bool foundmatch = 0;
209
210 typesymrec *typeprintptr; /* temporary pointer */
211 typeprintptr = (typesymrec *) malloc (sizeof (typesymrec)); /* allocate pointer space */
212 // printptr = scopetop -> typesymtableroot; //this is done later
213
214 typesymrec *typeunderprintptr; /* underlying type pointer to print underlyings */
215 typeunderprintptr = (typesymrec *) malloc (sizeof (typesymrec)); /* allocate pointer space */
216 bool typenextisnull;
217
218
219
220 scopestruct *scopeprintptr; //temp pointer to walk the scope and print
221 scopeprintptr = scopetop; //sets print ptr to top of scope stack
222 bool scopenextisnull;
223 scopenextisnull = 0;
224
225 while (scopenextisnull == false){//while the next entry of the scope table is not null
226
227 //reset the typenextisnull and reset the type symbol table root to the next scope
228 typenextisnull = 0;
229 typeprintptr = scopeprintptr -> typesymtableroot;
230// printf ("scopeptinrptr -> typesymtableroot -> name is %s\n",scopeprintptr -> typesymtableroot -> name);
231
232 // printf ("\n*********Printing Scope Level %d**********\n",scopeprintptr->level);
233
234 /* --------PRINT TYPE SYMBOL TABLE ----------- */
235 /* Walk the TYPE symbol table list */
236 /* stops when the NEXT pointer is null */
237
238
239 // printf ("-------------------------------- \n");
240 // printf ("*Printing the TYPE symbol table* \n");
241 // printf ("-------------------------------- \n");
242
243
244
245 if (typeprintptr != 0) {
246
247
248
249 while (typenextisnull == false){ //while the next entry of the type symbol table is not null
250
251 /* --------------- PRINT SYMBOL TABLE ENTRY -------------- */
252 // printf("type symbol record name:\t%s\n", typeprintptr->name);
253
254 //strcmp compares strings and if they match it's 0
255 if (strcmp (nameinput, typeprintptr -> name) == 0){
256 return (typeprintptr);
257 }
258
259
260
261 // printf("type symbol record basetype:\t%s\n", typeprintptr->basetype);
262 /* if there is no under type THEN PRINT "NULL", ELSE PRINT UNDERTYPE */
263
264
265 /* -----------END OF PRINTING SYMBOL TABLE ENTRY------------- */
266
267 /* go to the next type record if it's not null */
268 if (typeprintptr ->next != 0) {
269 typeprintptr = typeprintptr ->next;
270 } else typenextisnull = true;
271
272 } //end of WHILE the next entry of the type symbol table is not null
273 }//end if printptr != null
274 // else printf ("Symbol table root is set to null\n");
275
276 if (scopeprintptr -> next != NULL){
277 scopeprintptr = scopeprintptr -> next;
278 //printf ("scopeprintptr = scopeprintptr -> next\n");
279 //printf ("scopeptinrptr -> typesymtableroot -> name is %s\n",scopeprintptr -> typesymtableroot -> name);
280 } else scopenextisnull = true;
281
282 }//end of WHILE Scopenextisnull is not true
283
284 // IF NOTHING MATCHED RETURN NULL
285 return ( (typesymrec *)0 );
286
287}
288
289
290
291
292// ----------------END RETURN NODE FROM TYPE SYMBOL TABLE---------------------
293
294
295
296/* *************** ---print the TYPE symbol table--- ************** */
297void printsymtable(){
298
299
300 typesymrec *typeprintptr; /* temporary pointer */
301 typeprintptr = (typesymrec *) malloc (sizeof (typesymrec)); /* allocate pointer space */
302 // printptr = scopetop -> typesymtableroot; //this is done later
303
304 typesymrec *typeunderprintptr; /* underlying type pointer to print underlyings */
305 typeunderprintptr = (typesymrec *) malloc (sizeof (typesymrec)); /* allocate pointer space */
306 bool typenextisnull;
307
308
309
310 scopestruct *scopeprintptr; //temp pointer to walk the scope and print
311 scopeprintptr = scopetop; //sets print ptr to top of scope stack
312 bool scopenextisnull;
313 scopenextisnull = 0;
314
315 while (scopenextisnull == false){//while the next entry of the scope table is not null
316
317 //reset the typenextisnull and reset the type symbol table root to the next scope
318 typenextisnull = 0;
319 typeprintptr = scopeprintptr -> typesymtableroot;
320// printf ("scopeptinrptr -> typesymtableroot -> name is %s\n",scopeprintptr -> typesymtableroot -> name);
321
322 printf ("\n*********Printing Scope Level %d**********\n",scopeprintptr->level);
323
324 /* --------PRINT TYPE SYMBOL TABLE ----------- */
325 /* Walk the TYPE symbol table list */
326 /* stops when the NEXT pointer is null */
327
328
329 printf ("-------------------------------- \n");
330 printf ("*Printing the TYPE symbol table* \n");
331 printf ("-------------------------------- \n");
332
333
334
335 if (typeprintptr != 0) {
336
337
338
339 while (typenextisnull == false){ //while the next entry of the type symbol table is not null
340
341 /* --------------- PRINT SYMBOL TABLE ENTRY -------------- */
342 printf("type symbol record name:\t%s\n", typeprintptr->name);
343 printf("type symbol record basetype:\t%s\n", typeprintptr->basetype);
344 /* if there is no under type THEN PRINT "NULL", ELSE PRINT UNDERTYPE */
345
346 if (typeprintptr-> arraysize != 0){
347 printf("type symbol record arraysize:\t%d\n", typeprintptr->arraysize);
348 }
349
350 if ( typeprintptr->undertype == 0 ){
351 printf("type symbol record undertype:\t%s\n", "NULL");
352 }
353
354
355
356 /* ------ PRINT UNDERTYPES ------ */
357 /* ELSE if there is an undertype, it's an array and print all undertypes */
358 else {
359 typeunderprintptr = typeprintptr->undertype; // set the printptr to the undertype node
360 bool underisnull;
361 underisnull = false;
362
363
364 while (underisnull == false){
365 printf ("\t...........................\n");
366 printf("\tundertype basetype is \t%s\n", typeunderprintptr->basetype);
367
368 if (typeunderprintptr->arraysize !=0){
369 printf ("\tundertype arraysize is \t%d\n",typeunderprintptr->arraysize);
370 }
371
372 if (typeunderprintptr -> undertype == 0) underisnull = true;
373 typeunderprintptr = typeunderprintptr -> undertype; // advance to the next undertype
374 }//end while to print each undertype
375 } /* END ELSE if there is an undertype */
376 /* ------ END PRINT UNDERTYPE ----- */
377
378 printf ("-------------------------------- \n");
379 /* -----------END OF PRINTING SYMBOL TABLE ENTRY------------- */
380
381 /* go to the next type record if it's not null */
382 if (typeprintptr ->next != 0) {
383 typeprintptr = typeprintptr ->next;
384 } else typenextisnull = true;
385
386 } //end of WHILE the next entry of the type symbol table is not null
387 }//end if printptr != null
388 else printf ("Symbol table root is set to null\n");
389
390 if (scopeprintptr -> next != NULL){
391 scopeprintptr = scopeprintptr -> next;
392 //printf ("scopeprintptr = scopeprintptr -> next\n");
393 //printf ("scopeptinrptr -> typesymtableroot -> name is %s\n",scopeprintptr -> typesymtableroot -> name);
394 } else scopenextisnull = true;
395
396 }//end of WHILE Scopenextisnull is not true
397}
398 /* *************** END OF PRINTING THE TYPE SYMBOL TABLE ************** */
399
400/* return a pointer to an array underlying type */
401/* THIS IS AN ARRAY */
402// it's optional whether you want to give it another underptr, called rasunderunderptr
403typesymrec *typearraystruct(char* rasbasetype, typesymrec *rasunderunderptr, int rasarraysize){
404
405 // 3rd from bottom underlying type
406 typesymrec *underptrreturn; // pointer to array undertype to return
407 underptrreturn = (typesymrec *) malloc (sizeof (typesymrec)); /* allocate pointer space */
408 underptrreturn -> basetype = rasbasetype;
409 underptrreturn -> undertype = rasunderunderptr;
410 underptrreturn -> arraysize = rasarraysize;
411 return underptrreturn;
412}
413
414
415/* --------------------------END TYPE SYMBOL TABLE PROCEDURES--------------- */
416/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
417
418/* --------------VAR SYMBOL TABLE PROCEDURES------------ */
419
420varsymrec *vararraystruct(char* vasbasetype, varsymrec *vasunderunderptr, int vasarraysize){
421
422 // 3rd from bottom underlying type
423 varsymrec *underptrreturn; // pointer to array undertype to return
424 underptrreturn = (varsymrec *) malloc (sizeof (varsymrec)); /* allocate pointer space */
425 underptrreturn -> basetype = vasbasetype;
426 underptrreturn -> undertype = vasunderunderptr;
427 underptrreturn -> arraysize = vasarraysize;
428 return underptrreturn;
429}
430
431//returns 1 if successful, 0 if not - i.e. record is already added
432
433varsymrec *addvarsym (char *avsname, char *avsbasetype, varsymrec *avsundertype, int avsarraysize, int avsintvalue, bool avsboolvalue, char *avsstringvalue)
434{
435
436 // printf ("*FUNCTION ADDVARSYM*\n");
437
438 // ----------------DETERMINE IF THE TYPE EXISTS ---------------
439 // IF NOT THEN ERROR
440
441 char *typechecker;
442 typechecker = avsbasetype;
443 // POINT THE TYPE TO THE UNDERLYING TYPE
444
445 //if there's an undertype you need to lookup the final undertype and check that instead
446 if (avsundertype != 0){
447
448 varsymrec *tmpunder;
449 for (tmpunder = avsundertype; tmpunder; tmpunder = tmpunder ->undertype){
450 typechecker = tmpunder -> basetype;
451 }
452 }
453
454
455
456 if ( symreturntype(typechecker) == 0 ) {
457
458 yyerror("Error - invalid type");
459
460 }
461
462 // --------------- END DETERMINE IF TYPE EXISTS ---------------
463
464
465 /* DETERMINE IF THE NODE IS ALREADY ADDED / SCOPING */
466 // If the name is already in this scope, then it is a name clash
467 // If the name is in a prior scope, it is an override. the nature of scope does this
468
469 /* FIND THE LAST EMPTY RECORD IN THE SYMBOL TABLE AND ADD THE TEMP RECORD */
470 varsymrec *varfindwalkptr; /* temporary pointer to walk the symbol table */
471 varfindwalkptr = scopetop -> varsymtableroot; /* assign pointer to the root of current scope */
472 bool nextisnotnull;
473 nextisnotnull = true;
474
475
476 /* stops when the NEXT pointer is null */
477 if (varfindwalkptr != 0) {
478
479
480
481 /* printf ("findWalkptr != 0\n"); */ /* DEBUG */
482
483 while (nextisnotnull == true){
484 //check to see if the new name is in this record
485 //if so return a 0 to indicate the function was not successful
486
487 //printf ("varfindwalkptr name is %s\n",varfindwalkptr -> name);
488 // printf ("avsname is %s\n",avsname);
489
490 //strcmp compares strings and if they match it's 0
491 if (strcmp (avsname, varfindwalkptr -> name) == 0){
492 //printf ("it matched something\n");
493 // printf ("Unsuccessful\n");
494 yyerror("Error - Variable name already used in this scope");
495 return 0;
496
497 }
498
499 //check if next is null, if so change bool to quit searching
500 if (varfindwalkptr -> next == 0){
501 nextisnotnull = false;
502 }
503 else varfindwalkptr = varfindwalkptr ->next;
504 } //end while
505 } //end if varfindwalkptr != 0
506
507 /* ----------END DETERMINE IF NODE IS ALREADY ADDED----- */
508
509 varsymrec *addptr; /* temporary pointer used to create temporary record*/
510
511 /* CREATE A NEW NODE */
512
513 // (char *avsname, char *avsbasetype, varsymrec *avsundertype, int avsarraysize, uvalue avsvalue
514
515 addptr = (varsymrec *) malloc (sizeof (varsymrec)); /* alloc space for temp addptr symrecord */
516
517
518 addptr->name = (char *) malloc (strlen (avsname) + 1); /* allocates space */
519 strcpy (addptr->name,avsname); /* puts value in there */
520
521
522
523 // printf("var symbol record name:\t%s\n", addptr->name); // DEBUG
524
525 addptr->basetype = (char *) malloc (strlen (avsbasetype) + 1); /* allocates space */
526 strcpy (addptr->basetype,avsbasetype); /* puts value in there */
527
528 /* printf("type symbol record basetype:\t%s\n", addptr->basetype); */ /* DEBUG */
529
530
531 addptr->undertype = (varsymrec *) malloc (sizeof (varsymrec)); /* allocates space */
532 addptr->undertype = avsundertype; /* assigns the undertype */
533
534 addptr->arraysize = avsarraysize; // assigns arraysize
535
536
537 addptr -> stringvalue = (char *) malloc (strlen (avsstringvalue));
538 addptr -> intvalue = avsintvalue;
539 addptr -> boolvalue = avsboolvalue;
540 addptr -> stringvalue = avsstringvalue;
541
542
543 /* printf("type symbol record array size:\t%d\n\n", addptr->arraysize); */ /* DEBUG */
544
545 /* set the NEXT pointer to null */
546 addptr->next = (varsymrec *)0;
547
548
549
550 /* FIND THE LAST EMPTY RECORD IN THE SYMBOL TABLE AND ADD THE TEMP RECORD */
551
552 varsymrec *varwalkptr; /* temporary pointer to walk the symbol table */
553 varwalkptr = scopetop -> varsymtableroot; /* assign pointer to the root */
554
555 /* stops when the NEXT pointer is null */
556 if (varwalkptr != 0) {
557 //printf ("Varwalkptr != 0\n"); // DEBUG
558 while (varwalkptr->next !=0){
559 varwalkptr = varwalkptr ->next;
560 }
561 /* at this point, the root is not null, and it is pointing to the last record */
562 /* add the record to the end of the TYPE symbol table root */
563
564 /* printf ("varwalkptr-> next = addptr \n"); */ /* DEBUG */
565 varwalkptr-> next = addptr;
566 }
567 /* else if the root is null then point the root to the new record*/
568 else {
569 scopetop -> varsymtableroot = addptr;
570 /* printf ("scopetop -> varsymtableroot = addptr \n"); */ /* DEBUG */
571 }
572
573 /* DEBUG */
574 /* if walkptr is null THEN PRINT "NULL", ELSE PRINT UNDERTYPE */
575 /* if ( walkptr == 0 ){ */
576 /* printf("walkptr basetype:\tWALKPTR IS NULL CUZ TABLE waz null"); */
577 /* } else printf("walkptr basetype:\t%s\t%p\n", walkptr->basetype,walkptr->basetype); */
578 /* printf("addptr basetype:\t%s\t%p\n", addptr->basetype,addptr->basetype); */
579 /* printf("scopetop -> varsymtableroot basetype:\t%s\t%p\n", scopetop -> varsymtableroot->basetype,scopetop -> varsymtableroot->basetype); */
580 /* printf ("should have added pointer\n"); ?8 */
581
582// printf ("Successful\n");
583 return addptr; //it was successful
584
585}
586
587
588// ------------------- RETURN THE VAR RECORD ----------------------
589// returns null if var not found
590// input is the name of the var
591
592varsymrec *symreturnvar(char *nameinput){
593
594
595 varsymrec *varprintptr; /* temporary pointer */
596 varprintptr = (varsymrec *) malloc (sizeof (varsymrec)); /* allocate pointer space */
597 varprintptr = scopetop -> varsymtableroot; //this is done later
598 //printf ("line 527\n");
599 varsymrec *varunderprintptr; /* underlying type pointer to print underlyings */
600 varunderprintptr = (varsymrec *) malloc (sizeof (varsymrec)); /* allocate pointer space */
601 bool varnextisnull;
602
603
604
605 scopestruct *scopeprintptr; //temp pointer to walk the scope and print
606 scopeprintptr = scopetop; //sets print ptr to top of scope stack
607 bool scopenextisnull;
608 scopenextisnull = 0;
609
610 while (scopenextisnull == false){//while the next entry of the scope table is not null
611
612 //reset the varnextisnull and reset the type symbol table root to the next scope
613 varnextisnull = 0;
614 varprintptr = scopeprintptr -> varsymtableroot;
615 //printf ("scopeptinrptr -> varsymtableroot -> name is %s\n",scopeprintptr -> varsymtableroot -> name);
616
617 // printf ("\n*********Printing Scope Level %d**********\n",scopeprintptr->level);
618
619 /* --------PRINT TYPE SYMBOL TABLE ----------- */
620 /* Walk the TYPE symbol table list */
621 /* stops when the NEXT pointer is null */
622
623
624 // printf ("-------------------------------- \n");
625 // printf ("*Printing the VAR symbol table* \n");
626 // printf ("-------------------------------- \n");
627
628
629
630 if (varprintptr != 0) {
631
632
633
634 while (varnextisnull == false){ //while the next entry of the type symbol table is not null
635
636 /* --------------- PRINT SYMBOL TABLE ENTRY -------------- */
637 // printf("var symbol record name:\t%s\n", varprintptr->name);
638 // printf("var symbol record basetype:\t%s\n", varprintptr->basetype);
639 /* if there is no under type THEN PRINT "NULL", ELSE PRINT UNDERTYPE */
640
641 // 0 if a match
642 if (strcmp(nameinput,varprintptr->name) == 0) {
643 return (varprintptr);
644 }
645
646
647 // printf ("var symbol record int value:\t%d\n", varprintptr->intvalue);
648 // printf ("var symbol record bool value:\t%d\n", varprintptr->boolvalue);
649 // printf ("var symbol record string value:\t%s\n", varprintptr->stringvalue);
650
651
652
653 if (varprintptr-> arraysize != 0){
654 // printf("var symbol record arraysize:\t%d\n", varprintptr->arraysize);
655 }
656
657
658 /* ------ PRINT UNDERTYPES ------ */
659
660 //if ( varprintptr-> undertype == 0 ){
661 // printf("var symbol record undertype :\t%s\n", "NULL");
662 //}
663
664
665 // /* ELSE if there is an , it's an array and print all s */
666 // else {
667 // varunderprintptr = varprintptr-> undertype; // set the printptr to the node
668 // bool underisnull;
669 // underisnull = false;
670
671
672 // while (underisnull == false){
673 // printf ("\t...........................\n");
674 // printf("\t basetype is \t%s\n", varunderprintptr->basetype);
675
676 // if (varunderprintptr->arraysize !=0){
677 // printf ("\t arraysize is \t%d\n",varunderprintptr->arraysize);
678 // }
679
680 // if (varunderprintptr -> undertype == 0) underisnull = true;
681 // varunderprintptr = varunderprintptr -> undertype; // advance to the next undertype
682 // }//end while to print each
683 // } /* END ELSE if there is an */
684 /* ------ END PRINT UNDERTYPE ----- */
685
686 // printf ("-------------------------------- \n");
687 /* -----------END OF PRINTING SYMBOL TABLE ENTRY------------- */
688
689 /* go to the next type record if it's not null */
690 if (varprintptr ->next != 0) {
691 varprintptr = varprintptr ->next;
692 } else varnextisnull = true;
693
694 } //end of WHILE the next entry of the type symbol table is not null
695 }//end if printptr != null
696 else printf ("Symbol table root is set to null\n");
697
698 if (scopeprintptr -> next != NULL){
699 scopeprintptr = scopeprintptr -> next;
700 //printf ("scopeprintptr = scopeprintptr -> next\n");
701 //printf ("scopeptinrptr -> varsymtableroot -> name is %s\n",scopeprintptr -> varsymtableroot -> name);
702 } else scopenextisnull = true;
703
704 }//end of WHILE Scopenextisnull is not true
705
706
707 // ELSE RETURN NULL
708 return ((varsymrec *)0);
709
710}
711
712
713
714// -------------------- END RETURN VAR RECORD ---------------------
715
716
717
718
719/* *************** ---print the VAR symbol table--- ************** */
720void printvarsymtable(){
721
722
723 varsymrec *varprintptr; /* temporary pointer */
724 varprintptr = (varsymrec *) malloc (sizeof (varsymrec)); /* allocate pointer space */
725 varprintptr = scopetop -> varsymtableroot; //this is done later
726 //printf ("line 527\n");
727 varsymrec *varunderprintptr; /* underlying type pointer to print underlyings */
728 varunderprintptr = (varsymrec *) malloc (sizeof (varsymrec)); /* allocate pointer space */
729 bool varnextisnull;
730
731
732
733 scopestruct *scopeprintptr; //temp pointer to walk the scope and print
734 scopeprintptr = scopetop; //sets print ptr to top of scope stack
735 bool scopenextisnull;
736 scopenextisnull = 0;
737
738 while (scopenextisnull == false){//while the next entry of the scope table is not null
739
740 //reset the varnextisnull and reset the type symbol table root to the next scope
741 varnextisnull = 0;
742 varprintptr = scopeprintptr -> varsymtableroot;
743 //printf ("scopeptinrptr -> varsymtableroot -> name is %s\n",scopeprintptr -> varsymtableroot -> name);
744
745 printf ("\n*********Printing Scope Level %d**********\n",scopeprintptr->level);
746
747 /* --------PRINT TYPE SYMBOL TABLE ----------- */
748 /* Walk the TYPE symbol table list */
749 /* stops when the NEXT pointer is null */
750
751
752 printf ("-------------------------------- \n");
753 printf ("*Printing the VAR symbol table* \n");
754 printf ("-------------------------------- \n");
755
756
757
758 if (varprintptr != 0) {
759
760
761
762 while (varnextisnull == false){ //while the next entry of the type symbol table is not null
763
764 /* --------------- PRINT SYMBOL TABLE ENTRY -------------- */
765 printf("var symbol record name:\t%s\n", varprintptr->name);
766 printf("var symbol record basetype:\t%s\n", varprintptr->basetype);
767 /* if there is no under type THEN PRINT "NULL", ELSE PRINT UNDERTYPE */
768
769
770 printf ("var symbol record int value:\t%d\n", varprintptr->intvalue);
771 printf ("var symbol record bool value:\t%d\n", varprintptr->boolvalue);
772 printf ("var symbol record string value:\t%s\n", varprintptr->stringvalue);
773
774
775
776 if (varprintptr-> arraysize != 0){
777 printf("var symbol record arraysize:\t%d\n", varprintptr->arraysize);
778 }
779
780
781 /* ------ PRINT UNDERTYPES ------ */
782
783 if ( varprintptr-> undertype == 0 ){
784 printf("var symbol record undertype :\t%s\n", "NULL");
785 }
786
787
788 /* ELSE if there is an , it's an array and print all s */
789 else {
790 varunderprintptr = varprintptr-> undertype; // set the printptr to the node
791 bool underisnull;
792 underisnull = false;
793
794
795 while (underisnull == false){
796 printf ("\t...........................\n");
797 printf("\t basetype is \t%s\n", varunderprintptr->basetype);
798
799 if (varunderprintptr->arraysize !=0){
800 printf ("\t arraysize is \t%d\n",varunderprintptr->arraysize);
801 }
802
803 if (varunderprintptr -> undertype == 0) underisnull = true;
804 varunderprintptr = varunderprintptr -> undertype; // advance to the next undertype
805 }//end while to print each
806 } /* END ELSE if there is an */
807 /* ------ END PRINT UNDERTYPE ----- */
808
809 printf ("-------------------------------- \n");
810 /* -----------END OF PRINTING SYMBOL TABLE ENTRY------------- */
811
812 /* go to the next type record if it's not null */
813 if (varprintptr ->next != 0) {
814 varprintptr = varprintptr ->next;
815 } else varnextisnull = true;
816
817 } //end of WHILE the next entry of the type symbol table is not null
818 }//end if printptr != null
819 else printf ("Symbol table root is set to null\n");
820
821 if (scopeprintptr -> next != NULL){
822 scopeprintptr = scopeprintptr -> next;
823 //printf ("scopeprintptr = scopeprintptr -> next\n");
824 //printf ("scopeptinrptr -> varsymtableroot -> name is %s\n",scopeprintptr -> varsymtableroot -> name);
825 } else scopenextisnull = true;
826
827 }//end of WHILE Scopenextisnull is not true
828}
829
830bool equivvar(varsymrec *A, varsymrec *B){
831 //strcmp result, 0 means they match
832 //if strcmp result is (1), i.e. they don't match, return 0
833 if (strcmp(A->basetype,B->basetype) !=0) {return 0;}
834
835 // if either is null
836 if ( A->undertype == 0 || B->undertype == 0) {
837 if ( A->undertype == 0 && B->undertype == 0) {
838 return 1; //return true
839 }
840 } return 0; //return false
841 if (equivvar( A->undertype , B->undertype)) {return 1;}
842}
843
844
845
846 // //strcmp compares strings and if they match it's 0
847// if (strcmp (apsname, procfindwalkptr -> name) == 0){
848 // //printf ("it matched something\n");
849// printf ("Unsuccessful\n");
850// return 0;
851
852// }
853
854
855
856/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
857/* -------------END VAR SYMBOL TABLE PROCEDURES--------- */
858
859
860/* --------------PROC SYMBOL TABLE PROCEDURES BELOW------------ */
861
862
863// procargstruct *returnprocarglist( varsymrec * rpalnewarg, procargstruct *rpalUnderlist){
864
865
866
867// procsymrec *underptrreturn; // pointer to array undertype to return
868// underptrreturn = (procsymrec *) malloc (sizeof (procsymrec)); /* allocate pointer space */
869// underptrreturn -> basetype = vasbasetype;
870// underptrreturn -> undertype = vasunderunderptr;
871// underptrreturn -> arraysize = vasarraysize;
872// return underptrreturn;
873// }
874
875//returns 1 if successful, 0 if not - i.e. record is already added
876
877// bool addprocsym (char *apsname, char *apsbasetype, procsymrec *apsreturnvar, int DELETEWASAVSARRAYSIZE, int DELETEWASAVSINTVALUE, bool avsboolvalue, char *avsstringvalue)
878// {
879
880// printf ("*FUNCTION ADDPROCSYM*\n");
881
882 /* DETERMINE IF THE NODE IS ALREADY ADDED / SCOPING */
883 // If the name is already in this scope, then it is a name clash
884 // If the name is in a prior scope, it is an override. the nature of scope does this
885
886 /* FIND THE LAST EMPTY RECORD IN THE SYMBOL TABLE AND ADD THE TEMP RECORD */
887// procsymrec *procfindwalkptr; /* temporary pointer to walk the symbol table */
888// procfindwalkptr = scopetop -> procsymtableroot; /* assign pointer to the root of current scope */
889// bool nextisnotnull;
890// nextisnotnull = true;
891
892
893 /* stops when the NEXT pointer is null */
894// if (procfindwalkptr != 0) {
895
896
897
898 /* printf ("findWalkptr != 0\n"); */ /* DEBUG */
899
900// while (nextisnotnull == true){
901 //check to see if the new name is in this record
902 //if so return a 0 to indicate the function was not successful
903
904 //printf ("procfindwalkptr name is %s\n",procfindwalkptr -> name);
905 // printf ("apsname is %s\n",apsname);
906
907 //strcmp compares strings and if they match it's 0
908// if (strcmp (apsname, procfindwalkptr -> name) == 0){
909 // //printf ("it matched something\n");
910// printf ("Unsuccessful\n");
911// return 0;
912
913// }
914
915// //check if next is null, if so change bool to quit searching
916// if (procfindwalkptr -> next == 0){
917// nextisnotnull = false;
918 // }
919// else procfindwalkptr = procfindwalkptr ->next;
920// } //end while
921// } //end if procfindwalkptr != 0
922
923 /* ----------END DETERMINE IF NODE IS ALREADY ADDED----- */
924
925// procsymrec *addptr; /* temporary pointer used to create temporary record*/
926
927 /* CREATE A NEW NODE */
928
929 // (char *apsname, char *apsbasetype, procsymrec *apsreturnvar, int DELETEWASAVSARRAYSIZE, uvalue avsvalue
930
931// addptr = (procsymrec *) malloc (sizeof (procsymrec)); /* alloc space for temp addptr symrecord */
932
933
934// addptr->name = (char *) malloc (strlen (apsname) + 1); /* allocates space */
935// strcpy (addptr->name,apsname); /* puts value in there */
936
937
938
939 // printf("proc symbol record name:\t%s\n", addptr->name); // DEBUG
940
941// addptr->basetype = (char *) malloc (strlen (apsbasetype) + 1); /* allocates space */
942// strcpy (addptr->basetype,apsbasetype); /* puts value in there */
943
944 /* printf("type symbol record basetype:\t%s\n", addptr->basetype); */ /* DEBUG */
945
946
947// addptr->undertype = (procsymrec *) malloc (sizeof (procsymrec)); /* allocates space */
948// addptr->undertype = apsreturnvar; /* assigns the undertype */
949
950// addptr->arraysize = DELETEWASAVSARRAYSIZE; // assigns arraysize
951
952
953// addptr -> stringvalue = (char *) malloc (strlen (avsstringvalue));
954// addptr -> intvalue = DELETEWASAVSINTVALUE;
955// addptr -> boolvalue = avsboolvalue;
956// addptr -> stringvalue = avsstringvalue;
957
958
959 /* printf("type symbol record array size:\t%d\n\n", addptr->arraysize); */ /* DEBUG */
960
961 /* set the NEXT pointer to null */
962// addptr->next = (procsymrec *)0;
963
964
965
966 /* FIND THE LAST EMPTY RECORD IN THE SYMBOL TABLE AND ADD THE TEMP RECORD */
967
968// procsymrec *procwalkptr; /* temporary pointer to walk the symbol table */
969 // procwalkptr = scopetop -> procsymtableroot; /* assign pointer to the root */
970
971 /* stops when the NEXT pointer is null */
972// if (procwalkptr != 0) {
973// //printf ("procwalkptr != 0\n"); // DEBUG
974// while (procwalkptr->next !=0){
975// procwalkptr = procwalkptr ->next;
976// }
977 /* at this point, the root is not null, and it is pointing to the last record */
978 /* add the record to the end of the TYPE symbol table root */
979
980 /* printf ("procwalkptr-> next = addptr \n"); */ /* DEBUG */
981// procwalkptr-> next = addptr;
982// }
983 /* else if the root is null then point the root to the new record*/
984// else {
985 // scopetop -> procsymtableroot = addptr;
986// /* printf ("scopetop -> procsymtableroot = addptr \n"); */ /* DEBUG */
987// }
988
989 /* DEBUG */
990 /* if walkptr is null THEN PRINT "NULL", ELSE PRINT UNDERTYPE */
991 /* if ( walkptr == 0 ){ */
992 /* printf("walkptr basetype:\tWALKPTR IS NULL CUZ TABLE waz null"); */
993 /* } else printf("walkptr basetype:\t%s\t%p\n", walkptr->basetype,walkptr->basetype); */
994 /* printf("addptr basetype:\t%s\t%p\n", addptr->basetype,addptr->basetype); */
995 /* printf("scopetop -> procsymtableroot basetype:\t%s\t%p\n", scopetop -> procsymtableroot->basetype,scopetop -> procsymtableroot->basetype); */
996 /* printf ("should have added pointer\n"); ?8 */
997
998// printf ("Successful\n");
999// return 1; //it was successful
1000
1001//}
1002
1003
1004/* *************** ---print the PROC symbol table--- ************** */
1005//void printprocsymtable(){
1006
1007
1008// procsymrec *procprintptr; /* temporary pointer */
1009// procprintptr = (procsymrec *) malloc (sizeof (procsymrec)); /* allocate pointer space */
1010// procprintptr = scopetop -> procsymtableroot; //this is done later
1011// //printf ("line 527\n");
1012// procsymrec *procunderprintptr; /* underlying type pointer to print underlyings */
1013// procunderprintptr = (procsymrec *) malloc (sizeof (procsymrec)); /* allocate pointer space */
1014// bool procnextisnull;
1015
1016
1017
1018// scopestruct *scopeprintptr; //temp pointer to walk the scope and print
1019// scopeprintptr = scopetop; //sets print ptr to top of scope stack
1020// bool scopenextisnull;
1021// scopenextisnull = 0;
1022
1023// while (scopenextisnull == false){//while the next entry of the scope table is not null
1024
1025 //reset the procnextisnull and reset the type symbol table root to the next scope
1026// procnextisnull = 0;
1027// procprintptr = scopeprintptr -> procsymtableroot;
1028 //printf ("scopeptinrptr -> procsymtableroot -> name is %s\n",scopeprintptr -> procsymtableroot -> name);
1029
1030// printf ("\n*********Printing Scope Level %d**********\n",scopeprintptr->level);
1031
1032 /* --------PRINT TYPE SYMBOL TABLE ----------- */
1033 /* Walk the TYPE symbol table list */
1034 /* stops when the NEXT pointer is null */
1035
1036
1037// printf ("-------------------------------- \n");
1038// printf ("*Printing the proc symbol table* \n");
1039// printf ("-------------------------------- \n");
1040
1041
1042
1043// if (procprintptr != 0) {
1044
1045
1046
1047// while (procnextisnull == false){ //while the next entry of the type symbol table is not null
1048
1049// /* --------------- PRINT SYMBOL TABLE ENTRY -------------- */
1050// printf("proc symbol record name:\t%s\n", procprintptr->name);
1051// printf("proc symbol record basetype:\t%s\n", procprintptr->basetype);
1052// /* if there is no under type THEN PRINT "NULL", ELSE PRINT UNDERTYPE */
1053
1054
1055// printf ("proc symbol record int value:\t%d\n", procprintptr->intvalue);
1056// printf ("proc symbol record bool value:\t%d\n", procprintptr->boolvalue);
1057// printf ("proc symbol record string value:\t%s\n", procprintptr->stringvalue);
1058
1059
1060
1061// if (procprintptr-> arraysize != 0){
1062// printf("proc symbol record arraysize:\t%d\n", procprintptr->arraysize);
1063// }
1064
1065
1066// /* ------ PRINT UNDERTYPES ------ */
1067
1068// if ( procprintptr-> undertype == 0 ){
1069// printf("proc symbol record undertype :\t%s\n", "NULL");
1070// }
1071
1072
1073// /* ELSE if there is an , it's an array and print all s */
1074// else {
1075// procunderprintptr = procprintptr-> undertype; // set the printptr to the node
1076// bool underisnull;
1077// underisnull = false;
1078
1079
1080// while (underisnull == false){
1081// printf ("\t...........................\n");
1082// printf("\t basetype is \t%s\n", procunderprintptr->basetype);
1083
1084// if (procunderprintptr->arraysize !=0){
1085// printf ("\t arraysize is \t%d\n",procunderprintptr->arraysize);
1086// }
1087
1088// if (procunderprintptr -> undertype == 0) underisnull = true;
1089// procunderprintptr = procunderprintptr -> undertype; // advance to the next undertype
1090// }//end while to print each
1091// } /* END ELSE if there is an */
1092// /* ------ END PRINT UNDERTYPE ----- */
1093//
1094// printf ("-------------------------------- \n");
1095// /* -----------END OF PRINTING SYMBOL TABLE ENTRY------------- */
1096//
1097// /* go to the next type record if it's not null */
1098// if (procprintptr ->next != 0) {
1099// procprintptr = procprintptr ->next;
1100// } else procnextisnull = true;
1101//
1102// } //end of WHILE the next entry of the type symbol table is not null
1103// }//end if printptr != null
1104// else printf ("Symbol table root is set to null\n");
1105//
1106// if (scopeprintptr -> next != NULL){
1107// scopeprintptr = scopeprintptr -> next;
1108// //printf ("scopeprintptr = scopeprintptr -> next\n");
1109// //printf ("scopeptinrptr -> procsymtableroot -> name is %s\n",scopeprintptr -> procsymtableroot -> name);
1110// } else scopenextisnull = true;
1111//
1112// }//end of WHILE Scopenextisnull is not true
1113//}
1114
1115/* ---------------PROC SYMBOL TABLE PROCEDURES ABOVE ------------------ */
1116
1117
1118/* --------------SCOPE PROCEDURES BELOW--------------- */
1119void pushscope(){
1120 // printf ("*FUNCTION PUSHSCOPE*\n");
1121
1122 if (scopelevel == -1) // if this is our initalizing of the scope (in main scopelevel is initially -1)
1123 {
1124// printf ("Pushing global scope \n"); //DEBUG
1125 scopelevel = 0;
1126 } else scopelevel = scopelevel + 1; //else just increase the scope level
1127 // printf ("scopelevel is now %d\n",scopelevel);
1128
1129
1130 scopestruct *tempscopestruct; //temporary scope level to hold the new scope
1131 tempscopestruct = (scopestruct *) malloc (sizeof (scopestruct)); // alloc space for the scope
1132 tempscopestruct -> level = scopelevel;
1133 tempscopestruct -> typesymtableroot = (typesymrec *)0;
1134 tempscopestruct -> varsymtableroot = (varsymrec *)0;
1135 tempscopestruct -> procsymtableroot = (procsymrec *)0;
1136
1137 tempscopestruct -> next = scopetop; //the "next" scope is the prior top of scope stack
1138 scopetop = tempscopestruct; //set the newly created scope to the top of the scope stack
1139
1140}
1141
1142void popscope(){
1143// printf ("*FUNCTION POPSCOPE*\n");
1144
1145 if (scopelevel == 0){
1146 printf ("ERROR - Attempt to reduce global scope\n");
1147 exit (0);
1148 }
1149 scopelevel = scopelevel - 1; //reduce the scope level
1150 scopetop = scopetop -> next; //point to the next scope
1151 // printf ("scopelevel is now %d\n",scopelevel);
1152}
1153/* --------------SCOPE PROCEDURES ABOVE--------------- */
1154/* bison handy procedures below */
1155
1156
1157
1158
1159
1160
1161%}
1162
1163// BISON TYPES BELOW
1164// code requires includes it with the lex file
1165%code requires {
1166 typedef struct charlist{
1167 char *name;
1168 struct charlist *next;
1169 } charlist;
1170
1171 typedef struct arraylist{
1172 int size;
1173 struct arraylist *next;
1174 struct arraylist *prev;
1175 }arraylist;
1176
1177
1178
1179} // end of CODE REQUIRES (included in lex file)
1180
1181%union
1182{
1183
1184 int intbison;
1185 char *charbison; // used for input
1186 char *boolbison;
1187 int voidbison;
1188 charlist *charlistbison;
1189 arraylist *arraylistbison;
1190 expnode *expnodebison;
1191}
1192
1193
1194
1195%token TK_IF
1196%token TK_FI
1197%token TK_ELSE
1198%token TK_DO
1199%token TK_OD
1200%token TK_FA
1201%token TK_AF
1202%token TK_TO
1203%token TK_PROC
1204%token TK_END
1205%token TK_RETURN
1206%token TK_FORWARD
1207%token TK_VAR
1208%token TK_TYPE
1209%token TK_BREAK
1210%token TK_EXIT
1211%token TK_TRUE
1212%token TK_FALSE
1213%token TK_WRITE
1214%token TK_WRITES
1215%token TK_READ
1216%token TK_BOX
1217%token TK_ARROW
1218%token TK_LPAREN
1219%token TK_RPAREN
1220%token TK_LBRACK
1221%token TK_RBRACK
1222%token TK_COLON
1223%token TK_SEMI
1224%token TK_ASSIGN
1225%token TK_QUEST
1226%token TK_COMMA
1227%token TK_PLUS
1228%token TK_MINUS
1229%token TK_STAR
1230%token TK_SLASH
1231%token TK_MOD
1232%token TK_EQ
1233%token TK_NEQ
1234%token TK_GT
1235%token TK_LT
1236%token TK_GE
1237%token TK_LE
1238
1239%token <charbison> TK_SLIT
1240%token <intbison> TK_INT
1241%token <charbison> TK_ID
1242
1243%nonassoc TK_EQ TK_NEQ TK_GT TK_LT TK_GE TK_LE
1244%left TK_MINUS TK_PLUS
1245%left TK_STAR TK_SLASH TK_MOD
1246%right TK_QUEST NEG
1247
1248
1249/* involved in var */
1250%type <charlistbison> commaids
1251%type <charlistbison> idlist
1252%type <charbison> typeid
1253%type <arraylistbison> arraybrackets
1254
1255/* exp */
1256%type <expnodebison> exp
1257
1258%start program
1259
1260%%
1261
1262program: declarations programstms
1263 {
1264 printf ("program: declarations programstms\n");
1265 }
1266declarations: declarations declaration
1267 {
1268 printf ("declarations: declarations declaration\n");
1269 }
1270declarations:
1271 {
1272 printf ("declarations:\n");
1273 }
1274declaration: vars
1275 {
1276 printf ("declaration: vars\n");
1277 }
1278declaration: types
1279 {
1280 printf ("declaration: types\n");
1281 }
1282declaration: forwards
1283 {
1284 printf ("declaration: forwards\n");
1285 }
1286forwards: forwards forward
1287 {
1288 printf ("forwards: forwards forward\n");
1289 }
1290forwards: forward
1291 {
1292 printf ("forwards: forward\n");
1293 }
1294declaration: procs
1295 {
1296 printf ("declaration: procs\n");
1297 }
1298procs: procs proc
1299 {
1300 printf ("procs: procs proc\n");
1301 }
1302procs: proc
1303 {
1304 printf ("procs: proc\n");
1305 }
1306stms: stms stm
1307 {
1308 printf ("stms: stms stm\n");
1309 }
1310programstms: programstms stm
1311 {
1312 printf ("programstms: programstms stm\n");
1313 }
1314programstms:
1315 {
1316 printf ("programstms:\n");
1317 }
1318stms: stm
1319 {
1320 printf ("stms: stm\n");
1321 }
1322stm: TK_EXIT TK_SEMI
1323 {
1324 printf ("stm: TK_EXIT TK_SEMI\n");
1325 }
1326stm: TK_BREAK TK_SEMI
1327 {
1328 printf ("stm: TK_BREAK TK_SEMI\n");
1329 }
1330stm: TK_RETURN TK_SEMI
1331 {
1332 printf ("stm: TK_RETURN TK_SEMI\n");
1333 }
1334stm: lvalue TK_ASSIGN exp TK_SEMI
1335 {
1336 printf ("stm: lvalue TK_ASSIGN exp TK_SEMI\n");
1337 }
1338exp: TK_LPAREN exp TK_RPAREN
1339 {
1340 printf ("exp: TK_LPAREN exp TK_RPAREN\n");
1341 }
1342exp: TK_MINUS exp %prec NEG
1343 {
1344 printf ("exp: TK_MINUS exp %%prec NEG\n");
1345 }
1346exp: TK_QUEST exp
1347 {
1348 printf ("exp: TK_QUEST exp\n");
1349 }
1350exp: exp TK_STAR exp
1351 {
1352 printf ("exp: exp TK_STAR exp\n");
1353 $$ -> typesymrecptr = symreturntype ("bool");
1354
1355 if (equivtype($1 -> typesymrecptr, $3 -> typesymrecptr))
1356 {
1357 printf ("TM CODE \n");
1358 // compute the results
1359
1360 }
1361 else // the two types are different - error
1362 yyerror ("ERROR - types are not equivalent");
1363 }
1364exp: exp TK_SLASH exp
1365 {
1366 printf ("exp: exp TK_SLASH exp\n");
1367 $$ -> typesymrecptr = symreturntype ("bool");
1368
1369 if (equivtype($1 -> typesymrecptr, $3 -> typesymrecptr))
1370 {
1371 printf ("TM CODE \n");
1372 // compute the results
1373
1374 }
1375 else // the two types are different - error
1376 yyerror ("ERROR - types are not equivalent");
1377 }
1378exp: exp TK_MOD exp
1379 {
1380 printf ("exp: exp TK_MOD exp\n");
1381 $$ -> typesymrecptr = symreturntype ("bool");
1382
1383 if (equivtype($1 -> typesymrecptr, $3 -> typesymrecptr))
1384 {
1385 printf ("TM CODE \n");
1386 // compute the results
1387
1388 }
1389 else // the two types are different - error
1390 yyerror ("ERROR - types are not equivalent");
1391 }
1392exp: exp TK_PLUS exp
1393 {
1394 printf ("exp: exp TK_PLUS exp\n");
1395 $$ -> typesymrecptr = symreturntype ("bool");
1396
1397 if (equivtype($1 -> typesymrecptr, $3 -> typesymrecptr))
1398 {
1399 printf ("TM CODE \n");
1400 // compute the results
1401
1402 }
1403 else // the two types are different - error
1404 yyerror ("ERROR - types are not equivalent");
1405 }
1406exp: exp TK_MINUS exp
1407 {
1408 printf ("exp: exp TK_MINUS exp\n");
1409 $$ -> typesymrecptr = symreturntype ("bool");
1410
1411 if (equivtype($1 -> typesymrecptr, $3 -> typesymrecptr))
1412 {
1413 printf ("TM CODE \n");
1414 // compute the results
1415
1416 }
1417 else // the two types are different - error
1418 yyerror ("ERROR - types are not equivalent");
1419 }
1420exp: exp TK_EQ exp
1421 {
1422 printf ("exp: exp TK_EQ exp\n");
1423 $$ -> typesymrecptr = symreturntype ("bool");
1424
1425 if (equivtype($1 -> typesymrecptr, $3 -> typesymrecptr))
1426 {
1427 printf ("TM CODE \n");
1428 // compute the results
1429
1430 }
1431 else // the two types are different - error
1432 yyerror ("ERROR - types are not equivalent");
1433 }
1434exp: exp TK_NEQ exp
1435 {
1436 printf ("exp: exp TK_NEQ exp\n");
1437 $$ -> typesymrecptr = symreturntype ("bool");
1438
1439 if (equivtype($1 -> typesymrecptr, $3 -> typesymrecptr))
1440 {
1441 printf ("TM CODE \n");
1442 // compute the results
1443
1444 }
1445 else // the two types are different - error
1446 yyerror ("ERROR - types are not equivalent");
1447 }
1448exp: exp TK_GT exp
1449 {
1450 printf ("exp: exp TK_GT exp\n");
1451 $$ -> typesymrecptr = symreturntype ("bool");
1452
1453 if (equivtype($1 -> typesymrecptr, $3 -> typesymrecptr))
1454 {
1455 printf ("TM CODE \n");
1456 // compute the results
1457
1458 }
1459 else // the two types are different - error
1460 yyerror ("ERROR - types are not equivalent");
1461 }
1462exp: exp TK_LT exp
1463 {
1464 printf ("exp: exp TK_LT exp\n");
1465 $$ -> typesymrecptr = symreturntype ("bool");
1466
1467 if (equivtype($1 -> typesymrecptr, $3 -> typesymrecptr))
1468 {
1469 printf ("TM CODE \n");
1470 // compute the results
1471
1472 }
1473 else // the two types are different - error
1474 yyerror ("ERROR - types are not equivalent");
1475 }
1476exp: exp TK_GE exp
1477 {
1478 printf ("exp: exp TK_GE exp\n");
1479
1480 $$ -> typesymrecptr = symreturntype ("bool");
1481
1482 if (equivtype($1 -> typesymrecptr, $3 -> typesymrecptr))
1483 {
1484 printf ("TM CODE \n");
1485 // compute the results
1486
1487 }
1488 else // the two types are different - error
1489 yyerror ("ERROR - types are not equivalent");
1490 }
1491exp: exp TK_LE exp
1492 {
1493 printf ("exp: exp TK_LE exp\n");
1494
1495 $$ -> typesymrecptr = symreturntype ("bool");
1496
1497 if (equivtype($1 -> typesymrecptr, $3 -> typesymrecptr))
1498 {
1499 printf ("TM CODE");
1500 // compute the results
1501
1502 }
1503 else // the two types are different - error
1504 yyerror ("ERROR - types are not equivalent");
1505
1506
1507
1508 }
1509exp: TK_TRUE
1510 {
1511 printf ("exp: TK_TRUE\n");
1512 $$ -> typesymrecptr = symreturntype ("bool"); // returns type to exp node
1513
1514
1515 printf ("BEGIN TM CODE\n");
1516 printf (".DATA 1\n"); // loads the value into dmem
1517 printf ("END TM CODE\n");
1518
1519 $$ -> tmval = tmdctr; // records location of dmem value
1520 tmdctr = tmdctr + 1; // increment data ctr to next empty loc
1521 tmictr = tmictr + 1; //increment instruction ctr to next empty imem
1522
1523 }
1524exp: TK_FALSE
1525 {
1526 printf ("exp: TK_FALSE\n");
1527 $$ -> typesymrecptr = symreturntype ("bool");
1528
1529
1530 printf ("BEGIN TM CODE\n");
1531 printf (".DATA 0\n"); // loads the value into dmem
1532 printf ("END TM CODE\n");
1533
1534 $$ -> tmval = tmdctr; // records location of dmem value
1535 tmdctr = tmdctr + 1; // increment data ctr to next empty loc
1536 tmictr = tmictr + 1; //increment instruction ctr to next empty imem
1537
1538 }
1539exp: TK_SLIT // string literal
1540 {
1541 printf ("exp: TK_SLIT\n");
1542
1543 stringlength = strlen ($1);
1544
1545 // returns the pointer to the type
1546 $$ -> typesymrecptr = symreturntype ("string");
1547
1548
1549 printf ("strnln is %d\n",stringlength); //debug
1550
1551
1552 printf ("BEGIN TM CODE\n");
1553 printf (".DATA %d\n",stringlength); // loads the length of the string into DMem
1554 printf (".SDATA %s\n",$1); //loads the string into Dmem
1555 printf ("END TM CODE\n");
1556
1557
1558 $$ -> tmval = tmdctr; // records location of dmem value
1559 tmdctr = tmdctr + stringlength +1; // increment data ctr to next empty loc
1560 tmictr = tmictr + 2; //increment instruction ctr to next empty imem
1561
1562 printf ("The value of TK_SLIT is - %s\n",$1);
1563 }
1564
1565
1566stm: TK_IF exp TK_ARROW stms elseifs else TK_FI
1567 {
1568 printf ("stm: TK_IF exp TK_ARROW stms elseifs else TK_FI\n");
1569 }
1570stm: TK_WRITE exp TK_SEMI
1571 {
1572 printf ("stm: TK_WRITE exp TK_SEMI\n");
1573 }
1574stm: TK_WRITES exp TK_SEMI
1575 {
1576 printf ("stm: TK_WRITES exp TK_SEMI\n");
1577 }
1578elseifs: elseifs elseif
1579 {
1580 printf ("elseifs: elseifs elseif\n");
1581 }
1582elseifs:
1583 {
1584 printf ("elseifs:\n");
1585 }
1586elseif: TK_BOX exp TK_ARROW stms
1587 {
1588 printf ("elseif: TK_BOX exp TK_ARROW stms\n");
1589 }
1590else: TK_BOX TK_ELSE TK_ARROW stms
1591 {
1592 printf ("else: TK_BOX TK_ELSE TK_ARROW stms\n");
1593 }
1594else:
1595 {
1596 printf ("else:\n");
1597 }
1598stm: TK_DO exp TK_ARROW stms TK_OD
1599 {
1600 printf ("stm: TK_DO exp TK_ARROW stms TK_OD\n");
1601 }
1602stm: TK_FA TK_ID TK_ASSIGN exp TK_TO exp TK_ARROW stms TK_AF
1603 {
1604 printf ("stm: TK_FA TK_ID TK_ASSIGN exp TK_TO exp TK_ARROW stms TK_AF\n");
1605 }
1606proc: TK_PROC TK_ID TK_LPAREN declist TK_RPAREN zootypeid proctypes procvars stms TK_END
1607 {
1608 printf ("proc: TK_PROC TK_ID TK_LPAREN declist TK_RPAREN zootypeid proctypes procvars stms TK_END\n");
1609 }
1610zootypeid: TK_COLON typeid
1611 {
1612 printf ("zootypeid: TK_COLON typeid\n");
1613 }
1614zootypeid:
1615 {
1616 printf ("zootypeid:\n");
1617 }
1618types: types type
1619 {
1620 printf ("types: types type\n");
1621 }
1622types: type
1623 {
1624 printf ("types: type\n");
1625 }
1626vars: vars var
1627 {
1628 printf ("vars: vars var\n");
1629 }
1630vars: var
1631 {
1632 printf ("vars: var\n");
1633 }
1634idlist: TK_ID commaids
1635 {
1636 printf ("idlist: TK_ID commaids\n");
1637 $$ = malloc (sizeof(charlist*));
1638 $$ -> name = $1;
1639 $$ -> next = $2;
1640
1641 };
1642commaids: commaids TK_COMMA TK_ID
1643 {
1644 printf ("commaids: commaids TK_COMMA TK_ID\n");
1645 $$ = malloc (sizeof(charlist*));
1646 $$ -> name = $3;
1647 $$ -> next = $1;
1648 // printf ("commmaids: commaids TK_COMMA TK_ID created charlist \n");
1649 };
1650commaids:
1651 {
1652 printf ("commaids:\n");
1653 $$ = (charlist *)0;
1654 }
1655var: TK_VAR varlist TK_SEMI
1656 {
1657 printf ("var: TK_VAR varlist TK_SEMI\n");
1658 }
1659varlist: idlist TK_COLON typeid arraybrackets varlists
1660 {
1661 printf ("varlist: idlist TK_COLON typeid arraybrackets varlists\n");
1662
1663 charlist *vlcharlist;
1664 vlcharlist = malloc (sizeof(charlist*));
1665
1666 varsymrec *varsymrecarraytmp = malloc (sizeof(varsymrec*)); //to hold complete array if it exists
1667
1668 if ($4 != 0){ //if there is an array
1669
1670 // varsymrec *varsymrecarraytmp = malloc (sizeof(varsymrec*)); //to hold complete array
1671 arraylist *tmparraylist = $4; //temprary pointer to the array holds arraybrackets
1672 // has an int and a next
1673 // arraylist *forcreatearraylist = malloc (sizeof(arraylist*)); // for for loop
1674
1675 // printf ("there is an array\n");
1676 // create the base first with a basetype of typeid, no underptr, and an array size of the 1st
1677 varsymrecarraytmp = vararraystruct($3, (varsymrec *)0, 0); //this is the undertype, the top will have the size
1678
1679 // printf ("added arraystruct with typeid %s\n",varsymrecarraytmp->basetype);
1680
1681 // add the rest of the arrays and start at the next
1682 // start adding the array sizes... the last one should be the top one... so stop to add the last one
1683 for (tmparraylist = $4; tmparraylist; tmparraylist = tmparraylist ->next){
1684
1685 varsymrecarraytmp = vararraystruct("array", varsymrecarraytmp, tmparraylist->size );
1686 // printf ("added arraystruct with typeid array and undertype %s and size %d \n", varsymrecarraytmp -> undertype -> basetype, varsymrecarraytmp -> arraysize);
1687
1688 } // now we have a whole list of the arrays... but the top one needs to be converted to the add.. see below
1689
1690 // array list is in varsymrecarraytmp
1691 // printf ("WE HAVE LIST varsymrecarraytmp with typeid array and undertype %s and size %d \n", varsymrecarraytmp -> undertype -> basetype, varsymrecarraytmp -> arraysize);
1692
1693 } //end if there is an array
1694
1695 // addvarsym (char *avsname, char *avsbasetype, varsymrec *avsundertype, int avsarraysize, int avsintvalue, bool avsboolvalue, char *avsstringvalue)
1696
1697 // ------------------- ADD THE CHARLISTS AND THE SAME TYPE TO EACH CHAR ----------
1698
1699 for (vlcharlist = $1; vlcharlist; vlcharlist = vlcharlist ->next){
1700 //add the record for each variable
1701 //if it comes back 0 there was an error and print error
1702
1703 //if there's an array
1704 if ($4 != 0){
1705 // printf ("WE HAVE LIST varsymrecarraytmp with typeid array and undertype %s and size %d \n", varsymrecarraytmp -> undertype -> basetype, varsymrecarraytmp -> arraysize);
1706 addvarsym (vlcharlist -> name, "array", varsymrecarraytmp->undertype, varsymrecarraytmp->arraysize, 0,0,"");
1707
1708 } else addvarsym (vlcharlist -> name, $3, 0, 0, 0, 0, "");
1709
1710
1711 }
1712 // varsymrec *vararraystruct(char* vasbasetype, varsymrec *vasunderunderptr, int vasarraysize){
1713 };
1714arraybrackets: arraybrackets TK_LBRACK TK_INT TK_RBRACK
1715 {
1716 printf ("arraybrackets: arraybrackets TK_LBRACK TK_INT TK_RBRACK\n");
1717 $$ = malloc (sizeof(arraylist*));
1718 $$ -> size = $3;
1719 $$ -> next = $1;
1720 };
1721arraybrackets:
1722 {
1723 printf ("arraybrackets:\n");
1724 $$ = (arraylist *) 0;
1725 };
1726varlists: TK_COMMA varlist
1727 {
1728 printf ("varlists: TK_COMMA varlist\n");
1729 }
1730varlists:
1731 {
1732 printf ("varlists:\n");
1733 }
1734forward: TK_FORWARD TK_ID TK_LPAREN declist TK_RPAREN zootypeid TK_SEMI
1735 {
1736 printf ("forward: TK_FORWARD TK_ID TK_LPAREN declist TK_RPAREN zootypeid TK_SEMI\n");
1737 }
1738type: TK_TYPE TK_ID TK_EQ typeid arraybrackets TK_SEMI
1739 {
1740 printf ("type: TK_TYPE TK_ID TK_EQ typeid arraybrackets TK_SEMI\n");
1741 }
1742declist:
1743 {
1744 printf ("declist:\n");
1745 }
1746declist: idlist TK_COLON typeid declistx
1747 {
1748 printf ("declist: idlist TK_COLON typeid declistx\n");
1749 }
1750declistx: TK_COMMA declist declistx
1751 {
1752 printf ("declistx: TK_COMMA declist declistx\n");
1753 }
1754declistx:
1755 {
1756 printf ("declistx:\n");
1757 }
1758typeid: TK_ID
1759 {
1760 printf ("typeid: TK_ID\n");
1761 $$ = $1;
1762 };
1763exp: TK_READ
1764 {
1765 printf ("exp: TK_READ\n");
1766 }
1767exp: TK_INT
1768 {
1769 printf ("exp: TK_INT\n");
1770
1771 // $$ -> type = "int";
1772 // $$ -> typesymrecptr = symreturntype ("int");
1773 // $$ -> intval = $1;
1774 // printf ("ITS iTS THE INT YO - %d\n",$1);
1775 }
1776pcallexps:
1777 {
1778 printf ("pcallexps:\n");
1779 }
1780pcallexps: exp pcallcommaexps
1781 {
1782 printf ("pcallexps: exp pcallcommaexps\n");
1783 }
1784pcallcommaexps: pcallcommaexps TK_COMMA exp
1785 {
1786 printf ("pcallcommaexps: pcallcommaexps TK_COMMA exp\n");
1787 }
1788pcallcommaexps:
1789 {
1790 printf ("pcallcommaexps:\n");
1791 }
1792exp: TK_ID pcallparens
1793 {
1794 printf ("exp: TK_ID pcallparens\n");
1795 }
1796pcallparens: TK_LPAREN pcallexps TK_RPAREN
1797 {
1798 printf ("pcallparens: TK_LPAREN pcallexps TK_RPAREN\n");
1799 }
1800lvaluex: lvaluex TK_LBRACK exp TK_RBRACK
1801 {
1802 printf ("lvaluex: lvaluex TK_LBRACK exp TK_RBRACK\n");
1803 }
1804lvalue: TK_ID lvaluex
1805 {
1806 printf ("lvalue: TK_ID lvaluex\n");
1807 }
1808lvaluex:
1809 {
1810 printf ("lvaluex:\n");
1811 }
1812exp: lvalue
1813 {
1814 printf ("exp: lvalue\n");
1815 }
1816procvars: procvars vars
1817 {
1818 printf ("procvars: procvars vars\n");
1819 }
1820procvars:
1821 {
1822 printf ("procvars:\n");
1823 }
1824proctypes: proctypes types
1825 {
1826 printf ("proctypes: proctypes types\n");
1827 }
1828proctypes:
1829 {
1830 printf ("proctypes:\n");
1831 }
1832stm: exp TK_SEMI
1833 {
1834 printf ("stm: exp TK_SEMI\n");
1835 }
1836stm: TK_SEMI
1837 {
1838 printf ("stm: TK_SEMI\n");
1839
1840 }
1841
1842
1843%%
1844
1845int main() {
1846
1847 /* INITIALIZE SYMBOL TABLES AND SCOPE*/
1848
1849
1850
1851 scopelevel = -1; //this is the initial value of scope before it is defined
1852 pushscope(); //pushes the global scope and initializes symbol tables
1853
1854 tmdctr = 1; //location of first empty data locatoin
1855 tmictr = 0; // first empty instruction
1856
1857/* ----------- error checking project below ------------ */
1858
1859/* TYPE SYMBOL TABLE ERROR CHECK BELOW */
1860
1861
1862/* --- DEBUG - ADD TYPE SYMBOL TABLE RECORDS --- */
1863
1864 /* add a new record with an underlying type of null */
1865// if ( addtypesym ("test name", "test basetype", (typesymrec *)0 , 0) == 1){
1866// printf ("Successful\n");
1867// }
1868
1869
1870/* ------------------ CREATE UNDERLYING TYPE (ARRAYS) ---------------- */
1871/* this will be done within the bison productions and it will be synthesized up */
1872
1873// BOTTOM underlying type
1874// typesymrec *underptr1; // pointer to array undertype
1875// underptr1 = typearraystruct ("char",0,0);
1876
1877//2nd from bottom underlying type
1878
1879// typesymrec *underptr2; // pointer to array undertype
1880// underptr2 = typearraystruct ("array2ndfrombottom",underptr1,3);
1881
1882// 3rd from bottom underlying type
1883
1884// typesymrec *underptr3; // pointer to array undertype
1885// underptr3 = typearraystruct ("array3rdfrombottom",underptr2,3);
1886
1887// 4th from bottom underlying type
1888// typesymrec *underptr4; // pointer to array undertype
1889// underptr4 = typearraystruct ("array4thfrombottom", underptr3, 10);
1890
1891
1892 // TOP ARRAY TYPE
1893// if (addtypesym ("test name", "array basetype", underptr4, 4)==1)
1894// {
1895// printf ("Successful\n");
1896// }
1897// else printf ("Unsuccessful\n");
1898 //array top level
1899
1900/* ------------------------------- END ARRAY CREATE ------------------ */
1901
1902//pushscope();
1903//pushscope();
1904//addtypesym ("test name2", "test basetype2", (typesymrec *)0 , 0);
1905//addtypesym ("test name3", "test basetype2", (typesymrec *)0 , 0);
1906//popscope();
1907//addtypesym ("test name2", "test basetype2", (typesymrec *)0 , 0);
1908
1909
1910//bool addvarsym (char *avsname, char *avsbasetype, varsymrec *avsundertype, int avsarraysize, int avsintvalue, bool avsboolvalue, char *avsstringvalue)
1911
1912//uvalue *test;
1913//test = (uvalue *) malloc (sizeof (uvalue));
1914// test -> intvalue = 0;
1915// test -> stringvalue = "hello";
1916// test -> boolvalue = 0;
1917
1918
1919//addvarsym ("varname", "varbasetype", 0 , 2, 3,true,"hey");
1920//pushscope();
1921//addvarsym ("varname2", "varbasetype", 0 , 2, 3,true,"hey");
1922
1923
1924/* ----------- error checking project above ------------- */
1925
1926
1927//add the 3 types int, bool, and string
1928
1929 addtypesym ("int", "int", (typesymrec *)0 , 0);
1930 addtypesym ("bool", "bool", (typesymrec *)0 , 0);
1931 addtypesym ("string", "string", (typesymrec *)0 , 0);
1932
1933 yyparse (); /* uncomment when ready to run program for realz */
1934
1935 printsymtable(); //prints type symbol table
1936// printvarsymtable();
1937
1938
1939
1940} //end main
1941
1942
1943/* int tok;
1944 while (1) {
1945 tok = yylex();
1946 if (tok == 0) break;
1947 switch (tok) {
1948 Case TK_ID:
1949 printf("ID : \t\"%s\"\n", yylval.str);
1950 break;
1951 Case TK_INT:
1952 printf("ILIT:\t%d\n", yylval.intt);
1953 break;
1954default:
1955 printf("TOK : \t%d\n", tok);
1956 }
1957 } */