Token Scanner::moveP(){ char c; //Skipping Whitespaces while (std::cin.peek() == ' ' || std::cin.peek()=='\t') c = getchar(); while ((c = getchar()) != -1) { switch (c) { case '+': return T_PLUS; case '-': return T_MINUS; case '*': return (std::cin.peek() == '*' && getchar()) ? T_EXP : T_MULTIPLY; case '/': return T_DIVIDE; case '%': return T_MODULO; case '(': return T_OPENPAREN; case ')': return T_CLOSEPAREN; case ';': return T_SEMICOLON; case '\n': line++; return T_NEWLN; case EOF: return T_EOF; default: if (isdigit(c)) { std::string number(1, c); while (isdigit(std::cin.peek())) { number += getchar(); } try{ value = std::stoi(number); } catch (std::exception e){ std::cerr << "Semantic error: number " << number << " out of bounds at line " << line << std::endl; exit(1); } return T_NUMBER; } else scanError(line, c); } } return T_EOF; }
void Scanner::eatToken(token toConsume) { // This function consumes the next token. token foundToken = nextToken(); if( foundToken == toConsume) switch(toConsume) { case T_MINUS: case T_PLUS: case T_MULTIPLY: case T_DIVIDE: case T_SEMICOLON: case T_M: case T_EQUALS: case T_OPENPAREN: case T_CLOSEPAREN: case T_OPENBRACKET: case T_CLOSEBRACKET: std::cin.get(); break; case T_POWER: std::cin.get(); std::cin.get(); break; case T_NUMBER: int temp; number = (std::cin.get() - 48); while ( isdigit(std::cin.peek()) ) { temp = std::cin.get(); temp = temp - 48; number = ((number * 10) + temp); } break; case T_PRINT: std::string print = "print"; std::string cap_print = "PRINT"; char p; for(int i = 0; i < 5; i++) { p = std::cin.get(); if ((print[i] == p) || (cap_print[i] == p)) continue; else scanError(lineNumber(), p); } break; } else { mismatchError(lineNumber(), toConsume, foundToken); } }
void Tst_qofonoTest::testScan() { QOfonoManager manager; QOfonoModem modem; modem.setModemPath(manager.modems()[0]); QOfonoNetworkRegistration netreg; netreg.setModemPath(manager.modems()[0]); QSignalSpy spy_scanFinished(&netreg, SIGNAL(scanFinished())); QSignalSpy spy_scanError(&netreg, SIGNAL(scanError(QString))); netreg.scan(); ::waitForSignal(&netreg, SIGNAL(scanFinished()), 1000 * 500); QCOMPARE(spy_scanError.count(),0); QCOMPARE(spy_scanFinished.count(),1); }
token Scanner::nextToken() { // This is a placeholder token, you will need to replace this code // with code to return the correct next token. char peek; while ( isspace(peek = std::cin.peek()) ) { //check to see if nextToken is whitespace if(peek == '\n') line_Number++; std::cin.get(); //if so, consume } char c; c = std::cin.peek(); switch (c) { case '-': return T_MINUS; case '+': return T_PLUS; case '*': std::cin.get(); char temp; temp = std::cin.peek(); std::cin.putback ('*'); if(temp == '*') return T_POWER; else return T_MULTIPLY; case '/': return T_DIVIDE; case ';': return T_SEMICOLON; case 'M': case 'm': return T_M; case '[': return T_OPENBRACKET; case ']': return T_CLOSEBRACKET; case '(': return T_OPENPAREN; case ')': return T_CLOSEPAREN; case '=': return T_EQUALS; case 'P': case 'p': return T_PRINT; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': return T_NUMBER; case EOF: return T_EOF; default: scanError(lineNumber(),c); break; } }