Parser überliest Token
-
Tach, ich habe folgende Funktion, die einen Parser für einen kleinen Taschenrechner darstellt:
bool GetToken(std::istream& stream, ccToken& token) { char ch; do { if(!stream.get(ch)) return false; } while(isspace(ch) || !isprint(ch)); if(ch=='=') std::cout << "YEEHAA" << std::endl; switch(ch) { case tokAssign: case tokPlus: case tokMinus: case tokMul: case tokDiv: case tokPower: case tokFaculty: case tokLBracket: case tokRBracket: case tokSemicolon: case tokComma: { token.tokType = ccTokenType(ch); #ifdef CC_DEBUG std::cout << "OPERATOR \"" << char(token.tokType) << "\"" << std::endl; #endif return true; }; default: { // numeric literal if(isdigit(ch)) { stream.putback(ch); stream >> token.tokNumValue; token.tokType = tokNumLiteral; #ifdef CC_DEBUG std::cout << "NUMBER \"" << token.tokNumValue << "\"" << std::endl; #endif return true; }; // identifier & biult in commands /*if(isletter(ch)) { token.tokStrValue = ""; stream.putback(ch); while(stream.get(ch) && isletter(ch)) { //std::cout << "LESE ZEICHEN " << ch << std::endl; token.tokStrValue.append(1,ch); }; // built in - print if(!strcmp(token.tokStrValue.c_str(), "print") || !strcmp(token.tokStrValue.c_str(), "define")) { if(!strcmp(token.tokStrValue.c_str(), "print")) { token.tokType = tokPrint; #ifdef CC_DEBUG std::cout << "BUILT IN \"" << token.tokStrValue << "\"" << std::endl; #endif return true; }; if(!strcmp(token.tokStrValue.c_str(), "define")) { token.tokType = tokDefine; #ifdef CC_DEBUG std::cout << "BUILT IN \"" << token.tokStrValue << "\"" << std::endl; #endif return true; }; } else { token.tokType = tokIdentifier; #ifdef CC_DEBUG std::cout << "IDENTIFIER \"" << token.tokStrValue << "\"" << std::endl; #endif return true; }; };*/ }; }; std::cout << "FALSCHES ZEICHEN GEFUNDEN " << ch << " " << stream.tellg() <<std::endl; }Aus mir nicht nachvollziehbaren Gründen überliest diese aber (wenn der entsprechende Code auskommentiert ist) das ASSIGN Token. Ich verstehe einfach nicht warum??? In dem Zustand wie oben funktioniert sie einwandfrei, erkennt nur eben keine IDENTIFIER und BUILTINs. Ich finde den Fehler einfach nicht!
MfG