Professional Documents
Culture Documents
CO3005 Chapter 1 Introduction
CO3005 Chapter 1 Introduction
Introduction to Programming
Languages and Compilers Programming
languages
Language evaluation
Influences on Language design
The Structure of a
Compiler
Introduction.1
Introduction
Overview
Dr. Nguyen Hua
Phung, MEng. Tran
Ngoc Bao Duy
1 Programming languages
Language evaluation Programming
languages
Influences on Language design Language evaluation
Influences on Language design
Language
implementation
The Structure of a
2 Language implementation Compiler
Introduction.2
Introduction
Programming
languages
INTRODUCTION TO Language evaluation
Influences on Language design
The Structure of a
Compiler
Introduction.3
Introduction
Programming languages
Dr. Nguyen Hua
Phung, MEng. Tran
Ngoc Bao Duy
Programming
languages
Language evaluation
Influences on Language design
Language
implementation
The Structure of a
Compiler
Introduction.4
Introduction
Programming languages
Dr. Nguyen Hua
Phung, MEng. Tran
Ngoc Bao Duy
Introduction.4
Introduction
Language characteristics
Dr. Nguyen Hua
Phung, MEng. Tran
Ngoc Bao Duy
Language
independent mechanisms. implementation
Introduction.5
Introduction
Evaluation
Dr. Nguyen Hua
Phung, MEng. Tran
Ngoc Bao Duy
Programming
1 Readability languages
Language evaluation
Influences on Language design
2 Writability Language
implementation
3 Reliability The Structure of a
Compiler
4 Cost
Introduction.6
Introduction
Influences on Language design
Dr. Nguyen Hua
Phung, MEng. Tran
Ngoc Bao Duy
1 Computer Architecture
Programming
languages
Language evaluation
Influences on Language design
Language
implementation
The Structure of a
Compiler
Introduction.7
Introduction
Influences on Language design
Dr. Nguyen Hua
Phung, MEng. Tran
Ngoc Bao Duy
1 Computer Architecture
2 Programming methodologies:
Programming
• Declarative: on what the computer is to do. languages
• Functional: Lisp, ML, Haskell Language evaluation
Influences on Language design
• Logic or constraint-based: Prolog Language
• Data-flow: Verilog, Simulink implementation
• Ontology The Structure of a
• Query-based: SQL, GraphQL. Compiler
Introduction.7
Introduction
Language design: Computer Architecture
Dr. Nguyen Hua
Phung, MEng. Tran
Ngoc Bao Duy
Language
• Memory is separate from CPU implementation
• Instructions and data are piped from memory to CPU The Structure of a
• Basis for imperative languages Compiler
Introduction.8
Introduction
Language design: Programming methodologies
Dr. Nguyen Hua
Phung, MEng. Tran
Ngoc Bao Duy
• data abstraction
• Middle 1980s: Object-oriented programming
• Data abstraction + inheritance + polymorphism
Introduction.9
Introduction
Language Design Trade-Offs
Dr. Nguyen Hua
Phung, MEng. Tran
Ngoc Bao Duy
Language
E.g. APL provides many powerful operators (and a implementation
Introduction.10
Introduction
Programming
LANGUAGE languages
Language evaluation
Influences on Language design
Language
IMPLEMENTATION implementation
The Structure of a
Compiler
Introduction.11
Introduction
Implementation methods
Dr. Nguyen Hua
Phung, MEng. Tran
Ngoc Bao Duy
Translators
Before a program can be run, it first must be translated
into a form in which it can be executed by a computer.
This software systems that do this translation are called Programming
languages
translators. Language evaluation
Influences on Language design
Language
implementation
The Structure of a
Compiler
Introduction.12
Introduction
Implementation methods
Dr. Nguyen Hua
Phung, MEng. Tran
Ngoc Bao Duy
Translators
Before a program can be run, it first must be translated
into a form in which it can be executed by a computer.
This software systems that do this translation are called Programming
languages
translators. Language evaluation
Influences on Language design
cuted line-by-line.
3 Hybrid Implementation Systems: A compromise be-
tween compilers and pure interpreters.
4 Just-in-time Compiler : A compiler inside an interpreter
compiles just hot methods.
Introduction.12
Introduction
Compilers
Dr. Nguyen Hua
Phung, MEng. Tran
Ngoc Bao Duy
Programming
languages
Language evaluation
Influences on Language design
Language
implementation
Figure: A compiler
The Structure of a
Compiler
Introduction.13
Introduction
Compilers
Dr. Nguyen Hua
Phung, MEng. Tran
Ngoc Bao Duy
Programming
languages
Language evaluation
Influences on Language design
Language
implementation
Figure: A compiler
The Structure of a
Compiler
Definition
A compiler is a program that can read a program in one lan-
guage - the source language - and translate it into an equiv-
alent program in another language - the target language. If
the target program is an executable machine-language pro-
gram, it can then be called by the user to process inputs
and produce outputs.
Introduction.13
Introduction
Pure interpreters
Dr. Nguyen Hua
Phung, MEng. Tran
Ngoc Bao Duy
Programming
languages
Language evaluation
Figure: A pure interpreter Influences on Language design
Language
implementation
The Structure of a
Compiler
Introduction.14
Introduction
Pure interpreters
Dr. Nguyen Hua
Phung, MEng. Tran
Ngoc Bao Duy
Programming
languages
Language evaluation
Figure: A pure interpreter Influences on Language design
Language
implementation
Introduction.14
Introduction
Hybrid systems
Dr. Nguyen Hua
Phung, MEng. Tran
Ngoc Bao Duy
Introduction.15
Introduction
Hybrid systems
Dr. Nguyen Hua
Phung, MEng. Tran
Ngoc Bao Duy
Programming
languages
Language evaluation
Influences on Language design
Language
implementation
The Structure of a
Compiler
Figure: A hybrid system
Introduction.15
Introduction
Just-in-time (JIT) compilers
Dr. Nguyen Hua
Phung, MEng. Tran
Ngoc Bao Duy
Programming
languages
Language evaluation
Influences on Language design
Language
implementation
The Structure of a
Compiler
Figure: A just-in-time (JIT) compiler: Case study with Java
Introduction.16
Introduction
Just-in-time (JIT) compilers
Dr. Nguyen Hua
Phung, MEng. Tran
Ngoc Bao Duy
Programming
languages
Language evaluation
Influences on Language design
Language
implementation
The Structure of a
Compiler
Figure: A just-in-time (JIT) compiler: Case study with Java
Programming
languages
THE STRUCTURE OF Language evaluation
Influences on Language design
A COMPILER Language
implementation
The Structure of a
Compiler
Introduction.17
Introduction
An overview of compilation
Dr. Nguyen Hua
Phung, MEng. Tran
Ngoc Bao Duy
Character stream
Introduction.18
Introduction
Lexical Analysis
Dr. Nguyen Hua
Phung, MEng. Tran
Ngoc Bao Duy
Introduction.19
Introduction
Lexical Analysis
Dr. Nguyen Hua
Phung, MEng. Tran
Ngoc Bao Duy
Example
5 (ID, "c")
6 (MUL, "*")
7 (INT, "60")
8 (SEMI, ";")
Introduction.19
Introduction
Syntax Analysis
Dr. Nguyen Hua
• The second phase of the compiler is syntax analysis Phung, MEng. Tran
Ngoc Bao Duy
or parsing.
• Using the first components of the tokens produced by
the scanner to create a tree-like intermediate repre-
sentation (syntax tree) that depicts the grammatical Programming
structure of the token stream. languages
Language evaluation
Influences on Language design
Language
implementation
The Structure of a
Compiler
Introduction.20
Introduction
Syntax Analysis
Dr. Nguyen Hua
• The second phase of the compiler is syntax analysis Phung, MEng. Tran
Ngoc Bao Duy
or parsing.
• Using the first components of the tokens produced by
the scanner to create a tree-like intermediate repre-
sentation (syntax tree) that depicts the grammatical Programming
structure of the token stream. languages
Language evaluation
Influences on Language design
Language
implementation
The Structure of a
Compiler
Language
implementation
The Structure of a
Compiler
Introduction.21
Introduction
Abstract syntax tree (AST) generation
Dr. Nguyen Hua
• Parse tree demonstrates completely and concretely, how Phung, MEng. Tran
Ngoc Bao Duy
a particular sequence of tokens can be derived under the
rules of the grammar. Much of the information in the
parse tree is irrelevant to further phases of compilation.
• Removing most of the “artificial” nodes in the tree’s Programming
languages
interior and returning an abstract syntax tree (AST). Language evaluation
Influences on Language design
Language
Assignment implementation
The Structure of a
Compiler
Id(a) BinExp
+ Id(b) BinExp
* Id(c) IntLit(60)
Introduction.21
Introduction
Semantic analysis (Static checking)
Dr. Nguyen Hua
Phung, MEng. Tran
Ngoc Bao Duy
Static checks are consistency checks that are done during
compilation. Not only do they assure that a program can
be compiled successfully, but they also have the potential
for catching programming errors early, before a program is Programming
languages
run. Language evaluation
Language
grammars. implementation
Introduction.22
Introduction
Intermediate code generation
Dr. Nguyen Hua
Phung, MEng. Tran
Ngoc Bao Duy
Example
Example of intermediate representation
• Three-address code.
• Java bytecode.
• Common Intermediate Language (CIL).
Introduction.23
Introduction
Applications of Compiler technology
Dr. Nguyen Hua
Phung, MEng. Tran
Ngoc Bao Duy
Programming
1 Implementation of High-Level Programming Languages. languages
Language evaluation
2 Optimizations for Computer Architectures. Influences on Language design
Language
3 Design of New Computer Architectures. implementation
Introduction.24
Introduction
Related program
Dr. Nguyen Hua
Phung, MEng. Tran
Ngoc Bao Duy
Introduction.25
Introduction
Programming
languages
Language evaluation
Language
implementation
The Structure of a
Compiler
Introduction.26