Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 21

LAB MANUAL

CSE 353: COMPILER DESIGN LAB

L TP 301
Course Type CORE
Semester 5th
Offered
Academic Year 2018-2019
Slot
Class Room

Faculty Details:
Name Mr. Abhay Kumar
Website link
Designation Asst. Professor
School School of Computing Science and Engineering
Cabin No C-335
Intercom -------
Open Hours Thursday(11:50 AM to 3.10 PM)

Version 1.1 Date: 16/08/2018 Prepared By: Mr. Abhay Kumar


Table of Contents
1. Course details
1.1. Course objective (s)

1.2. Pre-requisites
1.3. Learning Outcomes

1.4. Syllabus & References

2. Experimental Setup details for the course.


3. List of Experiments

4. Experiment details
5.1. Format for continuous assessment
5. Guidelines for continuous assessment
5.1. Format for continuous assessment
5.2. Format for internal end semester assessment
5.2. Format for internal end semester assessment

5.1. Format for continuous assessment

5.2. Format for internal end semester assessment


1. COURSE DETAILS

1.1 Course Objective(s)


The objective of this course is to provide a student with an understanding of the fundamental principles
in compiler design and to provide the skills needed for building compilers for various situations that
one may encounter in a career in Computer Science.
To learn the process of translating a modern high-level language to executable code.
1.2 Pre-requisite
1. Theory of Automata and Formal Languages (CSE206)
2. Data Structure and Algorithm
3. Basics of ‘C’ Programming Language

1.3 Learning Out come


Ability of a compiler construction.
 After the course a student should have an understanding, based on knowledge of the underlying
machine architecture, the limitations and efficiency of various design techniques of compilers
implementation.

1.4 Syllabus and References

Module I Introduction
Introduction to Compiler, Phases and passes, Bootstrapping, Finite state machines and regular
expressions and their applications to lexical analysis, Optimization of DFA-Based Pattern Matchers
implementation of lexical analyzers, lexical-analyzer generator, LEX-compiler, Formal grammars and
their application to syntax analysis, BNF notation, ambiguity, YACC. The syntactic specification of
programming languages: Context free grammars, derivation and parse trees, capabilities of CFG.
Module II Basic Parsing Techniques:
Parsers, Shift reduce parsing, operator precedence parsing, top down parsing, predictive parsers
Automatic Construction of efficient Parsers: LR parsers, the canonical Collection of LR(0) items,
constructing SLR parsing tables, constructing Canonical LR parsing tables, Constructing LALR parsing
tables, using ambiguous grammars, an automatic parser generator, implementation of LR parsing tables.
Module III Syntax-directed Translation:
Syntax-directed Translation schemes, Implementation of Syntax directed Translators, Intermediate code,
postfix notation, Parse trees & syntax trees, three address code, quadruple & triples, translation of
assignment statements, Boolean expressions, statements that alter the flow of control, postfix translation,
translation with a top down parser. More about translation: Array references in arithmetic expressions,
procedures call, declarations and case statements.
Module IV Symbol Tables:
Data structure for symbols tables, representing scope information. Run-Time Administration:
Implementation of simple stack allocation scheme, storage allocation in block structured language. Error
Detection & Recovery: Lexical Phase errors, syntactic phase errors semantic errors.
Module V Code Generation:
Design Issues, the Target Language. Addresses in the Target Code, Basic Blocks and Flow Graphs,
Optimization of Basic Blocks, Code Generator. Code optimization: Machine-Independent Optimizations,
Loop optimization, DAG representation of basic blocks, value numbers and algebraic laws, Global Data-
Flow analysis
References:
1. Aho, Sethi & Ullman, "Compilers: Principles, Techniques and Tools”, Pearson Education
2. V Raghvan, “ Principles of Compiler Design”, TMH
3. Kenneth Louden,” Compiler Construction”, Cengage Learning.
4.. Charles Fischer and Ricard LeBlanc,” Crafting a Compiler with C”, Pearson Education
Mode of Evaluation: Quiz/Program Execution/Written Examination

Mid Term Internal Evaluation { Internal – 50 Marks }


Continuous Assessment [30 Marks]

Lab Experiment and Demonstration [10 Marks]

Viva Voce [10 Marks]

Lab Experiment and Demonstration marks details

Completed Partially
Completed Logically
and written with Unacceptable
Status but partially incorrect
Executed errors/no effort
Executed program
accurately execution
Marks 9 – 10 7-8 5-6 3–4 1–2

Viva – Voce marks details

Good Below
Excellent Very Good Average
Majority of Average
All answers Most of the Few answers
Status the answers Rarely any
are answers are are
are answer is
satisfactory satisfactory satisfactory
satisfactory satisfactory
Marks 9 – 10 7-8 5-6 3–4 1–2

Final Term Internal Evaluation { Internal – 50 Marks }


Continuous Assessment [30 Marks]

Lab Experiment and Demonstration [10 Marks]

Viva Voce [10 Marks]

Lab Experiment and Demonstration marks details

Completed Partially
Completed Logically
and written with Unacceptable
Status but partially incorrect
Executed errors/no effort
Executed program
accurately execution
Marks 9 – 10 7-8 5-6 3-4 1-2
Viva – Voce marks details

Good Below
Excellent Very Good Average
Majority of Average
All answers Most of the Few answers
Status the answers Rarely any
are answers are are
are answer is
satisfactory satisfactory satisfactory
satisfactory satisfactory
Marks 9 – 10 7-8 5-6 3-4 1-2

Continuous assessment for each experiment

 Experiment [5 Marks] File and Viva – Voce [5 Marks]


Experiment Marks details

Completed
Completed Partially Logically
and Unacceptable
Status but partially Written and Incorrect
Executed efforts
Executing Errors Program
perfectly
Marks 5 4 3 2 1

File and Viva-Voce Marks details

File Checked File not


File Checked File partially File Checked
Timely and Checked
Status Timely and checked and Timely and
Average Timely and
Good Viva Good Viva Bad Viva
Viva Bad Viva
Marks 5 4 3 2 1

Course Lab Outcomes Assessment

The laboratory component strongly contributes towards the program outcome Design/development
of solutions-PO (3).This evaluation method will be used for the evaluation of lab and program
outcomes of this course.

Direct Measurement Report

CSE353P Outcome (3) Report Form


Measure– percent of students scoring at least 70%marks in lab.

Target– 70% of students


Mapping between Cos and Pos

S. Course Outcomes Mapped Mapping of Rubrics for


No. Program mapping course outcomes
Outcomes
Implement finite state machines and Exp No. 1,2
PO1, PO2, Mid-term internal evaluation,
1 uses them for lexical scanning.
PO3, PSO3 End-term external evaluation
Use tools such as lex & yacc for Exp No. 4, 6 ,8
2 developing a lexical analyzer & PO5 Mid-term internal evaluation,
parser. End-term external evaluation
Design and implement efficient Exp No. 7,9,10,14,15
3 parsers.
PO1, PO2, End- term internal evaluation,
PO3, PSO3 End-term external evaluation
Recognize Nested programming Exp No. 11
4 PO3 End- term internal evaluation,
structures.
End-term external evaluation
Use efficient data structure for Exp No. 13
5 implementing symbol table. PO3, PSO3 End- term internal evaluation,
End-term external evaluation
Generate intermediate code and Exp No. 12
6 PO1, PO4 End- term internal evaluation,
machine code
End-term external evaluation
353P
CSE-
Code
Course
Program
Outcome→

Lab
Name

Compiler
Course

Lab
Theory
Engineering Knowledge

2
PO1

3=major part of course


Problem analysis

2= addressed significantly
PO2

Used for this course.


1=addressed to small extent
Design/development of solutions

3
PO3

Software Requirements

Hardware Requirements
Conduct investigations of complex problems

1
PO4
Modern tool usage

1
PO5
The engineer and society
PO6
Environment and sustainability
PO7
Ethics
PO8
Individual or team work
PO9
Communication
PO10
Project management and finance
PO11
Life-long Learning
PO12
Ability to design real world applications using high performance computing
systems, computer networks and mobile computing systems
PSO1

2. EXPERIMENTAL SETUP DETAILS FOR THE COURSE


Ability to apply contemporary technologies and tools associated with IOT,
Big Data, grid and cloud computing
PSO2

Ability to integrate the concepts of theoretical computer science, data


2

structure, algorithms and programming in to projects


PSO3

No specific requirements. Any computer Hardware capable of running DOS can be


Turbo C 2.0/ Turbo C++3.0+, Lex or Flex and YACC tools (Unix/Linux utilities)
Ability to develop intelligent software systems by integrating the knowledge
of system sciences and intelligent systems.
PSO4
3. EXPERIMENT LIST

School of Computing Science & Engineering


Course Name- Compiler Design Lab
Course Code- CSE-353

S. N. Title of Lab Experiments


1 WAP in C Simulating a DFA.

2 WAP in C For Optimizing a DFA.

3 Introduction to FLEX tool and simple examples.

4 Generating a lexical analyzer using Flex.

5 Introduction to YACC tool and simple examples.

6 Using Flex and YACC together.

7 Generating Calculator using YACC.

8 Writing a Program in C to generate three address code of an arithmetic statement.

9 Writing a program in C to generate symbol table using Array/Link List.

10 Implementing Heap in C for storing scope information.

11 Write a Program in C for Register allocation problem.

12 WAP in C for Optimizing a given three address code.

VALUE ADDED EXPERIMENT:


1 Design a lexical analyzer and parser for a very simple programming language using flex and
YACC.

2 Implement the code generator algorithm for a simple hypothetical machine in C.


4. EXPERIMENT DETAILS

Experiment No:1
Title Write a C program to simulate a DFA.

Objective To simulate a DFA


Pre-requisite Knowledge of DFA and basics of C programming language
Assumptions 1. Only two input symbols a and b
2. Maximum Number of states =10
3. Only one Initial state
4. State 0 is always the initial state
Algorithm 1. Ask the user to input the number of states
2. Ask the user to provide the number of final states
3. Get set of final states
4. Store set of final states in an array
5. Get transition function from the user
6. Store the transition function and some suitable data structure (2 dimensional array or
Link list)
7. Ask the user to provide the input string
8. Calculate the length of string. Let it be k.
9. set i=1, state=initial state
10. state=transition_function(state, ith character of the string)
11. if i!=k go to step 10
12. If state is in final state string accepted else string rejected
Sample Input the number of states:2
execution Number of Final states:1
Final states:1
Transition function
State 0 input a:0
State 0 input b:1
State 1 input a:0
State 1 input b:1
Provide Input string:aabb
String is accepted

Post Lab ----------------------


Assignment (If
Any)
EXPERIMENT DETAILS
Experiment No:2
Title Write a program to minimize the number of states of a DFA.

Objective To minimize the number of states in a DFA


Pre-requisite Knowledge of equivalence classes
Assumptions 1. Number of inputs =2: a, b
2. Maximum number of states =10

Algorithm
1. Ask the user to input the number of states
2. Ask the user to provide the number of final states
3. Get set of final states
4. Store set of final states in an array
5. Get transition function from the user
6. Store the transition function and some suitable data structure (2 dimensional array or Link list)
7. Divide the final states and non final states in to 2 different equivalence classes.
8. For each equivalence class do the following
8.1 Check for each pair of states(Qi and Qj) in an equivalence class whethe Tr_f(Qi,a) and
Tr_f(Qj,a) are in the same equivalence class for all input symbols a in Ʃ.
8.2 If yes put Qi and Qj in same equivalence class for next iteration
8.3 Else Put Qi and Qj in separate Equivalence classes for next iteration
9. If the equivalence classes created are not same as in previous iteration got step 8
10. The states in same equivalence classes are equivalent. Hence, retain only one of them.
11. Display the modified transition function of minimized automata

Sample Input the number of states:3


Execution Number of Final states:2
Final states:1, 2
Transition function
State 0 input a:0
State 0 input b:1
State 1 input a:0
State 1 input b:2
State 2 input a:0
State 2 input b:1
State 1 and 2 are equivalent
MINIMIZED AUTOMATA:
a b
00 1
10 1

Post Lab ----------------------


Assignment
(If Any)
EXPERIMENT DETAILS
Experiment No:3
Title Write a program using Lex to count the number of characters, words, spaces and lines in a given input
file.
Objective To count the no. of lines, words and characters in a given

Pre-requisite Knowledge of Lex and regular expression to recognize word, end of line etc.
Assumptions
1. The string length is of maximum 50 characters
2. Two words separated by only one white space of tab char.
Algorithm 1. Declare the necessary variables.
2. Define, regular definition in lex to recognize word, end of line, spaces
word [^ \t\n]+
eol [\n]
blank [ ]
3. Define actions with each lexeme.
{blank} bc++;
{word} {wc++;cc+=yyleng;}
{eol} {cc++;lc++;}
. {ECHO;cc++;}
4. Inside main function write the code to open the file which name has accepted by main
function arguments (int argc, char* argv[]) and associate it with laxer input
pointer(yyin).

Sample ./a.out inputfilename


execution Character count = 31
Word count = 8
Line count = 2
Blank count = 6
Post Lab -------------------------------------------------------
Assignment(if
any)
EXPERIMENT DETAILS
Experiment No:4
Title Write a C program for dividing the given input program into lexemes. and also simulate lexical
analyzer for validating operators.
Objective To Generate tokens for integers, operators and identifiers from an arithmetic expression
Pre-requisite Knowledge of pattern, lexeme and token
Assumptions 1. The string length is of maximum 50 characters
2. The length of identifier is maximum 8 char, first character is alphabet after that it may be an
alphabet/digit
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.

i) If the first character in the string is # then print that string as header file

ii) If the string matches with any of the keywords print that string is a keyword

iii) If the string matches with operator and special symbols print the corresponding message

iv) If the string is not a keyword then print that as an identifier.


Post Lab -------------------------------------------------------
Assignment(if
any)

EXPERIMENT DETAILS

Experiment No:5
Title Write a C program to recognize strings under 'a', 'a*b+', 'abb'.

Objective To recognize the string of regular languages.


Pre-requisite Knowledge of regular expression, transition diagram and basics of C programming language
Assumptions 5. Only two input symbols a and b
6. Only one Initial state
7. State 0 is always the initial state
Algorithm 1. Ask the user to input the string of symbol ‘a ‘and ‘b’.
2. Check whether the string is empty or not.
3. By using transition diagram we verify input of the state by Using switch case and where
every case is a state.
4. Choose a character from given string as input symbol and according to that symbol find
the next state.
5. Check whether it is accepting or not accepting to given regular expression.
6. If not accepting then go for next input symbol form string and repeat step-4 to 6 until
the end of string.
7. If the state recognize the given pattern rule.
8. Then print string is accepted under a*/ a*b+/ abb.
9. Else print string not accepted.
Sample Input : Enter a String: aaaabbbbb
execution
Output:
aaaabbbbb is accepted under rule 'a*b+'
Enter a string: cdgs
cdgs is not recognized

Post Lab ----------------------


Assignment (If
Any)

EXPERIMENT DETAILS
Experiment No:6
Title Study the structural and implementation process of lex and YACC tools and prepare a detail
analysis report.

Objective To learn about of lex and YACC tools


Pre-requisite Knowledge of lexical analysis and parsing

Study 1. Study the basics of lex and YACC tools linux manual pages
2. Study commands and how to integrate them in C program
3. Read sample programs and understand these.

Procedure for generating lexical analyzer using lex


Lex Source Program(Lex.l)Lex Compiler lex.yy.c
Lex.yy.cc compilera.out
Input streama.out sequence of tokens

Procedure for generating Parser using YACC


YACC specifications(translate.y)YACCy.tab.c
y.tab.c c compilera.out
inputa.outoutput

Sample Output Execution of sample lex programs


Post Lab ------------------------------------------
Assignment(if
any)
EXPERIMENT DETAILS
Experiment No:7
Title Write a YACC program using lex to evaluate an arithmetic expression involving operators +,-,*
and /.
Objective To learn how to compile and execute a lex program
Pre-requisite Knowledge of lex and C

Assumptions 1. Operators used in arithmetic operations are binary operators namely +, -, *, /, =

2. The numbers used are integers only

3. The first character of an identifier is an alphabet whereas the next characters may
be alphabet/digit.

4. Special characters are not allowed

Algorithm/Experiment Letter  [a-z][A-Z]


Specifications Digit 0|1|2|3|4|5|6|7|8|9
Operator+|-|*|/|=
Integer(Digit)+
IdentifierLetter|(Letter|Digit)+
Using these lexical specifications write a lex program including required C routines. The
program should be able to read a character string and should generate the required tokens
according to the lex specifications.
Sample Input/Out put Provide the arithmetic expression: a=10+c*d
a is an identifier
= is an operator
10 is an integer
+ is an operator
c is an identifier
* is an operator
d is an identifier
Post Lab ---------------------------------------------
Assignment(if any)

EXPERIMENT DETAILS
Experiment No:8
Title Write a lex program for separating integers, floating point numbers and reserve words from a file.
Objective To learn about writing lexical analyzer using flex tool

Pre-requisite File handling in C, command line argument


Assumptions 1. The program for which tokens have to be generated is available in a text file.

2. The name of the file is provided via command line arguments

3. Operators used in arithmetic operations are binary operators namely +, -, *, /, =

4. The numbers used are integers only

5. The first character of an identifier is an alphabet whereas the next characters may be
alphabet/digit.
6. Special characters are not allowed

Experimental Letter  [a-z][A-Z]


specifications Digit 0|1|2|3|4|5|6|7|8|9
Operator+|-|*|/|=
Integer(Digit)+
IdentifierLetter|(Letter|Digit)+
Reserve”MAIN”| “END”|”FOR”
1. Provide the above mentioned lex specifications in the beginning
2. While defining the main function use command line arguments argc and argv
3. Define file related structure.
4. Check whether the file name is provided at command line while executing the program. If not
display error message and exit the program
5. Try to open the file in input mode. If file opening fails display error message and exit the
program.
6. Set the file as source for input.
7. Compile the program with flex. The output will be “lex.yy.c”
8. Compile lex.yy.c with the help of gcc compiler include –l switch for lex libraries.
9. Execute the executable file ./a.out a1.txt
10. A1.txt file will be lexically analyzed

Post Lab -------------------------------------------------------


Assignment(if
any)

EXPERIMENT DETAILS
Experiment No:9
Title Write YACC program using lex to recognize a valid variable, which starts with a letter, followed by any
number of letters or digits
Objective To learn designing parser using YACC
Pre-requisite Knowledge of
 LR Parsing
 YACC
 lex
 Lexical Analysis
Assumptions
1. The numbers are only one digit integer(0-9).
2. Letters form a-zA-Z.
3. The Parser and lexical analyzer has also to be designed

Experiment LEX
Specifications
%{

#include"y.tab.h"

extern yylval;

%}

%%

[0-9]+ {yylval=atoi(yytext); return DIGIT;}


[a-zA-Z]+ {return LETTER;}

[\t] ;

\n return 0;

. {return yytext[0];}

%%

YACC

%{

#include<stdio.h>

%}

%token LETTER DIGIT

%%

variable: LETTER|LETTER rest;

rest: LETTER rest|DIGIT rest|LETTER|DIGIT;

%%

main()

{yyparse();

printf("The string is a valid variable\n");

int yyerror(char *s)

{printf("this is not a valid variable\n");

exit(0);}

Sample 89file
input/output This is not a valid variable

34

The string is a valid variable


EXPERIMENT DETAILS
Experiment No:10
Title Write a YACC program for desk calculator.
Objective To learn designing parser using YACC
Pre-requisite Knowledge of
 LR Parsing
 YACC
Assumptions
4. The operators available are + and * only
5. The numbers are only one digit integer.
6. The lexical analyzer has also to be designed

Experiment 1. Define following YACC specifications along with the required actions
Specifications Line Expr ‘\n’;
Expr Expr’+’ Term;
Term Term’*’ Factor
|Factor ;
Factor ‘(‘ expr ‘)’
|DIGIT;
2. Write a function yylex for lexical analysis of the input. The function generates
following tokens (a) DIGIT (b) + (c) * (d) ( (e) )
3. Compile the program and execute.
Sample 2+3*5
input/output 17
EXPERIMENT DETAILS
Experiment No:11
Title Write a YACC Program to recognize nested IF control statements and display the number of levels of
nesting.
Objective To learn how to use flex inside a YACC program.

Pre-requisite Knowledge of
 Flex
 YACC
 Parsing
 Lexical analysis
Assumptions
Experiment 1. Declare the necessary variables.
Specifications 2. Define regular definitions which are used to define regular expressions.
num [0-9]
alp[a-z]
id {alp}({num}|{alp})
bin[*/+-]
not[!]
eq[=]
3. Define regular expressions to recognize lexemes and associate action with each lexeme
to generate token.
4. The lexical analyzer generated tokens as required by parser.
5. Define grammar and action for implementing parser.
stmt→com_nest
com_nest→nest
nest→if cond one_stmt
|if cond many_stmt
|if cond nest
cond→cond rel id
|cond rel num
|num
|id
|alp
one_stmt→id eq id bin id
|alp bin alp'
|alp inc
|id inc
many_stmt→many_stmt one_stmt
|one_stmt one_stmt
Sample if(a>3){if(b==3)c++;}
Input/ Output
Valid no of nesting :2

if(a=1)a--;

Invalid
EXPERIMENT DETAILS
Experiment No:12
Title Write a program to implement 3 address code.
Objective To learn how to translate statements into 3 address code using semantic rules
Pre- Knowledge of
requisite  Flex
 YACC
 How to convert statements into 3 address code
Statements are of the form:
x:=y op z
where x,y and z are names, constants or compiler-generated temporary variables, and ‘op’
stands for any operator.

A more complicated statement like


d:=x+y*z
would have to be translated to
t1:=y*z
t2:=x+t1
d:=t2
where t1 and t2 are compiler-generated temporary variable.
Syntax-Directed Translation into Three-Address Code:
Production Semantic Rules
S->id:=E S.code := E.code || gen(id.place ‘:=’ E.place)

E->E1+E2 E.place := newtemp;


E.code :=E1.code || E2.code || gen(E.place ‘:=’ E1.place ‘+’ E2.place)

E->E1*E2 E.place := newtemp;


E.code := E1.code || E2.code || gen(E.place ‘:=’ E1.place ‘*’ E2.place)

E->-E1 E.place := newtemp;


E.code :=E1.code || gen(E.place ‘:=’ ‘uminus’ E1.place)

E->( E1 ) E.place := E1.place;


E.code := E1.code;

E->id E.place := id.place;


E.code := “”
EXPERIMENT DETAILS
Experiment No:13
Title Write a program to create and print a symbol table that contains the name, type and size of the
identifier from a C file.
Objective Generating a symbol table which is able store information about identifiers
.
Pre- Knowledge of
requisite  Symbol table
Algorithm
Step 1: Declare the necessary variables.

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

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.


Mid Term Internal Evaluation { Internal – 50 Marks }
Continuous Assessment [30 Marks]

Lab Experiment and Demonstration [10 Marks]

Viva Voce [10 Marks]

Lab Experiment and Demonstration marks details

Completed Partially
Completed Logically
and written with Unacceptable
Status but partially incorrect
Executed errors/no effort
Executed program
accurately execution
Marks 9 - 10 7-8 5-6 3-4 1-2
Viva – Voce marks details

Good Below
Excellent Very Good Average
Majority of Average
All answers Most of the Few answers
Status the answers Rarely any
are answers are are
are answer is
satisfactory satisfactory satisfactory
satisfactory satisfactory
Marks 9 - 10 7-8 5-6 3-4 1-2
Final Term Internal Evaluation { Internal – 50 Marks }
Continuous Assessment [30 Marks]

Lab Experiment and Demonstration [10 Marks]

Viva Voce [10 Marks]

Lab Experiment and Demonstration marks details

Completed Partially
Completed Logically
and written with Unacceptable
Status but partially incorrect
Executed errors/no effort
Executed program
accurately execution
Marks 9 - 10 7-8 5-6 3-4 1-2
Viva – Voce marks details

Good Below
Excellent Very Good Average
Majority of Average
All answers Most of the Few answers
Status the answers Rarely any
are answers are are
are answer is
satisfactory satisfactory satisfactory
satisfactory satisfactory
Marks 9 - 10 7-8 5-6 3-4 1-2

You might also like