Assignment 3 Solution Background

Slides:



Advertisements
Similar presentations
COMPILER CONSTRUCTION WEEK-2: LANGUAGE DESCRIPTION- SYNTACTIC STRUCTURE:
Advertisements

CPSC 388 – Compiler Design and Construction
Semantics Static semantics Dynamic semantics attribute grammars
1 Compiler Construction Intermediate Code Generation.
Code Generation Introduction. Compiler (scalac, gcc) Compiler (scalac, gcc) machine code (e.g. x86, arm, JVM) efficient to execute i=0 while (i < 10)
Prefix, Postfix, Infix Notation
Arithmetic Expressions Infix form –operand operator operand 2+3 or a+b –Need precedence rules –May use parentheses 4*(3+5) or a*(b+c)
1 Chapter 7: Expressions Expressions are the fundamental means of specifying computations in a programming language To understand expression evaluation,
Mathematical Operators: working with floating point numbers and more operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this.
Consider With x = 10 we may proceed as (10-1) = 9 (10-7) = 3 (9*3) = 27 (10-11) = -1 27/(-1) = -27 Writing intermediates on paper.
Compiler design Computer Science Rensselaer Polytechnic Lecture 1.
Code Generation Introduction. Compiler (scalac, gcc) Compiler (scalac, gcc) machine code (e.g. x86, arm, JVM) efficient to execute i=0 while (i < 10)
Syntax Directed Definitions Synthesized Attributes
Syntax Directed Translation. Syntax directed translation Yacc can do a simple kind of syntax directed translation from an input sentence to C code We.
1 Abstract Syntax Tree--motivation The parse tree –contains too much detail e.g. unnecessary terminals such as parentheses –depends heavily on the structure.
Expressions creating information. topics  operators: precedence, associativity, parentheses, overloading  operands: side-effects, coercion  arithmetic,
C H A P T E R S E V E N Expressions and Assignment Statements.
SUNY Oneonta Data Structures and Algorithms Visualization Teaching materials Generation Group.
CSC 205 Programming II Postfix Expressions. Recap: Stack Stack features Orderly linear structure Access from one side only – top item Stack operations.
Krakatoa: Decompilation in Java “Does Bytecode Reveal Source?” Todd A. Proebsting Scott A. Watterson The University of Arizona Presented by Karl von Randow.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
Compilers: IC/10 1 Compiler Structures Objective – –describe intermediate code generation – –explain a stack-based intermediate code for the expression.
Chapter 8: Intermediate Code Generation
Code Optimization 1 Course Overview PART I: overview material 1Introduction 2Language processors (tombstone diagrams, bootstrapping) 3Architecture of a.
Linear Data Structures LIFO – Polish notation Context Saving.
Mixing integer and floating point numbers in an arithmetic operation.
CPS 506 Comparative Programming Languages Syntax Specification.
Programming Languages
More on MIPS programs n SPIM does not support everything supported by a general MIPS assembler. For example, –.end doesn’t work Use j $ra –.macro doesn’t.
Prefix, Postfix and Infix. Infix notation  A-B/(C+D)  evaluate C+D (call the result X),  then B/X (call the result Y),  and finally A-Y.  The order.
Numeric Data Types There are six numeric data types: byte, short, int, long, float, and double. Sample variable declarations: int i, j, k; float numberOne,
Prefix, Postfix, Infix Notation. Infix Notation  To add A, B, we write A+B  To multiply A, B, we write A*B  The operators ('+' and '*') go in between.
Chapter 7 Expressions and Assignment Statements. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 7 Topics Introduction Arithmetic Expressions.
Prologue Sung-Dong Kim, Dept. of Computer Engineering, Hansung University.
Arithmetic Instructions. Integer and Float Conversions.
Expressions and Assignment Statements
The Java Virtual Machine (JVM)
Constructing Precedence Table
Infix to postfix conversion
Chapter 2 Assignment and Interactive Input
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
PROGRAMMING LANGUAGES
ASTs, Grammars, Parsing, Tree traversals
CS 153: Concepts of Compiler Design November 2 Class Meeting
Stacks Chapter 4.
Stack application: postponing data usage
Visit for more Learning Resources
PART II STACK APPLICATIONS
Assignment 3 Solution Background
Intermediate Code Generation Part I
ENERGY 211 / CME 211 Lecture 15 October 22, 2008.
Stack Memory 2 (also called Call Stack)
Overview of Compilation The Compiler BACK End
Infix to Postfix Conversion
Intermediate Code Generation Part I
Compiler design.
Intermediate Code Generation Part I
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Infix to Postfix Conversion
(Part 2) Infix, Prefix & Postfix
Data Types and Expressions
Building Java Programs
Compiler Construction
CMPE 152: Compiler Design April 11 Class Meeting
CO4301 – Advanced Games Development Week 3 Parsing Continued
Intermediate Code Generation Part I
Compiler Construction
Assignment Solution Sketch
Building Java Programs
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 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

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 i2d iload_3 dload_0 dmul dadd

Translation algorithm essence trans (e1 * e2) = trans(e1) [type coercion code?] trans(e2) trans(*) Map grammar rules to control structures E.g., alternatives, while-loop, etc