CSE302: Compiler Design

Similar documents
CSE302: Compiler Design

CSE302: Compiler Design

CSE302: Compiler Design

Compilers. Compiler Construction Tutorial The Front-end

CSE302: Compiler Design

CSE302: Compiler Design

Lecture 14 Sections Mon, Mar 2, 2009

Problem Score Max Score 1 Syntax directed translation & type

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

CSE 340 Fall 2014 Project 4

A Simple Syntax-Directed Translator

Intermediate Code Generation

Compiler Code Generation COMP360

Introduction to Compiler Design

Context-Free Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation

Lecture 8 CS 412/413 Spring '00 -- Andrew Myers 2. Lecture 8 CS 412/413 Spring '00 -- Andrew Myers 4

A programming language requires two major definitions A simple one pass compiler

Syntax and Grammars 1 / 21

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

KU Compilerbau - Programming Assignment

Project Compiler. CS031 TA Help Session November 28, 2011

Structure of a compiler. More detailed overview of compiler front end. Today we ll take a quick look at typical parts of a compiler.

Semantic Analysis. Compiler Architecture

Compiler Design (40-414)

Abstract Syntax Trees Synthetic and Inherited Attributes

CSE398: Network Systems Design

Building lexical and syntactic analyzers. Chapter 3. Syntactic sugar causes cancer of the semicolon. A. Perlis. Chomsky Hierarchy

CSE 307: Principles of Programming Languages

Lecture Compiler Middle-End

CMSC430 Spring 2014 Midterm 2 Solutions

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 INSTRUCTIONS

Recap: Functions as first-class values

CSE P 501 Compilers. Implementing ASTs (in Java) Hal Perkins Autumn /20/ Hal Perkins & UW CSE H-1

Defining Program Syntax. Chapter Two Modern Programming Languages, 2nd ed. 1

CSCE 314 Programming Languages. Type System

Lecture 15-16: Intermediate Code-Generation

Building the Abstract Syntax Trees

CSE 12 Abstract Syntax Trees

CSE302: Compiler Design

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres

Undergraduate Compilers in a Day

CSE P 501 Compilers. Implementing ASTs (in Java) Hal Perkins Winter /22/ Hal Perkins & UW CSE H-1

An Overview of Compilation

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

Relation Overriding. Syntax and Semantics. Simple Semantic Domains. Operational Semantics

A Tour of Language Implementation

CSE398: Network Systems Design

Introduction to Programming Using Java (98-388)

1 Lexical Considerations

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

Principles of Programming Languages COMP251: Syntax and Grammars

ELEC 876: Software Reengineering

3. Context-free grammars & parsing

Program Abstractions, Language Paradigms. CS152. Chris Pollett. Aug. 27, 2008.

Syntax/semantics. Program <> program execution Compiler/interpreter Syntax Grammars Syntax diagrams Automata/State Machines Scanning/Parsing

CUP. Lecture 18 CUP User s Manual (online) Robb T. Koether. Hampden-Sydney College. Fri, Feb 27, 2015

LECTURE 18. Control Flow

CS 314 Principles of Programming Languages. Lecture 9

CSE P 501 Compilers. Parsing & Context-Free Grammars Hal Perkins Winter /15/ Hal Perkins & UW CSE C-1

COMP 181 Compilers. Administrative. Last time. Prelude. Compilation strategy. Translation strategy. Lecture 2 Overview

Syntax. In Text: Chapter 3

Intermediate Representations

CSE398: Network Systems Design

Statements execute in sequence, one after the other, such as the following solution for a quadratic equation:

CPS 506 Comparative Programming Languages. Syntax Specification

Compiler Theory. (Semantic Analysis and Run-Time Environments)

intermediate-code Generation

Compiler Design Overview. Compiler Design 1

CSCE 314 Programming Languages

PRINCIPLES OF COMPILER DESIGN UNIT I INTRODUCTION TO COMPILING

Dr. D.M. Akbar Hussain

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

THEORY OF COMPILATION

EECS 6083 Intro to Parsing Context Free Grammars

Compiler Construction

CSE302: Compiler Design

The SPL Programming Language Reference Manual

Peace cannot be kept by force; it can only be achieved by understanding. Albert Einstein

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Decaf Language Reference Manual

Sardar Vallabhbhai Patel Institute of Technology (SVIT), Vasad M.C.A. Department COSMOS LECTURE SERIES ( ) (ODD) Code Optimization

Program Representations

Imperative Paradigm. Syntax. Role of Programming Languages. Expressions. Expressions. Role of Programming Languages. Symbols.

Compilers. Lecture 2 Overview. (original slides by Sam

Page No 1 (Please look at the next page )

A simple syntax-directed

Semantic analysis and intermediate representations. Which methods / formalisms are used in the various phases during the analysis?

Alternatives for semantic processing

Time : 1 Hour Max Marks : 30

Writing Evaluators MIF08. Laure Gonnord

The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

Acknowledgement. CS Compiler Design. Intermediate representations. Intermediate representations. Semantic Analysis - IR Generation

Syntax. A. Bellaachia Page: 1

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards

JavaCC Parser. The Compilation Task. Automated? JavaCC Parser

CSCE 531, Spring 2015 Final Exam Answer Key

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Transcription:

CSE302: Compiler Design Instructor: Dr. Liang Cheng Department of Computer Science and Engineering P.C. Rossin College of Engineering & Applied Science Lehigh University January 30, 2007

Outline Recap Introduction (Section 2.1) Syntax definition (Section 2.2) Syntax directed translation (Section 2.3) Parsing (Section 2.4) A translator for simple expressions (Section 2.5) Lexical analysis (Section 2.6) A simple syntax-directed translator (Chapter 2) The rest of Chapter 2 Summary and homework

You should now be able to Define language syntax using BNF grammar Parse sentences and detect syntax errors Use syntax-directed definition to perform language translation Understand lexical analysis

Outline Recap A simple syntax-directed translator (Chapter 2) Intermediate code generation (Section 2.8) Symbol tables (Section 2.7) Summary and homework

Two Kinds of Intermediate Representation Syntax tree representation Expressions E1 op E2 Statements while (expr) stmt do stmt while expr Use a translation scheme Semantic rules or semantic actions Three-address-code representation OP E1 E2 while expr stmt do stmt expr

A Translation Scheme for Constructing Syntax Tree Syntax tree for statements Syntax tree for expressions

Generating Syntax Tree: Syntax Tree for Statements In Parser.java Stmt stmts() throws IOException { if ( look.tag == '' ) return Stmt.Null; else return new Seq(stmt(), stmts()); Stmt stmt() throws IOException { Expr x; Stmt s, s1, s2; switch( look.tag ) { case Tag.IF: match(tag.if); match('('); x = bool(); match(')'); s1 = stmt(); if( look.tag!= Tag.ELSE ) return new If(x, s1); match(tag.else); s2 = stmt(); return new Else(x, s1, s2);

Generating Syntax Tree: Syntax Tree for Statements, Expressions In Parser.java Stmt stmt() throws IOException{ Expr x; Stmt s, s1, s2; switch( look.tag ) { case Tag.WHILE: While whilenode = new While(); match(tag.while); match('('); x = bool(); match(')'); s1 = stmt(); whilenode.init(x, s1); return whilenode; Expr expr() throws IOException { Expr x = term(); while( look.tag == + look.tag == - ) { Token tok = look; move(); x = new Arith(tok, x, term()); return x;

Translation to 3-Address Code Each node in the syntax tree may encapsulate various actions Translation actions Type checking actions Three-address code x = y op z where x, y, and z are names (ids), constants, or compiler-generated temps x = y, x[y] = z, or x = y[z] Sequential execution except conditional or unconditional jumps ifflase x goto L, ifture x goto L, or goto L

Translation of Statements Code layout for if (expr) stmt code to compute expr into x ifflase x goto after code for stmt after

Expression Translation Guidelines No code is generated for ids and constants They appear as addresses in instructions If a node x of class Expr has an operator op, e.g. i-j, then an instruction is emitted The value computed at node x is stored at a temp: t = i j Array accesses and assignments need to distinguish l-values and r-values 2*a[i] needs the r-value of a[i] a[i]=2 needs the l-value of a[i]

3-Address Code Generation Perform tree traversal to generate three-address code The function gen is called at the root of the syntax tree All statement classes contain a function gen

Outline Recap A simple syntax-directed translator (Chapter 2) Intermediate code generation (Section 2.8) Symbol tables (Section 2.7) Summary and homework

Symbol Tables Hold info of source program constructs Collected during analysis Used for synthesis Support multiple declarations of the same identifier within a program A separate symbol table for each scope A program block A class

Chained Symbol Tables for Nested Blocks { bool w; { int x; int y; { int w; bool y; int z; w ; x ; y ; z ; w ; x ; y ; w

Data Structure for Chained Symbol Tables public class Env { private Hashtable tab; protected Env prev; public Env(Env p) {table=new Hashtable(); prev=p; public void put(string s, Symbol sm) {tab.put(s,sm); public Symbol get(string s) { for (Env e=this; e!=null; e=e.prev) { Symbol found=e.table.get(s); if(found!=null) return found; return null; Syntax-directed translation using this data structure

Outline Recap A simple syntax-directed translator (Chapter 2) Intermediate code generation (Section 2.8) Symbol tables (Section 2.7) Summary and homework