compiler lab2
compiler lab2
M SANGEETHGOWTHAM
22BLC1386
AIM
● Keywords
● Numbers (integer values)
● Identifiers (variables and function names)
● Operators
● Punctuators
● Boolean literals
DESIGN CONSTRAINTS
Output Format:
● The tokenized representation of each statement will be printed.
● The symbol table will be displayed at the end of the process, listing all unique
identifiers encountered with their respective types.
● A validation message (Valid statement. or Invalid statement.) will be
shown for each processed statement.
Assumptions:
● The language syntax is simplified for the purposes of this lab and does not
include complex constructs like loops, conditionals, or functions.
● Only simple assignments are considered valid statements for this project.
CODE
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <map>
#include <string>
#include <stdexcept>
#include <cctype>
#include <iomanip>
using namespace std;
struct SymbolInfo {
string type;
double value;
};
struct LexicalEntry {
string name;
string type;
string category;
};
stringstream ss(st);
vector<string> split_vector;
string token;
if(!token.empty()){
split_vector.push_back(token);
compiled_stream.push_back(split_vector);
return false;
if (c == '.') {
if (hasDecimal) {
return false;
hasDecimal = true;
}
return true;
if (isNumeric(expression)) {
return stod(expression);
} else if (symbolTable.count(expression)) {
return symbolTable.at(expression).value;
} else {
// Keywords
//Operators
if(pair.second.name == token){
found = true;
break;
if (!found) {
if (isNumeric(token)) {
} else {
}
void processTokens(const vector<vector<string>>&
compiled_stream, const map<int, LexicalEntry>& lexicalTable,
map<string, SymbolInfo>& symbolTable, vector<string>&
tokenStream) {
string lineTokenStream;
if (pair.second.name == token){
if(pair.second.type == "Keyword"){
break;
tokenStream.push_back(lineTokenStream);
if (tokens.size() >= 3) {
if (symbolTable.count(name)) {
} else if (!isNumeric(value_str)) {
} else {
try {
symbolTable[name].value = newValue;
} else {
} else {
if (!tokens.empty()) {
int main() {
vector<vector<string>> compiled_stream;
string txt;
ifstream MyReadFile("tokenise.txt");
if (!MyReadFile.is_open()) {
return 1;
}
while (getline(MyReadFile, txt)) {
generateLexicalTable(compiled_stream, lexicalTable);
cout << setw(5) << "ID" << setw(10) << "Name" << setw(15) <<
"Type" << setw(15) << "Category" << endl;
try{
return 1;
cout << pair.first << "\t" << pair.second.type << "\t" <<
pair.second.value << endl;
}
cout << endl;
return 0;
INPUT FILE
OUTPUT