[go: up one dir, main page]

0% found this document useful (0 votes)
55 views9 pages

Compiler Construction: Bahria University, Islamabad

The document is a lab journal describing Task #1, which is to write a program to generate recursive descent parser functions and call the function exp() in main(). The program includes function definitions for Lexical(), Hasnat_E(), Hasnat_T(), and Hasnat_F() to perform lexical analysis and parsing. Lexical() handles tokenization, and the other functions implement a recursive descent parser to check if the input matches the grammar rules for expressions (Hasnat_E()), terms (Hasnat_T()), and factors (Hasnat_F()). The main() function calls Hasnat_E() to parse the input file "hasnat.txt" and prints "true" if it matches the grammar.
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)
55 views9 pages

Compiler Construction: Bahria University, Islamabad

The document is a lab journal describing Task #1, which is to write a program to generate recursive descent parser functions and call the function exp() in main(). The program includes function definitions for Lexical(), Hasnat_E(), Hasnat_T(), and Hasnat_F() to perform lexical analysis and parsing. Lexical() handles tokenization, and the other functions implement a recursive descent parser to check if the input matches the grammar rules for expressions (Hasnat_E()), terms (Hasnat_T()), and factors (Hasnat_F()). The main() function calls Hasnat_E() to parse the input file "hasnat.txt" and prints "true" if it matches the grammar.
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/ 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