This server is intended for use for Academic Classwork related Git repositories only. Projects/repositories will generally be removed after 6 months following close of the semester. Inactive repositories from previous semester are now being archived when no activity for 365 days. They are renamed and marked as 'archived'. After 90 days in that state they will be removed from the system completely.

evanshogan.y 15.9 KB
Newer Older
amh6wr's avatar
amh6wr committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
%{
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <stack>
#include "SymbolTable.h"
using namespace std;

stack<SYMBOL_TABLE> scopeStack;    // stack of scope hashtables

void beginScope();
void endScope();
void cleanUp();
void prepareToTerminate();
void bail();
bool findEntryInAnyScope(const string theName);

19 20
SYMBOL_TABLE_ENTRY getEntryInAnyScope(const string theName);

jdev52's avatar
jdev52 committed
21 22
void printToken(const char* tokenType, char* lexeme);

amh6wr's avatar
amh6wr committed
23 24 25
void printRule(const char*, const char*);


26
int yyerror(const char* s);
amh6wr's avatar
amh6wr committed
27 28 29 30 31 32 33 34 35 36 37 38 39

extern "C" 
{
    int yyparse(void);
    int yylex(void);
    int yywrap() {return 1;}
}

%}

%union 
{
  char* text;
jdev52's avatar
jdev52 committed
40
  int number;
amh6wr's avatar
amh6wr committed
41 42 43 44 45
  TYPE_INFO typeInfo;
};

/*
 *	Token declarations
jdev52's avatar
jdev52 committed
46
 */
amh6wr's avatar
amh6wr committed
47
%token  T_LPAREN T_RPAREN 
Hogan's avatar
Hogan committed
48
%token  T_IF T_LETSTAR T_PRINT T_INPUT T_PROGN T_EXIT
amh6wr's avatar
amh6wr committed
49 50 51 52
%token  T_ADD  T_SUB  T_MULT  T_DIV
%token  T_LT T_GT T_LE T_GE T_EQ T_NE T_AND T_OR T_NOT	 
%token  T_INTCONST T_STRCONST T_T T_NIL T_IDENT T_UNKNOWN

jdev52's avatar
jdev52 committed
53 54 55
/*
 *	Type Identifications
 */
amh6wr's avatar
amh6wr committed
56
%type <text> T_IDENT
jdev52's avatar
jdev52 committed
57 58
%type <text> T_STRCONST
%type <number> T_INTCONST
jdev52's avatar
jdev52 committed
59 60 61
%type <typeInfo> N_START N_EXPR N_CONST N_PARENTHESIZED_EXPR
%type <typeInfo> N_PROGN_OR_USERFUNCTCALL N_ACTUAL_PARAMS N_FUNCT_NAME 
%type <typeInfo> N_ARITHLOGIC_EXPR N_IF_EXPR N_LET_EXPR N_ID_EXPR_LIST
Hogan's avatar
Hogan committed
62
%type <typeInfo> N_PRINT_EXPR N_INPUT_EXPR 
jdev52's avatar
jdev52 committed
63
%type <typeInfo> N_EXPR_LIST N_BIN_OP N_ARITH_OP N_REL_OP N_LOG_OP N_UN_OP  
jdev52's avatar
jdev52 committed
64

amh6wr's avatar
amh6wr committed
65 66 67 68 69 70 71 72 73
/*
 *	Starting point.
 */
%start  N_START

/*
 *	Translation rules.
 */
%%
jdev52's avatar
jdev52 committed
74 75 76 77 78
N_START						: // epsilon 
							{
							}
							| N_START N_EXPR
							{
Hogan's avatar
Hogan committed
79
								/*
jdev52's avatar
jdev52 committed
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
								cout << "EXPR type is: ";
								switch ($2.type){
									case 1:
										cout<<"INT"<<"\n";
										break;
									case 2:
										cout<<"STR"<<"\n";
										break;
									case 4:
										cout<<"BOOL"<<"\n";
										break;
									case 8:
										cout<<"FUNCTION"<<"\n";
										break;
									case 3:
										cout<<"INT_OR_STR"<<"\n";
										break;
									case 5:
										cout<<"INT_OR_BOOL"<<"\n";
										break;
									case 6:
										cout<<"STR_OR_BOOL"<<"\n";
										break;
									case 7:
										cout<<"INT_OR_STR_OR_BOOL"<<"\n";
										break;
								}
Hogan's avatar
Hogan committed
107
								*/
jdev52's avatar
jdev52 committed
108
								cout << "\n---- Completed parsing ----\n" << endl;
109
								cout << "\nValue of the expression is: ";
jdev52's avatar
jdev52 committed
110
								cout << $2 <<"\n";
jdev52's avatar
jdev52 committed
111 112 113 114 115
							}
							;
N_EXPR						: N_CONST
							{
								//resulting type is the type of N_CONST
116
								//value is the value of N_CONST
jdev52's avatar
jdev52 committed
117 118 119
								$$.type=$1.type;
								$$.numParams=NOT_APPLICABLE;
								$$.returnType=NOT_APPLICABLE;
120
								$$.value=$1.value;								
jdev52's avatar
jdev52 committed
121 122 123
							}
							| T_IDENT
							{
124
								//look up T_IDENT in symbol table
jdev52's avatar
jdev52 committed
125 126 127 128 129 130
								string lexeme = string($1);
								if(!findEntryInAnyScope(lexeme)){
									yyerror("Undefined identifier");
								}
								//resulting type is the type of the identifier
								//look up in the symbol table
jdev52's avatar
jdev52 committed
131 132 133
								TYPE_INFO entryInfo = getEntryInAnyScope(lexeme).getTypeInfo();
								$$.type = entryInfo.type;
								$$.value = entryInfo.value;
jdev52's avatar
jdev52 committed
134 135 136
							}
							| T_LPAREN N_PARENTHESIZED_EXPR T_RPAREN
							{
137
								//resulting value is the value of the N_PARENTHESIZED_EXPR
jdev52's avatar
jdev52 committed
138
								//resulting type is the resulting type of the parenthesized expr
jdev52's avatar
jdev52 committed
139 140 141 142
								$$.type = $2.type;
								$$.numParams = $2.numParams;
								$$.returnType = $2.returnType;
								$$.value = $2.value;
jdev52's avatar
jdev52 committed
143 144 145 146
							}
							;
N_CONST						: T_INTCONST
							{
147
								//value is the value of the T_INTCONST
jdev52's avatar
jdev52 committed
148
								//type is int
jdev52's avatar
jdev52 committed
149 150 151 152
								$$.type = INT;
								$$.numParams = NOT_APPLICABLE;
								$$.returnType = NOT_APPLICABLE;
								$$.value.number = $1;
jdev52's avatar
jdev52 committed
153 154 155
							}
							| T_STRCONST
							{
156
								//value is the value of the T_STRCONST
jdev52's avatar
jdev52 committed
157 158 159 160
								//type is string
								$$.type=STR;
								$$.numParams=NOT_APPLICABLE;
								$$.returnType=NOT_APPLICABLE;
jdev52's avatar
jdev52 committed
161
								$$.value.text = $1;
jdev52's avatar
jdev52 committed
162 163 164
							}
							| T_T
							{
165
								//Be careful with choosing the const value for these
jdev52's avatar
jdev52 committed
166 167 168 169
								//type is bool
								$$.type=BOOL;
								$$.numParams=NOT_APPLICABLE;
								$$.returnType=NOT_APPLICABLE;
jdev52's avatar
jdev52 committed
170
								$$.value.boolean = true;
jdev52's avatar
jdev52 committed
171 172 173
							}
							| T_NIL
							{
174
								//Be careful with choosing the const value for these
jdev52's avatar
jdev52 committed
175 176 177 178
								//type is bool
								$$.type=BOOL;
								$$.numParams=NOT_APPLICABLE;
								$$.returnType=NOT_APPLICABLE;
jdev52's avatar
jdev52 committed
179
								$$.value.boolean = false;
jdev52's avatar
jdev52 committed
180 181 182
							}
							;
N_PARENTHESIZED_EXPR		: N_ARITHLOGIC_EXPR    
183 184
		      				{
							//check for divison by 0	
jdev52's avatar
jdev52 committed
185
							//resulting type of whatever rule is applied 
186
						
jdev52's avatar
jdev52 committed
187 188 189
								$$.type=$1.type;
								$$.numParams=$1.numParams;
								$$.returnType=$1.returnType;
jdev52's avatar
jdev52 committed
190
								$$.value=$1.value;
jdev52's avatar
jdev52 committed
191 192 193 194 195 196
							}
							| N_IF_EXPR 
							{
								$$.type=$1.type;
								$$.numParams=$1.numParams;
								$$.returnType=$1.returnType;
jdev52's avatar
jdev52 committed
197
								$$.value=$1.value;
jdev52's avatar
jdev52 committed
198 199 200 201 202 203
							}
							| N_LET_EXPR 
							{
								$$.type=$1.type;
								$$.numParams=$1.numParams;
								$$.returnType=$1.returnType;
jdev52's avatar
jdev52 committed
204
								$$.value=$1.value;
jdev52's avatar
jdev52 committed
205 206 207 208 209 210
							}
							| N_PRINT_EXPR 
							{
								$$.type=$1.type;
								$$.numParams=$1.numParams;
								$$.returnType=$1.returnType;
jdev52's avatar
jdev52 committed
211
								$$.value=$1.value;
jdev52's avatar
jdev52 committed
212 213 214 215 216 217
							}
							| N_INPUT_EXPR 
							{
								$$.type=$1.type;
								$$.numParams=$1.numParams;
								$$.returnType=$1.returnType;
jdev52's avatar
jdev52 committed
218
								$$.value=$1.value;
jdev52's avatar
jdev52 committed
219 220 221 222 223 224
							}
							| N_PROGN_OR_USERFUNCTCALL 
							{
								$$.type=$1.type;
								$$.numParams=$1.numParams;
								$$.returnType=$1.returnType;
jdev52's avatar
jdev52 committed
225
								$$.value=$1.value;
jdev52's avatar
jdev52 committed
226 227 228 229 230 231
							}
							| T_EXIT
							{
								bail();
							}
							;
amh6wr's avatar
amh6wr committed
232 233
N_PROGN_OR_USERFUNCTCALL	: N_FUNCT_NAME   N_ACTUAL_PARAMS
							{
234 235 236
								if($1.type == UNDEFINED){
									if($2.type == NOT_APPLICABLE){
										$$.type = BOOL;
237
										$$.value.boolean = false;
238 239
									} else {
										$$.type = $2.type;
240
										$$.value = $2.value;
241 242 243 244
									}
								} else {
									if($1.numParams < $2.numParams){
										yyerror("Too many parameters in function call");
jdev52's avatar
jdev52 committed
245
									} else if($1.numParams < $2.numParams){
246 247 248 249 250 251 252 253
										yyerror("Too few parameters in function call");
									} 

									$$.type = $1.type;
								}

								$$.numParams = $2.numParams;
								$$.returnType = UNDEFINED;
jdev52's avatar
jdev52 committed
254
								$$.value = $2.value;
amh6wr's avatar
amh6wr committed
255 256
							}
							;
jdev52's avatar
jdev52 committed
257 258 259 260 261
N_ACTUAL_PARAMS				: N_EXPR_LIST
							{
								$$.type = $1.type;
								$$.numParams = $1.numParams;
								$$.returnType = $1.type;
jdev52's avatar
jdev52 committed
262
								$$.value = $1.value;
263
							}
jdev52's avatar
jdev52 committed
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
							| /*epsilon*/
							{
								$$.type = NOT_APPLICABLE;
								$$.numParams = 0;
								$$.returnType = NOT_APPLICABLE;
							}						
N_FUNCT_NAME				: T_PROGN
							{
								$$.type = UNDEFINED;
								$$.numParams = 0;
								$$.returnType = UNDEFINED;
							}
							| T_IDENT
							{
								string lexeme = string($1);
								if(!findEntryInAnyScope(lexeme)){
									yyerror("Undefined identifier");
								} 

								TYPE_INFO typeInfo = getEntryInAnyScope(lexeme).getTypeInfo();

								if(typeInfo.type != FUNCTION){
									yyerror("Arg 1 must be a function");
								}

								$$.type = typeInfo.returnType;
								$$.numParams = typeInfo.numParams;
								$$.returnType = typeInfo.returnType;
							}
							;
N_ARITHLOGIC_EXPR			: N_UN_OP N_EXPR
							{
								if($2.type == FUNCTION){
									yyerror("Arg 1 cannot be a function");
								}
								$$.type = BOOL;
								$$.numParams = NOT_APPLICABLE;
								$$.returnType = NOT_APPLICABLE; 
jdev52's avatar
jdev52 committed
302 303 304 305 306 307
								//Slightly redundant, but good in the event of future expansion
								switch($1.operatorInstance){
									case NOT:
										$$.value.boolean = !$2;
										break;
								}
jdev52's avatar
jdev52 committed
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
							}
							| N_BIN_OP N_EXPR N_EXPR
							{
								//check if operation can be preformed
								switch($1.operatorType){
									case RELATIONAL:
										if(($2.type != INT) && ($2.type != STR)){
											yyerror("Arg 1 must be integer or string");
										}

										if(($3.type != INT) && ($3.type != STR)){
											yyerror("Arg 2 must be integer or string");
										}
										
										if(($2.type == INT) && (($3.type != INT) && ($3.type != INT_OR_STR))){
											yyerror("Arg 2 must be integer");
										}

										if(($2.type == STR) && (($3.type != STR) && ($3.type != INT_OR_STR))){
											yyerror("Arg 2 must be string");
										}

jdev52's avatar
jdev52 committed
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
										switch($1.operatorInstance){
											case LT:
												$$.value.boolean = ($2 < $3);
												break;
											case GT:
												$$.value.boolean = ($2 > $3);
												break;
											case LE:
												$$.value.boolean = ($2 <= $3);
												break;
											case GE:
												$$.value.boolean = ($2 >= $3);
												break;
											case EQ:
												$$.value.boolean = ($2 == $3);
												break;
											case NE:
												$$.value.boolean = ($2 != $3);
												break;
										}

jdev52's avatar
jdev52 committed
351 352 353 354 355 356 357 358 359 360
										break;
									case LOGICAL:
										if($2.type == FUNCTION) {
											yyerror("Arg 1 cannot be a function");
										}

										if($3.type == FUNCTION) {
											yyerror("Arg 2 cannot be a function");
										}

jdev52's avatar
jdev52 committed
361 362 363 364 365 366 367 368 369
										switch($1.operatorInstance){
											case AND:
												$$.value.boolean = (($2) && ($3));
												break;
											case OR:
												$$.value.boolean = (($2) || ($3));
												break;
										}

jdev52's avatar
jdev52 committed
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
										break;
									case ARITHMETIC:
										switch($2.type){
											case INT:
											case INT_OR_STR:
											case INT_OR_BOOL:
											case INT_OR_STR_OR_BOOL:
												break;
											default:
												yyerror("Arg 1 must be integer");
												break;
										}

										switch($3.type){
											case INT:
											case INT_OR_STR:
											case INT_OR_BOOL:
											case INT_OR_STR_OR_BOOL:
												break;
											default:
												yyerror("Arg 2 must be integer");
												break;
										}

jdev52's avatar
jdev52 committed
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
										switch($1.operatorInstance){
											case ADD:
												$$.value.number = $2.value.number + $3.value.number;
												break;
											case SUBTRACT:
												$$.value.number = $2.value.number - $3.value.number; 
												break;
											case MULTIPLY:
												$$.value.number = $2.value.number * $3.value.number;
												break;
											case DIVIDE:
												$$.value.number = $2.value.number / $3.value.number;
												break;
										}

jdev52's avatar
jdev52 committed
409 410 411 412 413 414
										break;
								}

								$$.type = $1.type;
								$$.numParams = $1.numParams;
								$$.returnType = $1.returnType;
415
							
jdev52's avatar
jdev52 committed
416 417 418 419 420 421 422 423 424 425 426 427 428
							}
							;
N_IF_EXPR    				: T_IF N_EXPR N_EXPR N_EXPR
							{
								//type of expr1 + type of expr2+ type of expr3= type of if statement
								if($2.type == FUNCTION){
									yyerror("Arg 1 cannot be a function");
								} else if($3.type == FUNCTION){
									yyerror("Arg 2 cannot be a function");
								} else if($4.type == FUNCTION){
									yyerror("Arg 3 cannot be a function");
								}

429 430 431 432 433 434 435 436
								if($2){
									$$.type = $3.type;
									$$.value = $3.value;
								} else {
									$$.type = $4.type;
									$$.value = $4.value;
								}

jdev52's avatar
jdev52 committed
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
								$$.numParams = NOT_APPLICABLE;
								$$.returnType = NOT_APPLICABLE;
							}
							;
N_LET_EXPR      			: T_LETSTAR T_LPAREN N_ID_EXPR_LIST T_RPAREN N_EXPR
							{
								endScope();
								if($5.type==FUNCTION){
									yyerror("Arg 2 cannot be a function");
								}

								$$.type=$5.type;
								$$.numParams = $5.numParams;
								$$.returnType = $5.returnType;
							}
							;
N_ID_EXPR_LIST  			: /* epsilon */
							{
							}
               			 	| N_ID_EXPR_LIST T_LPAREN T_IDENT N_EXPR T_RPAREN 
							{
								string lexeme = string($3);
jdev52's avatar
jdev52 committed
459
								// printf("___Adding %s to symbol table\n", $3);
jdev52's avatar
jdev52 committed
460 461 462 463 464 465 466 467 468 469 470 471 472 473
								bool found = scopeStack.top().findEntry(lexeme);
								if (found){
									yyerror("Multiply defined identifier");
								} 

								scopeStack.top().addEntry(SYMBOL_TABLE_ENTRY(lexeme, $4));	  
							}
							;
N_PRINT_EXPR    			: T_PRINT N_EXPR
							{
								if($2.type==FUNCTION){
									yyerror("Arg 1 cannot be a function");
								}

474 475 476
								cout << $2 << endl;

								$$.value=$2.value;
jdev52's avatar
jdev52 committed
477 478 479 480 481 482 483 484 485 486 487 488 489 490
								$$.type=$2.type;
								$$.numParams = $2.numParams;
								$$.returnType = $2.returnType;
							}
							;
N_INPUT_EXPR    			: T_INPUT
							{
								$$.type=INT_OR_STR;
								$$.numParams = NOT_APPLICABLE;
								$$.returnType = NOT_APPLICABLE;
							}
							;
N_EXPR_LIST     			: N_EXPR N_EXPR_LIST  
							{
491
								$$.value = $2.value;
jdev52's avatar
jdev52 committed
492 493 494 495 496 497
								$$.type = $2.type;
								$$.numParams = $2.numParams+1;
								$$.returnType = NOT_APPLICABLE;
							}
            				| N_EXPR
							{
498
								$$.value = $1.value;
jdev52's avatar
jdev52 committed
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
								$$.type=$1.type;
								if($1.type == FUNCTION) {
									$$.type = $1.returnType;
								}

								$$.numParams = 1;
								$$.returnType = NOT_APPLICABLE;
							}
							;
N_BIN_OP					: N_ARITH_OP
							{
								$$.type = INT;
								$$.numParams = NOT_APPLICABLE;
								$$.returnType = NOT_APPLICABLE;
								$$.operatorType = ARITHMETIC;
jdev52's avatar
jdev52 committed
514
								$$.operatorInstance = $1.operatorInstance;
jdev52's avatar
jdev52 committed
515 516 517 518 519 520 521
							}
							| N_LOG_OP
							{
								$$.type = BOOL;
								$$.numParams = NOT_APPLICABLE;
								$$.returnType = NOT_APPLICABLE;
								$$.operatorType = LOGICAL;
jdev52's avatar
jdev52 committed
522
								$$.operatorInstance = $1.operatorInstance;
jdev52's avatar
jdev52 committed
523 524 525 526 527 528 529
							}
							| N_REL_OP
							{
								$$.type = BOOL;
								$$.numParams = NOT_APPLICABLE;
								$$.returnType = NOT_APPLICABLE;
								$$.operatorType = RELATIONAL;
jdev52's avatar
jdev52 committed
530
								$$.operatorInstance = $1.operatorInstance;
jdev52's avatar
jdev52 committed
531 532 533 534
							}
							;
N_ARITH_OP					: T_ADD
							{
jdev52's avatar
jdev52 committed
535
								$$.operatorInstance = ADD;
jdev52's avatar
jdev52 committed
536 537 538
							}
							| T_SUB
							{
jdev52's avatar
jdev52 committed
539
								$$.operatorInstance = SUBTRACT;
jdev52's avatar
jdev52 committed
540 541 542
							}
							| T_MULT
							{
jdev52's avatar
jdev52 committed
543
								$$.operatorInstance = MULTIPLY;
jdev52's avatar
jdev52 committed
544 545 546
							}
							| T_DIV
							{
jdev52's avatar
jdev52 committed
547
								$$.operatorInstance = DIVIDE;
jdev52's avatar
jdev52 committed
548 549 550 551
							}
							;
N_REL_OP					: T_LT
							{
jdev52's avatar
jdev52 committed
552
								$$.operatorInstance = LT;
jdev52's avatar
jdev52 committed
553 554 555
							}	
							| T_GT
							{
jdev52's avatar
jdev52 committed
556
								$$.operatorInstance = GT;
jdev52's avatar
jdev52 committed
557 558 559
							}	
							| T_LE
							{
jdev52's avatar
jdev52 committed
560
								$$.operatorInstance = LE;
jdev52's avatar
jdev52 committed
561 562 563
							}	
							| T_GE
							{
jdev52's avatar
jdev52 committed
564
								$$.operatorInstance = GE;
jdev52's avatar
jdev52 committed
565 566 567
							}	
							| T_EQ
							{
jdev52's avatar
jdev52 committed
568
								$$.operatorInstance = EQ;
jdev52's avatar
jdev52 committed
569 570 571
							}	
							| T_NE
							{
jdev52's avatar
jdev52 committed
572
								$$.operatorInstance = NE;
jdev52's avatar
jdev52 committed
573 574 575 576
							}
							;	
N_LOG_OP					: T_AND
							{
jdev52's avatar
jdev52 committed
577
								$$.operatorInstance = AND;
jdev52's avatar
jdev52 committed
578 579 580
							}	
							| T_OR
							{
jdev52's avatar
jdev52 committed
581
								$$.operatorInstance = OR;
jdev52's avatar
jdev52 committed
582 583 584 585
							}
							;
N_UN_OP	     				: T_NOT
							{
jdev52's avatar
jdev52 committed
586
								$$.operatorInstance = NOT;
jdev52's avatar
jdev52 committed
587 588
							}
							;
amh6wr's avatar
amh6wr committed
589 590 591 592 593 594 595
%%

#include "lex.yy.c"
extern FILE *yyin;

void printRule(const char* lhs, const char* rhs) 
{
jdev52's avatar
jdev52 committed
596 597
	printf("%s -> %s\n", lhs, rhs);
	return;
amh6wr's avatar
amh6wr committed
598 599
}

600 601
int yyerror(const char* s) 
{
jdev52's avatar
jdev52 committed
602 603 604
	printf("Line %d: %s\n", numLines, s);
	bail();
	return 0;
605 606
}

jdev52's avatar
jdev52 committed
607 608 609 610
void printToken(const char* tokenType, char* lexeme){
    printf("TOKEN: %-8s  LEXEME: %s\n", tokenType, lexeme);
}

amh6wr's avatar
amh6wr committed
611 612
void beginScope() 
{
jdev52's avatar
jdev52 committed
613
	scopeStack.push(SYMBOL_TABLE());
jdev52's avatar
jdev52 committed
614
	// printf("\n___Entering new scope...\n\n");
amh6wr's avatar
amh6wr committed
615 616 617 618
}

void endScope() 
{
jdev52's avatar
jdev52 committed
619
	scopeStack.pop();
jdev52's avatar
jdev52 committed
620
	// printf("\n___Exiting scope...\n\n");
amh6wr's avatar
amh6wr committed
621 622 623 624
}

bool findEntryInAnyScope(const string theName) 
{
jdev52's avatar
jdev52 committed
625 626 627 628 629 630 631 632 633 634 635 636 637
	if(scopeStack.empty( )){
		return(false);  
	} 
	bool found = scopeStack.top().findEntry(theName);
	if (found){
		return(true);
	} else { // check in "next higher" scope
		SYMBOL_TABLE symbolTable = scopeStack.top( );
		scopeStack.pop( );
		found = findEntryInAnyScope(theName);
		scopeStack.push(symbolTable); // restore the stack
		return (found);
	}
amh6wr's avatar
amh6wr committed
638 639
}

jdev52's avatar
jdev52 committed
640
//Should only call this if it is known that the entry is in the scopestack
641 642
SYMBOL_TABLE_ENTRY getEntryInAnyScope(const string theName){
	SYMBOL_TABLE currentScope = scopeStack.top();
jdev52's avatar
jdev52 committed
643
	SYMBOL_TABLE_ENTRY target; 
644
	if(currentScope.findEntry(theName)){
jdev52's avatar
jdev52 committed
645
		target = currentScope.getEntry(theName);
646 647
	} else {
		scopeStack.pop();
jdev52's avatar
jdev52 committed
648
		target = getEntryInAnyScope(theName);
649 650
		scopeStack.push(currentScope);
	}
jdev52's avatar
jdev52 committed
651
	return target;
652 653
}

amh6wr's avatar
amh6wr committed
654 655
void cleanUp() 
{
jdev52's avatar
jdev52 committed
656 657 658 659 660 661
	if (scopeStack.empty()){
		return;
	} else {
		scopeStack.pop();
		cleanUp();
	}
amh6wr's avatar
amh6wr committed
662 663 664 665
}

void prepareToTerminate()
{
jdev52's avatar
jdev52 committed
666 667
	cleanUp();
	cout << endl << "Bye!" << endl;
amh6wr's avatar
amh6wr committed
668 669 670 671
}

void bail()
{
jdev52's avatar
jdev52 committed
672 673
	prepareToTerminate();
	exit(1);
amh6wr's avatar
amh6wr committed
674 675
}

Hogan's avatar
Hogan committed
676 677 678 679 680 681 682 683 684 685 686 687
int main(int argc, char** argv)  
{   
	if (argc < 2)    
	{     
		printf("You must specify a file in the command line!\n");     
		exit(1);   
	}   
	yyin = fopen(argv[1], "r");   
	do    
	{     
		yyparse(); 
  	} 	
jdev52's avatar
jdev52 committed
688 689
	while (!feof(yyin));
	bail();  
Hogan's avatar
Hogan committed
690 691
	return 0; 
}