TE CSE-II
Roll No:
LABORATORY MANUAL
In the subject of
Compiler Construction
Compiler Consrtuction
TE CSE-II
CERTIFICATE
This is to certify that, Mr/Msof Class T.E.
CSE Division,Roll Nohas completed all the practical work in the subject
of satisfactory in the department of Computer Science and
Engineering as prescribed by Solapur University, Solapur, in the semester-II of academic
year 2014-15.
Date:
Subject Incharge
Head of Department
Principal
2
Compiler Consrtuction
TE CSE-II
Name of Subject
INDEX
SR.
NO
.
TITLE OF THE
EXPERIMENT
PAGE
NO.
Date of
Performance
Date of
Evaluation
Mark
s (10)
Sign
1
2
3
4
5
6
7
8
9
10
Compiler Consrtuction
TE CSE-II
Date Date -
Signature of In-charge
EXP. No. 1
Date:
Compiler Consrtuction
TE CSE-II
Algorithm:
Step 1: Declare the necessary variables.
Step 2: Declare an array and store the keywords in that array
Step 3: Open the input file in read open
Step 4: read the string from the file till the end of file.
If the first character in the string is # then print that string as header file
If the string matches with any of the keywords print that string is a
5
Compiler Consrtuction
TE CSE-II
Keyword.
If the string matches with operator and special symbols print the
corresponding message.
Input:
#include<stdio.h>
void main()
{
int a;
double b;
char c;
printf("%d %b %c",a,b,c);
}
Output:
#include<stdio.h> header file
void Keyword
{ Open Brace
Identifier
main Keyword
( Left Parenthesis
int Keyword
a Identifier
) Right Parenthesis
; Semicolon
%c Control string
, Comma
, Comma
, Comma
) Right Parenthesis
a Identifier
a Identifier
Compiler Consrtuction
TE CSE-II
Signature of In-charge
Compiler Consrtuction
TE CSE-II
EXP. No.
Date:
Compiler Consrtuction
TE CSE-II
Compiler Consrtuction
TE CSE-II
10
Compiler Consrtuction
CSE-II
of Computer Science & Engg.
N B TE
Navale
Sinhgad College of Department
Engineering
11
Compiler Consrtuction
TE CSE-II
EXP. No.
Date:
Recursive-descent Parser
Aim: Write a C program to implement the recursive-descent Parser.
THEORY: Q.1 Explain in detail the recursive-descent parser.
Q.2 Explain with example top-down Parsing with backtracking.
12
Compiler Consrtuction
TE CSE-II
13
Compiler Consrtuction
TE CSE-II
CONCLUSION:
14
Compiler Consrtuction
TE CSE-II
EXP. No.
Date:
15
Compiler Consrtuction
TE CSE-II
Compiler Consrtuction
TE CSE-II
Compiler Consrtuction
TE CSE-II
1. Create functions f a for each grammar terminal a and for the end of string symbol;
2. Partition the symbols in groups so that f a and g b are in the same group if a = b
(There can be symbols in the same group even if they are not connected by this relation);
3. Create a directed graph whose nodes are in the groups, next for each symbols a and b
do:
Place an edge from the group of g b to the group of f a if a < b, otherwise if a > b place
an edge from the group of f a to that of g b;
4. If the constructed graph has a cycle then no precedence functions exist. When there are
no cycles collect the length of the longest paths from the groups of f a and g b
respectively.
CONCLUSION:
Compiler Consrtuction
TE CSE-II
EXP. No.
Date:
Shift-Reduce Parser
Aim: Write a C program to implement Shift-Reduce Parser.
THEORY: Q.1 Explain stack implementation of Shift-Reduce parser.
Q.2 what is handle? Explain with suitable example.
19
Compiler Consrtuction
TE CSE-II
20
Compiler Consrtuction
TE CSE-II
21
Compiler Consrtuction
TE CSE-II
CONCLUSION:
22
Compiler Consrtuction
TE CSE-II
EXP. No.
Date:
2) Arithmetic statement
3) Relational statement
THEORY: Q.1 Explain the following intermediate code representations with example.
1. Quadruples
2.Triple
3. Indirect Triple
23
Compiler Consrtuction
TE CSE-II
24
Compiler Consrtuction
TE CSE-II
25
Compiler Consrtuction
TE CSE-II
26
Compiler Consrtuction
TE CSE-II
EXP. NO.
Date:
27
Compiler Consrtuction
TE CSE-II
28
Compiler Consrtuction
TE CSE-II
MUL
STA
T1
LDA
A
29
Compiler Consrtuction
TE CSE-II
T1
STA
T2
CONCLUSION:
30
Compiler Consrtuction
TE CSE-II
EXP. No.
Date:
Calculator using LEX and YACC
31
Compiler Consrtuction
TE CSE-II
Compiler Consrtuction
TE CSE-II
a.out
The executable program file
To run the program directly from the a.out file, type:
$ a.out
In either case, after you start the program, the cursor moves to the line below the $
(command prompt). Then, enter numbers and operators as you would on a calculator. When you
press the Enter key, the program displays the result of the operation. After you assign a value to a
variable, as follows, the cursor moves to the next line. m=4 <enter> .
When you use the variable in subsequent calculations, it will have the assigned value:
m+5 <enter>
9
CONCLUSION:
33
Compiler Consrtuction
TE CSE-II
34