Download - Assignment 3 Solution Background
![Page 1: Assignment 3 Solution Background](https://reader036.vdocument.in/reader036/viewer/2022082710/56812c7e550346895d912a6b/html5/thumbnails/1.jpg)
Assignment 3 Solution Background
![Page 2: Assignment 3 Solution Background](https://reader036.vdocument.in/reader036/viewer/2022082710/56812c7e550346895d912a6b/html5/thumbnails/2.jpg)
• Constant Expression : Infix to postfix2 + 3 * 4
( 2 + (3 * 4 ) )
2 3 4 * +
• Evaluating postfix expression using stack| 2 |
| 2 | 3 | 4 |
| 2 | 12 |
|14|
![Page 3: Assignment 3 Solution Background](https://reader036.vdocument.in/reader036/viewer/2022082710/56812c7e550346895d912a6b/html5/thumbnails/3.jpg)
• Evaluating postfix expression using stack| 2 |
| 2 | 3 | 4 |
| 2 | 12 |
|14|
• Compiling constant expression for a stack machine
Push 2
Push 3
Push 4
Mul
Add
![Page 4: Assignment 3 Solution Background](https://reader036.vdocument.in/reader036/viewer/2022082710/56812c7e550346895d912a6b/html5/thumbnails/4.jpg)
• Generalizing to expressions with variables
i + j * k Push i
Push j
Push k
Mul
Add
• Conversion to abstract syntax tree
+
i *
j k
![Page 5: Assignment 3 Solution Background](https://reader036.vdocument.in/reader036/viewer/2022082710/56812c7e550346895d912a6b/html5/thumbnails/5.jpg)
• 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
![Page 6: Assignment 3 Solution Background](https://reader036.vdocument.in/reader036/viewer/2022082710/56812c7e550346895d912a6b/html5/thumbnails/6.jpg)
Byte code for i + j + k for static f(int i,j,k)
• Right associative “+”
iload_0
iload_1
iload_2
iadd
iadd
• Left associative “+”
iload_0
iload_1
iadd
iload_2
iadd
![Page 7: Assignment 3 Solution Background](https://reader036.vdocument.in/reader036/viewer/2022082710/56812c7e550346895d912a6b/html5/thumbnails/7.jpg)
• 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
![Page 8: Assignment 3 Solution Background](https://reader036.vdocument.in/reader036/viewer/2022082710/56812c7e550346895d912a6b/html5/thumbnails/8.jpg)
• Mixing int and double variables (requiring coercion code) for static f(double a,int i, j)
i + j * a iload_2
i2d
iload_3
i2d
dload_0
dmul
dadd
![Page 9: Assignment 3 Solution Background](https://reader036.vdocument.in/reader036/viewer/2022082710/56812c7e550346895d912a6b/html5/thumbnails/9.jpg)
• Translation algorithm essence
trans (e1 * e2) =
trans(e1)
[type coercion code?]
trans(e2)
[type coercion code?]
trans(*)
• Map grammar rules to control structures– E.g., alternatives, while-loop, etc