SLIDE 1 CSCI 3136 Principles of Programming Languages
Summer 2013
Faculty of Computer Science Dalhousie University
1 / 62
SLIDE 2 Course Outline
2 / 62
SLIDE 3 Course Outline
- Introduction
- Programming language, History, Paradigms
3 / 62
SLIDE 4 Course Outline
- Introduction
- Programming language, History, Paradigms
- Implementation
4 / 62
SLIDE 5 Course Outline
- Introduction
- Programming language, History, Paradigms
- Implementation
- Lexical analysis and automata theory
- Syntactic Analysis and Context-Free Grammars
- Semantic analysis
- Names, scopes, and binding
- Control flow
- Data types and object-oriented programming
- Specialized topics
5 / 62
SLIDE 6 {calculate greatest common divisor} program gcd(input, output); var i, j : integer; begin read(i, j); while i <> j do if i > j then i := i - j else j := j - i; writeln(i); end.
SLIDE 7 {calculate greatest common divisor} program gcd(input, output); var i, j : integer; begin read(i, j); while i <> j do if i > j then i := i - j else j := j - i; writeln(i); end.
SLIDE 8 {calculate greatest common divisor} program gcd(input, output); var i, j : integer; begin read(i, j); while i <> j do if i > j then i := i - j else j := j - i; writeln(i); end.
SLIDE 9 {calculate greatest common divisor} program gcd(input, output); var i, j : integer; begin read(i, j); while i <> j do if i > j then i := i - j else j := j - i; writeln(i); end.
SLIDE 10 {calculate greatest common divisor} program gcd(input, output); var i, j : integer; begin read(i, j); while i <> j do if i > j then i := i - j else j := j - i; writeln(i); end.
SLIDE 11 {calculate greatest common divisor} program gcd(input, output); var i, j : integer; begin read(i, j); while i <> j do if i > j then i := i - j else j := j - i; writeln(i); end.
SLIDE 12 {calculate greatest common divisor} program gcd(input, output); var i, j : integer; begin read(i, j); while i <> j do if i > j then i := i - j else j := j - i; writeln(i); end.
Implementation
12 / 62
SLIDE 13 Compiler/ Interpreter
{calculate greatest common divisor} program gcd(input, output); var i, j : integer; begin read(i, j); while i <> j do if i > j then i := i - j else j := j - i; writeln(i); end.
Implementation
13 / 62
SLIDE 14
SLIDE 15
Compilation
SLIDE 16
Compiler Compilation
SLIDE 17
Compiler Source Program Compilation
SLIDE 18
Compiler Source Program Compilation Target Program
SLIDE 19
Compiler Source Program Compilation Target Program Target Program
SLIDE 20
Compiler Source Program Compilation Target Program Target Program Input
SLIDE 21
Compiler Source Program Compilation Target Program Target Program Input Output
SLIDE 22
Compiler Source Program Compilation Target Program Target Program Input Output Interpretation
SLIDE 23
Compiler Source Program Compilation Target Program Target Program Input Output Interpreter Interpretation
SLIDE 24
Compiler Source Program Compilation Target Program Target Program Input Output Interpreter Source Program Interpretation
SLIDE 25
Compiler Source Program Compilation Target Program Target Program Input Output Interpreter Source Program Interpretation Input
SLIDE 26 Compiler Source Program Compilation Target Program Target Program Input Output Interpreter Source Program Interpretation Input Output
26 / 62
SLIDE 27
Implementation Strategies: Example
Virtual machine
SLIDE 28 Implementation Strategies: Example
Virtual machine Translator Source Program Intermediate Program Virtual Machine Intermediate Program Input Output
28 / 62
SLIDE 29
Implementation Strategies: Example
Linker
SLIDE 30 Implementation Strategies: Example
Linker
Incomplete Machine Language Compiler Source Program Linker Incomplete Machine Language Library Routines Machine Language Program
30 / 62
SLIDE 31
Implementation Strategies: Example
The Preprocessor
SLIDE 32 Implementation Strategies: Example
The Preprocessor Preprocessor Source Program Modified Source Pro- gram Compiler Modified Source Program Assembly Language
32 / 62
SLIDE 33
Implementation Strategies: Example
Source-to-Source Translation (C++)
SLIDE 34 Implementation Strategies: Example
Source-to-Source Translation (C++) Preprocessor Source Program Modified Source Program C++ compiler Modified Source Program C code C compiler C code Assembly Language
34 / 62
SLIDE 35 Phases of Compilation
Semantic Analysis and Inter- mediate Code Generation Machine-independent Code Improvement (Optional) Target Code Generation Machine-specific Code Im- provement (Optional) Parser (syntactic analysis) Scanner (lexical analysis) Character Stream Token Stream Parse Tree Abstract Syntax Tree or Other Intermediate Form Modified Intermediate Form Target Language (e.g., assembly) Modified Target Language Symbol Table
35 / 62
SLIDE 36 Phases of Compilation
Semantic Analysis and Inter- mediate Code Generation Machine-independent Code Improvement (Optional) Target Code Generation Machine-specific Code Im- provement (Optional) Parser (syntactic analysis) Scanner (lexical analysis) Character Stream Token Stream Parse Tree Abstract Syntax Tree or Other Intermediate Form Modified Intermediate Form Target Language (e.g., assembly) Modified Target Language Symbol Table
36 / 62
SLIDE 37
Phases of Compilation
SLIDE 38
Phases of Compilation
Semantic Analysis and Inter- mediate Code Generation Parser (syntactic analysis) Scanner (lexical analysis)
SLIDE 39
Phases of Compilation
Semantic Analysis and Inter- mediate Code Generation Parser (syntactic analysis) Scanner (lexical analysis) Character Stream
SLIDE 40
Phases of Compilation
Semantic Analysis and Inter- mediate Code Generation Parser (syntactic analysis) Scanner (lexical analysis) Character Stream Token Stream
SLIDE 41
Phases of Compilation
Semantic Analysis and Inter- mediate Code Generation Parser (syntactic analysis) Scanner (lexical analysis) {calculate greatest common divisor} program gcd(input, output); var i, j : integer; begin read(i, j); while i <> j do if i > j then i := i - j else j := j - i; writeln(i); end.
SLIDE 42
Phases of Compilation
Semantic Analysis and Inter- mediate Code Generation Parser (syntactic analysis) Scanner (lexical analysis) {calculate greatest common divisor} program gcd(input, output); var i, j : integer; begin read(i, j); while i <> j do if i > j then i := i - j else j := j - i; writeln(i); end.
SLIDE 43 Phases of Compilation
Semantic Analysis and Inter- mediate Code Generation Parser (syntactic analysis) Scanner (lexical analysis) {calculate greatest common divisor} program gcd(input, output); var i, j : integer; begin read(i, j); while i <> j do if i > j then i := i - j else j := j - i; writeln(i); end. {calculate greatest common divisor} program gcd ( input ,
) ; var i , j : integer ; begin read ( i , j ) ; while i <> j do if i > j then i := i
else j := j
; writeln ( i ) ; end .
SLIDE 44 Phases of Compilation
Semantic Analysis and Inter- mediate Code Generation Parser (syntactic analysis) Scanner (lexical analysis) {calculate greatest common divisor} program gcd(input, output); var i, j : integer; begin read(i, j); while i <> j do if i > j then i := i - j else j := j - i; writeln(i); end. {calculate greatest common divisor} program gcd ( input ,
) ; var i , j : integer ; begin read ( i , j ) ; while i <> j do if i > j then i := i
else j := j
; writeln ( i ) ; end .
SLIDE 45 Phases of Compilation
Semantic Analysis and Inter- mediate Code Generation Parser (syntactic analysis) Scanner (lexical analysis) {calculate greatest common divisor} program gcd(input, output); var i, j : integer; begin read(i, j); while i <> j do if i > j then i := i - j else j := j - i; writeln(i); end. {calculate greatest common divisor} program gcd ( input ,
) ; var i , j : integer ; begin read ( i , j ) ; while i <> j do if i > j then i := i
else j := j
; writeln ( i ) ; end .
- Remove comments
- Remove extraneous
characters
SLIDE 46 Phases of Compilation
Semantic Analysis and Inter- mediate Code Generation Parser (syntactic analysis) Scanner (lexical analysis) {calculate greatest common divisor} program gcd(input, output); var i, j : integer; begin read(i, j); while i <> j do if i > j then i := i - j else j := j - i; writeln(i); end. {calculate greatest common divisor} program gcd ( input ,
) ; var i , j : integer ; begin read ( i , j ) ; while i <> j do if i > j then i := i
else j := j
; writeln ( i ) ; end .
- Remove comments
- Remove extraneous
characters
SLIDE 47 Phases of Compilation
Semantic Analysis and Inter- mediate Code Generation Parser (syntactic analysis) Scanner (lexical analysis) {calculate greatest common divisor} program gcd(input, output); var i, j : integer; begin read(i, j); while i <> j do if i > j then i := i - j else j := j - i; writeln(i); end. {calculate greatest common divisor} program gcd ( input ,
) ; var i , j : integer ; begin read ( i , j ) ; while i <> j do if i > j then i := i
else j := j
; writeln ( i ) ; end .
- Remove comments
- Remove extraneous
characters
SLIDE 48 Phases of Compilation
Semantic Analysis and Inter- mediate Code Generation Parser (syntactic analysis) Scanner (lexical analysis) {calculate greatest common divisor} program gcd(input, output); var i, j : integer; begin read(i, j); while i <> j do if i > j then i := i - j else j := j - i; writeln(i); end. {calculate greatest common divisor} program gcd ( input ,
) ; var i , j : integer ; begin read ( i , j ) ; while i <> j do if i > j then i := i
else j := j
; writeln ( i ) ; end .
- Remove comments
- Remove extraneous
characters
SLIDE 49 Phases of Compilation
Semantic Analysis and Inter- mediate Code Generation Parser (syntactic analysis) Scanner (lexical analysis) {calculate greatest common divisor} program gcd(input, output); var i, j : integer; begin read(i, j); while i <> j do if i > j then i := i - j else j := j - i; writeln(i); end. {calculate greatest common divisor} program gcd ( input ,
) ; var i , j : integer ; begin read ( i , j ) ; while i <> j do if i > j then i := i
else j := j
; writeln ( i ) ; end .
- Remove comments
- Remove extraneous
characters
characters into tokens
49 / 62
SLIDE 50 Phases of Compilation
Semantic Analysis and Inter- mediate Code Generation Machine-independent Code Improvement (Optional) Target Code Generation Machine-specific Code Im- provement (Optional) Parser (syntactic analysis) Scanner (lexical analysis) Character Stream Token Stream Parse Tree Abstract Syntax Tree or Other Intermediate Form Modified Intermediate Form Target Language (e.g., assembly) Modified Target Language Symbol Table
50 / 62
SLIDE 52
Phases of Compilation
SLIDE 53
Phases of Compilation
Semantic Analysis and Inter- mediate Code Generation Parser (syntactic analysis) Parser (syntactic analysis)
SLIDE 54
Phases of Compilation
Semantic Analysis and Inter- mediate Code Generation Parser (syntactic analysis) Parser (syntactic analysis) Token Stream
SLIDE 55
Phases of Compilation
Semantic Analysis and Inter- mediate Code Generation Parser (syntactic analysis) Parser (syntactic analysis) Token Stream Parse Tree
SLIDE 56 Phases of Compilation
Semantic Analysis and Inter- mediate Code Generation Parser (syntactic analysis) Parser (syntactic analysis) {calculate greatest common divisor} program gcd ( input ,
) ; var i , j : integer ; begin read ( i , j ) ; while i <> j do if i > j then i := i
else j := j
; writeln ( i ) ; end .
SLIDE 57 Phases of Compilation
Semantic Analysis and Inter- mediate Code Generation Parser (syntactic analysis) Parser (syntactic analysis) {calculate greatest common divisor} program gcd ( input ,
) ; var i , j : integer ; begin read ( i , j ) ; while i <> j do if i > j then i := i
else j := j
; writeln ( i ) ; end .
SLIDE 58 Phases of Compilation
Semantic Analysis and Inter- mediate Code Generation Parser (syntactic analysis) Parser (syntactic analysis) {calculate greatest common divisor} program gcd ( input ,
) ; var i , j : integer ; begin read ( i , j ) ; while i <> j do if i > j then i := i
else j := j
; writeln ( i ) ; end .
58 / 62
SLIDE 60 Phases of Compilation
Semantic Analysis and Inter- mediate Code Generation Machine-independent Code Improvement (Optional) Target Code Generation Machine-specific Code Im- provement (Optional) Parser (syntactic analysis) Scanner (lexical analysis) Character Stream Token Stream Parse Tree Abstract Syntax Tree or Other Intermediate Form Modified Intermediate Form Target Language (e.g., assembly) Modified Target Language Symbol Table
60 / 62