Assignment 3 Solution Background

Slides:



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

Lesson 6 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
CPSC 388 – Compiler Design and Construction
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)
Lexical and Syntactic Analysis Here, we look at two of the tasks involved in the compilation process –Given source code, we need to first break it into.
9/27/2006Prof. Hilfinger, Lecture 141 Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik)
1 Chapter 7: Expressions Expressions are the fundamental means of specifying computations in a programming language To understand expression evaluation,
1 Chapter 5 Compilers Source Code (with macro) Macro Processor Expanded Code Compiler or Assembler obj.
Environments and Evaluation
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.
Syntax Directed Definitions Synthesized Attributes
C H A P T E R S E V E N Expressions and Assignment Statements.
1 Week 4 Questions / Concerns Comments about Lab1 What’s due: Lab1 check off this week (see schedule) Homework #3 due Wednesday (Define grammar for your.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
CISC 471 First Exam Review Game Questions. Overview 1 Draw the standard phases of a compiler for compiling a high level language to machine code, showing.
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
10/13/2015IT 3271 Tow kinds of predictive parsers: Bottom-Up: The syntax tree is built up from the leaves Example: LR(1) parser Top-Down The syntax tree.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 3, 09/11/2003 Prof. Roy Levow.
CPS 506 Comparative Programming Languages Syntax Specification.
Programming Languages
INTRODUCTION TO COMPILERS(cond….) Prepared By: Mayank Varshney(04CS3019)
Compilers I CNS History Wires Wires Machine Language Machine Language FFBA FFBA No Translation necessary No Translation necessary Assembly Language.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Generics with ArrayList and HashSet 1 ge·ner·ic adjective \jə ̇ˈ nerik, -rēk\ relating or applied to or descriptive of all members of a genus, species,
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
Language Implementation Overview John Keyser Spring 2016.
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 8: Semantic Analyzer1 Compiler Designs and Constructions Chapter 8: Semantic Analyzer Objectives: Syntax-Directed Translation Type Checking Dr.
Prologue Sung-Dong Kim, Dept. of Computer Engineering, Hansung University.
Arithmetic Instructions. Integer and Float Conversions.
CC410: System Programming Dr. Manal Helal – Fall 2014 – Lecture 12–Compilers.
Lecture 12 Intermediate Code Generation Translating Expressions
Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University.
Lecture 9 Symbol Table and Attributed Grammars
Expressions and Assignment Statements
Chapter 3 – Describing Syntax
Semantic analysis Jakub Yaghob
Constructing Precedence Table
G. Pullaiah College of Engineering and Technology
Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications) cs7100(Prasad) L8Interp.
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
Chapter 1: Introduction to Compiling (Cont.)
Stacks Chapter 4.
Stack application: postponing data usage
TDDD55- Compilers and Interpreters Lesson 3
CPSC 388 – Compiler Design and Construction
CPSC 388 – Compiler Design and Construction
ENERGY 211 / CME 211 Lecture 15 October 22, 2008.
Assignment 3 Solution Background
Overview of Compilation The Compiler BACK End
Compilers B V Sai Aravind (11CS10008).
Chapter-3 Operators.
Lecture 4: Lexical Analysis & Chomsky Hierarchy
Designing a Predictive Parser
Procedures App B: SLLGEN 1.
Programming Languages 2nd edition Tucker and Noonan
Compiler design.
3.4 Local Binding Recall Scheme's let: > (let ((x 5)‏ (y 6))
Data Types and Expressions
High-Level Programming Language
CO4301 – Advanced Games Development Week 3 Parsing Continued
A Tour of Language Implementation
Assignment Solution Sketch
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

Scanner Spec (define the-lexical-spec '((whitespace (whitespace) skip) (identifier ((or "i" "j" "k")) symbol) (number (digit (arbno digit)) number)) )

Parser Spec (define the-grammar '((expression (term (arbno "+" term)) add-exps) … (factor ("(" expression ")") paren-exp) ) )