Compiler design lab
assessment
Name:- Swastik raj
Register no. :- 22BCE0411
Branch:- Computer science and engineering
core
Faculty name:- Dr. Sathya K
Slot:- L1+L2
CODE:-
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <unordered_set>
#include <unordered_map>
#include <regex>
using namespace std;
unordered_set<string> keywords = {"int", "printf", "void", "main", "if", "then", "else", "endif"};
unordered_set<string> operators = {"+", "-", "*", "/", "=", ">", "<"};
unordered_set<string> delimiters = {",", ";", "(", ")", "{", "}"};
bool isKeyword(const string &word) {
return keywords.find(word) != keywords.end();
bool isOperator(const string &word) {
return operators.find(word) != operators.end();
bool isDelimiter(const char &ch) {
return delimiters.find(string(1, ch)) != delimiters.end();
bool isVariable(const string &word) {
regex variableRegex("[a-zA-Z_][a-zA-Z0-9_]*");
return regex_match(word, variableRegex) && !isKeyword(word);
bool isConstant(const string &word) {
regex constantRegex("-?\\d+");
return regex_match(word, constantRegex);
bool isLiteral(const string &word) {
regex literalRegex("\".*?\"");
return regex_match(word, literalRegex);
void extractTokens(const string &line, unordered_map<string, vector<string>> &tokens) {
string word;
stringstream ss(line);
while (ss >> word) {
if (isLiteral(word)) {
tokens["literals"].push_back(word);
} else if (isKeyword(word)) {
tokens["keywords"].push_back(word);
} else if (isOperator(word)) {
tokens["operators"].push_back(word);
} else if (isVariable(word)) {
tokens["variables"].push_back(word);
} else if (isConstant(word)) {
tokens["constants"].push_back(word);
}
for (char ch : line) {
if (isDelimiter(ch)) {
tokens["delimiters"].push_back(string(1, ch));
void printTokens(const unordered_map<string, vector<string>> &tokens) {
for (const auto &tokenType : tokens) {
cout << tokenType.first << ": ";
for (const auto &token : tokenType.second) {
cout << token << " ";
cout << endl;
int main() {
ifstream inputFile("input.txt");
if (!inputFile.is_open()) {
cerr << "Failed to open input file." << endl;
return 1;
unordered_map<string, vector<string>> tokens;
string line;
bool inComment = false;
while (getline(inputFile, line)) {
// Handle block comments
if (line.find("/*") != string::npos) {
inComment = true;
tokens["Comments"].push_back(line.substr(line.find("/*")));
continue;
if (line.find("*/") != string::npos) {
inComment = false;
tokens["Comments"].push_back(line.substr(0, line.find("*/") + 2));
continue;
if (inComment) {
tokens["Comments"].push_back(line);
continue;
size_t commentPos = line.find("//");
if (commentPos != string::npos) {
tokens["Comments"].push_back(line.substr(commentPos));
line = line.substr(0, commentPos);
extractTokens(line, tokens);
inputFile.close();
cout << "Tokens" << endl;
printTokens(tokens);
return 0;
}
OUTPUT:-
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <unordered_map>
struct Symbol {
std::string name;
std::string type;
std::string scope;
int size;
int address;
};
std::vector<std::string> tokenize(const std::string &str, char delim) {
std::vector<std::string> tokens;
std::stringstream ss(str);
std::string token;
while (getline(ss, token, delim)) {
tokens.push_back(token);
return tokens;
void processLine(const std::string &line, std::vector<Symbol> &symbols, int &address, const
std::string &scope) {
std::string type;
if (line.find("int ") != std::string::npos) type = "int";
else if (line.find("float ") != std::string::npos) type = "float";
else if (line.find("char ") != std::string::npos) type = "char";
else if (line.find("double ") != std::string::npos) type = "double";
else return;
auto tokens = tokenize(line, ' ');
std::string name;
for (const auto &token : tokens) {
if (token.find(type) != std::string::npos) continue;
if (token.find("=") != std::string::npos) {
name = tokenize(token, '=')[0];
break;
} else if (token.find(";") != std::string::npos) {
name = token.substr(0, token.size() - 1);
break;
if (!name.empty()) {
int size = (type == "int") ? 4 : (type == "float" || type == "double") ? 8 : 1;
symbols.push_back({name, type, scope, size, address});
address += size;
void processFunction(const std::string &line, std::vector<Symbol> &symbols, int &address) {
auto tokens = tokenize(line, ' ');
std::string returnType = tokens[0];
std::string funcName = tokens[1].substr(0, tokens[1].find('('));
std::string params = line.substr(line.find('(') + 1, line.find(')') - line.find('(') - 1);
symbols.push_back({funcName, "function", "global", 0, 0});
auto paramTokens = tokenize(params, ',');
for (const auto ¶m : paramTokens) {
auto paramParts = tokenize(param, ' ');
if (paramParts.size() == 2) {
std::string paramType = paramParts[0];
std::string paramName = paramParts[1];
int size = (paramType == "int") ? 4 : (paramType == "float" || paramType == "double") ? 8 : 1;
symbols.push_back({paramName, paramType, "function parameter", size, address});
address += size;
void parseFile(const std::string &filename, std::vector<Symbol> &symbols) {
std::ifstream file(filename);
std::string line;
std::string scope = "global";
int address = 1000;
while (getline(file, line)) {
if (line.find("main") != std::string::npos) {
scope = "main";
} else if (line.find("int ") != std::string::npos && line.find('(') != std::string::npos && line.find(')') !=
std::string::npos) {
processFunction(line, symbols, address);
} else if (line.find("int ") != std::string::npos || line.find("float ") != std::string::npos ||
line.find("char ") != std::string::npos || line.find("double ") != std::string::npos) {
processLine(line, symbols, address, scope);
}
}
file.close();
void printSymbolTable(const std::vector<Symbol> &symbols) {
std::cout << "NAME\tTYPE\tSCOPE\tSIZE\tADDRESS\n";
for (const auto &symbol : symbols) {
std::cout << symbol.name << "\t" << symbol.type << "\t" << symbol.scope << "\t" << symbol.size
<< "\t" << symbol.address << "\n";
int main() {
std::vector<Symbol> symbols;
parseFile("input.txt", symbols);
printSymbolTable(symbols);
return 0;