// Tokenizer.cpp Dave Reed 9/30/03 //////////////////////////////////////////////// #include #include "Token.h" #include "Tokenizer.h" using namespace std; Tokenizer::Tokenizer(const string & filename) : ifstr(filename.c_str()) // constructor: reads first token into // nextToken buffer (setting remaining flag as a result) { if (!ifstr) { cout << "Input file not found." << endl; exit(1); } remaining = ReadToken(nextToken); } bool Tokenizer::TokensRemain() const // returns true if tokens remain to be processed (based on remaining flag) { return remaining; } Token Tokenizer::GetToken() // returns next token from input stream (already buffered in nextToken) // and reads ahead, storing the nextToken and updating the remaining flag { Token retValue = nextToken; remaining = ReadToken(nextToken); return retValue; } Token Tokenizer::PeekAhead() const // returns next token from input stream (already buffered in nextToken) // but does not remove it from the stream { return nextToken; } bool Tokenizer::ReadToken(Token & t) // private member function: reads a token t from input stream instr // returns whether the read was successful { t.SetValue(""); t.SetType(UNKNOWN); string temp; if (ifstr >> temp) { t.SetValue(temp); if (temp == "begin" || temp == "end" || temp == "output" || temp == "if" || temp == "while") { t.SetType(KEYWORD); } else if (temp == "+" || temp == "-" || temp == "=" || temp == "<" || temp == "<=" || temp == ">" || temp == ">=" || temp == "==" || temp == "!=") { t.SetType(OPERATOR); } else if (temp[0] == '\"') { char ch; while ((temp.length() == 1 || temp[temp.length()-1] != '\"') && ifstr.get(ch) && ch != '\n') { temp = temp + ch; } t.SetValue(temp); if (temp[temp.length()-1] == '\"') { t.SetType(STRING); } } else if (isdigit(temp[0])) { bool integer = true; for (int k = 1; k < temp.length(); k++) { if (!isdigit(temp[k])) { integer = false; } } if (integer) { t.SetType(INTEGER); } } else if (isalpha(temp[0]) && (temp.length() == 1 || (temp.length() == 2 && isdigit(temp[1])))) { t.SetType(IDENTIFIER); } return true; } return false; }