CSE2304, feedback on prac-1, 2006

To start compilation of an expression, exp:
comp 0 "=" exp
case action
comp reg "=" x R reg = x
comp reg op x R reg op= x
comp reg "=" (exp op exp') comp reg "=" exp
comp reg op exp'
comp reg op (exp op' exp') comp (reg+1) "=" exp
comp (reg+1) op' exp'
R(reg) op= R(reg+1)
for all variables, x, expressions exp & exp', and arithmetic operators, op ("+", "-", "*", "/").
Note how comp recursively traverses the parse-tree of an expression.
comp 0 "=" x*y+z*w comp 0 "=" x*y comp 0 "=" x R0=x
comp 0 "*" y R0*=y
comp 0 "+" z*w comp 1 "=" z R1=z
comp 1 "*" w R1*=w
That is sufficient for the prac. By the way, an optimising compiler would do a lot more, possibly rearranging an expression, provided that the semantics of the programming language allow it to do so.
E.g. x+y*z (2 registers, 4 instructions) ---> y*z+x (1 register, 3 instructions),
but that was not required for the prac.

© 5/4/2006 L.Allison, Faculty of Information Technology (Clayton School of IT), ('05 was School of Computer Science and Software Engineering), Monash University, Australia 3800.
Created with "vi (Linux & Solaris)",   charset=iso-8859-1