SlideShare a Scribd company logo
Compiler Construction 1
Compiler Construction
A Compulsory Module for Students in
Computer Science Department
Faculty of IT / Al – Al Bayt University
First Semester 2009/2010
Compiler Construction 2
Compiler Construction
Lecturer: Dr. Venus W. Samawi
Email: drvenus_uni@yahoo.com
Compiler Construction 3
Recommended Reading
The text book is:
Alfred V. Aho, Ravi Sethi and Jeffry D. Ulman,
Compilers Principles, Techniques and Tools, Addison
Wesley, 2007,
Supporting References:
1- W. Appel, Modern Compiler Implementation in Java,
Prentice Hall, 2002
2- D. Watt, Brown, Programming Language Processors in
Java: Compilers and Interpreters, Prentice hall, 2000
Compiler Construction 4
Language Processing System
Compiler Construction 5
Language Processors.
 A translator inputs and then converts a source program
into an object or target program.
 Source program is written in a source language
 Object program belongs to an object language
 A translators could be: Assembler, Compiler,
Interpreter
Assembler:
source program object program
(in assembly language) (in machine
language)
Assembler
Compiler Construction 6
Language Processors.
 a compiler is a program that can read a program in one language the source
language - and translate it into an equivalent program in another language - the
target language;
 An important role of the compiler is to report any errors in the source program that
it detects during the translation process
 If the target program is an executable machine-language program, it can then be
called by the user to process inputs and produce outputs;
Compiler Construction 7
Interpreter
•An interpreter is another common kind of language processor. Instead of producing a
target program as a translation, an interpreter appears to directly execute the operations
specified in the source program on inputs supplied by the user
The machine-language target program produced by a compiler is usually much faster
than an interpreter at mapping inputs to outputs . An interpreter, however, can usually
give better error diagnostics than a compiler, because it executes the source program
statement by statement.
Compiler Construction 8
Overview of Compilers
- Compiler: translates a source program written in a High-
Level Language (HLL) such as Pascal, C++ into
computer’s machine language (Low-Level Language
(LLL)).
* The time of conversion from source program into
object program is called compile time
* The object program is executed at run time
- Interpreter: processes an internal form of the source
program and data at the same time (at run time); no
object program is generated.
Compiler Construction 9
Compilers and Interpreters
Why Interpretation
 A higher degree of machine independence: high
portability.
 Dynamic execution: modification or addition to user
programs as execution proceeds.
 Dynamic data type: type of object may change at
runtime
 Easier to write – no synthesis part.
 Better diagnostics: more source text information
available
Compiler Construction 10
Overview of Compilers
Compilation Process:
Interpretive Process:
Source
program
Data
Object
program
Results
Data
Compiler
Executing
Computer
Result
Source
program Compiler
Compile time run time
Compiler Construction 11
Example Of Combining Both Interpreter and Compiler
 Java language processors combine
compilation and interpretation,
 A Java source program may first be compiled into an intermediate form called
bytecodes.
 The bytecodes are then interpreted by a virtual machine. A benefit of this
arrangement is that bytecodes compiled on one machine can be interpreted on
another machine, perhaps across a network.
 In order to achieve faster processing of inputs to outputs, some Java compilers,
called just-in-time compilers, translate the bytecodes into machine language
immediately before they run the intermediate program to process the input.
Compiler Construction 12
Model of A Compiler
 A compiler must perform two tasks:
- analysis of source program: The analysis part breaks up
the source program into constituent pieces and imposes a
grammatical structure on them. It then uses this structure to
create an intermediate representation of the source program.
- synthesis of its corresponding program: constructs the
desired target program from the intermediate representation
and the information in the symbol table.
 The analysis part is often called the front end of the
compiler; the synthesis part is the back end.
Compiler Construction 13
source program object program
Analysis
Lexical Syntactic Semantic
Analysis Analysis Analysis
Tables
Synthesis
Code Code
Generator optimizer
Compiler Construction 14
Tasks of Compilation Process and Its Output
Error
handler
Compiler phases
Compiler Construction 15
Lexical Analysis (scanner): The first phase of a compiler
 Lexical analyzer reads the stream of characters making up the
source program and groups the characters into meaningful
sequences called lexeme
 For each lexeme, the lexical analyzer produces a token of the form
that it passes on to the subsequent phase, syntax analysis
(token-name, attribute-value)
 Token-name: an abstract symbol is used during syntax analysis, an
 attribute-value: points to an entry in the symbol table for this
token.
Compiler Construction 16
Example: position =initial + rate * 60
1.”position” is a lexeme mapped into a token (id, 1), where id is an abstract symbol
standing for identifier and 1 points to the symbol table entry for position. The
symbol-table entry for an identifier holds information about the identifier, such as its
name and type.
2. = is a lexeme that is mapped into the token (=). Since this token needs no attribute-
value, we have omitted the second component. For notational convenience, the
lexeme itself is used as the name of the abstract symbol.
3. “initial” is a lexeme that is mapped into the token (id, 2), where 2 points to the
symbol-table entry for initial.
4. + is a lexeme that is mapped into the token (+).
5. “rate” is a lexeme mapped into the token (id, 3), where 3 points to the symbol-table
entry for rate.
6. * is a lexeme that is mapped into the token (*) .
7. 60 is a lexeme that is mapped into the token (60)
Blanks separating the lexemes would be discarded by the lexical analyzer.
Compiler Construction 17
Syntax Analysis (parser) : The second phase of the compiler
 The parser uses the first components of the tokens produced by the lexical
analyzer to create a tree-like intermediate representation that depicts the
grammatical structure of the token stream.
 A typical representation is a syntax tree in which each interior node
represents an operation and the children of the node represent the
arguments of the operation
Compiler Construction 18
Syntax Analysis Example
Pay := Base + Rate* 60
 The seven tokens are grouped into a parse tree
Assignment stmt
identifier
pay
:= expression
expression expression
+
identifier
base
Rate*60
Compiler Construction 19
Semantic Analysis: Third phase of the compiler
 The semantic analyzer uses the syntax tree and the information in the symbol
table to check the source program for semantic consistency with the language
definition.
 Gathers type information and saves it in either the syntax tree or the symbol
table, for subsequent use during intermediate-code generation.
 An important part of semantic analysis is type checking, where the compiler
checks that each operator has matching operands. For example, many
programming language definitions require an array index to be an integer; the
compiler must report an error if a floating-point number is used to index an
array.
 The language specification may permit some type conversions called
coercions. For example, a binary arithmetic operator may be applied to either a
pair of integers or to a pair of floating-point numbers. If the operator is applied
to a floating-point number and an integer, the compiler may convert or coerce
the integer into a floating-point number.
Compiler Construction 20
Intermediate Code Generation: three-address code
After syntax and semantic analysis of the source program, many compilers
generate an explicit low-level or machine-like intermediate representation
(a program for an abstract machine). This intermediate representation
should have two important properties:
 it should be easy to produce and
 it should be easy to translate into the target machine.
The considered intermediate form called three-address code, which consists of
a sequence of assembly-like instructions with three operands per
instruction. Each operand can act like a register.
Compiler Construction 21
Code Optimization: to generate better target code
 The machine-independent code-optimization phase attempts to improve the
intermediate code so that better target code will result.
 Usually better means:
 faster, shorter code, or target code that consumes less power.
 The optimizer can deduce that the conversion of 60 from integer to floating
point can be done once and for all at compile time, so the int to float
operation can be eliminated by replacing the integer 60 by the floating-point
number 60.0. Moreover, t3 is used only once
 There are simple optimizations that significantly improve the running time
of the target program without slowing down compilation too much.
Compiler Construction 22
Code Generation: takes as input an intermediate representation of the
source program and maps it into the target language
 If the target language is machine, code, registers or memory locations
are selected for each of the variables used by the program.
 Then, the intermediate instructions are translated into sequences of
machine instructions that perform the same task.
 A crucial aspect of code generation is the judicious assignment of
registers to hold variables.
Compiler Construction 23
Symbol-Table Management:
 The symbol table is a data structure containing a record for
each variable name, with fields for the attributes of the
name.
 The data structure should be designed to allow the compiler
to find the record for each name quickly and to store or
retrieve data from that record quickly
 These attributes may provide information about the storage
allocated for a name, its type, its scope (where in the
program its value may be used), and in the case of
procedure names, such things as the number and types of its
arguments, the method of passing each argument (for
example, by value or by reference), and the type returned.
Compiler Construction 24
Translation of an assignment
statement
Compiler Construction 25
Grouping of Compiler Phases
 Front end
 Consist of those phases that depend on the source language but
largely independent of the target machine.
 Back end
 Consist of those phases that are usually target machine dependent
such as optimization and code generation.
Compiler Construction 26
Common Back-end Compiling System
Fortran C/C++ Pascal Cobol
Common IR (e.g. Ucode)
Optimizer
Target Machine Code Gen
Compiler Construction 27
Compiling Passes
Several phases can be implemented as a
single pass consist of reading an input file
and writing an output file.
A typical multi-pass compiler looks like:
First pass: preprocessing, macro expansion
Second pass: syntax-directed translation, IR
code generation
Third pass: optimization
Last pass: target machine code generation
Compiler Construction 28
Cousins of Compilers
 Preprocessors
 Assemblers
 Compiler may produce assembly code instead of
generating relocatable machine code directly.
 Loaders and Linkers
 Loader copies code and data into memory, allocates
storage, setting protection bits, mapping virtual
addresses, .. Etc
 Linker handles relocation and resolves symbol
references.
 Debugger
Compiler Construction 29
Tasks of Compilation Process and Its Output
 Each tasks is assigned to a phase, e.g. Lexical
Analyzer phase, Syntax Analyzer phase, and so on.
 Each task has input and output.
 Any thing between brackets in the last figure is
output of a phase.
 The compiler first analyzes the program, the result
is representations suitable to be translated later on:
- Parse tree
- Symbol table
Compiler Construction 30
Parse Tree and Symbol Table
 Parse tree defines the program structure; how to
combine parts of the program to produce larger part
and so on.
 Symbol table provides
- the associations between all occurrences of each
name given in the program.
- It provides a link between each name and it
declaration.

More Related Content

Similar to A basic introduction to compiler design.ppt (20)

PPT
Compiler Construction introduction
Rana Ehtisham Ul Haq
 
PPTX
16 compiler-151129060845-lva1-app6892-converted.pptx
nandan543979
 
PPTX
Pros and cons of c as a compiler language
Ashok Raj
 
PPT
Introduction to Compiler Construction
Sarmad Ali
 
PDF
Lecture 01 introduction to compiler
Iffat Anjum
 
PPT
Chapter One
bolovv
 
PPTX
Compiler an overview
amudha arul
 
PDF
Chapter#01 cc
abdulbaki3
 
PPTX
Unit2_CD.pptx more about compilation of the day
k12196987
 
PPTX
Compiler Design Slides for Third Year Computer Science and Engineering
DrRajurkarArchanaMil
 
PPT
Compiler Design Basics
Akhil Kaushik
 
PPTX
Chapter 1.pptx
NesredinTeshome1
 
PDF
unit1pdf__2021_12_14_12_37_34.pdf
DrIsikoIsaac
 
PPTX
Phases of Compiler.pptx
ssuser3b4934
 
PPT
1 - Introduction to Compilers.ppt
Rakesh Kumar
 
PPT
Compiler Design Basics
Akhil Kaushik
 
PPT
Introduction to Compiler design
Dr. C.V. Suresh Babu
 
PPTX
The Phases of a Compiler
Radhika Talaviya
 
PDF
COMPILER DESIGN Engineering learinin.pdf
TechSkills7
 
PDF
3_1_COMPILER_DESIGNGARGREREGREGREGREGREGRGRERE
solomonraju17
 
Compiler Construction introduction
Rana Ehtisham Ul Haq
 
16 compiler-151129060845-lva1-app6892-converted.pptx
nandan543979
 
Pros and cons of c as a compiler language
Ashok Raj
 
Introduction to Compiler Construction
Sarmad Ali
 
Lecture 01 introduction to compiler
Iffat Anjum
 
Chapter One
bolovv
 
Compiler an overview
amudha arul
 
Chapter#01 cc
abdulbaki3
 
Unit2_CD.pptx more about compilation of the day
k12196987
 
Compiler Design Slides for Third Year Computer Science and Engineering
DrRajurkarArchanaMil
 
Compiler Design Basics
Akhil Kaushik
 
Chapter 1.pptx
NesredinTeshome1
 
unit1pdf__2021_12_14_12_37_34.pdf
DrIsikoIsaac
 
Phases of Compiler.pptx
ssuser3b4934
 
1 - Introduction to Compilers.ppt
Rakesh Kumar
 
Compiler Design Basics
Akhil Kaushik
 
Introduction to Compiler design
Dr. C.V. Suresh Babu
 
The Phases of a Compiler
Radhika Talaviya
 
COMPILER DESIGN Engineering learinin.pdf
TechSkills7
 
3_1_COMPILER_DESIGNGARGREREGREGREGREGREGRGRERE
solomonraju17
 

Recently uploaded (20)

PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PDF
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
PDF
community health nursing question paper 2.pdf
Prince kumar
 
PDF
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
PPTX
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PDF
IMP NAAC REFORMS 2024 - 10 Attributes.pdf
BHARTIWADEKAR
 
PPTX
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PDF
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
PDF
CHILD RIGHTS AND PROTECTION QUESTION BANK
Dr Raja Mohammed T
 
PPTX
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PDF
IMP NAAC-Reforms-Stakeholder-Consultation-Presentation-on-Draft-Metrics-Unive...
BHARTIWADEKAR
 
PPTX
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PPTX
THE TAME BIRD AND THE FREE BIRD.pptxxxxx
MarcChristianNicolas
 
PPTX
Mathematics 5 - Time Measurement: Time Zone
menchreo
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
community health nursing question paper 2.pdf
Prince kumar
 
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
ASRB NET 2023 PREVIOUS YEAR QUESTION PAPER GENETICS AND PLANT BREEDING BY SAT...
Krashi Coaching
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
IMP NAAC REFORMS 2024 - 10 Attributes.pdf
BHARTIWADEKAR
 
HYDROCEPHALUS: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
People & Earth's Ecosystem -Lesson 2: People & Population
marvinnbustamante1
 
CHILD RIGHTS AND PROTECTION QUESTION BANK
Dr Raja Mohammed T
 
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
IMP NAAC-Reforms-Stakeholder-Consultation-Presentation-on-Draft-Metrics-Unive...
BHARTIWADEKAR
 
2025 Winter SWAYAM NPTEL & A Student.pptx
Utsav Yagnik
 
Dimensions of Societal Planning in Commonism
StefanMz
 
THE TAME BIRD AND THE FREE BIRD.pptxxxxx
MarcChristianNicolas
 
Mathematics 5 - Time Measurement: Time Zone
menchreo
 
Ad

A basic introduction to compiler design.ppt

  • 1. Compiler Construction 1 Compiler Construction A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University First Semester 2009/2010
  • 2. Compiler Construction 2 Compiler Construction Lecturer: Dr. Venus W. Samawi Email: [email protected]
  • 3. Compiler Construction 3 Recommended Reading The text book is: Alfred V. Aho, Ravi Sethi and Jeffry D. Ulman, Compilers Principles, Techniques and Tools, Addison Wesley, 2007, Supporting References: 1- W. Appel, Modern Compiler Implementation in Java, Prentice Hall, 2002 2- D. Watt, Brown, Programming Language Processors in Java: Compilers and Interpreters, Prentice hall, 2000
  • 5. Compiler Construction 5 Language Processors.  A translator inputs and then converts a source program into an object or target program.  Source program is written in a source language  Object program belongs to an object language  A translators could be: Assembler, Compiler, Interpreter Assembler: source program object program (in assembly language) (in machine language) Assembler
  • 6. Compiler Construction 6 Language Processors.  a compiler is a program that can read a program in one language the source language - and translate it into an equivalent program in another language - the target language;  An important role of the compiler is to report any errors in the source program that it detects during the translation process  If the target program is an executable machine-language program, it can then be called by the user to process inputs and produce outputs;
  • 7. Compiler Construction 7 Interpreter •An interpreter is another common kind of language processor. Instead of producing a target program as a translation, an interpreter appears to directly execute the operations specified in the source program on inputs supplied by the user The machine-language target program produced by a compiler is usually much faster than an interpreter at mapping inputs to outputs . An interpreter, however, can usually give better error diagnostics than a compiler, because it executes the source program statement by statement.
  • 8. Compiler Construction 8 Overview of Compilers - Compiler: translates a source program written in a High- Level Language (HLL) such as Pascal, C++ into computer’s machine language (Low-Level Language (LLL)). * The time of conversion from source program into object program is called compile time * The object program is executed at run time - Interpreter: processes an internal form of the source program and data at the same time (at run time); no object program is generated.
  • 9. Compiler Construction 9 Compilers and Interpreters Why Interpretation  A higher degree of machine independence: high portability.  Dynamic execution: modification or addition to user programs as execution proceeds.  Dynamic data type: type of object may change at runtime  Easier to write – no synthesis part.  Better diagnostics: more source text information available
  • 10. Compiler Construction 10 Overview of Compilers Compilation Process: Interpretive Process: Source program Data Object program Results Data Compiler Executing Computer Result Source program Compiler Compile time run time
  • 11. Compiler Construction 11 Example Of Combining Both Interpreter and Compiler  Java language processors combine compilation and interpretation,  A Java source program may first be compiled into an intermediate form called bytecodes.  The bytecodes are then interpreted by a virtual machine. A benefit of this arrangement is that bytecodes compiled on one machine can be interpreted on another machine, perhaps across a network.  In order to achieve faster processing of inputs to outputs, some Java compilers, called just-in-time compilers, translate the bytecodes into machine language immediately before they run the intermediate program to process the input.
  • 12. Compiler Construction 12 Model of A Compiler  A compiler must perform two tasks: - analysis of source program: The analysis part breaks up the source program into constituent pieces and imposes a grammatical structure on them. It then uses this structure to create an intermediate representation of the source program. - synthesis of its corresponding program: constructs the desired target program from the intermediate representation and the information in the symbol table.  The analysis part is often called the front end of the compiler; the synthesis part is the back end.
  • 13. Compiler Construction 13 source program object program Analysis Lexical Syntactic Semantic Analysis Analysis Analysis Tables Synthesis Code Code Generator optimizer
  • 14. Compiler Construction 14 Tasks of Compilation Process and Its Output Error handler Compiler phases
  • 15. Compiler Construction 15 Lexical Analysis (scanner): The first phase of a compiler  Lexical analyzer reads the stream of characters making up the source program and groups the characters into meaningful sequences called lexeme  For each lexeme, the lexical analyzer produces a token of the form that it passes on to the subsequent phase, syntax analysis (token-name, attribute-value)  Token-name: an abstract symbol is used during syntax analysis, an  attribute-value: points to an entry in the symbol table for this token.
  • 16. Compiler Construction 16 Example: position =initial + rate * 60 1.”position” is a lexeme mapped into a token (id, 1), where id is an abstract symbol standing for identifier and 1 points to the symbol table entry for position. The symbol-table entry for an identifier holds information about the identifier, such as its name and type. 2. = is a lexeme that is mapped into the token (=). Since this token needs no attribute- value, we have omitted the second component. For notational convenience, the lexeme itself is used as the name of the abstract symbol. 3. “initial” is a lexeme that is mapped into the token (id, 2), where 2 points to the symbol-table entry for initial. 4. + is a lexeme that is mapped into the token (+). 5. “rate” is a lexeme mapped into the token (id, 3), where 3 points to the symbol-table entry for rate. 6. * is a lexeme that is mapped into the token (*) . 7. 60 is a lexeme that is mapped into the token (60) Blanks separating the lexemes would be discarded by the lexical analyzer.
  • 17. Compiler Construction 17 Syntax Analysis (parser) : The second phase of the compiler  The parser uses the first components of the tokens produced by the lexical analyzer to create a tree-like intermediate representation that depicts the grammatical structure of the token stream.  A typical representation is a syntax tree in which each interior node represents an operation and the children of the node represent the arguments of the operation
  • 18. Compiler Construction 18 Syntax Analysis Example Pay := Base + Rate* 60  The seven tokens are grouped into a parse tree Assignment stmt identifier pay := expression expression expression + identifier base Rate*60
  • 19. Compiler Construction 19 Semantic Analysis: Third phase of the compiler  The semantic analyzer uses the syntax tree and the information in the symbol table to check the source program for semantic consistency with the language definition.  Gathers type information and saves it in either the syntax tree or the symbol table, for subsequent use during intermediate-code generation.  An important part of semantic analysis is type checking, where the compiler checks that each operator has matching operands. For example, many programming language definitions require an array index to be an integer; the compiler must report an error if a floating-point number is used to index an array.  The language specification may permit some type conversions called coercions. For example, a binary arithmetic operator may be applied to either a pair of integers or to a pair of floating-point numbers. If the operator is applied to a floating-point number and an integer, the compiler may convert or coerce the integer into a floating-point number.
  • 20. Compiler Construction 20 Intermediate Code Generation: three-address code After syntax and semantic analysis of the source program, many compilers generate an explicit low-level or machine-like intermediate representation (a program for an abstract machine). This intermediate representation should have two important properties:  it should be easy to produce and  it should be easy to translate into the target machine. The considered intermediate form called three-address code, which consists of a sequence of assembly-like instructions with three operands per instruction. Each operand can act like a register.
  • 21. Compiler Construction 21 Code Optimization: to generate better target code  The machine-independent code-optimization phase attempts to improve the intermediate code so that better target code will result.  Usually better means:  faster, shorter code, or target code that consumes less power.  The optimizer can deduce that the conversion of 60 from integer to floating point can be done once and for all at compile time, so the int to float operation can be eliminated by replacing the integer 60 by the floating-point number 60.0. Moreover, t3 is used only once  There are simple optimizations that significantly improve the running time of the target program without slowing down compilation too much.
  • 22. Compiler Construction 22 Code Generation: takes as input an intermediate representation of the source program and maps it into the target language  If the target language is machine, code, registers or memory locations are selected for each of the variables used by the program.  Then, the intermediate instructions are translated into sequences of machine instructions that perform the same task.  A crucial aspect of code generation is the judicious assignment of registers to hold variables.
  • 23. Compiler Construction 23 Symbol-Table Management:  The symbol table is a data structure containing a record for each variable name, with fields for the attributes of the name.  The data structure should be designed to allow the compiler to find the record for each name quickly and to store or retrieve data from that record quickly  These attributes may provide information about the storage allocated for a name, its type, its scope (where in the program its value may be used), and in the case of procedure names, such things as the number and types of its arguments, the method of passing each argument (for example, by value or by reference), and the type returned.
  • 24. Compiler Construction 24 Translation of an assignment statement
  • 25. Compiler Construction 25 Grouping of Compiler Phases  Front end  Consist of those phases that depend on the source language but largely independent of the target machine.  Back end  Consist of those phases that are usually target machine dependent such as optimization and code generation.
  • 26. Compiler Construction 26 Common Back-end Compiling System Fortran C/C++ Pascal Cobol Common IR (e.g. Ucode) Optimizer Target Machine Code Gen
  • 27. Compiler Construction 27 Compiling Passes Several phases can be implemented as a single pass consist of reading an input file and writing an output file. A typical multi-pass compiler looks like: First pass: preprocessing, macro expansion Second pass: syntax-directed translation, IR code generation Third pass: optimization Last pass: target machine code generation
  • 28. Compiler Construction 28 Cousins of Compilers  Preprocessors  Assemblers  Compiler may produce assembly code instead of generating relocatable machine code directly.  Loaders and Linkers  Loader copies code and data into memory, allocates storage, setting protection bits, mapping virtual addresses, .. Etc  Linker handles relocation and resolves symbol references.  Debugger
  • 29. Compiler Construction 29 Tasks of Compilation Process and Its Output  Each tasks is assigned to a phase, e.g. Lexical Analyzer phase, Syntax Analyzer phase, and so on.  Each task has input and output.  Any thing between brackets in the last figure is output of a phase.  The compiler first analyzes the program, the result is representations suitable to be translated later on: - Parse tree - Symbol table
  • 30. Compiler Construction 30 Parse Tree and Symbol Table  Parse tree defines the program structure; how to combine parts of the program to produce larger part and so on.  Symbol table provides - the associations between all occurrences of each name given in the program. - It provides a link between each name and it declaration.