Compiler Construction: Chapter 1: Introduction To Compilation
Compiler Construction: Chapter 1: Introduction To Compilation
Chapter 1: Introduction to
Compilation
CA – 501
Khan S. N.
8275517509
[email protected]
Compilers
• Compilers translate from a source language (typically a high
level language) to a functionally equivalent target language
(typically the machine code of a particular machine or a
machine-independent virtual machine).
• Compilers for high level programming languages are among
the larger and more complex pieces of software
– Original languages included Fortran and Cobol
• Often multi-pass compilers (to facilitate memory reuse)
– Compiler development helped in better programming language design
• Early development focused on syntactic analysis and optimization
– Commercially, compilers are developed by very large software groups
• Current focus is on optimization and smart use of resources for
modern RISC (reduced instruction set computer) architectures.
2
What is a compiler?
A computer program translates one language to another
Source Compiler Target
Program Program
A compiler is a complex program
From 10,000 to 1,000,000 lines of codes
Compilers are used in many forms of computing
Command interpreters, interface programs
Brief History of Compiler
BACK
Programs related to Compiler
Interpreters
Execute the source program immediately rather than generating
object code
Examples: BASIC, LISP, used often in educational or
development situations
Speed of execution is slower than compiled code by a factor of
10 or more
Share many of their operations with compilers
Assemblers
A translator for the assembly language of a particular computer
Assembly language is a symbolic form of one machine language
A compiler may generate assembly language as its target
language and an assembler finished the translation into object
code
Linkers
Semantics Analyzer
Symbol
Annotated Tree
Table
Source Code Optimizer
Intermediate code
Error
Code Generator Handler
Target code
expression
Assign-expression
expression = expression
subscript-expression additive-expression
Assign-expression
subscript-expression additive-expression
RETURN
The Semantic Analyzer
Assign-expression
subscript-expression additive-expression
integer integer
RETURN
The Source Code Optimizer
Assign-expression
subscript-expression additive-expression
integer integer
Assign-expression
subscript-expression
integer
t = 4 + 2
a[index]=t
t= 6
a[index]=t
a[index]=6
RETURN
The Code Generate
RETURN
The Target Code Optimizer
BACK
Bootstrapping and Porting
Third Language for Compiler
Construction
Machine language
compiler to execute immediately;
Another language with existed compiler on the same
target machine : (First Scenario)
Compile the new compiler with existing compiler
Another language with existed compiler on different
machine : (Second Scenario)
Compilation produce a cross compiler
T-Diagram Describing Complex
Situation
A compiler written in language H that translates language S into
language T.
S T
H
T-Diagram can be combined in two basic ways.
The First T-diagram Combination
A B B C A C
H H H
Original compiler
Compiler source code retargeted to K
Result in Cross Compiler
The step 2 in porting
A K A K
A A K K
H
Cross compiler
Compiler source code retargeted to K
Result in Retargeted Compiler
BACK
Compiler versus Interpreter
Compiler translates to machine code
Debuggers,
Version managers,
Profilers and so on.
56
Compiler-Construction Tools:
57
Compiler-Construction Tools:
58
Compiler-Construction Tools:
Some general tools have been created for the automatic design of
specific compiler components.
These tools use specialized languages for specifying and
implementing the component, and many use algorithms that
are quite sophisticated.
59
Compiler-Construction Tools:
The most successful tools are those that hide the details of the
generation algorithm and produce components that can be
easily integrated into the remainder of a compiler.
60
Compiler-Construction Tools:
61
Compiler-Construction Tools:
Parser generators
These produce syntax analyzers, normally from input that is based on a
context-free grammar.
In early compilers, syntax analysis consumed not only a large fraction of
the running time of a compiler, but a large fraction of the intellectual
effort of writing a compiler.
This phase is considered one of the easiest to implement.
62
Compiler-Construction Tools:
Scanner generators:
These tools automatically generate lexical analyzers, normally from a
specification based on regular expressions.
63
Compiler-Construction Tools:
The basic idea is that one or more “translations” are associated with each
node of the parse tree, and each translation is defined in terms of
translations at its neighbor nodes in the tree.
64
Compiler-Construction Tools:
Such a tool takes a collection of rules that define the translation of each
operation of the intermediate language into the machine language for
the target machine.
65