/* 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; } }
/* 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()); } }
/* 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(); }
/* 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; }
/* 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; } } } } }