Sei sulla pagina 1di 22

Compiler Construction

Mr Sadaquat Ali Bhatti


Source Code
Optimized for human readability
Matches human notions of grammar
Uses named constructs such as variables
and procedures

2
Assembly Code
.globl _expr
_expr:
imull %eax,%edx
pushl %ebp
movl 8(%ebp),%eax
movl %esp,%ebp
incl %eax
subl $24,%esp
imull %eax,%edx
movl 8(%ebp),%eax
movl %edx,-4(%ebp)
movl %eax,%edx
movl -4(%ebp),%edx
leal 0(,%edx,4),%eax
movl %edx,%eax
movl %eax,%edx
jmp L2
imull 8(%ebp),%edx
.align 4
movl 8(%ebp),%eax
L2:
incl %eax
leave
ret
3
Assembly Code
Optimized for hardware
Consists of machine instructions
Uses registers and unnamed memory
locations
Much harder to understand by humans

4
How to Translate
Correctness:
the generated machine code must
execute precisely the same computation
as the source code

5
How to Translate
Is there a unique translation? No!
Is there an algorithm for an ideal
translation? No!

6
How to Translate
Translation is a complex
process
source language and
generated code are very
different
Need to structure the
translation
7
Scanner
source tokens IR
code scanner parser

errors

8
Scanner
Maps character stream into
words basic unit of syntax
Produces pairs
a word and
its part of speech

9
Scanner
Example
x = x + y
becomes
<id,x>
<assign,=> <id,x>
<id,x>
<op,+> word
<id,y> token type

10
Scanner
we call the pair
<token type, word> a
token
typical tokens: number,
identifier, +, -, new, while, if

11
Parser

source tokens IR
code scanner parser

errors

12
Parser
Recognizes context-free
syntax and reports errors
Guides context-sensitive
(semantic) analysis
Builds IR for source
program
13
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

14
end

Mr Sadaquat Ali Bhatti


Context-Free Grammars
Grammar for expressions
1. goal expr
2. expr expr op term
3. | term
4. term number
5. | id
6. op +
7. | -

16
The Front End
For this CFG
S = goal
T = { number, id, +, -}
N = { goal, expr, term, op}
P = { 1, 2, 3, 4, 5, 6, 7}

17
Context-Free Grammars
Given a CFG, we can derive
sentences by repeated
substitution
Consider the sentence
(expression)
x+2y
18
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+2y
19
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
20
Parse
Result
Production
goal
1 expr
2 expr op term
5 expr op y
7 expr y
expr op term y
2
4 expr op 2 y
6 expr + 2 y
3 term + 2 y 21
Mr Sadaquat Ali Bhatti

Potrebbero piacerti anche