Compiler Construction Assignment
Compiler Construction Assignment
Assignment 1
Q) Write a program in C++ or Java that reads a source file and performs the followings
operations:
1. Removal of white space:
#include <iostream>
#include <algorithm>
#include <cctype>
#include <string>
using namespace std;
int main(){
// Creating a string containing multiple whitespaces.
string s;
cout<<"Enter sentence: "<<endl;
getline(cin, s);
std::cout << "Sentence before removing whitespaces: " << s<<endl;
// Using the erase, remove_if, and ::isspace functions.
s.erase(std::remove_if(s.begin(), s.end(), ::isspace),
s.end());
cout << "String s after removing whitespaces: " << s;
return 0;
}
2. Removal of comments
#include <iostream>
using namespace std;
string removeComments(string prgm)
{
int n = prgm.length();
string res;
// single line or multiple line comments
bool s_cmt = false;
bool m_cmt = false;
for (int i=0; i<n; i++)
{
if (s_cmt == true && prgm[i] == '\n')
s_cmt = false;
else if (m_cmt == true && prgm[i] == '*' && prgm[i+1] == '/')
m_cmt = false, i++;
else if (s_cmt || m_cmt)
continue;
else if (prgm[i] == '/' && prgm[i+1] == '/')
s_cmt = true, i++;
else if (prgm[i] == '/' && prgm[i+1] == '*')
m_cmt = true, i++;
else res += prgm[i];
}
return res;
}
int main()
{
string s;
cout<<"Enter sentence"<<endl;
getline(cin,s);
cout << "\n\nGiven sentence \n";
cout << s << endl;
cout << "Modified sentence "<<endl;
cout << removeComments(s);
return 0;
}
3. Combined ALL
#include <iostream>
#include <algorithm>
#include <cctype>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
// Function to tokenize a string by spaces
vector<string> tokenize(const string& input) {
vector<string> tokens;
istringstream iss(input);
string token;
while (iss >> token) {
tokens.push_back(token);
}
return tokens;
}
//REMOVE COMMENTS
string removeComments(string prgm) {
int n = prgm.length();
string res;
// single line or multiple line comments
bool s_cmt = false;
bool m_cmt = false;
for (int i = 0; i < n; i++) {
if (s_cmt == true && prgm[i] == '\n')
s_cmt = false;
else if (m_cmt == true && prgm[i] == '*' && prgm[i + 1] == '/')
m_cmt = false, i++;
else if (s_cmt || m_cmt)
continue;
else if (prgm[i] == '/' && prgm[i + 1] == '/')
s_cmt = true, i++;
else if (prgm[i] == '/' && prgm[i + 1] == '*')
m_cmt = true, i++;
else
res += prgm[i];
}
return res;
}
int main() {
vector<string> keywords = {"int", "float", "if", "else", "while", "for", "return"};
while (true) {
string s;
cout << "Enter sentence" << endl;
getline(cin, s);
if (s == "exit") {
break;
}
cout << "Sentence before removing comments: "<<endl << s << endl;
cout<<"Sentence after removing comments:
"<<endl<<removeComments(s)<<endl;
// Tokenize the input sentence
vector<string> tokens = tokenize(s);
// Process each token
for (const string& token : tokens) {
// Remove comments from the token
string cleanedToken = removeComments(token);