Compiler
Compiler
Total Marks: 04
Obtained Marks:
Compiler Construction
Assignment # 01
Student Name:
Reg Number:
Instructions: Copied or shown assignments will be marked zero. Late submissions are not
entertained in any case.
Create a lexical analyzer to tokenize a basic programming language named C--. The lexer
should identify various components such as identifiers, keywords, constants (both integers
and floating-point numbers), and operators all following specific token definitions.
Implement C++ or Java program to properly parse input programs, while ignoring whitespace
and managing comments. The output should display the identified tokens and their
corresponding lexemes. Provide the source code, and demonstrate the compilation process
along with the program's execution.
Use the following sample input file (input.txt):
int main() {
float x = 10.5;
printf("x is 10\n");
return 0;
}
Note:
1. Change the filename to your ID, e.g. 2073105.doc
2. Upload the .doc on Google Classroom.
3. Submit the hard print (single-sided) in class.
4. Make sure that the output screen does not have black or colored background.
5. Poor indentation and wrong format will result in deduction of marks.
Solution
#include <fstream>
#include <cctype>
#include <string>
#include <unordered_set>
return true;
if (ch == '.') {
decimal_point = true;
} else if (!isdigit(ch)) {
return false;
return decimal_point;
int i = 0;
// Skip whitespace
if (isspace(code[i])) {
i++;
continue;
// Handling comments
while (i < code.length() && code[i] != '\n') i++; // Skip until the end of the line
continue;
i += 2; // Skip '/*'
while (i < code.length() && !(code[i] == '*' && code[i + 1] == '/')) i++;
i += 2; // Skip '*/'
continue;
if (isalpha(code[i])) {
string buffer;
buffer += code[i];
i++;
if (isKeyword(buffer)) {
} else {
continue;
if (isdigit(code[i])) {
string buffer;
buffer += code[i];
i++;
if (is_float) {
} else {
continue;
if (code[i] == '"') {
string buffer;
buffer += code[i];
i++;
cout << "String Literal: \"" << buffer << "\"" << endl;
continue;
// Handling operators
if (isOperator(code[i])) {
i++;
continue;
// Handling delimiters
if (isDelimiter(code[i])) {
i++;
int main() {
ifstream inputFile("input.txt");
if (!inputFile.is_open()) {
inputFile.close();
tokenize(code);
return 0;
int main() {
float x = 10.5;
printf("x is 10\n");
return 0;
}
./lexer