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:

Slides:



Advertisements
Similar presentations
Simplifications of Context-Free Grammars
Advertisements

Chapter 2-2 A Simple One-Pass Compiler
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.
1 ARM Movement Instructions u MOV Rd, ; updates N, Z, C Rd = u MVN Rd, ; Rd = 0xF..F EOR.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 2: Compilation and Interpretation COMP 144 Programming Language Concepts Spring.
CPSC Compiler Tutorial 4 Midterm Review. Deterministic Finite Automata (DFA) Q: finite set of states Σ: finite set of “letters” (input alphabet)
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.
1 Compiler Construction Intermediate Code Generation.
Generation of Intermediate Code Compiler Design Lecture (03/30//98) Computer Science Rensselaer Polytechnic.
Cse321, Programming Languages and Compilers 1 6/12/2015 Lecture #10, Feb. 14, 2007 Modified sets of item construction Rules for building LR parse tables.
Courtesy Costas Buch - RPI1 Simplifications of Context-Free Grammars.
CPSC Compiler Tutorial 5 Parser & Bison. Bison Concept Bison reads tokens and pushes them onto a stack along with the semantic values. The process.
Costas Busch - RPI1 Context-Free Languages. Costas Busch - RPI2 Regular Languages.
Compiler Design Lexical Analysis Syntactical Analysis Semantic Analysis Optimization Code Generation.
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
1 Simplifications of Context-Free Grammars. 2 A Substitution Rule Substitute Equivalent grammar.
1 Compilers. 2 Compiler Program v = 5; if (v>5) x = 12 + v; while (x !=3) { x = x - 3; v = 10; } Add v,v,0 cmp v,5 jmplt ELSE THEN: add x, 12,v.
Yu-Chen Kuo1 Chapter 2 A Simple One-Pass Compiler.
Fall 2004COMP 3351 Context-Free Languages. Fall 2004COMP 3352 Regular Languages.
4/6/08Prof. Hilfinger CS164 Lecture 291 Code Generation Lecture 29 (based on slides by R. Bodik)
CPSC Compiler Tutorial 3 Parser. Parsing The syntax of most programming languages can be specified by a Context-free Grammar (CGF) Parsing: Given.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 7 Mälardalen University 2010.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 5 Mälardalen University 2005.
CSC 338: Compiler design and implementation
1 Semantic Analysis Aaron Bloomfield CS 415 Fall 2005.
Lexical Analysis - An Introduction. The Front End The purpose of the front end is to deal with the input language Perform a membership test: code  source.
COMPILER DESIGN Fourth Year (First Semester) Lecture 1
Lecture # 3 Chapter #3: Lexical Analysis. Role of Lexical Analyzer It is the first phase of compiler Its main task is to read the input characters and.
1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 5 Mälardalen University 2010.
Making Decision – Microprocessor
Dr. Mohamed Ben Othman 1 Project phase 3 1)Rewrite the code of the project in the slides, run it: 1)change it so the input should be from file with extension.exp.
1 Lex. 2 Lex is a lexical analyzer Var = ; if (test > 20) temp = 0; else while (a < 20) temp++; Lex Ident: Var Integer: 12 Oper: + Integer: 9 Semicolumn:
Simple One-Pass Compiler
. n COMPILERS n n AND n n INTERPRETERS. -Compilers nA compiler is a program thatt reads a program written in one language - the source language- and translates.
DOCUMENTATION SECTION GLOBAL DECLARATION SECTION
Simple One-Pass Compiler part II Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
1 Control Flow Analysis Topic today Representation and Analysis Paper (Sections 1, 2) For next class: Read Representation and Analysis Paper (Section 3)
1 Compilation and Interpretation (Sections ) Compilation and Interpretation (Sections ) CSCI 431 Programming Languages Fall 2003 A modification.
1 A Simple Syntax-Directed Translator CS308 Compiler Theory.
11 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 7 School of Innovation, Design and Engineering Mälardalen University 2012.
Context-Free Languages. Regular Languages Context-Free Languages.
Costas Busch - LSU1 Parsing. Costas Busch - LSU2 Compiler Program File v = 5; if (v>5) x = 12 + v; while (x !=3) { x = x - 3; v = 10; } Add v,v,5.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
Compilers: Bottom-up/6 1 Compiler Structures Objective – –describe bottom-up (LR) parsing using shift- reduce and parse tables – –explain how LR.
Software Engineering Algorithms, Compilers, & Lifecycle.
YACC Primer CS 671 January 29, CS 671 – Spring Yacc Yet Another Compiler Compiler Automatically constructs an LALR(1) parsing table from.
Road Map Regular Exprs, Context-Free Grammars Regular Exprs, Context-Free Grammars LR parsing algorithm LR parsing algorithm Building LR parse tables Building.
A Simple Syntax-Directed Translator
G. Pullaiah College of Engineering and Technology
COP 4620 / 5625 Programming Language Translation / Compiler Writing Fall 2003 Lecture 5, 09/25/2003 Prof. Roy Levow.
Computer Architecture & Operations I
Simplifications of Context-Free Grammars
PROGRAMMING LANGUAGES
ASTs, Grammars, Parsing, Tree traversals
Intermediate Code Generation
CS 3304 Comparative Languages
Instructions for Making Decisions
Basic Program Analysis: AST
(Slides copied liberally from Ruth Anderson, Hal Perkins and others)
Syntax-Directed Translation
Lecture 30 (based on slides by R. Bodik)
Parsing Costas Busch - LSU.
There are 10 types of people of people in this world…
System Programming and administration
Hmjngj jxhngh.
Expr ( ). Expr ( ) E  T E’ E’  + T E’ | ε T  F T’ T’  * F T’ | ε F  ( E ) | id Orig. Grammar LL(1) Grammar E  E + T | T T  T.
MIPS assembly.
TARGET CODE GENERATION
MIPS instructions.
Presentation transcript:

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: expr has the same rules seen in Phase 3

Translation rules: 1- Translation of id=expr: translation of expr pop id.lexeme 2- Translation of if (expr) then stmt: translation of expr pop r2 cmp r2, 0 be else translation of stmt else: 3- Translation of while (expr) do stmt : while: translation of expr pop r2 cmp r2, 0 be endwhile translation of stmt b while endwhile: 4- Translation of begin CS end is the translation of CS.

If (1) then X = – 5; Compiler Input file Push 1 Pop r2 Cmp r2,0 Be else Push 1 Push 2 Pop r1 Pop r2 Add r2, r1 Push r2 Push 5 Pop r1 Pop r2 Sub r2, r1 Push r2 Pop x else Output (Intermediate code)

New intermediate instructions: be: branch if equal b : unconditional branch Translation scheme Start  stmt eof Stmt  id = expr {printf(“pop %s”, id.lexeme)} | if (expr) {if1()} then stmt {if2()} | while {w1()} (expr) {w2()} do stmt {w3()} | begin CS end CS  stmt ; CS | 

Actions If1() { printf(“pop r2\ncmp r2,0\nbe else\n”); } If2() { printf(“else\n”); }

Actions (Cond) w1() { printf(“while\n”); } w2() { printf(“pop r2\ncmp r2,0\nbe endwhile\n”); } w1() { printf(“b while\nendwhile\n”); }