Assignment 3 Solution Background

1 / 9

# Assignment 3 Solution Background - PowerPoint PPT Presentation

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

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'Assignment 3 Solution Background' - venus-maxwell

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

### Assignment 3 Solution Background

Constant Expression : Infix to postfix

2 + 3 * 4

( 2 + (3 * 4 ) )

2 3 4 * +

• Evaluating postfix expression using stack

| 2 |

| 2 | 3 | 4 |

| 2 | 12 |

|14|

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

Generalizing to expressions with variables

i + j * k

Push i

Push j

Push k

Mul

• Conversion to abstract syntax tree

+

i *

j k

Generalizing to expressions with variables

i + j * k

Push i

Push j

Push k

Mul

• Byte code generation for static f(int i,j,k)

imul

Right associative “+”

Left associative “+”

Byte code for i + j + k for static f(int i,j,k)

Push a

Push b

• Byte code generation for static f(double a,b)

Mixing int and double variables (requiring coercion code) for static f(double a,int i, j)

i + j * a

i2d

i2d

dmul

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