Theory of Programming Languages: An Overview Lecture # 1
Theory of Programming Languages: An Overview Lecture # 1
Programming
Languages
AN OVERVIEW
LECTURE # 1
The Course
Course Code: CS-0000
Course Title: Theory of Programming Languages
Instructor: Amna Anjum
◦ Email Address: [email protected]
Mid-Term Exam 20
Assignments and Quizzes 20
Presentation & Project 20
Final Exams. 40
5
Why Take this Course
Understand compilers and languages
Understand the code structure
Understand language semantics
Understand relation between source code and
generated machine code
Become a better programmer
Theory
◦ mathematical models: regular expressions, automata, grammars, graphs
◦ algorithms that use these models
6
What are Compilers
Translate information from one representation to another
Usually information = program
7
Examples
Typical Compilers:
◦ VC, VC++, GCC, JavaC
◦ FORTRAN, Pascal, VB(?)
Translators
◦ Word to PDF
◦ PDF to Postscript
8
Typical Compilation
High-level source code
Compiler
10
Source Code
Optimized for human readability
Matches human notions of grammar
Uses named constructs such as variables and procedures
11
Assembly Code
.globl _expr imull %eax,%edx
_expr: movl 8(%ebp),%eax
pushl %ebp incl %eax
movl %esp,%ebp imull %eax,%edx
13
How to Translate
Correctness:
the generated machine code must execute precisely the same computation
as the source code
14
How to Translate
Is there a unique translation? No!
Is there an algorithm for an “ideal translation”? No!
15
How to Translate
16
Two-pass Compiler
errors
17
Two-pass Compiler
Use an intermediate representation (IR)
Front end maps legal source code into IR
Back end maps IR into target machine code
Admits multiple front ends & multiple passes
Front end is O(n) or O(n log n)
Back end is NP-Complete (NPC)
18
Front End
19
The Front-End
source tokens IR
scanner parser
code
Modules errors
Scanner
Parser
20
Scanner
source tokens IR
scanner parser
code
errors
21
Scanner
22
Scanner
Example
x = x + y
becomes
<id,x>
<assign,=> <id,x>
<id,x>
<op,+>
<id,y> word
token type
23
Scanner
24
Parser
source tokens IR
scanner parser
code
errors
25
Parser
Recognizes context-free syntax and reports
errors
Guides context-sensitive (“semantic”)
analysis
Builds IR for source program
26
Context-Free Grammars
Context-free syntax is specified
with a grammar G=(S,N,T,P)
S is the start symbol
N is a set of non-terminal symbols
T is set of terminal symbols or words
P is a set of productions or rewrite rules
27
Context-Free Grammars
28
The Front End
For this CFG
S = goal
T = { number, id, +, -}
N = { goal, expr, term, op}
P = { 1, 2, 3, 4, 5, 6, 7}
29
Context-Free Grammars
30
Derivation
Production Result
goal
1 expr
2 expr op term
5 expr op y
7 expr – y
2 expr op term – y
4 expr op 2 – y
6 expr + 2 – y
3 term + 2 – y
5 x+2–y 31
The Front End
To recognize a valid sentence in some CFG,
we reverse this process and build up a
parse
A parse can be represented by a tree:
parse tree or syntax tree
32
Parse
Production Result
goal
1 expr
2 expr op term
5 expr op y
7 expr – y
2 expr op term – y
4 expr op 2 – y
6 expr + 2 – y
3 term + 2 – y
5 x+2–y
33
goal
Syntax Tree
expr
x+2-y
expr op term
<id,x> 34
Abstract Syntax Trees
35
Abstract Syntax Trees
–
+ <id,y>
<id,x> <number,2>
+ <id,y>
<id,x> <number,2>
37
Abstract Syntax Trees
–
+ <id,y>
<id,x> <number,2>
ASTs are one kind of intermediate
representation (IR)
38