0% found this document useful (0 votes)
4 views

Assignment No- 01

Writing a program for a simple lexical analyzer

Uploaded by

unimpor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Assignment No- 01

Writing a program for a simple lexical analyzer

Uploaded by

unimpor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Assignment No: 01

Assignment Name: Writing a program for a simple lexical analyzer.

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:

Features Put into Practice:

● “isValidIdentifier(const string& str)” : Verifies whether a given string is a valid


variable name or identifier.
● “isValidNumber(const string& str)” : determines if a string is a vaild number using
the function.
● “lexicalAnalyzer(const string& input)” : Performs lexical analysis on the input
string, identifying tokens and printing their classifications.

Classification of Tokens:

● Identifiers: Alphanumeric or underscore characters come first, then alphabetic or


underscore characters.
● Numbers: Are made up solely of digits.
● Operators: + , - , * , / , and = .
● Strings: Double quotations are used around them (").
Code:

#include <iostream>
#include <fstream>
#include <unordered_set>
#include <unordered_map>
#include <vector>
#include <cctype>

using namespace std;

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 == '_';
}

void processToken(const string& token) {


if (token.empty()) return;

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);
}
}

void analyze(string line) {


string token;
for (size_t i = 0; i < line.size(); ++i) {
char c = line[i];
if (isspace(c) || math_operators.find(c) != math_operators.end() || c == ';' || c == ',' || c ==
'(' || c == ')' || c == '{' || c == '}') {
processToken(token);
token.clear();

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);
}

void readFileAndAnalyze(const string& filename) {


ifstream file(filename);
if (!file) {
cerr << "Error: Cannot open file: " << filename << endl;
return;
}

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:

You might also like