You are on page 1of 9

Compiler Construction

CSC-323

Lab Journal 7

Student Name: Muhammad Hasnat


Enrolment No: 01-134202-087
Class and Section: BSCS-5B

Department of Computer Science


BAHRIA UNIVERSITY, ISLAMABAD
Lab # 7: Grammar for calculator EBNF form and implementation

Objectives:

To help students in implementation of parsing phase of compiler.

Tools Used:

Submission Date:
Evaluation: Signatures of Lab Engineer:
Task # 1:

Write a program to generate recursive descent parser functions also the function exp() should be
call in main function.

Program/Procedure:
#include<iostream>
#include<conio.h>
#include <fstream>
#include<string>

using namespace std;


ifstream myfile;
int Count = 0;
typedef enum { Hasnat_IF, Hasnat_THEN, Hasnat_ELSE, Hasnat_PLUS, Hasnat_LOB, Hasnat_ROB,
Hasnat_MINUs, Hasnat_MUL, Hasnat_PLUSPLUS, Hasnat_NUM, Hasnat_PLUSEQ, Hasnat_PLUSMINUS,
Hasnat_PLUSGREATER, Hasnat_PLUSLESS, Hasnat_ID } TokenType;
struct Record
{
TokenType Tk;
string name;
int value;

};
Record Lexical();
bool Hasnat_E();
bool Hasnat_T();
bool Hasnat_F();
int main()
{
Record obj1;
myfile.open("hasnat.txt", ios::in);
if (myfile.is_open())
{
while (myfile.good())
{
if (Hasnat_E())
{
cout << "true";
}
}

else {
cout << "Unable to open file";
}
myfile.close();
_getch();
}
Record Lexical()
{
Record obj;
Count++;
obj.value = Count;
char line;
while (1)
{
myfile.get(line);

if (line == ' ') {

}
else if (isdigit(line))
{
string a;

a += line;
myfile.get(line);
do
{

if (isdigit(line))
{
a += line;

}
else
{
break;
}
} while (myfile.get(line));
obj.name = "NUM";
obj.Tk = Hasnat_NUM;
return obj;

}
else if (isalpha(line))
{
string a;

a += line;
myfile.get(line);
do
{
if (isalpha(line) || isdigit(line))
{
a += line;
}
else
{
break;
}
} while (myfile.get(line));
obj.name = "ID";
obj.Tk = Hasnat_ID;
return obj;

else
{

if (line == '+')
{
myfile.get(line);
if (line == '+')
{
obj.name = "++";
obj.Tk = Hasnat_PLUSPLUS;
return obj;
}
else if (line == '=')
{
obj.name = "+=";
obj.Tk = Hasnat_PLUSEQ;
return obj;
}
else if (line == '-')
{
obj.name = "+-";
obj.Tk = Hasnat_PLUSMINUS;
return obj;
}
else if (line == '>')
{
obj.name = "+>";
obj.Tk = Hasnat_PLUSGREATER;
return obj;
}
else if (line == '<')
{
obj.name = "+<";
obj.Tk = Hasnat_PLUSLESS;
return obj;
}
else
{
obj.name = "+";
obj.Tk = Hasnat_PLUS;
return obj;
}

}
else if (line == '*')
{
obj.name = "*";
obj.Tk = Hasnat_MUL;
return obj;
}
else if (line == '(')
{
obj.name = "(";
obj.Tk = Hasnat_LOB;
return obj;
}
else if (line == ')')
{
obj.name = ")";
obj.Tk = Hasnat_ROB;
return obj;
}

}
}
}
bool Hasnat_E() {
Record obj;
do
{
if (Hasnat_T())
{
return false;
}
obj = Lexical();
} while (obj.Tk == '+');
return true;
}
bool Hasnat_T() {
Record obj;
do
{
if (Hasnat_F())
{
return false;
}
obj = Lexical();
} while (obj.Tk == '*');
return true;
}
bool Hasnat_F() {
Record obj;
obj = Lexical();
if (obj.name == "ID")
{
return true;
}
else if (obj.name == "NUM")
{
return true;
}
else if (obj.name == "LOB")
{
if (Hasnat_E())
{
obj = Lexical();
if (obj.name == "ROB")
{
return true;
}
}
return false;
}

Output:

Analysis:

You might also like