Professional Documents
Culture Documents
Chapter 01
Introduction to Compiler Design (CD)
Course number: CSE 404 Instructor: Desta Gebre
Email: dhesta26@yahoo.com
Lecture 3, Practice 3
Office: 209
Credit 4 Lab: Aynalem
Pre-Requisite CSE 405
Text book:
Alfred V Aho, Ravi Sethi, Jeffrey D. Ullman,
“Compilers – Principles,Techniques and Tools”
Addison Wesley
1
Course Organization
• Lectures
• Organized in to eight chapters
• Labs
• Up to five reports
• Projects and seminars
• In groups of at most 5 students 1. Introduction
2. Lexical Analysis
3. Syntax Analysis
4. Syntax Directed Translation
5. Run Time Environments
6. Intermediate Code Generation
7. Code Generation
8. Code Optimization
2
Evaluation
• Quiz (10%)
• Lab assignments (up to 20%)
• Project (up to 20%)
• Mid exam (up to 30%)
• Final Exam (up to 40%)
• Total out of 100%
Caution
• Direct copying from other student or from the Internet is not allowed. (Results in 0).
• Each group member must contribute in project and all lab works.
• A group member who does not know what is in a lab report or project will be
given zero. Up to 5 marks will also be deducted from each group member.
• Cheating in exams will not be tolerated.
• Will be reported to discipline committee right away. 3
¿Why sudy Compilers?
• Become a better programmer
• Insight into interaction between languages, compilers, and hardware
• Compiler techniques are everywhere
• Parsing (little languages, interpreters, HTML)
• Database engines, query languages
• Text processing
• Fascinating blend of theory and engineering
• Direct applications of theory to practice
• Parsing, scanning, static analysis
• Resource allocation, “optimization”, etc.
• You might even write a compiler some day!
• Etc
4
Overview and History
Cause
Software for early computers was written in assembly language
The benefits of reusing software on different CPUs started to
become significantly greater than the cost of writing a compiler
5
Compilers
Programming Machine
Language Compiler Language
(Source) (Target)
6
…Cont’d
Compilers may generate three types of code:
Pure Machine Code
Machine instructions set without assuming the existence of any
operating system or library.
Mostly being OS or embedded applications.
Augmented Machine Code
Code with OS routines and runtime support routines.
More often.
Virtual Machine Code
Virtual instructions, can be run on any architecture with a virtual
machine interpreter or a just-in-time compiler
Ex. Java
7
…Cont’d
Another way that compilers differ from one another is
in the format of the target machine code they
generate:
Assembly or other source format
Relocatable binary
Relative address
A linkage step is required
Absolute binary
Absolute address
Can be executed directly
8
Phases of Compilation
Source
Program Tokens Syntactic Semantic
Scanner Parser
(Character Stream) Structure Routines
Intermediate
Representation
Code
Generator
9
Target machine code
Phases of Compilation: Scanner
Source
Program Tokens Syntactic Semantic
Scanner Parser
(Character Stream) Structure Routines
Intermediate
Scanner Representation
Source
Program Tokens Syntactic Semantic
Scanner Parser
(Character Stream) Structure Routines
Intermediate
Parser Representation
Source
Program Tokens Syntactic Semantic
Scanner Parser
(Character Stream) Structure Routines
Intermediate
Semantic Routines Representation
Source
Program Tokens Syntactic Semantic
Scanner Parser
(Character Stream) Structure Routines
Intermediate
Optimizer Representation
Source
Program Tokens Syntactic Semantic
Scanner Parser
(Character Stream) Structure Routines
Intermediate
Code Generator Representation
Interpretive Code Generation
Generating Code from Tree/Dag
Grammar-Based Code Generator
Optimizer
Code
Generator
14 Target machine code
Phases: Summary
Code Generator
[Intermediate Code Generator]
Tokens
Code Optimizer
Parser
[Syntax Analyzer]
Optimized Intermediate Code
Parse tree
Code Generator
Semantic Process
[Semantic analyzer] Target machine code
15
Grouping of Phases
Compiler
Analysis Synthesis
16
Grouping of Phases …cont’d
In analysis phase, an intermediate representation is
created from the given source program.
Lexical Analyzer, Syntax Analyzer and Semantic Analyzer are the parts of
this phase.
17
Syntax and Semantics of Programming Language
19
Generators
Automatic Compiler Generator
Compiler
Compiler Compiler
Definition in
(Metalanguage)
Error Message
Automatic tool Generator
Tool
Definition in Compiler Tool
(Metalanguage)
Error Message
20
Computer Architecture and Compiler Design
21
Compiler Design Considerations
Debugging Compilers
Designed to aid in the development and debugging of
programs.
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.
22