/** * Function representing the <oelem> productions */ attr RecursiveDescentParser::oelem(ParamList* &pList) { attr retVal; retVal.type = T_ERROR; VariableEntry* vEntry = NULL; if(errorCondition) return retVal; if(token == TK_IDENTIFIER) { #if DEBUG_PARSER std::cout << "<oelem> --> <var>\n"; #endif vEntry = var(); } else if(token == TK_NUM) { #if DEBUG_PARSER std::cout << "<oelem> --> n\n"; #endif //TODO fix memory leak here vEntry is never deleted vEntry = new VariableEntry(T_INT, VA_SIMPLE); vEntry->setAddress( symTab.getNextAddress() ); std::string val = token.getValue(); int litTabIndex = litTab.insert(atoi(val.c_str())); iCode.threeAddressCode(vEntry->getAttr(), TAC_NUM_CONST, litTabIndex); token = scanner->getToken(); } else if(token == TK_CHAR_CONST) { #if DEBUG_PARSER std::cout << "<oelem> --> 'c'\n"; #endif //TODO fix memory leak here vEntry is never deleted vEntry = new VariableEntry(T_CHAR, VA_SIMPLE); vEntry->setAddress( symTab.getNextAddress() ); std::string val = token.getValue(); int litTabIndex = litTab.insert(val[1]); iCode.threeAddressCode(vEntry->getAttr(), TAC_CHAR_CONST, litTabIndex); token = scanner->getToken(); } else { errorHandler(); } if(vEntry) { pList->push_back(vEntry); retVal = vEntry->getAttr(); } return retVal; }
/** * Function representing the <var> productions */ VariableEntry* RecursiveDescentParser::var() { VariableEntry* ve=NULL; if(errorCondition) return ve; if(token == TK_IDENTIFIER) { #if DEBUG_PARSER std::cout << "<var> --> i<var_prime>\n"; #endif TableEntry* te=NULL; if(!symTab.search(token, te)) { std::stringstream errorMsg; errorMsg << "Undeclared variable '" << token.getValue() << "'"; errorHandler(errorMsg.str(), token); } if(te && te->getEntryType()==TableEntry::VARIABLE) { ve = (VariableEntry*)te; } token = scanner->getToken(); attr varP = var_prime(ve); if(ve && ve->getAttribute() == VA_ARRAY && varP.attrib == VA_SIMPLE) { VariableEntry* vEntry = new VariableEntry(ve->getType(), VA_SIMPLE); vEntry->setAddress( symTab.getNextAddress() ); iCode.threeAddressCode(vEntry->getAttr(), TAC_ADDRESS_OF, ve->getAttr(), varP); ve = vEntry; } } else { errorHandler(); } return ve; }