1 / 9

Converting Infix Expressions to Postfix and Evaluating using Stack Techniques

This assignment focuses on the conversion of infix expressions to postfix notation while employing stack evaluation techniques. It covers the evaluation of postfix expressions using stacks with examples like `2 3 4 * +`, followed by compiling constant expressions for a stack machine. The process involves generating bytecode for static functions and exploring right and left associative operations with numerics, both integer and double-inclusive. Additionally, grammar rules are mapped to control structures, demonstrating the link between expression evaluation and programming control flow.

howe
Download Presentation

Converting Infix Expressions to Postfix and Evaluating using Stack Techniques

An Image/Link below is provided (as is) to download presentation 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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Assignment 2 Solution Sketch

  2. 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|

  3. 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

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

  5. 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

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

  7. 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

  8. 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

  9. 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

More Related