Simple One-Pass Compiler part II Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.

Slides:



Advertisements
Similar presentations
Chapter 2-2 A Simple One-Pass Compiler
Advertisements

Compiler Construction Sohail Aslam Lecture StackInput ¤0¤0 id – id  id $ s4 ¤0 id 4 – id  id $ r6 F → id ¤0F3¤0F3 – id  id $ r5 T → F ¤0T2¤0T2.
ANALYSIS OF PROG. LANG. PROGRAM ANALYSIS Instructors: Crista Lopes Copyright © Instructors. 1.
Intermediate Code Generation
Compiler construction in4020 – lecture 4 Koen Langendoen Delft University of Technology The Netherlands.
Chapter 8 ICS 412. Code Generation Final phase of a compiler construction. It generates executable code for a target machine. A compiler may instead generate.
CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
Short circuit code for boolean expressions: Boolean expressions are typically used in the flow of control statements, such as if, while and for statements,
CS 338 Project: Phase 4. Grammar Start  stmt eof Stmt  id = expr | if (expr) then stmt | while (expr) do stmt | begin CS end CS  stmt ; CS |   ote:
8 Intermediate code generation
1 Compiler Construction Intermediate Code Generation.
Generation of Intermediate Code Compiler Design Lecture (03/30//98) Computer Science Rensselaer Polytechnic.
Apr. 12, 2000Systems Architecture I1 Systems Architecture I (CS ) Lecture 6: Branching and Procedures in MIPS* Jeremy R. Johnson Wed. Apr. 12, 2000.
CS 536 Spring Code generation I Lecture 20.
Chapter 2 Chang Chi-Chung Lexical Analyzer The tasks of the lexical analyzer:  Remove white space and comments  Encode constants as tokens.
Chapter 2 Chang Chi-Chung Lexical Analyzer The tasks of the lexical analyzer:  Remove white space and comments  Encode constants as tokens.
Yu-Chen Kuo1 Chapter 2 A Simple One-Pass Compiler.
CH2.1 CSE4100 Chapter 2: A Simple One Pass Compiler Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut 371.
Chapter 2 A Simple Compiler
4/6/08Prof. Hilfinger CS164 Lecture 291 Code Generation Lecture 29 (based on slides by R. Bodik)
Data Structures Using C++ 2E Chapter 7 Stacks. Data Structures Using C++ 2E2 Objectives Learn about stacks Examine various stack operations Learn how.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
Lexical Analysis Natawut Nupairoj, Ph.D.
What is Three Address Code? A statement of the form x = y op z is a three address statement. x, y and z here are the three operands and op is any logical.
Machine level architecture Computer Architecture Basic units of a Simple Computer.
1 Structure of a Compiler Front end of a compiler is efficient and can be automated Back end is generally hard to automate and finding the optimum solution.
LANGUAGE TRANSLATORS: WEEK 24 TRANSLATION TO ‘INTERMEDIATE’ CODE (overview) Labs this week: Tutorial Exercises on Code Generation.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
Chapter 8 Intermediate Code Zhang Jing, Wang HaiLing College of Computer Science & Technology Harbin Engineering University.
Computer Science 101 Assembly Language. Problems with Machine Language Uses binary - No English-like words to make it more readable Uses binary - No English-like.
CSC 3210 Computer Organization and Programming Chapter 1 THE COMPUTER D.M. Rasanjalee Himali.
Lesson 7 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Syntactic Analysis Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
CPSC 388 – Compiler Design and Construction Code Generation.
PZ07A Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ07A - Expressions Programming Language Design and Implementation.
1 Ethics of Computing MONT 113G, Spring 2012 Session 7 Computer Architecture The Internet.
Simple One-Pass Compiler
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.
Winter Compilers Software Eng. Dept. – Ort Braude Compiling Assignments and Expressions Lecturer: Esti Stein brd4.ort.org.il/~esti2.
Computer Architecture CSE 3322 Lecture 3 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/3/09 Read 2.8.
Intermediate Code Representations
Compilers Modern Compiler Design
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
Programming Assignment 4 Code generator Md. Zahurul Islam Center for Research on Bangla Language Processing (CRBLP) BRAC University.
An Attribute Grammar for Tiny Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 18.
Code Generation CPSC 388 Ellen Walker Hiram College.
1 February 23, February 23, 2016February 23, 2016February 23, 2016 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
Intermediate Code Generation CS 671 February 14, 2008.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Operational Semantics (Slides mainly.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Road Map Regular Exprs, Context-Free Grammars Regular Exprs, Context-Free Grammars LR parsing algorithm LR parsing algorithm Building LR parse tables Building.
Chapter 2: A Simple One Pass Compiler
Chapter 14 Functions.
Data Structures Using C++ 2E
G. Pullaiah College of Engineering and Technology
An Attribute Grammar for Tiny
Intermediate Code Generation
Computer Organization & Compilation Process
Programming Languages (CS 550) Mini Language Compiler
Design of the Control Unit for One-cycle Instruction Execution
Lecture 30 (based on slides by R. Bodik)
Designing a Predictive Parser
Phillipa Gill SCI 199 Y Tutorial Sept. 14, 2009 Phillipa Gill
Selection Statements.
See requirements for practice program on next slide.
Some Assembly (Part 2) set.html.
Computer Organization & Compilation Process
Programming Languages (CS 360) Mini Language Compiler
Presentation transcript:

Simple One-Pass Compiler part II Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University

Outline Abstract Stack Machine. Simple Code Generation.

Abstract Stack Machine Basic machine with stack only for computation. Ex: push 10 push 5 add push 2 minus

Abstract Stack Machine Variables  Use value from variable r-value.  Store value to variable l-value. k := k Ex: lvalue k push 5 := push 2 rvalue k add (addr) k k := 5

Stack Manipulation push vpush v on top of stack pop vpop v from top of stack rvalue lpush content of data l lvalue lpush address of data l :=put r-value on top to l-value below and pop both from top copypush copy of top

Translation of Expression day := (1461 * y) div 4 + (153 * m + 2) div 5 + d lvalue daypush 153div push 1461rvalue madd rvalue ymulrvalue d mulpush 2add push 4add:= divpush 5

Translation Scheme for Simple Expression expr ::= expr + term {print(‘add’);} expr ::= expr – term {print(‘minus’);} expr ::= term term ::= 0{print(‘push 0’);} term ::= 1{print(‘push 1’);}... term ::= 9{print(‘push 9’);}

Example expr +{ print(‘add’) } exprterm - { print(‘minus’) } 2{ print(‘push 2’) } exprterm term 5{ print(‘push 5’) } 9 { print(‘push 9’) } Input: 9 – Output: push 9 push 5 minus push 2 add

Translation Scheme for Simple Expression with Tokens expr ::= expr + term {print(‘add’);} expr ::= expr – term {print(‘minus’);} expr ::= term term ::= number {print(‘push ’); print(number.lexeme);} term ::= id {print(‘lvalue ); print(id.lexeme);}

Example expr ::= expr + term {print(‘add’);} expr ::= expr – term {print(‘minus’);} expr ::= term term ::= number {print(‘push ’); print(number.lexeme);} term ::= id {print(‘rvalue ’); print(id.lexeme);} Input: 2 + k – m Output: push 2 rvalue k add rvalue m minus

Translation Scheme for Assignment Statement stmt ::= id := {print(‘lvalue’); print(id.lexeme);} expr {print(‘:=‘);} Input: k := k + 1 Output: lvalue k rvalue k push 1 add := exprstmt

Control Flow eqpop top two values and compare, if equal push 1 on top of the stack, otherwise push 0. ltpop top two values and compare, if bottom less than first push 1 on top of thestack, otherwise push 0. gtsimilar to “lt”. label ltarget for jump goto ljump to lable l gofalse lpop top; jump if zero gotrue lpop top; jump if nonzero

Translation of if Statement stmt ::= if expr {out:=newlabel; print(‘gofalse’); print(out);} then stmt1 {print(‘label ’); print(out);} Code for expr gofalse lb01 Code for stmt1 label lb01 if k+1 < 0 then m := 3; rvalue k rush 1 add push 0 lt gofalse lb01 lvalue m push 3 := label lb01 stmt