Download as pdf or txt
Download as pdf or txt
You are on page 1of 39

Department of Computer Science and Engineering

Compiler Design Lab Manual

(CSE382)

Student Name : ……………………………………………………….

Register Number : ……………………………………………………….

Section : ………………………………………………………..
TABLE OF CONTENTS

S.No Topic Page No.

1 Bonafide Certificate 3

2 Experiment Evaluation Summary 4

3 Course Plan 5

4 Introduction 11

Experiments

5 Implementation of a lexical analyzer

6 Simulation of Symbol Table

7 Construction of a NFA from a Regular expression

8 Construction of a DFA from a Regular expression

9 Finding First and Follow of a given grammar.

10 Implementation of shift reduce parsing Algorithm

Implementation of Operator precedence Parsing


11
Algorithm

12 Generation of DAG for the given expression

13 Generation of a code for a given intermediate code

14 Use LEX tool to implement a lexical analyzer

15 Use LEX and YACC tool to implement a parser

Use LEX and YACC tool to implement a desktop


16
calculator
Anand Nagar,Krishnankoil-626126
Srivilliputtur(via),Virudhunagar(Dt),Tamil Nadu

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

BONAFIDE CERTIFICATE

Bonafide record of work done by


of _ in
during even/odd semester in academic year

Staff In-charge Head of the Department

Submitted to the practical Examination held at Kalasalingam University, Krishnankoil on

________________________________
____________

REGISTER NUMBER

Internal Examiner External Examiner


EXPERIMENT EVALUATION SUMMARY

Name: Reg No :

Class : Faculty :

Marks Faculty
S.No Date Experiment
(100) Signature

1 Implementation of a lexical analyzer

2 Simulation of Symbol Table

Construction of a NFA from a


3
Regular expression

Construction of a DFA from a


4
Regular expression

Finding First and Follow of a given


5
grammar.

Implementation of shift reduce


6
parsing Algorithm

Implementation of Operator
7
precedence Parsing Algorithm

8 Generation of DAG for the given


expression

Generation of a code for a given


9
intermediate code

Use LEX tool to implement a lexical


10
analyzer

Use LEX and YACC tool to


11
implement a parser

Use LEX and YACC tool to


12
implement a desktop calculator
INTRODUCTION

COMPILER

Compiler is a program that reads a program written in one language – the


source language – and translates it in to an equivalent program in another language
– the target language.
ANALYSIS-SYNTHESIS MODEL OF COMPILATION
There are two parts to compilation: Analysis and Synthesis. The analysis part
breaks up the source program into constituent pieces and creates an intermediate
representation of source program. The synthesis part constructs the desired target
program from the intermediate representation. Of the two parts, synthesis requires
the most specialize technique.
PHASES OF A COMPILER

A compiler operates in six phases, each of which transforms the source


program from one representation to another. The first three phases are forming the
bulk of analysis portion of a compiler. Two other activities, symbol table
management and error handling, are also interacting with the six phases of compiler.
These six phases are lexical analysis, syntax analysis, semantic analysis, intermediate
code generation, code optimization and code generation.
LEXICAL ANALYSIS
In compiler, lexical analysis is also called linear analysis or scanning. In lexical
analysis the stream of characters making up the source program is read from left to
right and grouped into tokens that are sequences of characters having a collective
meaning.
SYNTAX ANALYSIS
It is also called as Hierarchical analysis or parsing. It involves grouping the
tokens of the source program into grammatical phrases that are used by the compiler
to synthesize output. Usually, a parse tree represents the grammatical phrases of the
source program.
SEMANTIC ANALYSIS
The semantic analysis phase checks the source program for semantic errors

MKCE-DEPARTMERMATION TECHNOLOGY Page 4


and gathers type information for the subsequent code generation phase. It uses the
hierarchical structure determined by the syntax-analysis phase to identify the operators and
operands of expressions and statements. An important component of semantic analysis is
type checking. Here the compiler checks that each operator has operands that are permitted
by the source language specification.
SYMBOL TABLE MANAGEMENT
Symbol table is a data structure containing the record of each identifier, with fields for
the attributes of the identifier. The data structure allows us to find the record for each
identifier quickly and store or retrieve data from that record quickly. When the lexical
analyzer detects an identifier in the source program, the identifier is entered into symbol
table. The remaining phases enter information about identifiers in to the symbol table.
ERROR DETECTION
Each phase can encounter errors. The syntax and semantic analysis phases usually
handle a large fraction of the errors detectable by compiler. The lexical phase can detect
errors where the characters remaining in the input do not form any token of language. Errors
where the token stream violates the structure rules of the language are determined by the
syntax analysis phase.
INTERMEDIATE CODE GENERATION
After syntax and semantic analysis, some compilers generate an explicit intermediate
representation of the source program. This intermediate representation should have two
important properties: it should be easy to produce and easy to translate into target program
CODE OPTIMIZATION
The code optimization phase attempts to improve the intermediate code so that the
faster running machine code will result. There are simple optimizations that significantly
improve the running time of the target program without slowing down compilation too
much.
CODE GENERATION
The final phase of compilation is the generation of target code, consisting normally of
reloadable machine code or assembly code.

6
COURSE PLAN – ODD SEMESTER 2017-2018

Subject with code Compiler Design Lab- CSE382

Course B.Tech (CSE)

Semester / Sec V / (A-C)

Course Credit 2

Course Coordinator Dr.R.Murugeswari

Module Coordinator Mr.S.Sankara Narayanan

Programme Coordinator Dr.K.Kartheeban

COURSE DESCRIPTION

This course provides students with a comprehensive study of how to design


compilers. The students will be able to design the different phases that include lexical
analyzer, syntax analyzer, semantic analyzer, code optimizer and code generator. This course
also emphasis on problem solving and implementation of code and to optimize the code
using a universal subset of the C programming language. Students will also learn how to
construct their own compiler using the model tools .

PREREQUISITES

 Programming Languages Lab/CSE182


 System Software Lab /CSE282

PROGRAMME SPECIFIC OUTCOMEs

PSOs DESCRIPTION

PSO1 Problem-Solving Skills: The ability to apply mathematics, science and


computer engineering knowledge to analyze, design and develop cost
effective computing solutions for complex problems with environmental
considerations.

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.

PSO4 Successful Career and Entrepreneurship: The ability to create a inventive


career path by applying innovative project management techniques to become
a successful software professional, an entrepreneur or zest for higher studies.

PROGRAMME OUTCOMES

POs DESCRIPTION

PO1 Ability to apply knowledge of mathematics, science and computer engineering to


solve computational problems.

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.

PO7 Ability to understand the impact of computing solutions in economic,


environmental and societal context for sustainable development.

PO8 Applying ethical principles and commitment to ethics of IT and software


profession.

PO9 Ability to work effectively as an individual as well as in teams.

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

Expression and Parsing problem.

CO2: Design and implement solutions for lexical analysis and syntax analysis problems

using C, Lex and Yacc tools

CO3: Work effectively as an individual and communicate effectively with technical

Community.

CO WITH PO & PSO MAPPING

PO PO PO PO PO PO PO PO PO PO PO PO PSO PSO PSO PSO


1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4

CO1 S M M S M

CO2 S M M S M

CO3 M L L

S- Strong Correlation M- Medium Correlation L – Low Correlation

9
LIST OF EXPERIMENTS

Number Cumulative
S.No Experiment Details of Number of
Periods Periods

CYCLE I

1 Implementation of a lexical analyzer 3 6

2 Simulation of Symbol table

3 Construction of a NFA from a Regular expression 3 9

4 Construction of a DFA from a Regular expression 3 12

5 Finding First and Follow of a given grammar.

6 Implementation of shift reduce parsing Algorithm 3 21

CYCLE II

Implementation of Operator precedence Parsing


7 3 24
Algorithm

Generation of DAG for the given expression


8 3 27

9 Generation of a code for a given intermediate code 6 36

10 Use LEX tool to implement a lexical analyzer 3 39

11 Use LEX and YACC tool to implement a parser 3 42

Use LEX and YACC tool to implement a desktop


12 3 45
calculator

ADDITIONAL EXPERIMENTS:
1. Peephole optimization

2. Loops in flow graphs

ASSESSMENT METHOD:

10
S.No Assessment Split up

Regular Lab Exercises (20)


1 Internal Assessment (50 marks)
Model Lab (30)

2 External Assessment (50 marks) Algorithm(10)

End Semester program and


output(30)

Viva voce(10)

RUBRICS FOR INDIVIDUAL EXPERIMENTS

Modules Unacceptable Fair Acceptable Excellent


Level of Very little background Some Introduction is Introduction
understanding information provided or introductory nearly complete, complete, provides
information is incorrect information, missing some all necessary
(1) but still minor points(7) background
missing some principles for the
major points(4) experiment(10)
Algorithm Several major aspects of Algorithm Algorithm is Algorithm is
the exercise are missing, misses one or nearly complete, complete and well-
student displays a lack more major missing some written; provides all
of understanding about aspects of minor points necessary
how to write an carrying out background
algorithm (2) the exercise (6) (10) principles for the
exercise(15)
Design Missing several Written in Written in Program Logic is
principles & important experimental proper logic, proper logic, well written, all
Program Logic details or not written in still missing important details details are covered
proper logic in program some are covered, (40)
(10) important some minor
details details missing
(20) (30)
Output Output contains errors Partial output; Output is good
or are poorly missing some but some minor Output is excellent
11
constructed, important problems or (10)
(2) output could still be
features(4) improved(7)
Discussion/Vi Answered for less than Answered for Answered for Answered for more
va 40% of the questions 60% of the 60% of the than 90% of the
indicating a lack of questions. but questions. Still questions correctly,
understanding of results incomplete need some good understanding
(2) understanding improvements of results is
of results is still (7) conveyed(10)
evident(4)

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 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


13
Step 4: Read the string from the file till the end of file.

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

Step 8 If the string is not a keyword then print that as an identifier.

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

1. What is meant by tokens, patterns and lexemes?

2. State the functions of preprocessors?

3. What is the role of lexical analyzer?

4. What are the phases of the compiler?

5. Mention the possible error recovery actions in lexical analysis.

Evaluation

Assessment Marks Scored


Understanding Problem statement (10)
Efficiency of understanding algorithm (15)
Efficiency of program (40)
Output (15)
Viva (20)
(Technical – 10 and Communications - 10)
Total (100)

Result

Thus the program for token separation in lexical analysis is done.

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

Symbol table is an important data structure created and maintained by compilers in


order to store information about the occurrence of various entities such as variable names,
function names, objects, classes, interfaces, etc. Symbol table is used by both the analysis and
the synthesis parts of a compiler.
A symbol table may serve the following purposes depending upon the language in hand:
 To store the names of all entities in a structured form at one place.
 To verify if a variable has been declared.
 To implement type checking, by verifying assignments and expressions in the source
code are semantically correct.
 To determine the scope of a name (scope resolution).
If a compiler is to handle a small amount of data, then the symbol table can be implemented
as an unordered list, which is easy to code, but it is only suitable for small tables only. A
symbol table can be implemented in one of the following ways:
 Linear (sorted or unsorted) list
 Binary Search Tree
 Hash table
Among all, symbol tables are mostly implemented as hash tables, where the source code
symbol itself is treated as a key for the hash function and the return value is the information
about the symbol.
Algorithm

Step 1: Declare the necessary variables.

Step 2: Open the file that contains the simple C program.

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

SYMBOL NAME TYPE SIZE

first int
second double
c char

Viva Questions

1. What is meant by Symbol table?

2. Mention the various types of symbol table implementation.

3. What are the advantages of using symbol table?

17
Evaluation

Assessment Marks Scored


Understanding Problem statement (10)
Efficiency of understanding algorithm (15)
Efficiency of program (40)
Output (15)
Viva (20)
(Technical – 10 and Communications - 10)
Total (100)

Result

Thus the simple program using symbol table is done.

18
Ex .No 3 Construction of NFA from a Regular Expression

Aim:

To convert the given regular expression to NFA

Algorithm for Construction of NFA

The regular expression is a sequence of symbols and characters expressing a


string or pattern to be searched for within a longer piece of text. Finite automata are
computing devices that accept/recognize regular languages and are used to model
operations of many systems we find in practice. Their operations can be simulated by a very
simple computer program. There are two types such as Non-deterministic Finite Automata
and Deterministic Finite Automata. By using Thompsons construction algorithm to find out a
Finite Automaton from a Regular Expression. The steps are

Case 1 : The NFA representing the empty string is:

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

Enter the regular expression : (a/b) abb

SAMPLE OUTPUT

a
e e
2 3 b b
Start a
1 6 7 8 9
b
4 5
e e

Viva Questions

1.What is meant by Regular Expression ?


2.List the algebraic properties of regular expressions.
3.Compare NFA and DFA.
4.Mention the goals of error handler in a parser?

Evaluation

Assessment Marks Scored


Understanding Problem statement (10)
Efficiency of understanding algorithm (15)
Efficiency of program (40)
Output (15)
Viva (20)
(Technical – 10 and Communications - 10)
Total (100)

Result

Thus the construction of NFA from the regular expression is done.

20
Ex .No 4 Construction of DFA from a Regular Expression
Aim:

To construct the DFA for the given regular expression.

Algorithhm for Construction of DFA

Regular Expression is converted into DFA is accomplished in two steps: first it


converts a RE into a non-deterministic finite automaton (NFA) using Thompsons subset
construction algorithm and then it converts the NFA into a DFA. To perform this operation,
let us define two functions:

 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.

This set of NFA states will be a single state in the DFA.

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

1.What is meant by e-closure ?


2.Define transition diagram
3.Define transition table
4.List the functions used for converting NFA to DFA.

Evaluation

Assessment Marks Scored


Understanding Problem statement (10)
Efficiency of understanding algorithm (15)
Efficiency of program (40)
Output (15)
Viva (20)
(Technical – 10 and Communications - 10)
Total (100)
Result

Thus the construction of DFA from the regular expression is done.


22
Ex.No. 5 First and Follow of a given context free grammar

Aim

To find first and follow of a given context free grammar

Algorithm

Computing the Function FIRST

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:

 if X is terminal, then FIRST( X ) is { X }.

 if X   is a production, then add  to FIRST( X ).

 if X is nonterminal and X  Y1, Y2, ..., Yk is a production, then place a in FIRST( X )


if a is in FIRST(Yi ) for some i, and  is in all of FIRST(Y1 ), FIRST(Y2 ), ...,FIRST(Yi-1 ); that is
Y1, Y2, ..., Yi-1 * . If  is in FIRST(Yj ), all j = 1,2, ..., k then add  to FIRST( X ).

Computing the Function FOLLOW

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;

 if there is a production A  B , then everything in FIRST(  ) except the symbol  is


placed in FOLLOW( B );

 if there is a production A  B , or a rpoduction A  B where FIRST(  ) contains


, then everything in FOLLOW( A ) is also in FOLLOW( B ).

SAMPLE INPUT

Consider the expression grammar (4.11), repeated below:


E →T E’
E’→ + T E’ | ε
T →F T’
T’→* F T’ | ε
F →( E ) | id

23
SAMPLE OUTPUT

FIRST(E) = FIRST(T) = FIRST(F) = {( , id}


FIRST(E’) = {+, ε}
FIRST(T’) = {*, ε}
FOLLOW(E) = FOLLOW(E’) = {) , $}
FOLLOW(T) = FOLLOW(T’) = {+, ), $}

Viva Questions

1.What is meant by left factoring? Give an example.


2.State the rules for computing FIRST.
3.State the rules for computing FIRST.
4.Define predictive parsing

Evaluation

Assessment Marks Scored


Understanding Problem statement (10)
Efficiency of understanding algorithm (15)
Efficiency of program (40)
Output (15)
Viva (20)
(Technical – 10 and Communications - 10)
Total (100)

Result
Thus the program for computing first and follow function is done.

24
Ex.No 6 Shift Reduce parsing

Aim

To implement the shift reduce parsing technique using array implementation.

Algorithm

Shift-reduce parsing is an example of bottom-up-parsing .It attempts to construct a


parse tree for an input string beginning at the leaves and working up towards the root. In
other words, it is a process of “reducing” a string w to the start symbol of a grammar. At
every (reduction) step, a particular substring matching the RHS of a production rule is
replaced by the symbol on the LHS of the production.
A general form of shift-reduce parsing is LR (scanning from Left to right and using
Right-most derivation in reverse) parsing, which is used in a number of automatic parser
generators like Yacc, Bison, etc.
Shift reduce parsing uses a stack to hold grammar symbols (it is convenient to think of
a horizontal stack with its bottom on the left and its top on the right) and has four possible
actions:

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

The shift-reduce parser for the context-free grammar

EE+E
EE*E
E(E)
E  id
performs the following steps when analyzing the input string: id1 + id2 * id3
25
SAMPLE OUTPUT

Stack Input Action

$ id1 + id2 * id3 $ shift


$ id1 + id2 * id3 $ reduce by E  id
$E + id2 * id3 $ shift
$ E+ id2 * id3 $ shift
$ E+id2 * id3 $ reduce by E  id
$ E+E * id3 $ shift
$ E+E* id3 $ shift
$ E+E* id3 $ reduce by E  id
$ E+E*E $ reduce by E  E*E
$ E+E $ reduce by E  E+E
$E $ Accept Viva
Questions

1.What is meant by viable prefixes ?


2.List the possible actions for shift- reduce parsing
3.Define bottom up parsing

Evaluation

Assessment Marks Scored


Understanding Problem statement (10)
Efficiency of understanding algorithm (15)
Efficiency of program (40)
Output (15)
Viva (20)
(Technical – 10 and Communications - 10)
Total (100)
Result

Thus the program for shift reduce parsing using array implementations is done.

26
Ex .No 7 Implementation of Operator precedence Parsing Algorithm
Aim

To Write a C program to implement operator precedence parsing

Operator Precedence Parsing Algorithm

An operator precedence parsing is a bottom-up parsing that interprets an operator-


precedence grammar.

Initialize: Set ip to point to the first symbol of w$

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

Enter the Expression: [a+b*c]

SAMPLE OUTPUT

Operator Precedence Matrix

----------------------------------

+ * < > ]

+ > < < >

* > > < >

< < < < =

> > > > >

[ < < < >

Viva Questions

1.What is meant by operator precedence relation ?


2.Whether operator precedence parsing is the top down or
bottom up parsing?
3.Define operator precedence parsing.

Evaluation

Assessment Marks Scored


Understanding Problem statement (10)
Efficiency of understanding algorithm (15)
Efficiency of program (40)
Output (15)
Viva (20)
(Technical – 10 and Communications - 10)
Total (100)

Result

Thus the operator parsing program is done.

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.

The steps are

1. Start the program


2.Include all the header files
3. Check for postfix expression and construct the in order DAG representation
4. Print the output
5. Stop the program

SAMPLE INPUT

Enter the expression in the folloing form:

Id1 = id2 op id3 op id4

SAMPLE OUTPUT

Enter the Expression: a=b+c*d

The DAG representation of the given expression is:

Node Left Right

= 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

Assessment Marks Scored


Understanding Problem statement (10)
Efficiency of understanding algorithm (15)
Efficiency of program (40)
Output (15)
Viva (20)
(Technical – 10 and Communications - 10)
Total (100)

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

1. Start the program


2. Open the input file
3. Enter the intermediate code as an input to the program
4. Apply conditions for checking the keywords in the intermediate code
5. Analyze each instruction in switch case
6. After generating machine code, copy it to the output file
7. Stop the program

SAMPLE INPUT

Input file: in.txt


[ ]=a.. i 1 *x y t1
+t1 z t2
> t2 num 6 goto 8
+x x x +y y y
print x =y z
print z

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

1.What is intermediate code?


2.How the 3 address code is generated?
3.Define Target Machine.

Evaluation

Assessment Marks Scored


Understanding Problem statement (10)
Efficiency of understanding algorithm (15)
Efficiency of program (40)
Output (15)
Viva (20)
(Technical – 10 and Communications - 10)
Total (100)

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.

Lex FLEX lex.yy.c


Progra
m

lex.yy.c C compiler a.out

Input a.out Sequence


Stream

Algorithm

1. Start the program


2. Lex program consists of three parts.
3. Declaration %%
4. Translation rules %%

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

begins int is a keyword

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

1) What is the function of a hierarchical analysis?


2) What does a semantic analysis do?
3) List the various error recovery strategies for a lexical analysis.
4) List the operations on languages.

Evaluation

Assessment Marks Scored


Understanding Problem statement (10)
Efficiency of understanding algorithm (15)
Efficiency of program (40)
Output (15)
Viva (20)
(Technical – 10 and Communications - 10)
Total (100)

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

how the generated lexical analyzer reacts to extended regular expressions it


recognizes. The format and logic allowed in this file are discussed in the lex
Specification File section of the lex command.

Algorithm

1. Start the program


2. Initialize the name of the each operators
3. Get the expression from the user
36
4. Check condition in related to the initialized operators
5. Repeat until the condition is satisfied
6. Stop the program

SAMPLE INPUT

[root@localhost]# lex arith_id.1


[root@localhost]# yacc –d arith_id.y
[root@localhost]# gcc lex.yy.c y.tab.c
[root@localhost]# ./a.out
x=a+b; Identifier is x
Operator is EQUAL
Identifier is a Operator is
PLUS Identifier is b

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

Assessment Marks Scored


Understanding Problem statement (10)
Efficiency of understanding algorithm (15)
Efficiency of program (40)
Output (15)
Viva (20)
(Technical – 10 and Communications - 10)
Total (100)

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

SAMPLE INPUT and OUTPUT


Input:
1+2
Output:
3.000000
Input:
1*6
Output:
6.000000

Viva Questions

1) What is lex?
2) Define YACC.
3)List some of the applications of lex and yacc?

Evaluation

Assessment Marks Scored


Understanding Problem statement (10)
Efficiency of understanding algorithm (15)
Efficiency of program (40)
Output (15)
Viva (20)
(Technical – 10 and Communications - 10)
Total (100)

Result
Thus the program to implement the lex and yacc tool for designing a calculator was
implemented and verified.

39

You might also like