Gordon S. Novak Jr. Department of Computer Sciences University of Texas at Austin novak@cs.utexas.edu http://www.cs.utexas.edu/users/novak
Copyright c Gordon S. Novak Jr.1
A few slides reproduce figures from Aho, Lam, Sethi, and Ullman, Compilers: Principles, Techniques, and Tools, Addison-Wesley; these have footnote credits.
1
1
I wish to preach not the doctrine of ignoble ease, but the doctrine of the strenuous life. – Theodore Roosevelt
Innovation requires Austin, Texas. We need faster chips and great compilers. Both those things are from Austin. – Guy Kawasaki
2
Course Topics • Introduction • Lexical Analysis – Regular grammars – Hand-written lexical analyzer – Number conversion – Regular expressions – LEX • Syntax Analysis – Context-free grammars – Operator precedence – Recursive descent parsing – Shift-reduce parsing, YACC – Intermediate code – Symbol tables • Code Generation – Code generation from trees – Register assignment – Array references – Subroutine calls • Optimization – Constant folding, partial evaluation – Data flow analysis
3
Pascal Test Program
{ program 4.9 from Jensen & Wirth: graph1.pas } program graph1(output); const d = 0.0625; {1/16, 16 lines for [x,x+1]} s = 32; {32 character widths for [y,y+1]} h = 34; {character position of x-axis} c = 6.28318; {2*pi} lim = 32; var x,y : real; i,n : integer; begin for i := 0 to lim do begin x := d*i; y := exp(-x)*sin(c*x); n := round(s*y) + h; repeat write(’ ’); n := n-1 until n=0; writeln(’*’) end end. calling graph1 * * * * * * * * * * * * * * * * * * * * *
4
Introduction • What a compiler does; why we need compilers • Parts of a compiler and what they do • Data flow between the parts
5
Machine Language Machine language is the only language directly executable on a computer, but it is very hard for humans to write: • Absolute Addresses: hard to insert code. • Numeric Codes, e.g. remember. for operations: hard to
• Bit fields, e.g. for registers: hard to