Assignment No- 01
Assignment No- 01
Objective:
This lab report is about writing a simple lexical analyzer program in C++ that reads
input, identifies tokens (such as numbers, identifiers, operators, and strings), and
classifies them accordingly. This assignment is to write a software that analyzes the
given input string (input.txt) lexically. Tokens are identified and categorized by the
application using pre-established rules for strings, operators, integers, and identifiers.
Tools:
● C Programming Language
● CodeBloks
Solution Approach:
Classification of Tokens:
#include <iostream>
#include <fstream>
#include <unordered_set>
#include <unordered_map>
#include <vector>
#include <cctype>
unordered_set<string> keywords = {"int", "if", "else", "float", "for", "while", "return", "double"};
unordered_set<char> math_operators = {'+', '-', '*', '/', '='};
unordered_set<string> logical_operators = {"<", ">", "<=", ">=", "!=", "=="};
vector<string> keywords_list;
vector<string> identifiers_list;
vector<string> math_operators_list;
vector<string> logical_operators_list;
vector<string> numerical_values_list;
vector<string> others_list;
bool isIdentifierChar(char c) {
return isalnum(c) || c == '_';
}
if (keywords.find(token) != keywords.end()) {
keywords_list.push_back(token);
} else if (math_operators.find(token[0]) != math_operators.end() && token.size() == 1) {
math_operators_list.push_back(token);
} else if (logical_operators.find(token) != logical_operators.end()) {
logical_operators_list.push_back(token);
} else if (isdigit(token[0])) {
numerical_values_list.push_back(token);
} else if (isIdentifierChar(token[0])) {
identifiers_list.push_back(token);
} else {
others_list.push_back(token);
}
}
if (!isspace(c)) {
token += c;
processToken(token);
token.clear();
}
} else {
if (i < line.size() - 1 && logical_operators.find(string(1, c) + line[i + 1]) !=
logical_operators.end()) {
processToken(token);
token.clear();
token += c;
token += line[i + 1];
processToken(token);
token.clear();
++i;
} else {
token += c;
}
}
}
processToken(token);
}
string line;
while (getline(file, line)) {
analyze(line);
}
file.close();
}
void printSymbolTable() {
auto printCategory = [](const string& category, const vector<string>& list) {
cout << category << ": ";
for (size_t i = 0; i < list.size(); ++i) {
cout << list[i];
if (i != list.size() - 1) cout << ", ";
}
cout << endl;
};
printCategory("Keywords", keywords_list);
printCategory("Identifiers", identifiers_list);
printCategory("Math Operators", math_operators_list);
printCategory("Logical Operators", logical_operators_list);
printCategory("Numerical Values", numerical_values_list);
printCategory("Others", others_list);
}
int main() {
readFileAndAnalyze("input.txt");
printSymbolTable();
return 0;
}
Output: