01 Intro
01 Intro
Chapter 1 Introduction
Dongjie He
University of New South Wales
https://dongjiehe.github.io/teaching/compiler/
29 Jun 2023
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
UNSW, Sydney Compilers 29 Jun 2023 1 / 21
Outline
1 Programming Languages
Evolution
Basics
2 Language Processors
Compilers, Interpreters, and Other Language Processors
A Language-Processing System
3 Compiler Structure
Lexical Analysis
Syntax Analysis
Semantic Analysis
Intermediate Code Generation and Optimization
Code Generation
Symbol-Table Management
4 Applications of Compiler Technology
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
UNSW, Sydney Compilers 29 Jun 2023 2 / 21
Programming Languages Evolution
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
UNSW, Sydney Compilers 29 Jun 2023 5 / 21
Programming Languages Basics
Compilers
source program Compiler target program
e.g., hello.c e.g., gcc e.g., hello.o
Interpreters
source program
e.g., hello.py Interpreter output
input e.g., python3
Hybrid Compilers
Translator source program
e.g., javac e.g., hello.java
intermediate program
e.g., hello.class Virtual Machine output
input e.g., JVM
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
UNSW, Sydney Compilers 29 Jun 2023 8 / 21
Language Processors Compilers, Interpreters, and Other Language Processors
A Language-Processing System
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
UNSW, Sydney Compilers 29 Jun 2023 10 / 21
Compiler Structure
Phases of a Compiler
front end or analysis part
character stream
intermediate representation
Symbol Table
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
UNSW, Sydney Compilers 29 Jun 2023 11 / 21
Compiler Structure Lexical Analysis
Lexical Analysis
lexical analysis/scanning
grouping characters into meaningful lexemes sequences
lexeme = ⟨token-name, attribute-value⟩
token-name: abstract symbol used during syntax analysis
attribute-value: stored in symbol-table and used in semantic analysis
and code generation
An example
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
UNSW, Sydney Compilers 29 Jun 2023 12 / 21
Compiler Structure Syntax Analysis
Syntax Analysis
syntax: describes the proper form of programs
syntax analysis/parsing
transform tokens into an Intermediate Representation, e.g., syntax tree
IR depicts the grammatical structure of the token stream
An example
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
UNSW, Sydney Compilers 29 Jun 2023 13 / 21
Compiler Structure Semantic Analysis
Semantic Analysis
semantics: define what programs mean
semantic analysis
check semantic consistency with the language definition
gather and save type information in syntax tree or symbol table
type checking and type conversions (coercions)
An example
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
UNSW, Sydney Compilers 29 Jun 2023 14 / 21
Compiler Structure Intermediate Code Generation and Optimization
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
UNSW, Sydney Compilers 29 Jun 2023 15 / 21
Compiler Structure Intermediate Code Generation and Optimization
Code Optimization
machine-independent/machine-dependent code-optimization
objectives: produce better target code
faster running time
shorter target code
consuming less power
...
An example
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
UNSW, Sydney Compilers 29 Jun 2023 16 / 21
Compiler Structure Code Generation
Code Generation
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
UNSW, Sydney Compilers 29 Jun 2023 17 / 21
Compiler Structure Symbol-Table Management
Symbol-Table Management
Symbol table:
a data structure recording each variable name and attributes, including
storage, type, scope, ...
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
UNSW, Sydney Compilers 29 Jun 2023 18 / 21
Applications of Compiler Technology
Dongjie He
University of New South Wales
https://dongjiehe.github.io/teaching/compiler/
29 Jun 2023
. . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . .
UNSW, Sydney Compilers 29 Jun 2023 20 / 21
End-of-Chapter