0% found this document useful (0 votes)
9 views

Compiler Lab Assignment-2

The document describes a program that identifies keywords, identifiers, numbers, and literals in C++ code. It explains functions to check for keywords and count tokens in a file, and a main function that calls the token counting function. The program outputs token classifications and counts.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Compiler Lab Assignment-2

The document describes a program that identifies keywords, identifiers, numbers, and literals in C++ code. It explains functions to check for keywords and count tokens in a file, and a main function that calls the token counting function. The program outputs token classifications and counts.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

ASSIGMENT-2

AIM: Program to identify Keywords, Identifiers, Numbers and Literals.

CODE:
#include <iostream>

#include <fstream>

#include <string>

#include <vector>

#include <algorithm>

using namespace std;

bool isKeyword(string word) {

vector<string> keywords = {"int", "float", "double", "char", "if", "else", "for", "while", "do",
"switch", "case", "break", "continue", "return"};

return find(keywords.begin(), keywords.end(), word) != keywords.end();

void countTokens(string filename) {

ifstream inputFile(filename);

string line;

int identifierCount = 0;

int keywordCount = 0;

int numberCount = 0;

int literalCount = 0;

while (getline(inputFile, line)) {

string token;

for (char c : line) {

if (isalnum(c) || c == '_') {

token += c;

} else {

if (!token.empty()) {

if (isKeyword(token)) {
cout << "Keyword: " << token << endl;

keywordCount++;

} else if (isdigit(token[0])) {

cout << "Number: " << token << endl;

numberCount++;

} else {

cout << "Identifier: " << token << endl;

identifierCount++;

token.clear();

if (c == '"' || c == '\'') {

size_t endQuotePos = line.find(c, line.find(c) + 1);

if (endQuotePos != string::npos) {

cout << "Literal: " << line.substr(line.find(c), endQuotePos - line.find(c) + 1) << endl;

literalCount++;

if (!token.empty()) {

if (isKeyword(token)) {

cout << "Keyword: " << token << endl;

keywordCount++;

} else if (isdigit(token[0])) {

cout << "Number: " << token << endl;

numberCount++;

} else {

cout << "Identifier: " << token << endl;

identifierCount++;

}
}

inputFile.close();

int main() {

string filename = "input.txt";

countTokens(filename);

return 0;

INPUT:

int main() {
int num4 = 88;
float num2 = 3.14;
char ch = 'A';
if (num1 > 0) {
cout << "Positive number" << endl;
} else {
cout << "Non-positive number" << endl;
}
for (int i = 0; i < 10; ++i) {
cout << "Iteration " << i << endl;
}
return 0;
}
OUTPUT:

Result Explanation:
isKeyword function: This function takes a string argument and checks if it
matches any of the predefined C++ keywords stored in the keywords vector. It
returns true if the word is a keyword and false otherwise.

countTokens function: This function reads the contents of a file named


"input.txt" line by line and tokenizes each line to count and classify the tokens.
It initializes counters for identifiers, keywords, numbers, and literals.

For each line, it tokenizes the line character by character.

It identifies tokens based on whether they are keywords (using the isKeyword
function), numbers (if the first character is a digit), or identifiers.

It also identifies literals enclosed in single or double quotes.

main function: It simply calls the countTokens function with the filename
"input.txt" and then exits.

Expected results:
The program will read the content of "input.txt" and tokenize each line.
For each token, it will print its type (identifier, keyword, number, or literal)
along with the token itself.
At the end, it will print out the total counts of each token type.

You might also like