Compiler Lab (CSE353) Lab Course File
Compiler Lab (CSE353) Lab Course File
L TP 301
Course Type CORE
Semester Of- 5th
fered
Academic Year 2017-2018
Slot
Class Room
Faculty Details:
Name Mr. Chetan Agarwal
Website link
Designation Asst. Professor
School School of Computing Science and Engineering
Cabin No C-324
Intercom -------
Open Hours Thursday(9:30 AM to 11.30 AM)
1.2. Pre-requisites
1.3. Learning Outcomes
4. Experiment details
1.2 Pre-requisite
1. Theory of Automata and Formal Languages (CSE206)
2. Data Structure and Algorithm
3. Basics of ‘C’ Programming Language
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
Completed Partially
Completed Logically
and written with Unacceptabl
Status but partially incorrect
Executed errors/no e effort
Executed program
accurately execution
Marks 9 – 10 7-8 5-6 3-4 1-2
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
Completed Partially
Completed Logically
and written with Unacceptabl
Status but partially incorrect
Executed errors/no e effort
Executed program
accurately execution
Marks 9 – 10 7-8 5-6 3-4 1-2
Completed
Completed Partially Logically
and Unacceptabl
Status but partially Written and Incorrect
Executed e efforts
Executing Errors Program
perfectly
Marks 5 4 3 2 1
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.
Lab
Lab
Name
Compiler
Course
Theory
2
PO1
3
PO3
1
PO4
1
PO5
PO6
PO7
PO8
PO9
PO10
PO11
PO12
Software Requirements
Turbo C 2.0/ Turbo C++3.0+, Lex or Flex and YACC tools (Unix/Linux utilities)
Hardware Requirements
No specific requirements. Any computer Hardware capable of running DOS can be
Used for this course.
3. EXPERIMENT LIST
Experiment No:1
Title Write a C program to simulate a DFA.
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
Pre- Knowledge of Lex and regular expression to recognize word, end of line etc.
requisite
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).
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
Experiment No:5
Title Write a C program to recognize strings under 'a', 'a*b+', 'abb'.
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.
3. The first character of an identifier is an alphabet whereas the next characters may
be alphabet/digit.
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
5. The first character of an identifier is an alphabet whereas the next characters may be
alphabet/digit.
Experiment LEX
Specifications
%{
#include"y.tab.h"
extern yylval;
%}
%%
[\t] ;
\n return 0;
. {return yytext[0];}
%%
YACC
%{
#include<stdio.h>
%}
%%
main()
{yyparse();
exit(0);}
Sample 89file
input/output This is not a valid variable
34
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
x:=y op z
where x,y and z are names, constants or compiler-generated temporary variables, and ‘op’ stands for
any operator.
d:=x+y*z
t1:=y*z
t2:=x+t1
d:=t2
Objective Generating a symbol table which is able store information about identifiers
.
Pre- Knowledge of
requisite Symbol table
Algorithm
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.
Semantic analysis is one in which certain checks are performed to ensure that components of a
program fit together meaningfully. Mainly performs type checking.
Q10 List the various error recovery strategies for a lexical analysis.
Possible error recovery actions are:
Panic mode recovery
Deleting an extraneous character
Inserting a missing character
Replacing an incorrect character by a correct character
Transposing two adjacent characters
5.GUIDELINES FOR CONTINUOUS ASSESSMENT
Internal Lab Assessment (End Semester)
Mark
Continuou Lab Viva
Tota s
S. Enrol. Name of the s Experimen -
l (in
No. No. Student assessmen t & Demo Voce
(50) word
t (30) (10) (10)
s)
Completed Partially
Completed Logically
and written with Unacceptabl
Status but partially incorrect
Executed errors/no e effort
Executed program
accurately execution
Marks 9 - 10 7-8 5-6 3-4 1-2
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]
Completed Partially
Completed Logically
and written with Unacceptabl
Status but partially incorrect
Executed errors/no e effort
Executed program
accurately execution
Marks 9 - 10 7-8 5-6 3-4 1-2
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
Completed
Completed Partially Logically
and Unacceptabl
Status but partially Written and Incorrect
Executed e efforts
Executing Errors Program
perfectly
Marks 5 4 3 2 1