Download as pdf or txt
Download as pdf or txt
You are on page 1of 36

Introduction

Dr. Nguyen Hua


Phung, MEng. Tran
Ngoc Bao Duy

Introduction to Programming
Languages and Compilers Programming
languages
Language evaluation
Influences on Language design

Principles of Programming Languages Language


implementation

The Structure of a
Compiler

Dr. Nguyen Hua Phung, MEng. Tran Ngoc Bao Duy


Department of Computer Science
Faculty of Computer Science and Engineering
Ho Chi Minh University of Technology, VNU-HCM

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

3 The Structure of a Compiler

Introduction.2
Introduction

Dr. Nguyen Hua


Phung, MEng. Tran
Ngoc Bao Duy

Programming
languages
INTRODUCTION TO Language evaluation
Influences on Language design

PROGRAMMING LANGUAGES Language


implementation

The Structure of a
Compiler

Introduction.3
Introduction
Programming languages
Dr. Nguyen Hua
Phung, MEng. Tran
Ngoc Bao Duy

Programming languages are notations for describing com-


putations to people and to machines.

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

Programming languages are notations for describing com-


putations to people and to machines.
Programming domains Programming
languages
• Scientific Applications: Fortran, ALGOL 60, Julia, Language evaluation
Influences on Language design
MATLAB. Language
implementation
• Business Applications: COBOL, DATABUS, PL/B, ...
The Structure of a
• Artificial Intelligence: LISP, Prolog, ... Compiler

• Systems Programming: PL/S, BLISS, Extended AL-


GOL, C, ...
• Web Software: XHTML, JavaScript, PHP, Java, C#
(ASP.NET), ...

Introduction.4
Introduction
Language characteristics
Dr. Nguyen Hua
Phung, MEng. Tran
Ngoc Bao Duy

• Clarity, simplicity, and unity: provides both a frame-


work for thinking about algorithms and a means of ex-
pressing those algorithms. Programming
• Orthogonality: every combination of features is mean- languages
Language evaluation

ingful. Independent functions should be controlled by Influences on Language design

Language
independent mechanisms. implementation

• Support of abstraction (Control, Data): program The Structure of a


Compiler
data reflects problem being solved, program structure
reflects the logical structure of algorithm.
• Safety: The ease of which errors can be found and
corrected and new features added.
• ...

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

• Imperative: on how the computer should do it.


• Procedural-based: Fortran, ALGOL, COBOL, PL/I,
BASIC, Pascal and C.
• Object-oriented (OOP).

Introduction.7
Introduction
Language design: Computer Architecture
Dr. Nguyen Hua
Phung, MEng. Tran
Ngoc Bao Duy

• Well-known computer architecture: von Neumann.


• Imperative languages, most dominant, because of von Programming
languages
Neumann computers. Language evaluation

• Data and programs stored in memory


Influences on Language design

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

• Variables model memory cells


• Assignment statements model writing to memory cell
• Iteration is efficient

Introduction.8
Introduction
Language design: Programming methodologies
Dr. Nguyen Hua
Phung, MEng. Tran
Ngoc Bao Duy

• 1950s and early 1960s: Simple applications; worry about


machine efficiency. Programming
• Late 1960s: Efficiency became important; readability, languages
Language evaluation
better control structures. Influences on Language design

• Structured programming. Language


implementation
• Top-down design and step-wise refinement.
The Structure of a
• Late 1970s: Process-oriented to data-oriented Compiler

• 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

1 Reliability vs. cost of execution


E.g. Java demands all references to array elements be
checked for proper indexing but that leads to increased Programming
execution costs. languages
Language evaluation

2 Readability vs. writability Influences on Language design

Language
E.g. APL provides many powerful operators (and a implementation

large number of new symbols), allowing complex com- The Structure of a


Compiler
putations to be written in a compact program but at
the cost of poor readability.
3 Writability (flexibility) vs. reliability
E.g. C++ pointers are powerful and very flexible but
not reliably used.

Introduction.10
Introduction

Dr. Nguyen Hua


Phung, MEng. Tran
Ngoc Bao Duy

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

1 Compilation: Programs are entirely translated into ma- Language


implementation
chine language and then executed.
The Structure of a
2 Pure Interpretation: Programs are translated and exe- Compiler

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

Definition The Structure of a


Compiler

A pure interpreter is another common kind of language


processor. Instead of producing a target program as a trans-
lation, an interpreter appears to directly execute the opera-
tions specified in the source program on inputs supplied by
the user

Introduction.14
Introduction
Hybrid systems
Dr. Nguyen Hua
Phung, MEng. Tran
Ngoc Bao Duy

• The machine-language target program produced by a Programming


languages
compiler is usually much faster than an interpreter at Language evaluation
Influences on Language design
mapping inputs to outputs. Language
implementation
• An interpreter, however, can usually give better error
The Structure of a
diagnostics than a compiler, because it executes the Compiler

source program statement by statement.

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

Intermediate code compiled on one machine can be inter-


preted on another machine (virtual machine).

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

• Code starts executing interpreted with no delay.


• Methods that are found commonly executed (hot) are
JIT compiled.
• Once compiled code is available, the execution switches
to it. Introduction.16
Introduction

Dr. Nguyen Hua


Phung, MEng. Tran
Ngoc Bao Duy

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

Lexical analysis (scanner)


Token stream

Syntax analysis (parser)


Parse tree
Programming
languages
Abstract syntax tree (AST) generation Front-end Language evaluation
Abstract syntax tree
Influences on Language design

Semantic analysis (Static checking) Language


(Modified) AST implementation

Intermediate code generation The Structure of a


Compiler
Intermediate code
Machine-independent code
optimization (optional)
Modified IC
Target code generation Back-end
Target language
Machine-specific end code
optimization (optional)
Modified
target language

Introduction.18
Introduction
Lexical Analysis
Dr. Nguyen Hua
Phung, MEng. Tran
Ngoc Bao Duy

• The first phase of a compiler is called lexical analysis


Programming
or scanning. languages

• Reading the stream of characters making up the source Language evaluation


Influences on Language design

program and groups the characters into meaningful se- Language


implementation
quences called lexemes.
The Structure of a
• Removing extraneous characters like white space. Compiler

• Typically, removing comments and tags tokens with line


and column numbers.

Introduction.19
Introduction
Lexical Analysis
Dr. Nguyen Hua
Phung, MEng. Tran
Ngoc Bao Duy

Example

• Input: a = b + c * 60; Programming


• Output: languages
Language evaluation
Influences on Language design
1 (ID, "a")
Language
2 (EQUAL, "=") implementation
3 (ID, "b") The Structure of a
4 (PLUS, "+") Compiler

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

Figure: An example of parse tree


Introduction.20
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
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)

Figure: An example of abstract syntax tree (AST)

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

• Syntactic Checking: There is more to syntax than Influences on Language design

Language
grammars. implementation

For example, constraints such as an identifier being declared at The Structure of a


Compiler
most once in a scope, or that a break statement must have an
enclosing loop or switch statement, are syntactic, although they
are not encoded in, or enforced by, a grammar used for parsing.
• Type Checking: The type rules of a language assure
that an operator or function is applied to the right num-
ber and type of operands.

Introduction.22
Introduction
Intermediate code generation
Dr. Nguyen Hua
Phung, MEng. Tran
Ngoc Bao Duy

• After syntax and semantic analysis of the source pro-


gram, many compilers generate an explicit low-level
or machine-like intermediate representation, which can
think of as a program for an abstract machine. Programming
languages
• This intermediate representation should have two im- Language evaluation
Influences on Language design
portant properties:
Language
1 easy to produce. implementation

2 easy to translate into the target machine. The Structure of a


Compiler

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

4 Program Translations. The Structure of a


Compiler
5 Software Productivity Tools.

Introduction.24
Introduction
Related program
Dr. Nguyen Hua
Phung, MEng. Tran
Ngoc Bao Duy

• Preprocessor: processes its input data to produce out-


put that is used as input in another program.
• Assembler: changes computer instructions into ma-
Programming
chine code. languages

• Linker: takes one or more object files (generated by a Language evaluation


Influences on Language design

compiler or an assembler) and combines them into a Language


implementation
file.
The Structure of a
• Loader: a major component of an operating system Compiler

that ensures all necessary programs and libraries are


loaded
• Debugger: test and debug other programs.
• Editor: enable the user to create and edit (source)
files.

Introduction.25
Introduction

Dr. Nguyen Hua


Phung, MEng. Tran
Ngoc Bao Duy

Programming
languages
Language evaluation

THANK YOU. Influences on Language design

Language
implementation

The Structure of a
Compiler

Introduction.26

You might also like