void Expression1::parseExpression() { curToken=t.getToken(); trm->parseTrm(); if(curToken == 22) { altNum=2; t.skipToken(); parseExpression(); } else if(curToken == 23) { altNum=3; t.skipToken(); parseExpression(); } }
void Assign::parseAssign() { curToken=t.getToken(); #ifdef ENABLE_DEBUG_PARSE cout<<"curToken in parseAssign-1 "<<curToken<<endl; #endif //Check if current Token is an Identifier, Integer, =, +, - or ; //and call appropriate function if(curToken == 32) { exp.parseExpression(); } else if(curToken == 31) { exp.parseExpression(); } else if(curToken == 14) { t.skipToken(); parseAssign(); } else if(curToken == 22) { t.skipToken(); parseAssign(); } else if(curToken == 23) { t.skipToken(); parseAssign(); } else if(curToken == 12) { t.skipToken(); } else { cout<<"Error-while parsing "<<endl; exit(1); } }
void IN::parseIN() { curToken=t.getToken(); while(curToken != 12){ idl.parseIdListForSS(); curToken=t.getToken(); t.skipToken(); } }
void Program::printProgram() { t.setIndex(-1); t.skipToken(); curToken=t.getToken(); //----------------------------------------------------------------// if(curToken == 1) { cout<<"program"<<endl; t.skipToken(); while(curToken != 2) { ds.printDS(); t.skipToken(); curToken=t.getToken(); } } else { cout<<"error 1"<<endl; exit(1); } cout<<endl; //----------------------------------------------------------------// if(curToken == 2) { cout<<"begin"<<endl; id.insertSpace(); t.skipToken(); while(curToken != 3) { ss.printSS(); t.skipToken(); curToken=t.getToken(); } } else { cout<<"error 2"<<endl; exit(1); } cout<<endl; //----------------------------------------------------------------// if(curToken == 3) { cout<<"end"<<endl; } else { cout<<"error 3"<<endl; } }
void Condition::parseCondition() { curToken=t.getToken(); if(curToken == 20) { altNum=1; comp.parseCompare(); } else if(curToken == 15) { altNum=2; t.skipToken(); parseCondition(); } else if(curToken == 16) { t.skipToken(); parseCondition(); curToken=t.getToken(); if(curToken == 18){ altNum=3; } else if(curToken == 19){ altNum=4; } else{ cout<<"Error-expecting && or ||"<<endl; exit(0); } t.skipToken(); parseCondition(); curToken=t.getToken(); if(curToken == 17) { t.skipToken(); } else { cout<<"Error-expecting ]"<<endl; exit(0); } } else { cout<<"Error. Exiting"<<endl; exit(0); } }
void Decl::parseDecl() { curToken=t.getToken(); if(curToken != 4) { cout<<"Error - expecting int"<<endl; exit(1); } t.skipToken(); idl.parseIdListForDS(); }
void Compare::parseCompare() { curToken=t.getToken(); if(curToken != 20) { cout<<"error in parseCompare-Compare.h"<<endl; exit(0); } t.skipToken(); op.parseOperator(); compOp.parseCompareOperator(); op.parseOperator(); curToken=t.getToken(); if(curToken != 21) { cout<<"error in parseCompare-Compare.h"<<endl; exit(0); } t.skipToken(); }
void Expression::parseExpression() { curToken=t.getToken(); #ifdef ENABLE_DEBUG_PARSE cout<<"curToken in parseExpression "<<curToken<<endl; #endif altNum=1; this->getTrmInstance()->parseTrm(); if(curToken == 22) { altNum=2; t.skipToken(); parseExpression(); } else if(curToken == 23) { altNum=3; t.skipToken(); parseExpression(); } }
void OUT::parseOUT() { curToken=t.getToken(); if(curToken == 11){ t.skipToken(); this->getidListInstance()->parseIDListForSS(); } else { cout<<"Error-expecting write"<<endl; exit(0); } t.skipToken(); curToken=t.getToken(); if(curToken == 12) { cout<<""; } else { cout<<"Error-expecting ;"<<endl; exit(0); } }
void Stmt::parseStmt() { curToken=t.getToken(); #ifdef ENABLE_DEBUG_PRINT cout<<"curToken in parseStmt "<<curToken<<endl; #endif if(curToken == 32) { altNum=1; t.skipToken(); #ifdef ENABLE_DEBUG_PRINT cout<<"calling parseAssign"<<endl; #endif assign.parseAssign(); } else if(curToken == 5) { altNum=2; #ifdef ENABLE_DEBUG_PRINT cout<<"calling parseIF"<<endl; #endif this->getIfInstanceStmt()->parseIF(); } else if(curToken == 8) { altNum=3; #ifdef ENABLE_DEBUG_PRINT cout<<"calling parseLoop"<<endl; #endif this->getLoopInstanceStmt()->parseLoop(); } else if(curToken == 10) { altNum=4; #ifdef ENABLE_DEBUG_PRINT cout<<"calling parseIN"<<endl; #endif in.parseIN(); } else if(curToken == 11) { #ifdef ENABLE_DEBUG_PRINT cout<<"curToken-before calling OUT "<<curToken<<endl; #endif altNum=5; out.parseOUT(); } }
void Operator::parseOperator() { curToken=t.getToken(); if(curToken==31){ altNum=1; no=t.intVal(); t.skipToken(); } else if(curToken==32){ altNum=2; id.parseIDForSS(); } else if(curToken==20) { altNum=3; t.skipToken(); exp->parseExpression(); curToken=t.getToken(); if(curToken != 21){ cout<<"Error-expecting )"<<endl; exit(0); } t.skipToken(); } }
ID* ID::parseIDForSS() { string idName=t.getIdName(); for(int k=0; k<idCount; k++) { if(idName==eIDs[k]->name) { t.skipToken(); return eIDs[k]; } else { cout<<"Error - variable has already been declared"<<endl; exit(0); } } }
void idList::parseIDListForDS() { curToken=t.getToken(); if(curToken == 13){ t.skipToken(); curToken=t.getToken(); if(curToken == 32){ id.parseIDForDS(); } else { cout<<"Error-Expecting an integer value. Exiting"<<endl; exit(0); } } }
void Program::parseProgram() { t.setIndex(-1); t.skipToken(); curToken=t.getToken(); //----------------------------------------------------------------// if(curToken == 1) { t.skipToken(); curToken=t.getToken(); while(curToken != 2) { ds.parseDS(); t.skipToken(); curToken=t.getToken(); } } else { cout<<"error 1"<<endl; exit(1); } cout<<endl; //---------------------------------------------------------------- if(curToken == 2) { t.skipToken(); curToken=t.getToken(); while(curToken != 3) { ss.parseSS(); t.skipToken(); curToken=t.getToken(); } } else { cout<<"error 2"<<endl; exit(1); } cout<<endl; //----------------------------------------------------------------// if(curToken == 3) { t.skipToken(); curToken=t.getToken(); cout<<"End of Parsing. No errors found"<<endl; } else { cout<<"error 3"<<endl; exit(1); } }
/* ParseTreeNode::parse * Parses formatted text data. Current valid formatting is: * (type) child = value; * (type) child = value1, value2, ...; * (type) child = { value1, value2, ... } * (type) child { grandchild = value; etc... } * (type) child : inherited { ... } * All values are read as strings, but can be retrieved as string, * int, bool or float. *******************************************************************/ bool ParseTreeNode::parse(Tokenizer& tz) { // Get first token string token = tz.getToken(); // Keep parsing until final } is reached (or end of file) while (!(S_CMP(token, "}")) && !token.IsEmpty()) { // Check for preprocessor stuff if (parser) { // #define if (S_CMPNOCASE(token, "#define")) { parser->define(tz.getToken()); token = tz.getToken(); continue; } // #if(n)def if (S_CMPNOCASE(token, "#ifdef") || S_CMPNOCASE(token, "#ifndef")) { bool test = true; if (S_CMPNOCASE(token, "#ifndef")) test = false; string define = tz.getToken(); if (parser->defined(define) == test) { // Continue token = tz.getToken(); continue; } else { // Skip section int skip = 0; while (true) { token = tz.getToken(); if (S_CMPNOCASE(token, "#endif")) skip--; else if (S_CMPNOCASE(token, "#ifdef")) skip++; else if (S_CMPNOCASE(token, "#ifndef")) skip++; if (skip < 0) break; if (token.IsEmpty()) { wxLogMessage("Error: found end of file within #if(n)def block"); break; } } continue; } } // #include (ignore) if (S_CMPNOCASE(token, "#include")) { tz.skipToken(); // Skip include path token = tz.getToken(); continue; } // #endif (ignore) if (S_CMPNOCASE(token, "#endif")) { token = tz.getToken(); continue; } } // If it's a special character (ie not a valid name), parsing fails if (tz.isSpecialCharacter(token.at(0))) { wxLogMessage("Parsing error: Unexpected special character '%s' in %s (line %d)", CHR(token), CHR(tz.getName()), tz.lineNo()); return false; } // So we have either a node or property name string name = token; // Check next token to determine what we're doing string next = tz.peekToken(); // Check for type+name pair string type = ""; if (next != "=" && next != "{" && next != ";" && next != ":") { type = name; name = tz.getToken(); next = tz.peekToken(); } // Assignment if (S_CMP(next, "=")) { // Skip = tz.skipToken(); // Create item node ParseTreeNode* child = (ParseTreeNode*)addChild(name); child->type = type; // Check type of assignment list token = tz.getToken(); string list_end = ";"; if (token == "{") { list_end = "}"; token = tz.getToken(); } // Parse until ; or } while (1) { // Check for list end if (S_CMP(token, list_end) && !tz.quotedString()) break; // Setup value Property value; // Detect value type if (tz.quotedString()) // Quoted string value = token; else if (S_CMPNOCASE(token, "true")) // Boolean (true) value = true; else if (S_CMPNOCASE(token, "false")) // Boolean (false) value = false; else if (re_int1.Matches(token) || // Integer re_int2.Matches(token)) { long val; token.ToLong(&val); value = (int)val; } else if (re_int3.Matches(token)) // Hex (0xXXXXXX) { long val; token.ToLong(&val, 0); value = (int)val; //wxLogMessage("%s: %s is hex %d", CHR(name), CHR(token), value.getIntValue()); } else if (re_float.Matches(token)) // Floating point { double val; token.ToDouble(&val); value = (double)val; //LOG_MESSAGE(3, S_FMT("%s: %s is float %1.3f", CHR(name), CHR(token), val)); } else // Unknown, just treat as string value = token; // Add value child->values.push_back(value); // Check for , if (S_CMP(tz.peekToken(), ",")) tz.skipToken(); // Skip it else if (!(S_CMP(tz.peekToken(), list_end))) { string t = tz.getToken(); string n = tz.getName(); wxLogMessage("Parsing error: Expected \",\" or \"%s\", got \"%s\" in %s (line %d)", CHR(list_end), CHR(t), CHR(n), tz.lineNo()); return false; } token = tz.getToken(); } } // Child node else if (S_CMP(next, "{")) { // Add child node ParseTreeNode* child = (ParseTreeNode*)addChild(name); child->type = type; // Skip { tz.skipToken(); // Parse child node if (!child->parse(tz)) return false; } // Child node (with no values/children) else if (S_CMP(next, ";")) { // Add child node ParseTreeNode* child = (ParseTreeNode*)addChild(name); child->type = type; // Skip ; tz.skipToken(); } // Child node + inheritance else if (S_CMP(next, ":")) { // Skip : tz.skipToken(); // Read inherited name string inherit = tz.getToken(); // Check for opening brace if (tz.checkToken("{")) { // Add child node ParseTreeNode* child = (ParseTreeNode*)addChild(name); child->type = type; // Set its inheritance child->inherit = inherit; // Parse child node if (!child->parse(tz)) return false; } } // Unexpected token else { wxLogMessage("Parsing error: \"%s\" unexpected in %s (line %d)", CHR(next), CHR(tz.getName()), tz.lineNo()); return false; } // Continue parsing token = tz.getToken(); } return true; }
void Stmt::printStmt() { curToken=t.getToken(); if(curToken == 32){ cout<<t.getIdName(); t.skipToken(); curToken=t.getToken(); #ifdef ENABLE_DEBUG_PRINT cout<<" curToken-printStmt- Var "<<curToken<<endl; #endif assign.printAssign(); } else if(curToken==5) { cout<<" if "; t.skipToken(); cond.printCondition(); curToken=t.getToken(); while(curToken != 3) { this->getIfInstanceStmt()->printIF(); t.skipToken(); curToken=t.getToken(); #ifdef ENABLE_DEBUG_PRINT cout<<" curToken-printStmt- if "<<curToken<<endl; #endif if(curToken == 6){ cout<<" then "<<endl; t.skipToken(); } else if(curToken == 3) { cout<<" end "; } else if(curToken == 12) { cout<<" ; "<<endl; } } } else if(curToken==8) { cout<<" while "; t.skipToken(); curToken=t.getToken(); while(curToken != 3){ this->getLoopInstanceStmt()->printLoop(); t.skipToken(); curToken=t.getToken(); #ifdef ENABLE_DEBUG_PRINT cout<<" curToken-printStmt- while "<<curToken<<endl; #endif if(curToken == 9){ cout<<" loop "<<endl; t.skipToken(); } else if(curToken == 3) { cout<<" end "; } else if(curToken == 12) { cout<<" ; "<<endl; } } } else if(curToken==10) { cout<<" read "; t.skipToken(); curToken=t.getToken(); while(curToken != 12){ in.printIN(); t.skipToken(); curToken=t.getToken(); #ifdef ENABLE_DEBUG_PRINT cout<<" curToken-printStmt- read "<<curToken<<endl; #endif if(curToken==12) { cout<<"; "<<endl; } else if(curToken==13) { cout<<", "; } else if(curToken==14) { cout<<" = "; } } } else if(curToken==11) { cout<<" write "; t.skipToken(); curToken=t.getToken(); #ifdef ENABLE_DEBUG_PRINT cout<<" curToken-printStmt- write "<<curToken<<endl; #endif out.printOUT(); } }