Compiler Design Lab Manual
Compiler Design Lab Manual
(CSE382)
Section : ………………………………………………………..
TABLE OF CONTENTS
1 Bonafide Certificate 3
3 Course Plan 5
4 Introduction 11
Experiments
BONAFIDE CERTIFICATE
________________________________
____________
REGISTER NUMBER
Name: Reg No :
Class : Faculty :
Marks Faculty
S.No Date Experiment
(100) Signature
Implementation of Operator
7
precedence Parsing Algorithm
COMPILER
6
COURSE PLAN – ODD SEMESTER 2017-2018
Course Credit 2
COURSE DESCRIPTION
PREREQUISITES
PSOs DESCRIPTION
7
PSO2 Professional Skills: The ability to apply modern tools and strategies in
software project development using modern programming environments to
deliver a quality product for business accomplishment.
PSO3 Communication and Team Skill: The ability to exhibit proficiency in oral and
written communication as individual or as part of a team to work effectively
with professional behaviors and ethics.
PROGRAMME OUTCOMES
POs DESCRIPTION
PO2 Ability to identify, formulates, analyze and solve complex computing problems.
PO3 Capability to design and develop computing systems to meet the requirement of
industry and society with due consideration for public health, safety and
environment.
PO4 Ability to apply knowledge of design of experiment and data analysis to derive
solutions in complex computing problems and society with due consideration for
public health, safety and environment.
PO5 Ability to develop and apply modeling, simulation and prediction tools and
techniques to engineering problems.
PO6 Ability to assess and understand the professional, legal, security and societal
responsibilities Relevant to computer engineering practice.
PO10 Ability to communicate effectively with technical community and with society.
8
PO11 Demonstrating and applying the knowledge of computer engineering and
management principles in software project development and in multidisciplinary
areas.
PO12 Understanding the need for technological changes and engage in life-long learning.
COURSE OUTCOMES
CO1: Develop efficient algorithms with good space and time complexity for Regular
CO2: Design and implement solutions for lexical analysis and syntax analysis problems
Community.
CO1 S M M S M
CO2 S M M S M
CO3 M L L
9
LIST OF EXPERIMENTS
Number Cumulative
S.No Experiment Details of Number of
Periods Periods
CYCLE I
CYCLE II
ADDITIONAL EXPERIMENTS:
1. Peephole optimization
ASSESSMENT METHOD:
10
S.No Assessment Split up
Viva voce(10)
12
Ex .No 1 Implementation of a Lexical Analayzer
Aim:
To separate the tokens from the given source program
Lexical Analyzer
Lexical analysis is the first phase of a compiler. Its job is to turn a raw byte or character
input stream coming from the source file into a token stream by chopping the input into
pieces and skipping over irrelevant details. The primary benefits of doing so include
significantly simplified jobs for the subsequent syntactical analysis, which would otherwise
have to expect whitespace and comments all over the place. The job of the lexical analysis is
also to classify input tokens into types like integer or identifier or while-keyword or opening
bracket. Correlate errors messages from the compiler with the source program (eg,keep track
of the number of lines). The interaction with the parser is usually done by making the lexical
analyzer be a sub-routine of the parser. Another benefit of the lexical analysis phase is that it
greatly compresses the input by about 80%
Algorithm:
Step 5: If the first character in the string is # then print that string as header file
Step 6: If the string matches with any of the keywords print that string is a keyword
Step 7: If the string matches with operator and special symbols print the corresponding
message
SAMPLE INPUT:
#include<stdio.h>
void main()
{
int a;
double b;
char c;
printf("%d %b %c",a,b,c);
}
SAMPLE OUTPUT
#include<stdio.h> header file
void Keyword
main Keyword
( Left Parenthesis
) Right Parenthesis
{ Open Brace
int Keyword
a Identifier
; Semicolon
b Identifier
; Semicolon
char Keyword
c Identifier
14
( Left Parenthesis
%c Control string
, Comma
a Identifier
, Comma
a Identifier
, Comma
) Right Parenthesis
; Semicolon
} Close Brace
Viva Questions
Evaluation
Result
15
Ex .No 2 Simulation of Symbol Table
Aim
To create and print a symbol table that contains the name, type and size of the identifier.
Symbol Table
16
Step 3: Read word by word (using fscanf function) from the file and perform the following
operations until the end of file is reached.
a) Compare the word with the data types that are supported by C.
b) Then store the type, size (according to the data types in C) and name of the Identifier
in the symbol table.
c) Print the symbol table in a neat format.
Step 4: Stop the program execution
SAMPLE INPUT
#include<stdio.h>
void main()
{
int first;
double second;
char c;
printf("%d %b %c",c);
}
SAMPLE OUTPUT
SYMBOL TABLE
first int
second double
c char
Viva Questions
17
Evaluation
Result
18
Ex .No 3 Construction of NFA from a Regular Expression
Aim:
Case ii : If the regular expression is just a character, eg. a, then the corresponding NFA
is :
Case iii: The union operator is represented by a choice of transitions from a node; thus
a|b can be represented as:
Case iv Concatenation simply involves connecting one NFA to the other; eg. ab is:
Case v : The Kleene closure must allow for taking zero or more instances of the letter
from the input; thus a* looks like:
19
SAMPLE INPUT
SAMPLE OUTPUT
a
e e
2 3 b b
Start a
1 6 7 8 9
b
4 5
e e
Viva Questions
Evaluation
Result
20
Ex .No 4 Construction of DFA from a Regular Expression
Aim:
The -closure function takes a state and returns the set of states reachable from it
based on (one or more) -transitions. Note that this will always include the state itself.
We should be able to get from a state to any state in its -closure without consuming
any input.
The function move takes a state and a character, and returns the set of states reachable
by one transition on this character.
The Subset Construction Algorithm is used to find the conversion of NFA to DFA. The
steps are
1. Create the start state of the DFA by taking the -closure of the start state of the NFA.
2. Perform the following for the new DFA state:
For each possible input symbol:
a. Apply move to the newly-created state and the input symbol; this will return a
set of states.
b. Apply the -closure to this set of states, possibly resulting in a new set.
3. Each time we generate a new DFA state, we must apply step 2 to it. The process is
complete when applying step 2 does not yield any new states.
4. The finish states of the DFA are those which contain any of the finish states of the NFA
SAMPLE INPUT
NFA:
21
DFA:
Viva Questions
Evaluation
Aim
Algorithm
To compute FIRST( X ) for all grammar symbols X apply the following rules until no more
terminals or can be added to any FIRST set:
To compute FOLLOW( A ) for all non terminals A apply the following rules until
nothing can be added to any FOLLOW set:
place $ in FOLLOW( S ) where S is the start symbol and $ is the input right marker;
SAMPLE INPUT
23
SAMPLE OUTPUT
Viva Questions
Evaluation
Result
Thus the program for computing first and follow function is done.
24
Ex.No 6 Shift Reduce parsing
Aim
Algorithm
Shift
Move the next input symbol on to the top (right) of the stack.
Reduce
Reduce a handle on the right most part of the stack by popping it off the stack and
pushing the left side of the appropriate production on to the right end of the stack.
Accept
Announce successful completion of parsing.
Error
Signal discovery of a syntax error.
SAMPLE INPUT
EE+E
EE*E
E(E)
E id
performs the following steps when analyzing the input string: id1 + id2 * id3
25
SAMPLE OUTPUT
Evaluation
Thus the program for shift reduce parsing using array implementations is done.
26
Ex .No 7 Implementation of Operator precedence Parsing Algorithm
Aim
Repeat:
Let X be the top stack symbol, and a the symbol pointed to by ip
if $ is on the top of the stack and ip points to $ then return
else
Let a be the top terminal on the stack, and b the symbol pointed to by ip
if a <· b or a =· b then
push b onto the stack
advance ip to the next input symbol
else if a ·> b then
repeat
pop the stack
until the top stack terminal is related by <·
to the terminal most recently popped
else error()
end
Algorithm for Constructing Precedence Functions
1. Create functions fa for each grammar terminal a and for the end of string symbol;
2. Partition the symbols in groups so that fa and gb 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 gb to the group of fa if a <· b, otherwise if a ·> b
place an edge from the group of fa to that of gb;
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 fa and gb
respectively.
27
SAMPLE INPUT
SAMPLE OUTPUT
----------------------------------
+ * < > ]
Viva Questions
Evaluation
Result
28
Ex .No 8 Generation of DAG for the given expression
Aim
To write a C program to construct of DAG for the given expression
Algorithm
The code optimization is required to produce an efficient target code. These are two
important issues that used to be considered while applying the techniques for code
optimization. They are
The semantics equivalences of the source program must not be changed.
The improvement over the program efficiency must be achieved without
changing
the algorithm.
SAMPLE INPUT
SAMPLE OUTPUT
= a +
a - -
+ b *
29
b - -
* c d
c - -
d - -
Viva Questions
1.What is DAG?
2.What is the differences between DAG and syntax tree?
3.Give any applications of DAG.
Evaluation
Result
Thus the program for implementation of DAG has been successfully executed and
output is verified.
30
Ex .No 9 Generation of a code for a given intermediate code
Aim
To write a C program for implementing back end of the compiler which takes
three address codes as input and produces 8086 assembly language instruction.
Algorithm
Modern processors have only a limited number of register. Although some
processors, such as the x86, can perform operations directly on memory locations,
we will for now assume only register operations. Some processors (e.g., the MIPS
architecture) use three-address instructions. However, some processors permit only
two addresses; the result overwrites one of the sources. With these assumptions,
code something like the following would be produced for our example, after first
assigning memory locations to id1 and id2.
LD R1, id2
ADDF R1, R1, #3.0 // add float
RTOI R2, R1 // real to int
ST id1, R2
Algorithm
SAMPLE INPUT
SAMPLE OUTPUT
MOV R0,y
MOV R1,t1
31
MUL R0,R1
MOV +t1,R0
JGT t2num label#6
JMP label#8
MOV R0,x
MOV R1,x
ADD R0,R1
MOV +y,R0
OUT x
MOV printz
Viva Questions
Evaluation
Result
Thus the program generation of code from three address code is done.
32
Ex .No 10 Use LEX tool to implement a lexical analyzer
Aim
To write a program to implement the Lexical Analyzer using lex tool.
LEX Tool
A language for specifying lexical analyzer. There is a wide range of tools for
construction of lexical analyzer. The majority of these tools are based on regular
expressions. The one of the traditional tools of that kind is lex. The lex is used in the manner
depicted. First, a specification of a lexical analyzer is prepared by creating a program prgm.l in the
lex language. Then prgm.l is run through the lex compiler to produce a c program lex.yy.c. The
program lex.yy.c consists of a tabular representation of a transition diagram constructed from the
regular expressions of prgm.l, together with a standard routine that uses the table to recognize
lexemes. The actions associated with regular expressions in prgm.l are pieces of c code and are
carried over directly to lex.yy.c. Finally, lex.yy.c is run through the c compiler to produce an object
program a.out, which is the lexical analyzer that transforms an input stream in to a sequence of
tokens.
Algorithm
33
5. Auxiliary procedure.
6. The declaration section includes declaration of variables, main test, constants and
regular
7. Definitions.
8. Translation rule of lex program are statements of the form
9. P1{action}
10. P2{action}
11. …..
12. …..
13. Pn{action}
14. Write program in the vi editor and save it with .1 extension.
15. Compile the lex program with lex compiler to produce output file as lex.yy.c.
16. Eg. $ lex filename.1
17. $gcc lex.yy.c-11
18. Compile that file with C compiler and verify the output.
SAMPLE INPUT
#include<stdio.h> void
main()
{
int a,b,c;
printf("enter the value for a,b");
scanf("%d%d",&a,&b)';
c=a+b;
printf("the value of c:%d",&c);
}
SAMPLE OUTPUT
[3cse01@localhost ~]$ lex ex3.l [3cse01@localhost
~]$ cc lex.yy.c [3cse01@localhost ~]$ ./a.out
#include<stdio.h> is a preprocessor directive void is
a keyword
function main( block
a is an identifier b is an
34
identifier c is an
identifier function printf(
"enter the value for a,b" is a string
function scanf(
"%d%d" is a string
& is a operator a is an
identifier
& is a operator b is an
identifier c is an
identifier = is a operator
a is an identifier + is a
operator b is an
identifier function printf(
"the value of c:%d" is a string
& is a operator
c is an identifier block
end
Viva Questions
Evaluation
Result
Thus the program to implement the lexical analyzer using lex tool for a subset of C
language was implemented and verified.
35
Ex .No 11 Use LEX and YACC tool to implement a parser
Aim:
To write a YACC program for recognizing valid arithmetic expression that
uses the operator +, - , *, /.
Description :
Creating an Input Language with the lex and yacc Commands. For a program to
receive input, either interactively or in a batch environment, you must provide another
program or a routine to receive the input. Complicated input requires additional code to
break the input into pieces that mean something to the program. You can use the lex and
yacc commands to develop this type of input program. lex Generates a lexical analyzer
program that analyzes input and breaks it into tokens, such as numbers, letters, or
operators. The tokens are defined by grammar rules set up in the lex specification file.
yacc Generates a parser program that analyzes input using the tokens identified by
the lexical analyzer (generated by the lex command and stored in the lex specification file)
and performs specified actions, such as flagging improper syntax.
Writing a Lexical Analyzer Program with the lex Command
The lex command helps write a C language program that can receive and
translate character-stream input into program actions. To use the lex command, you
must supply or write a specification file that contains.
Extended regular expressions Character patterns that the generated lexical
analyzer recognizes.
Action statements C language program fragments that define
Algorithm
SAMPLE INPUT
SAMPLE OUTPUT:
[root@localhost]# lex variable_test.I
[root@localhost]# yacc –d variable_test.y
[root@localhost]# gcc lex.yy.c y.tab.c
[root@localhost]# ./a.out
int a,b; Identifier is a
Identifier is b[root@localhost]#
Viva Questions
1. What is lex?
2. Define YACC.
3 List the various error recovery strategies for a syntax analysis.
Evaluation
Result
Thus the program to implement the lex and yacc tool for a subset of C language was
implemented and verified.
37
EDEPARTFORMGY
Ex. No. 12 Use LEX and YACC to implement Desktop calculator
Aim: To write a lex program to implement a desktop calculator without giving the priority
to operators.
Theory
Declare a variable op which shows the status of various operators. Initialize a
variable
“res” as 0, to hold the result after each operation specified in the input. Scan the input from
left to right. If the pattern of a number is found convert the lexeme to corresponding
number by using the “atof” function. Take the first number to the “res” variable directly. If
an operator is found set the status by op variable. If second number is verified, look at the
status of op and do the corresponding operation with the value in the “res”. This process
will repeat till a new line is recognized.
Algorithm
1. Start
2. Declare variables op and n.
3. Initialize variable res as 0.
4. If -?[0-9]+ pattern is recognized, apply atof function to yytext and store the result
into n. Call the function cal.
5. If -?[0-9]*”.”[0-9]+ pattern is recognized, apply atof function to yytext and store the
result into n. Call the function cal.
6. If “+” is recognized, set op as 1.
7. If “-” is recognized, set op as 2.
8. If “*” is recognized, set op as 3.
9. If “/” is recognized, set op as 4.
10. If “\n” is recognized, return
11. Stop.
Algorithm for cal function
1. Start.
2. Set a switch case according to the value of op.
3. If op=1 do res ←res +n
4. If op=2 do res ← res -n
38
5. If op=3 do res ←res *n
6. If op=4 do res ← res /n
7. Otherwise do res ← n
8. Stop
Viva Questions
1) What is lex?
2) Define YACC.
3)List some of the applications of lex and yacc?
Evaluation
Result
Thus the program to implement the lex and yacc tool for designing a calculator was
implemented and verified.
39