Example #1
0
/*
Function: writeToFile()

Description: Will write the contents of the symbol table to the output stream. 
*/
void symbolTable::writeToScreen() {
	// variables
	bstItr bstItr; 
	symTblItr symbolTableItr; 
	Bst* currentBst;
	int scopeLevel = table.size() - 1; 

	if (scopeLevel == -1) {
		std::cout << "Symbol table is empty!" << std::endl;
	}

	else {
		std::cout << "=== DISPLAYING SYMBOL TABLE ===" << std::endl; 
		for (symbolTableItr = table.begin(); 
				symbolTableItr != table.end(); 
				symbolTableItr++) {
			currentBst = symbolTableItr->getBst();
			for (int i = 0; i < symbolTableItr->getScopeLevel(); i++) {
				std::cout << "\t";
			}

			std::cout << ">> Scope level " << symbolTableItr->getScopeLevel() << " in scope ";
			std::cout << symbolTableItr->getOuterScope() << "." << std::endl;
			if (currentBst->empty() ) {
				std::cout << "\tNo identifiers in this scope." << std::endl; 
			}
			else {
				for (bstItr = currentBst->begin(); bstItr != currentBst->end(); bstItr++) {
					bstItr->second.displayIdentifierAttributes(symbolTableItr->getScopeLevel()); 
				}
			}
		}
		std::cout << "=== END OF SYMBOL TABLE DISPLAY ===" << std::endl; 
	}
}
Example #2
0
/*
Function: symbolTableEntry* searchHelper(std::string symbolToSearch, 
											int& levelSymbolWasFound,
											int searchLevel)

Description: This function will search through the binary seach tree at a 
specific scope level. If the identifier is not located within the current
binary search tree, the function will recursively check the outer scope levels
to see if the identifier can be located. 

Parameters:
std::string symbolToSearch: The name of the identifier to be searched for.
int& levelSymbolWasFound: Will contain the scope level where symbolToSearch
was located.
int searchLevel: Used to resursively check the outer scope levels of a given
scope.  
*/
symbolTableEntry* symbolTable::searchHelper(std::string symbolToSearch, 
											int&levelSymbolWasFound, 
											int searchLevel){
	// variables
	Bst* searchBst = table[searchLevel].getBst();
	bstItr scopeItr;

	// iterate through the current BST in the symbol table
	for (scopeItr = searchBst->begin(); scopeItr != searchBst->end(); scopeItr++) {
		// check for the desired symbol
		if (symbolToSearch == scopeItr->first) {
			levelSymbolWasFound = searchLevel;
			return &(scopeItr->second); 
		}
	}

	// if identifier not found and in global scope, there is nowhere else
	// to search
	if(searchLevel == 0){
		levelSymbolWasFound = -1;
		return NULL;
	}

	// check next outer scope
	else{
		return searchHelper(symbolToSearch, levelSymbolWasFound, 
							table[searchLevel].getOuterScope());
	}
}
Example #3
0
/*
Function: insertNewSymbol(std::string name, int line)

Description: This function will add a new entry to the current scope level
on the stack. 

Parameters:
std::string name: The name of the identifier to be added to the current scope 
level. 
int line: The line number that the associated identifier is located at in the
source program. 
*/
symbolTableEntry* symbolTable::insertNewSymbol(std::string name, int line) {
	symbolTableEntry* newEntry = new symbolTableEntry(name, line);
	Bst* currentVars = table[table.size() - 1].getBst();
	currentVars->insert(entry(name, *newEntry));
	bstItr bitr = currentVars->find(name);
	if(bitr != currentVars->end()){
		return &bitr->second; 
	}
	else{
		return NULL;
	}
}
Example #4
0
/*
Function: insertNewSymbol(std::string name, int line)

Description: This function will add a new entry to the current scope level
on the stack. 

Parameters:
std::string name: The name of the identifier to be added to the current scope 
level. 
int line: The line number that the associated identifier is located at in the
source program. 
*/
symbolTableEntry* symbolTable::insertNewSymbol(std::string name, int line) {
	// allocate a new symbol table entry object and add it to the BST at
	// the current scope 
	symbolTableEntry* newEntry = new symbolTableEntry(name, line);
	Bst* currentVars = table[table.size() - 1].getBst();
	currentVars->insert(entry(name, *newEntry));
	
	// search for and declare a pointer to the symbol table entry object in the
	// BST and NOT the one we just created in this function 
	bstItr bItr = currentVars->find(name);
	if(bItr != currentVars->end()) {
		return &bItr->second; 
	}
	else return NULL; 
}
Example #5
0
/*
Function: writeToFile()

Description: Will write the contents of the symbol table to a file that
will be located in the "outputFiles" directory.  
*/
void symbolTable::writeToFile() {
	// variables
	bstItr bstItr; 
	symTblItr symbolTableItr; 
	Bst* currentBst;
	std::ofstream outFile;
	outFile.open("../outputFiles/symbolTableContents.txt", std::ofstream::out); 
	int scopeLevel = table.size() - 1;  

	if (scopeLevel == -1) {
		outFile << "Symbol table is empty!" << std::endl;
	}

	else {
		outFile << "=== DISPLAYING SYMBOL TABLE ===" << std::endl;
		for (symbolTableItr = table.begin(); 
				symbolTableItr != table.end(); 
				symbolTableItr++) {
			currentBst = symbolTableItr->getBst();
			for (int i = 0; i <= symbolTableItr->getScopeLevel(); i++) {
				outFile << "\t";
			}

			outFile << ">> Scope level " << symbolTableItr->getScopeLevel() << " in scope ";
			outFile << symbolTableItr->getOuterScope() << "." << std::endl;
			if (currentBst->empty() ) {
				outFile << "\tNo identifiers in this scope." << std::endl; 
			}

			else {
				for (bstItr = currentBst->begin(); bstItr != currentBst->end(); bstItr++) {
					for (int i = 0; i < symbolTableItr->getScopeLevel(); i++) {
						outFile << "\t";
					}
					outFile << "\tVariable: " << bstItr->first << std::endl;
					for (int i = 0; i < symbolTableItr->getScopeLevel(); i++) {
						outFile << "\t";
					}
					//outFile << "\tType: " << bstItr->second.getTypeStr() << std:: endl;
				}
			}
		}
		outFile << "=== END OF SYMBOL TABLE DISPLAY ===" << std::endl;
	}

	// file writing complete 
	outFile.close(); 	
}
Example #6
0
/*
Function: searchForSymbol(std::string symbolToSearch)

Description: This function will only search the top of the symbol table
for a given variable. If this variable is found, a pointer to the symbol
table entry will be returned. Otherwise, if the function was unable to locate
the symbol table entry or if the symbol table is empty, the function will 
return NULL. 
*/
symbolTableEntry* symbolTable::searchTopOfStack(std::string symbolToSearch) {
	if (table.size() == 0) {
		return NULL; 
	}

	// variables
	Bst* topBst = table[table.size()-1].getBst();
	bstItr scopeItr;

	// iterate through the top BST in the symbol table
	for (scopeItr = topBst->begin(); scopeItr != topBst->end(); scopeItr++) {
		// check for the desired symbol
		if (symbolToSearch == scopeItr->first) {
			return &(scopeItr->second); 
		}
	}
	return NULL; 
}
Example #7
0
/*
Function: writeToFile()

Description: Will write the contents of the symbol table to the output stream. 
*/
void symbolTable::writeToScreen() {
	// variables
	bstItr bstItr; 
	symTblItr symbolTableItr; 
	Bst* currentBst;
	int scopeLevel = table.size() - 1; 

	if(scopeLevel == -1){
		std::cout << "Symbol table is empty!" << std::endl;
	}

	else {
		for (symbolTableItr = table.begin(); symbolTableItr != table.end(); symbolTableItr++) {
			currentBst = symbolTableItr->getBst();
			for( int i = 0; i <= symbolTableItr->getScopeLevel(); i++ ){
				std::cout << "\t";
			}
			std::cout << "Scope Level " << symbolTableItr->getScopeLevel() << " in Scope ";
			std::cout << symbolTableItr->getOuterScope() << std::endl;
			if (currentBst->empty() ) {
				std::cout << "\tNo identifiers in this scope." << std::endl; 
			}
			else {
				for (bstItr = currentBst->begin(); bstItr != currentBst->end(); bstItr++) {
					for( int i = 0; i < symbolTableItr->getScopeLevel(); i++ ){
						std::cout << "\t";
					}
				
					std::cout << "\tVariable: " << bstItr->first << std::endl;
					for( int i = 0; i < symbolTableItr->getScopeLevel(); i++ ){
						std::cout << "\t";
					}
					std::cout << "\tType: " << bstItr->second.getTypeStr() << std:: endl;
				}
			}
		}
	}
}