0% found this document useful (0 votes)
63 views5 pages

Compiler Construction Lab Quiz 1

This document contains a lab quiz for a Compiler Construction course. It has two questions - the first asks to write code for a lexical analyzer that recognizes certain operators and parenthesis. The second asks to write code that recognizes multiline comments. For each question, the student has provided the required code and output.

Uploaded by

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

Compiler Construction Lab Quiz 1

This document contains a lab quiz for a Compiler Construction course. It has two questions - the first asks to write code for a lexical analyzer that recognizes certain operators and parenthesis. The second asks to write code that recognizes multiline comments. For each question, the student has provided the required code and output.

Uploaded by

Anmol Hamid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Bahria University, Islamabad Campus

Department of Computer Science


Lab Quiz # 1
Class: BS(CS)-5A
(Spring 2021 Semester)

Course: Compiler Construction Date: / /2020


Time Allowed: 30 mins Total Marks: 6.510

Name: _______________________ Enrollment#: ________________________

Question #1: Write a small lexical analyzer code. which recognizes the following patterns and
returns appropriate tokens.
I. Operators, ++, --, +-, //, and parenthesis (,)
II. Multiline comments

Place screenshot of file which you read for the above code.
FILE:

READING FROM FILE:


#include<fstream>
#include<iostream>
#include<conio.h>

using namespace std;

int main()
{
fstream obj;
obj.open("D://5th//Compiler Construction Lab//QUIZ.txt", ios::in);
char ch;
if (!obj)
{
cerr << "Unable to create file";
throw; //throws exception if file not open
}
else //writing in file
{
while (!obj.eof())
{
obj.read((char*)&ch, sizeof(char));
cout << ch;
}
}
_getch();
}
OUTPUT:

• Operators, ++, --, +-, //, and parenthesis (,)

CODE:
#include<fstream>
#include<iostream>
#include<conio.h>

using namespace std;

int main()
{
fstream obj;
obj.open("D://5th//Compiler Construction Lab//QUIZ.txt", ios::in);
char ch;
if (!obj)
{
cerr << "Unable to create file";
throw; //throws exception if file not open
}
else
{
obj.read((char*)&ch, sizeof(char));
while (!obj.eof())
{
if (ch == '+')
{
obj.read((char*)&ch, sizeof(char));
if (ch == '+')
cout << "++ (INCREMENT)\n";
}
else if (ch == '-')
{
obj.read((char*)&ch, sizeof(char));
if (ch == '-')
cout << "-- (DECREMENT)\n";
}
else if (ch == '+')
{
obj.read((char*)&ch, sizeof(char));
if (ch == '-')
cout << "+-\n";
}
else if (ch == '/')
{
obj.read((char*)&ch, sizeof(char));
if (ch == '/')
cout << "// (COMMENT)\n";
}
else if (ch == '(')
{
obj.read((char*)&ch, sizeof(char));
if (ch == ')')
cout << "() (Parenthesis)\n";
}
else
{
obj.read((char*)&ch, sizeof(char)); //for next charc. if
not find DOUBLE same chac.
}
}
}
_getch();
}
OUTPUT:
• Multiline comments
CODE:
#include<iostream>
#include<conio.h>
#include<fstream>

using namespace std;

int main()
{
fstream obj1;
char ch_file;

obj1.open("D://5th//Compiler Construction Lab//QUIZ.txt", ios::in);

if (!obj1)
{
cerr << "--------Unable to open file-----------"; //THROWS THERE IF
FILE NOT OPENS
throw;
}
else
{
obj1.read((char*)&ch_file, sizeof(char));
while (!obj1.eof())
{
if (ch_file == '/')
{
obj1.read((char*)&ch_file, sizeof(char));
while (ch_file != '/')
{
obj1.read((char*)&ch_file, sizeof(char));
}
obj1.read((char*)&ch_file, sizeof(char));
}
cout << ch_file;
obj1.read((char*)&ch_file, sizeof(char));
}
}
_getch();
}
OUTPUT:

You might also like