1-Introduction to programming language translators-13-12-2024
1-Introduction to programming language translators-13-12-2024
1
Module:1 Introduction to Compilation and Lexcial Analysis Introduction to programming language translators-Structure and
phases of a compiler-Design issues- Patterns- lexemes-Tokens-Attributes-Specification of Tokens- Extended Regular
expression, Regular expression to Deterministic Finite Automata (Direct method). 7 hours
Module:2 Syntax Analysis –Top Down Role of parser- Parse Tree - Elimination of ambiguity - Top down parsing - Recursive
Descent parsing - Non Recursive Descent parsing - Predictive Parsing - LL(1) grammars. 5 hours
Module:3 Syntax Analysis –Bottom Up Shift Reduce Parsers- Operator Precedence Parsing ,LR parsers:-Construction of SLR
parser tables and parsing , CLR parsing-LALR parsing 7 hours
Module:4 Semantics Analysis Syntax Directed Definition – Evaluation Order - Applications of Syntax Directed Translation -
Syntax Directed Translation Schemes - Implementation of L attributed Syntax Directed Definition. 6 hours
Module:5 Intermediate Code Generation Variants of syntax trees - Three address code- Types – Declarations - Procedures -
Assignment Statements - Translation of Expressions - Control Flow - Back Patching- Switch Case Statements. 7 hours
Module:6 Code Optimization Loop optimizations- Principal sources of optimization -Introduction to Data Flow Analysis -
Basic Blocks - The DAG Representation of Basic Blocks -Loops in Flow Graphs. 6 hours
Module:7 Code Generation & Other Translations Issues in the design of a code generator- Target Machine- Next-Use
Information - Optimization of basic blocks - Peephole Optimization - Register Allocation and Assignment. 5 hours
Course Outcome:
1. Demonstrate the functioning of a Compiler and to develop a firm and enlightened grasp of
concepts such as higher level programming, assemblers, automata theory, and formal
languages, language specifications.
3. Apply the ideas, the techniques, and the knowledge acquired for the purpose of
developing software systems.
3
Internal Components
• Quiz -1 – 10m
• DA -1- 10m
• Quiz 2 / DA 2 – 10m
4
Module– 1
Introduction to
Compiler Design
5
Topics to be covered
Looping
• Translator
• Analysis synthesis model of compilation
• Phases of compiler
• Grouping of the Phases
• Difference between compiler & interpreter
• Context of compiler (Cousins of compiler)
• Pass structure
• Types of compiler
6
Translator
• A translator is a program that takes one form of program as input and converts
it into another form.
• Types of translators are:
1. Compiler
2. Interpreter
3. Assembler
Source Translator Target
Program Program
Error
Messages (If any)
7
Compiler
• A compiler is a program that reads a program written in source language and
translates it into an equivalent program in target language.
void main()
{
int a=1,b=2,c; 10100100101
Compiler 1001010
c=a+b;
printf(“%d”,c);
}
10
Analysis Synthesis model of
compilation
11
Analysis synthesis model of compilation
• There are two parts of compilation.
1. Analysis Phase
2. Synthesis Phase
12
Analysis phase & Synthesis phase
Analysis Phase Synthesis Phase
• Analysis part breaks up the source The synthesis part constructs the desired
program into constituent pieces and target program from the intermediate
representation.
creates an intermediate
Synthesis phase consist of the following sub
representation of the source
phases:
program.
1. Code optimization
• Analysis phase consists of three sub
2. Code generation
phases:
1. Lexical analysis
2. Syntax analysis
3. Semantic analysis
13
Phases of compiler
14
Phases of compiler
Compiler
Lexical analysis
Intermediate Code
code optimization
Syntax analysis generation
Code
Semantic analysis generation
15
Lexical analysis
• Lexical Analysis is also called linear analysis or
scanning. Position = initial + rate*60
• Lexical Analyzer divides the given source statement
into the tokens.
Lexical analysis
• Ex: Position = initial + rate * 60 would be grouped into
the following tokens: id1=id2+ id3 * 60
Position (identifier)
= (Assignment symbol)
initial (identifier)
+ (Plus symbol)
rate (identifier)
* (Multiplication symbol)
60 (Number) 16
Syntax analysis
• Syntax Analysis is also called Parsing or
Position = initial + rate*60
Hierarchical Analysis.
• The syntax analyzer checks each line of the Lexical analysis
code and spots every tiny mistake. id1 = id2 + id3 *
• If code is error free then syntax analyzer 60
generates the tree. Syntax analysis
id1 +
id2 *
id3 60
17
Semantic analysis
• Semantic analyzer determines the meaning of =
a source string. id1 +
• It performs following operations: id2 * int to
1. matching of parenthesis in the expression. real
id3 60
2. Matching of if..else statement.
3. Performing arithmetic operation that are type
Semantic analysis
compatible.
4. Checking the scope of operation. =
*Note: Consider id1, id2 and id3 are real
id1 +
id2 *
id3 inttoreal
60 18
Intermediate code generator
• Two important properties of intermediate =
code : id1 +
1. It should be easy to produce.
id2 *
2. Easy to translate into target program.
t3 id3 inttoreal
• Intermediate form can be represented using t2 t1
60
“three address code”.
Intermediate code
• Three address code consist of a sequence of
instruction, each of which has at most three t1= int to real(60)
operands. t2= id3 * t1
t3= t2 + id2
id1= t3
19
Code optimization
• It improves the intermediate code.
• This is necessary to have a faster execution Intermediate code
of code or less consumption of memory.
t1= int to real(60)
t2= id3 * t1
t3= t2 + id2
id1= t3
Code optimization
20
Code generation
• The intermediate code instructions are
translated into sequence of machine Code optimization
instruction.
t1= id3 * 60.0
id1 = id2 + t1
Code generation
MOV id3, R2
MUL #60.0, R2
MOV id2, R1
ADD R2,R1
MOV R1, id1
Id3R2
Id2R1
21
Phases of compiler
Source
program
Analysis
Lexical analysis Phase
Syntax analysis
Semantic
analysis Error
Symbol
table detection
Intermediate
and recovery
code
Variable Type Address Code
Name optimization
Position Float 0001
Code Synthesis
Initial Float 0005 generation Phase
Rate Float 0009 Target
Program 22
Exercise
• Write output of all the phases of compiler for following statements:
1. x = b-c*2
2. I=p*n*r/100
23
Grouping of Phases
24
Front end & back end (Grouping of
phases)
Front end
• Depends primarily on source language and largely independent of the target machine.
• It includes following phases:
1. Lexical analysis
2. Syntax analysis
3. Semantic analysis
4. Intermediate code generation
5. Creation of symbol table
Back end
Depends on target machine and do not depends on source program.
It includes following phases:
1. Code optimization
2. Code generation phase
3. Error handling and symbol table operation 25
Difference between compiler & interpreter
Compiler Interpreter
Scans the entire program and translates it It translates program’s one statement at a
as a whole into machine code. time.
It generates intermediate code. It does not generate intermediate code.
An error is displayed after entire program An error is displayed for every instruction
is checked. interpreted if any.
Memory requirement is more. Memory requirement is less.
Example: C compiler Example: Basic, Python, Ruby
26
Context of Compiler
(Cousins of compiler)
27
Context of compiler (Cousins of compiler)
Skeletal Source Program
• In addition to compiler, many other system
programs are required to generate absolute Preprocessor
machine code. Source
• These system programs are: Program
Compiler
Target Assembly
• Preprocessor Program
• Assembler Assembler
• Linker Relocatable Object
• Loader Code
Libraries & Linker / Loader
Object Files
Absolute Machine
Code
28
Context of compiler (Cousins of compiler)
Skeletal Source Program
Preprocessor
Some of the task performed by preprocessor: Preprocessor
Target Assembly
Program
Assembler
Relocatable Object
Code
Libraries & Linker / Loader
Object Files
Absolute Machine
Code
30
Context of compiler (Cousins of compiler)
Skeletal Source Program
Assembler
Assembler is a translator which takes the assembly Preprocessor
Target Assembly
Program
Assembler
Relocatable Object
Code
Libraries & Linker / Loader
Object Files
Absolute Machine
Code
31
Context of compiler (Cousins of compiler)
Skeletal Source Program
Linker
Linker makes a single program from a several files Preprocessor
33
Pass structure
• One complete scan of a source program is called pass.
• Pass includes reading an input file and writing to the output file.
• In a single pass compiler analysis of source statement is immediately followed
by synthesis of equivalent target statement.
• While in a two pass compiler intermediate code is generated between analysis
and synthesis phase.
• It is difficult to compile the source program into single pass due to: forward
reference
34
Pass structure
Forward reference: A forward reference of a program entity is a reference to the
entity which precedes its definition in the program.
• This problem can be solved by postponing the generation of target code until
more information concerning the entity becomes available.
• It leads to multi pass model of compilation.
Pass I:
36
Types of compiler
37
Types of compiler
1. One pass compiler
• It is a type of compiler that compiles whole process in one-pass.
2. Two pass compiler
• It is a type of compiler that compiles whole process in two-pass.
• It generates intermediate code.
3. Incremental compiler
• The compiler which compiles only the changed line from the source code and update the object
code.
4. Native code compiler
• The compiler used to compile a source code for a same type of platform only.
5. Cross compiler
• The compiler used to compile a source code for a different kinds platform.
38