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 13.8 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 40 41 42 43 44

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

%}

%union 
{
  char* text;
  TYPE_INFO typeInfo;
};

/*
 *	Token declarations
jdev52's avatar
jdev52 committed
45
 */
amh6wr's avatar
amh6wr committed
46 47 48 49 50 51
%token  T_LPAREN T_RPAREN 
%token  T_IF T_LETSTAR T_LAMBDA T_PRINT T_INPUT T_PROGN T_EXIT
%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
52 53 54
/*
 *	Type Identifications
 */
amh6wr's avatar
amh6wr committed
55
%type <text> T_IDENT
jdev52's avatar
jdev52 committed
56 57 58 59 60
%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
%type <typeInfo> N_LAMBDA_EXPR N_ID_LIST N_PRINT_EXPR N_INPUT_EXPR 
%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
61

amh6wr's avatar
amh6wr committed
62 63 64 65 66 67 68 69 70
/*
 *	Starting point.
 */
%start  N_START

/*
 *	Translation rules.
 */
%%
jdev52's avatar
jdev52 committed
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
N_START						: // epsilon 
							{
							}
							| N_START N_EXPR
							{
								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;
								}

								cout << "---- Completed parsing ----\n" << endl;
							}
							;
N_EXPR						: N_CONST
							{
								//resulting type is the type of N_CONST
								$$.type=$1.type;
								$$.numParams=NOT_APPLICABLE;
								$$.returnType=NOT_APPLICABLE;
							}
							| T_IDENT
							{
								string lexeme = string($1);
								if(!findEntryInAnyScope(lexeme)){
									yyerror("Undefined identifier");
								}
								//resulting type is the type of the identifier
								//look up in the symbol table
								$$.type = getEntryInAnyScope(lexeme).getTypeInfo().type;
							}
							| T_LPAREN N_PARENTHESIZED_EXPR T_RPAREN
							{
								//resulting type is the resulting type of the parenthesized expr
								$$.type=$2.type;
								$$.numParams=$2.numParams;
								$$.returnType=$2.returnType;
							}
							;
N_CONST						: T_INTCONST
							{
								//type is int
								$$.type=INT;
								$$.numParams=NOT_APPLICABLE;
								$$.returnType=NOT_APPLICABLE;
							}
							| T_STRCONST
							{
								//type is string
								$$.type=STR;
								$$.numParams=NOT_APPLICABLE;
								$$.returnType=NOT_APPLICABLE;
							}
							| T_T
							{
								//type is bool
								$$.type=BOOL;
								$$.numParams=NOT_APPLICABLE;
								$$.returnType=NOT_APPLICABLE;
							}
							| T_NIL
							{
								//type is bool
								$$.type=BOOL;
								$$.numParams=NOT_APPLICABLE;
								$$.returnType=NOT_APPLICABLE;
							}
							;
N_PARENTHESIZED_EXPR		: N_ARITHLOGIC_EXPR    
							//resulting type of whatever rule is applied 
							{
								$$.type=$1.type;
								$$.numParams=$1.numParams;
								$$.returnType=$1.returnType;
							}
							| N_IF_EXPR 
							{
								$$.type=$1.type;
								$$.numParams=$1.numParams;
								$$.returnType=$1.returnType;
							}
							| N_LET_EXPR 
							{
								$$.type=$1.type;
								$$.numParams=$1.numParams;
								$$.returnType=$1.returnType;
							}
							| N_LAMBDA_EXPR 
							{
								$$.type=$1.type;
								$$.numParams=$1.numParams;
								$$.returnType=$1.returnType;
							}
							| N_PRINT_EXPR 
							{
								$$.type=$1.type;
								$$.numParams=$1.numParams;
								$$.returnType=$1.returnType;
							}
							| N_INPUT_EXPR 
							{
								$$.type=$1.type;
								$$.numParams=$1.numParams;
								$$.returnType=$1.returnType;
							}
							| N_PROGN_OR_USERFUNCTCALL 
							{
								$$.type=$1.type;
								$$.numParams=$1.numParams;
								$$.returnType=$1.returnType;
							}
							| T_EXIT
							{
								bail();
							}
							;
amh6wr's avatar
amh6wr committed
209 210
N_PROGN_OR_USERFUNCTCALL	: N_FUNCT_NAME   N_ACTUAL_PARAMS
							{
211 212 213 214 215 216 217 218 219
								if($1.type == UNDEFINED){
									if($2.type == NOT_APPLICABLE){
										$$.type = BOOL;
									} else {
										$$.type = $2.type;
									}
								} else {
									if($1.numParams < $2.numParams){
										yyerror("Too many parameters in function call");
jdev52's avatar
jdev52 committed
220
									} else if($1.numParams < $2.numParams){
221 222 223 224 225 226 227 228
										yyerror("Too few parameters in function call");
									} 

									$$.type = $1.type;
								}

								$$.numParams = $2.numParams;
								$$.returnType = UNDEFINED;
amh6wr's avatar
amh6wr committed
229 230 231
							}
							|   T_LPAREN  N_LAMBDA_EXPR  T_RPAREN  N_ACTUAL_PARAMS 
							{
232 233
								if($2.numParams < $4.numParams){
									yyerror("Too many parameters in function call");
jdev52's avatar
jdev52 committed
234
								} else if($4.numParams < $2.numParams){
235 236 237 238 239 240
									yyerror("Too few parameters in function call");
								} 

								$$.type = $2.returnType;
								$$.numParams = $4.numParams;
								$$.returnType = $2.returnType;
amh6wr's avatar
amh6wr committed
241 242
							}
							;
jdev52's avatar
jdev52 committed
243 244 245 246 247
N_ACTUAL_PARAMS				: N_EXPR_LIST
							{
								$$.type = $1.type;
								$$.numParams = $1.numParams;
								$$.returnType = $1.type;
248
							}
jdev52's avatar
jdev52 committed
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
							| /*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; 
							}
							| 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");
										}

										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");
										}

										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;
										}

										break;
								}

								$$.type = $1.type;
								$$.numParams = $1.numParams;
								$$.returnType = $1.returnType;
349
							
jdev52's avatar
jdev52 committed
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
							}
							;
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");
								}

								$$.type = $3.type | $4.type;
								$$.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);
								printf("___Adding %s to symbol table\n", $3);
								bool found = scopeStack.top().findEntry(lexeme);
								if (found){
									yyerror("Multiply defined identifier");
								} 

								scopeStack.top().addEntry(SYMBOL_TABLE_ENTRY(lexeme, $4));	  
							}
							;
N_LAMBDA_EXPR   			: T_LAMBDA T_LPAREN N_ID_LIST T_RPAREN N_EXPR
							{
								if($5.type==FUNCTION){
									yyerror("Arg 2 cannot be a function");
								}
jdev52's avatar
jdev52 committed
400
								endScope();
jdev52's avatar
jdev52 committed
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
								
								$$.type=FUNCTION;
								$$.numParams=$3.numParams;
								$$.returnType=$5.type;
							}
							;
N_ID_LIST       			: /* epsilon */
							{
								$$.numParams = 0;
							}
							| N_ID_LIST T_IDENT 
							{
								string lexeme = string($2);
								printf("___Adding %s to symbol table\n", $2);
								bool found = scopeStack.top().findEntry(lexeme);
								if(found){
									yyerror("Multiply defined identifier");
								} 
								scopeStack.top().addEntry(SYMBOL_TABLE_ENTRY(lexeme, INT_OR_STR_OR_BOOL));

								$$.type = INT;
								$$.numParams = $1.numParams+1;
								$$.returnType = NOT_APPLICABLE;
							}
							;
N_PRINT_EXPR    			: T_PRINT N_EXPR
							{
								if($2.type==FUNCTION){
									yyerror("Arg 1 cannot be a function");
								}

								$$.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  
							{
								$$.type = $2.type;
								$$.numParams = $2.numParams+1;
								$$.returnType = NOT_APPLICABLE;
							}
            				| N_EXPR
							{
								$$.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;
							}
							| N_LOG_OP
							{
								$$.type = BOOL;
								$$.numParams = NOT_APPLICABLE;
								$$.returnType = NOT_APPLICABLE;
								$$.operatorType = LOGICAL;
							}
							| N_REL_OP
							{
								$$.type = BOOL;
								$$.numParams = NOT_APPLICABLE;
								$$.returnType = NOT_APPLICABLE;
								$$.operatorType = RELATIONAL;
							}
							;
N_ARITH_OP					: T_ADD
							{
							}
							| T_SUB
							{
							}
							| T_MULT
							{
							}
							| T_DIV
							{
							}
							;
N_REL_OP					: T_LT
							{
							}	
							| T_GT
							{
							}	
							| T_LE
							{
							}	
							| T_GE
							{
							}	
							| T_EQ
							{
							}	
							| T_NE
							{
							}
							;	
N_LOG_OP					: T_AND
							{
							}	
							| T_OR
							{
							}
							;
N_UN_OP	     				: T_NOT
							{
							}
							;
amh6wr's avatar
amh6wr committed
526 527 528 529 530 531 532
%%

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

void printRule(const char* lhs, const char* rhs) 
{
jdev52's avatar
jdev52 committed
533 534
	printf("%s -> %s\n", lhs, rhs);
	return;
amh6wr's avatar
amh6wr committed
535 536
}

537 538
int yyerror(const char* s) 
{
jdev52's avatar
jdev52 committed
539 540 541
	printf("Line %d: %s\n", numLines, s);
	bail();
	return 0;
542 543
}

jdev52's avatar
jdev52 committed
544 545 546 547
void printToken(const char* tokenType, char* lexeme){
    printf("TOKEN: %-8s  LEXEME: %s\n", tokenType, lexeme);
}

amh6wr's avatar
amh6wr committed
548 549
void beginScope() 
{
jdev52's avatar
jdev52 committed
550 551
	scopeStack.push(SYMBOL_TABLE());
	printf("\n___Entering new scope...\n\n");
amh6wr's avatar
amh6wr committed
552 553 554 555
}

void endScope() 
{
jdev52's avatar
jdev52 committed
556 557
	scopeStack.pop();
	printf("\n___Exiting scope...\n\n");
amh6wr's avatar
amh6wr committed
558 559 560 561
}

bool findEntryInAnyScope(const string theName) 
{
jdev52's avatar
jdev52 committed
562 563 564 565 566 567 568 569 570 571 572 573 574
	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
575 576
}

jdev52's avatar
jdev52 committed
577
//Should only call this if it is known that the entry is in the scopestack
578 579
SYMBOL_TABLE_ENTRY getEntryInAnyScope(const string theName){
	SYMBOL_TABLE currentScope = scopeStack.top();
jdev52's avatar
jdev52 committed
580
	SYMBOL_TABLE_ENTRY target; 
581
	if(currentScope.findEntry(theName)){
jdev52's avatar
jdev52 committed
582
		target = currentScope.getEntry(theName);
583 584
	} else {
		scopeStack.pop();
jdev52's avatar
jdev52 committed
585
		target = getEntryInAnyScope(theName);
586 587
		scopeStack.push(currentScope);
	}
jdev52's avatar
jdev52 committed
588
	return target;
589 590
}

amh6wr's avatar
amh6wr committed
591 592
void cleanUp() 
{
jdev52's avatar
jdev52 committed
593 594 595 596 597 598
	if (scopeStack.empty()){
		return;
	} else {
		scopeStack.pop();
		cleanUp();
	}
amh6wr's avatar
amh6wr committed
599 600 601 602
}

void prepareToTerminate()
{
jdev52's avatar
jdev52 committed
603 604
	cleanUp();
	cout << endl << "Bye!" << endl;
amh6wr's avatar
amh6wr committed
605 606 607 608
}

void bail()
{
jdev52's avatar
jdev52 committed
609 610
	prepareToTerminate();
	exit(1);
amh6wr's avatar
amh6wr committed
611 612 613 614
}

int main() 
{
jdev52's avatar
jdev52 committed
615 616 617
	do {
		yyparse();
	} while (!feof(yyin));
amh6wr's avatar
amh6wr committed
618

jdev52's avatar
jdev52 committed
619 620
	prepareToTerminate();
	return 0;
Hogan's avatar
Hogan committed
621
}