Assignment 3 Solution Background Constant Expression Infix to

Assignment 3 Solution Background

• Constant Expression : Infix to postfix 2+3*4 ( 2 + (3 * 4 ) ) 2 3 4*+ • Evaluating postfix expression using stack |2|3|4| | 2 | 12 | |14|

• Evaluating postfix expression using stack |2|3|4| | 2 | 12 | |14| • Compiling constant expression for a stack machine Push 2 Push 3 Push 4 Mul Add

• Generalizing to expressions with variables i+j*k Push i Push j Push k Mul Add • Conversion to abstract syntax tree + i * j k

• Generalizing to expressions with variables i+j*k Push i Push j Push k Mul Add • Byte code generation for static f(int i, j, k) iload_0 iload_1 iload_2 imul iadd

Byte code for i + j + k for static f(int i, j, k) • Right associative “+” iload_0 iload_1 iload_2 iadd • Left associative “+” iload_0 iload_1 iadd iload_2 iadd

• Introducing numeric types with real variables a+b Push a Push b Add • Byte code generation for static f(double a, b) dload_0 dload_2 dadd

• Mixing int and double variables (requiring coercion code) for static f(double a, int i, j) i+j*a iload_2 i 2 d iload_3 i 2 d dload_0 dmul dadd

• Translation algorithm essence trans (e 1 * e 2) = trans(e 1) [type coercion code? ] trans(e 2) [type coercion code? ] trans(*) • Map grammar rules to control structures – E. g. , alternatives, while-loop, etc
- Slides: 9