string CompiledModelGenerator::substituteTerms(const string& reactionName, const string& inputEquation, bool bFixAmounts) { string equation = cleanEquation(inputEquation); if (equation.size() < 1) { return string("0"); } Scanner s; stringstream ss; ss<<equation; s.AssignStream(ss); s.startScanner(); s.nextToken(); CodeBuilder sb; try { while (s.token() != CodeTypes::tEndOfStreamToken) { substituteToken(reactionName, bFixAmounts, s, sb); s.nextToken(); } } catch (const Exception& e) { throw Exception(e.Message()); } return sb.ToString(); }
// final -> Num | "(" Expression ")" | m [ Expression ] void Parser:: Final() { switch (scanner.nextToken()) { case T_OPENPAREN: // ( { scanner.eatToken(T_OPENPAREN); switch(scanner.nextToken()) { case T_M: case T_NUMBER: case T_OPENPAREN: Expression(); case T_EOF: break; } scanner.eatToken(T_CLOSEPAREN); break; } case T_M: // m { scanner.eatToken(T_M); scanner.eatToken(T_OPENBRACKET); switch(scanner.nextToken()) { case T_M: case T_NUMBER: case T_OPENPAREN: Expression(); case T_EOF: break; } scanner.eatToken(T_CLOSEBRACKET); break; } case T_NUMBER: // num { scanner.eatToken(T_NUMBER); num_parser = scanner.getNumberValue(); break; } case T_EOF: break; default: { parseError(scanner.lineNumber(), scanner.nextToken()); break; } } }
RationalExpression::RationalExpression(Scanner& s) { this->numerator = StringToInteger(s.nextToken()); string lookahead = s.nextToken(); if (lookahead == "/") { this->denominator = StringToInteger(s.nextToken()); } else { s.saveToken(lookahead); this->denominator = 1; } reduce(); }
// StatementS -> Statement Term void Parser::Statements() { Statement(); if (scanner.nextToken() == T_SEMICOLON) Term(); }
int main(int argc, char* argv[]) { if (argc == 2 && (strcmp(argv[1], "-s") == 0)) { Scanner scanner; while (scanner.nextToken() != T_EOF) { std::cout << tokenToString(scanner.nextToken()) << " "; scanner.eatToken(scanner.nextToken()); } std::cout<<std::endl; } else if (argc == 2 && (strcmp(argv[1], "-e") == 0)) { Parser parser(true); parser.parse(); } else { Parser parser(false); parser.parse(); } return 0; }
int main( int /* argc */, char** /* argv */ ) { Scanner scanner = Scanner(); SymbolTable st = SymbolTable(); Token *currTok; currTok = scanner.nextToken(); while( 1 ) { std::cout << currTok->tokenCodeToString(); if( currTok->getDataType() == dt_OP ) std::cout << "(" << currTok->opCodeToString() << ")"; else if( currTok->getDataType() != dt_KEYWORD && currTok->getDataType() != dt_NONE ) { std::cout << "(" << currTok->getDataValue().lexeme << ")"; } std::cout << " "; //symtab if( currTok->getTokenCode() == tc_ID || currTok->getTokenCode() == tc_NUMBER ) { SymbolTableEntry *entry = st.lookup( currTok->getDataValue().lexeme ); if(!entry) { entry = st.insert( currTok->getDataValue().lexeme ); currTok->setSymTabEntry( entry ); } currTok->setSymTabEntry( entry ); } if(currTok->getTokenCode() == tc_EOF) break; currTok = scanner.nextToken(); } std::cout << "\n\n"; st.print(); return 0; }
void Parser::parse() { // This is the entry point for the parser, which simply starts parsing // the grammar by calling the function for the start symbol. //set lookahead to nextToken() lookahead = scanner.nextToken(); //call the start symbol Start(); match(T_EOF); }
void Parser::match(token current_token) { if(current_token == lookahead) { scanner.eatToken(current_token); lookahead = scanner.nextToken(); } else { mismatchError(scanner.lineNumber(),current_token , lookahead); } }
// power_Tail -> "**" final power_Tail | Epsilon void Parser::Power_Tail() { switch (scanner.nextToken()) { case T_POWER: // ** { scanner.eatToken(T_POWER); Final(); if (scanner.nextToken() == T_POWER) Power_Tail(); break; } case T_EOF: break; default: { parseError(scanner.lineNumber(), scanner.nextToken()); break; } } }
/** * logics: import, declare, init a token string, ... * patt: iterator for items */ void countTokens() { Scanner scanner; // uppercamelcase for Class cout << "Enter a sentence as a token string: "; scanner.setInput(getLine()); // scanner.setInput(string str) while(scanner.hasMoreToken()) { // bool scanner.hasMoretoken() cout << scanner.nextToken() << " "; // string scanner.nextToken() } cout << endl; }
// Factor_Tail-> "*" power Factor_Tail | "/"" power Factor_Tail | Epsolon void Parser::Factor_Tail() { switch (scanner.nextToken()) { case T_MULTIPLY: // * { scanner.eatToken(T_MULTIPLY); Power(); if (scanner.nextToken() == T_MULTIPLY || scanner.nextToken() == T_DIVIDE) Factor_Tail(); break; } case T_DIVIDE: // / { scanner.eatToken(T_DIVIDE); Power(); if (scanner.nextToken() == T_MULTIPLY || scanner.nextToken() == T_DIVIDE) Factor_Tail(); break; } case T_EOF: break; default: { parseError(scanner.lineNumber(), scanner.nextToken()); break; } } }
//Exp_Tail -> "+" Factor Exp_Tail | "-" Factor Exp_Tail | EPSOLON void Parser::Exp_Tail() { switch (scanner.nextToken()) { case T_PLUS: // + { scanner.eatToken(T_PLUS); Factor(); if (scanner.nextToken() == T_PLUS || scanner.nextToken() == T_MINUS) Exp_Tail(); break; } case T_MINUS: // - { scanner.eatToken(T_MINUS); Factor(); if (scanner.nextToken() == T_PLUS || scanner.nextToken() == T_MINUS) Exp_Tail(); break; } case T_EOF: break; default: { parseError(scanner.lineNumber(), scanner.nextToken()); break; } } }
// Term -> ; Statement Term | Epsolon void Parser::Term() { //cout<< tokenToString(scanner.nextToken()) <<endl; switch (scanner.nextToken()) { case T_SEMICOLON: //; { scanner.eatToken(T_SEMICOLON); Statement(); if (scanner.nextToken() == T_SEMICOLON) Term(); //Term(); break; } case T_EOF: break; default: { parseError(scanner.lineNumber(), scanner.nextToken()); break; } } }
// Factor -> power Factor_Tail void Parser:: Factor() { Power(); switch (scanner.nextToken()) { case T_MULTIPLY: // * { Factor_Tail(); break; } case T_DIVIDE: // / { Factor_Tail(); break; } } }
// Expression -> Factor Exp_Tail void Parser::Expression() { Factor(); switch (scanner.nextToken()) { case T_PLUS: // + { Exp_Tail(); break; } case T_MINUS: // - { Exp_Tail(); break; } } }
TokenSequence* Parser::composeTable(char* filename) { TokenSequence* sequence = new TokenSequence(10, 10); Token* singulum = nullptr; Symboltable* table = new Symboltable(); Scanner* scanner = new Scanner(filename, table); std::cout << "Initialization works\n"; do { std::cout << "debuging *** Parser::composeTable DO-STRT" << std::endl; singulum = scanner->nextToken(); if (singulum != nullptr) { if (singulum->getType() == Syntax::PROH_Z) { std::cerr << "error line " << singulum->getLine() << " column " << singulum->getColumn() << ": syntax error type 1 (unsupported token)\n"; exit(1); } std::cout << "crashing?\n"; sequence->add(singulum, false); } std::cout << "debuging *** Parser::composeTable DO-END" << std::endl; } while(singulum != nullptr); std::cout << "Scanning works\n"; return sequence; }
// Statement -> "m" "[" Expression "]" "=" Expression | print Expression void Parser::Statement() { switch(scanner.nextToken()) { case T_M: { scanner.eatToken(T_M); scanner.eatToken(T_OPENBRACKET); switch(scanner.nextToken()) { case T_M: case T_NUMBER: case T_OPENPAREN: Expression(); case T_EOF: break; } scanner.eatToken(T_CLOSEBRACKET); scanner.eatToken(T_EQUALS); switch(scanner.nextToken()) { case T_M: case T_NUMBER: case T_OPENPAREN: Expression(); case T_EOF: break; } break; } case T_PRINT: { scanner.eatToken(T_PRINT); switch(scanner.nextToken()) { case T_M: case T_NUMBER: case T_OPENPAREN: Expression(); case T_EOF: break; } break; } case T_EOF: break; default: { parseError(scanner.lineNumber(), scanner.nextToken()); break; } } }
// power -> final Power_Tail void Parser::Power() { Final(); if (scanner.nextToken() == T_POWER) Power_Tail(); }