Mid Term Project
Mid Term Project
struct Token {
string value;
string type;
};
// Skip spaces
if (isspace(ch)) {
i++;
continue;
}
// Handle comments
if (ch == '/' && i + 1 < code.length()) {
if (code[i + 1] == '/') {
string comment = "//";
i += 2;
while (i < code.length()) comment += code[i++];
tokens.push_back({comment, "Single-line Comment"});
continue;
} else if (code[i + 1] == '*') {
string comment = "/*";
i += 2;
while (i < code.length() - 1 && !(code[i] == '*' && code[i + 1] ==
'/')) {
comment += code[i++];
}
comment += "*/";
i += 2;
tokens.push_back({comment, "Multi-line Comment"});
continue;
}
}
// Handle operators
if (isOperator(ch)) {
tokens.push_back({string(1, ch), "Operator"});
i++;
continue;
}
// Handle punctuation
if (isPunctuation(ch)) {
tokens.push_back({string(1, ch), "Punctuation"});
i++;
continue;
}
// Handle parentheses
if (isParenthesis(ch)) {
tokens.push_back({string(1, ch), "Parenthesis"});
i++;
continue;
}
if (isKeyword(word))
tokens.push_back({word, "Keyword"});
else if (isNumber(word))
tokens.push_back({word, "Number"});
else
tokens.push_back({word, "Identifier"});
continue;
}
// Unknown character
tokens.push_back({string(1, ch), "Unknown"});
i++;
}
return tokens;
}
// Print tokens
void displayTokens(const vector<Token> &tokens) {
cout << "\nTokens Found:\n";
for (const auto &t : tokens) {
cout << t.value << " -> " << t.type << endl;
}
}
int main() {
string line;
cout << "Enter a line of C++ code:\n";
getline(cin, line);
return 0;
}