Professional Documents
Culture Documents
Introduction to Compilers
Partha Sarathi Chakraborty
Assistant Professor
Department of Computer Science and Engineering
SRM University, Delhi NCR Campus
Ch. 1: Introduction
Ch. 3: Lexical Analysis and Lex/Flex
Ch. 4: Syntax Analysis and Yacc/Bison
Ch. 5: Syntax-Directed Translation
Ch. 6: Type Checking
Ch. 7: Run-Time Environments
Ch. 8: Intermediate Code Generation
Ch. 9: Code Generation
Ch.10: Code Optimization
Reference:
Aho, Ullman, and Sethi, Compilers: Principles, Techniques, and Tools
Textbook
Dragon
Contents
Compiler technology
is more broadly applicable and has been employed in
rather unexpected areas.
Text-formatting languages, like nroff and troff;
preprocessor packages like eqn, tbl, pic
Silicon compiler for the creation of VLSI circuits
Command languages of OS
Query languages of Database systems
What Do Compilers Do
Absolute binary
Absolute address
Can be executed directly
Compilation
Translation of a program written in a source
language into a semantically equivalent
program written in a target language
Input
Source
Program
Compiler
Target
Program
Error messages
Output
10
Compilers
Source languages: Fortran, Pascal, C, etc.
Target languages: another PL, machine Lang
Compilers:
Single-pass
Multi-pass
Load-and-Go
Debugging
Optimizing
11
Interpretation
Performing the operations implied by the
source program
Source
Program
Interpreter
Input
Error messages
Output
12
13
Preprocessors, Compilers,
Assemblers, and Linkers
(C) 2014, Prepared by Partha Sarathi Chakraborty
Preprocessor
Source Program
Compiler
Target Assembly Program
Assembler
Relocatable Object Code
Linker
Absolute Machine Code
Libraries and
Relocatable Object Files
14
15
Phases of Compiler
16
(Character Stream)
Scanner
Tokens
Parser
Syntactic
Structure
Semantic
Routines
Intermediate
Representation
Symbol and
Attribute
Tables
Optimizer
17
(Character Stream)
Scanner
Tokens
Scanner
Parser
Syntactic
Structure
RE ( Regular expression )
(Used
by all )
NFA ( Non-deterministic Finite
Automata
DFA ( Deterministic FinitePhases
Automataof
)
LEX
The Compiler)
Semantic
Routines
Intermediate
Representation
Optimizer
Code
Generator
Target machine code
18
(Character Stream)
Scanner
Parser
Tokens
Parser
Syntactic
Structure
Given a formal syntax specification (typically as a contextfree grammar [CFG] ), the parse reads tokens and groups
Symbol and
them into units as specified by the productions of the CFG
Attribute
being used.
Tables
As syntactic structure is recognized, the parser either calls
corresponding semantic routines directly or builds a syntax
(Used by all
tree.
Phases
of
CFG ( Context-Free Grammar
)
Compiler)
BNF ( Backus-Naur FormThe
)
GAA ( Grammar Analysis Algorithms )
LL, LR, SLR, LALR Parsers
YACC
Semantic
Routines
Intermediate
Representation
Optimizer
Code
Generator
Target machine code
19
(Character Stream)
Scanner
Tokens
Semantic Routines
Parser
Syntactic
Structure
Semantic
Routines
Intermediate
Representation
Optimizer
Code
Generator
Target machine code
20
(Character Stream)
Optimizer
Scanner
Tokens
Parser
Syntactic
Structure
Semantic
Routines
Intermediate
Representation
Optimizer
Code
Generator
Target machine code
21
(Character Stream)
Scanner
Tokens
Code Generator
Interpretive Code Generation
Generating Code from Tree/Dag
Grammar-Based Code Generator
Parser
Syntactic
Structure
Semantic
Routines
Intermediate
Representation
Optimizer
Code
Generator
Target machine code
22
Symbol-table Management
23
Symbol-table Management
24
25
Translation of A Statement
26
Translation of A Statement
27
Translation of A Statement
Properties:
It should be easy to produce.
It should be easy to translate into the target machine.
28
29
Linear representations
Three-address code
30
Code Optimization
It attempts to improve the intermediate code, to
produce more efficient code that will result a fasterrunning machine code.
Compiler Optimizations must meet the following
objectives:
The optimization must be correct, that is, preserve the
meaning of the compiled program,
The optimization must improve the performance of many
programs,
The compilation time must be kept reasonable, and
The engineering effort required must be manageable.
31
Register Allocation
Register Scheduling
Code Selection
Addressing Modes
Instruction format
Power of instructions
Optimization at the machine code level
Back patching
32
33
34
Passes
A collection of phases may be repeated only once
(single pass) or multiple times (multi pass)
Single pass: usually requires everything to be defined
before being used in source program
Multi pass: compiler may have to keep entire program
representation in memory
35
36
37
Runtime semantics
Semantics rules that can be checked only at run time
38
39
Optimizing Compilers
Designed to produce efficient target code
Retargetable Compilers
A compiler whose target architecture can be changed
without its machine-independent components having to be
rewritten.
40
Compiler-Construction Tools
Software development tools are available to
implement one or more compiler phases
Scanner generators
Parser generators
Syntax-directed translation engines
Automatic code generators
Data-flow engines
Compiler-construction toolkits