Lecture 18 Read S&G ch. 10 (Models of Computation) Compiler: A

advertisement
Lecture 18
3/17/04 22:24
Lecture 18
Read S&G ch. 10
(Models of Computation)
Compilers and
Language Translation
(S&G, ch. 9)
3/17/04
CS 100 - Lecture 18
1
Program Development
Editor
Create high-level
language program
Not OK (error messages)
Compiler
Check for syntax
errors and translate
to machine code
OK
3/17/04
CS 100 - Lecture 18
Compiler: A
Programming
Language Translator
Set sum to 0
Set count to 1
While count <= 10 do
Set sum to sum + count
Increment count
Output sum
Run-time
system
Compile
Execute the machine
language program on
any machine
3/17/04
CS 100 - Lecture 18
3
3/17/04
slide < S. Levy
slide < S. Levy
CS 100
CS 100 - Lecture 18
4
Recognizing Constructs: Parsing
• Could try to translate, e.g., from every
possible if/else to its corresponding
assembly code:
• Recognize programming constructs
(conditionals, loops) and translate them to
assembly language (compare, jump)
• Reject and report unrecognized programs
• Keep track of symbols (sum, count) and
declare each one using a .data directive
• Generate efficient assembly language code
CS 100 - Lecture 18
.begin
clear sum
load one
store count
while: load count
compare eleven
jumpeq endwhile
add sum
store sum
increment count
jump while
endwhile: out sum
halt
one:
.data 1
eleven: .data 11
sum:
.data 0
count: .data 0
.end
slide < S. Levy
Compiler: Requirements / Tasks
3/17/04
2
if x > y
output x
else
output y
5
3/17/04
Rule #18734
CS 100 - Lecture 18
load x
compare y
jumpgt outx
out y
jump next
outx: out x
next: ....
6
slide < S. Levy
1
Lecture 18
3/17/04 22:24
Recognizing Constructs: Parsing (2)
• Could try to translate, e.g., from every
possible if/else to its corresponding
assembly code:
Rule #22102
if a < b
output a
else
output b
Recognizing Constructs: Parsing (3)
• Instead, we recognize (abstract) structures,
and work on their components one at a time:
if/else
load a
compare b
jumplt outa
out b
jump next
outa: out a
next: ....
output
<
a
b
output
a
b
• Impossible, because we’d need an infinite
number of rules!
3/17/04
CS 100 - Lecture 18
7
3/17/04
CS 100 - Lecture 18
slide < S. Levy
Recognizing Constructs: Parsing (4)
BNF Grammar
• Now we can work on smaller pieces:
Parse
Generate
<
a < b
a
load a
compare b
b
• When parsing fails, compiler reports a syntax error:
Parse
a << b
3/17/04
8
slide < S. Levy
Unrecognized operator:
<<
CS 100 - Lecture 18
9
• A notation for expressing all the allowable
statements in a language
• A finite description of an infinite number of
possibilities
• Each distinct arrangement is shown in
schematic form
• Because languages are nested hierarchically,
the definitions are recursive
3/17/04
CS 100 - Lecture 18
10
slide < S. Levy
Example Grammar
Additional Example
1. 〈assignment statement〉 ::=
〈variable〉 = 〈expression〉
2. 〈expression〉 ::= 〈variable〉
| 〈expression〉 〈arith op〉 〈variable〉
| ( 〈expression〉 )
3. 〈arith op〉 ::= + | – | * | /
4. 〈variable〉 ::= x | y | z
⇐ Unrealistic!
3/17/04
CS 100
CS 100 - Lecture 18
1. 〈if statement〉 ::=
if ( 〈Boolean expression〉 ) 〈assignment statement〉 ;
〈else clause〉
2. 〈Boolean expression〉 ::= 〈variable〉
| 〈expression〉 〈relation〉 〈expression〉
3. 〈relation〉 ::= == | < | >
4. 〈else clause〉 ::= else 〈assignment statement〉
|Λ
11
3/17/04
CS 100 - Lecture 18
12
2
Lecture 18
3/17/04 22:24
Keeping Track of Symbols:
Symbol Tables
Keeping Track of Symbols:
Symbol Tables (2)
• When a symbol is first set, compiler should
declare it:
• Failure to set initial value should result in an error:
Compile
Set sum to 0
Set sum to 0
While count <= 10 do
sum: .data 0
• Subsequent references to symbol don't need
a new declaration:
• Compiler uses a symbol table (dictionary;
phonebook) to keep track of values we’ve
declared
Compile
load sum
add count
store sum
Set sum to sum + count
3/17/04
CS 100 - Lecture 18
13
3/17/04
CS 100 - Lecture 18
slide < S. Levy
• In a language like Java, symbol table would contain
info about variable types:
int
int
String
double
• Then compiler can use symbol table to detect “type
errors”:
a = a + s;
3/17/04
can't set int to String
CS 100 - Lecture 18
Optimization: Generating
Efficient Assembly Code
15
• “Laundry metaphor”: Unoptimized
1.
2.
3.
4.
5.
Load dirty clothes into washer
Run washer
Unload wet clothes from washer to basket
Load wet clothes into dryer from basket
Run dryer
• Unoptimized version allows us to break down the
task into simple parts
• Requires less coordination between washer & dryer
3/17/04
slide < S. Levy
unoptimized
Set a to a + b
Output a
Set a to a + c
1. Load dirty clothes into washer
2. Run washer
3. Load wet clothes from washer to dryer
4. Run dryer
optimized
• Optimized version saves a step and doesn't require a
basket
• Requires more coordination between washer &
dryer
slide < S. Levy
CS 100
16
Optimization
• “Laundry metaphor”: Optimized
CS 100 - Lecture 18
CS 100 - Lecture 18
slide < S. Levy
Optimization: Generating
Efficient Assembly Code (2)
3/17/04
14
slide < S. Levy
Symbol Tables
a:
b:
s:
x:
“count” not
declared
17
load a
add b
store a
out a
load a
add c
store a
load a
add b
store a
out a
add c
store a
• Note coordination required between first and third
steps of pseudocode
3/17/04
CS 100 - Lecture 18
18
slide < S. Levy
3
Download