0% found this document useful (0 votes)
34 views7 pages

Compiler Construction Assignment

1. The document describes a C++ program that performs three operations on a source code file: removal of whitespace, removal of comments, and combining all operations. 2. It removes whitespace by erasing and removing spaces from a string. It removes comments by using flags to ignore text between // and /* */ comment markers. 3. The combined program tokenizes the input, removes comments, removes whitespace from each token, and identifies the token as a constant, keyword, or identifier.

Uploaded by

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

Compiler Construction Assignment

1. The document describes a C++ program that performs three operations on a source code file: removal of whitespace, removal of comments, and combining all operations. 2. It removes whitespace by erasing and removing spaces from a string. It removes comments by using flags to ignore text between // and /* */ comment markers. 3. The combined program tokenizes the input, removes comments, removes whitespace from each token, and identifies the token as a constant, keyword, or identifier.

Uploaded by

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

Compiler Construction

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

// Remove white spaces from the cleaned token


cleanedToken.erase(std::remove_if(cleanedToken.begin(),
cleanedToken.end(), ::isspace), cleanedToken.end());
if (!cleanedToken.empty()) {
if (cleanedToken == "const") {
cout << "Constant: " << cleanedToken << endl;
} else if (find(keywords.begin(), keywords.end(), cleanedToken) !=
keywords.end()) {
cout << "Keyword: " << cleanedToken << endl;
} else {
cout << "Identifier: " << cleanedToken << endl;
}
}
}
}
return 0;
}

You might also like