Compiling expressions

Similar documents
Tree Applications. Processing sentences (computer programs or natural languages) Searchable data structures

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux

A Simple Syntax-Directed Translator

Parsing CSCI-400. Principles of Programming Languages.

CPS 506 Comparative Programming Languages. Syntax Specification

Project 1: Scheme Pretty-Printer

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

CSC Java Programming, Fall Java Data Types and Control Constructs

Introduction to Programming Using Java (98-388)

Building Java Programs

MIT Top-Down Parsing. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

Syntax-Directed Translation. Lecture 14

LECTURE 3. Compiler Phases

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

Data Structures and Algorithms

Compiler Code Generation COMP360

CA Compiler Construction

Question Points Score

Postfix Notation is a notation in which the operator follows its operands in the expression (e.g ).

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

Classes Basic Overview

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS

About This Lecture. Trees. Outline. Recursive List Definition slide 1. Recursive Tree Definition. Recursive List Definition slide 2

syntax tree - * * * - * * * * * 2 1 * * 2 * (2 * 1) - (1 + 0)

ITI Introduction to Computing II

A simple syntax-directed

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

ITI Introduction to Computing II

Module 6: Binary Trees

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

Definition of a tree. A tree is like a binary tree, except that a node may have any number of children

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS

CSE 12 Abstract Syntax Trees

Why do we need an interpreter? SICP Interpretation part 1. Role of each part of the interpreter. 1. Arithmetic calculator.

Apache Felix Shell. Apache Felix Shell. Overview. How the Shell Service Works. package org.apache.felix.shell;

Project 2: Scheme Interpreter

2.2 Syntax Definition

a graph is a data structure made up of nodes in graph theory the links are normally called edges

Compilers. Compiler Construction Tutorial The Front-end

Semantic Analysis. Compiler Architecture

1. Consider the following program in a PCAT-like language.

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Computing Science 114 Solutions to Midterm Examination Tuesday October 19, In Questions 1 20, Circle EXACTLY ONE choice as the best answer

CSC 1214: Object-Oriented Programming

i219 Software Design Methodology 9. Dynamic modeling 2 Kazuhiro Ogata (JAIST) Outline of lecture

PROGRAMMING FUNDAMENTALS

Compiler Construction D7011E

Getting started with Java

About this exam review

Syntax. A. Bellaachia Page: 1

Introduction to Computer Science II (ITI 1121) Midterm Examination

ITI Introduction to Computing II

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

Pace University. Fundamental Concepts of CS121 1

CSE 143 Lecture 19. Binary Trees. read slides created by Marty Stepp

Test I Solutions MASSACHUSETTS INSTITUTE OF TECHNOLOGY Spring Department of Electrical Engineering and Computer Science

ITI Introduction to Computing II

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

Computational Expression

COMP3131/9102: Programming Languages and Compilers

Assignment 5. Introduction

Computer Science Fundamentals 107. A binary tree applica.on - Expression Trees

Static Semantics. Lecture 15. (Notes by P. N. Hilfinger and R. Bodik) 2/29/08 Prof. Hilfinger, CS164 Lecture 15 1

Computer Science 1 Bh

CS 6353 Compiler Construction Project Assignments

Computer Science 1 Bh

THE COMPILATION PROCESS EXAMPLE OF TOKENS AND ATTRIBUTES

Parsing and AST construction with PCCTS

Chapter 4 Defining Classes I

Semantic Analysis. Lecture 9. February 7, 2018

CS 403: Scanning and Parsing

COMPILER CONSTRUCTION LAB 2 THE SYMBOL TABLE. Tutorial 2 LABS. PHASES OF A COMPILER Source Program. Lab 2 Symbol table

Java Primer 1: Types, Classes and Operators

Related Course Objec6ves

TML Language Reference Manual

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

CS2110 Assignment 2 Lists, Induction, Recursion and Parsing, Summer

CS 406: Syntax Directed Translation

ADT Stack. Inserting and deleting elements occurs at the top of Stack S. top. bottom. Stack S

Chapter 2 A Quick Tour

Java: framework overview and in-the-small features

Introduction to Computing II (ITI 1121) Final Examination

CS 2230 CS II: Data structures. Meeting 21: trees Brandon Myers University of Iowa

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

Jim Lambers ENERGY 211 / CME 211 Autumn Quarter Programming Project 4

Section A. A grammar that produces more than one parse tree for some sentences is said to be ambiguous.

Also, recursive methods are usually declared private, and require a public non-recursive method to initiate them.

Public-Service Announcement

CSE 431S Final Review. Washington University Spring 2013

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

RAIK 183H Examination 2 Solution. November 10, 2014

Array. Prepared By - Rifat Shahriyar

TDDD55- Compilers and Interpreters Lesson 3

JavaCC Parser. The Compilation Task. Automated? JavaCC Parser

COP 3402 Systems Software Top Down Parsing (Recursive Descent)

Lecture 12: Parser-Generating Tools

from inheritance onwards but no GUI programming expect to see an inheritance question, recursion questions, data structure questions

4. Semantic Processing and Attributed Grammars

Transcription:

E H U N I V E R S I T Y T O H F R G Compiling expressions E D I N B U Javier Esparza Computer Science School of Informatics The University of Edinburgh

The goal 1 Construct a compiler for arithmetic expressions [criollo]jav: java Compiler "2 + ( 3 * 4 )" iconst_2 iconst_3 iconst_4 imul iadd [criollo]jav: java Compiler "2 + (" expected expression [criollo]jav: java Compiler "2 + ( 3 * 4" expected )

Simplifying assumptions 2 Adjacent tokens separated by at least one space [criollo]jav: java Compiler "2+3" expected integer Never two consecutive constants [criollo]jav: java Compiler "2 + 3 4" iconst_2 iconst_4 iadd

Expression is fully parenthesised (except for the outermost parenthesis) 3 [criollo]jav: java Compiler "2 * 3 + 4" iconst_2 iconst_3 iconst_4 iadd imul [criollo]jav: java Compiler "( 2 * 3 ) + 4" iconst_2 iconst_3 imul iconst_4 iadd

Stages of the process 4 Lexical analysis array of tokens "2 + ( 3 * 4 )" {"2", "+", "(", "3", "*", "4", ")" Syntactic analysis syntax tree {"2", "+", "(", "3", "*", "4", ")" + / \ 2 * / \ 3 4 Semantic analysis (empty stage for arithmetic expressions) Translation Java byte code + / \ 2 * / \ 3 4 iconst_2 iconst_3 iconst_4 imul iadd

Why do we need the syntax tree? 5 Reading the syntax tree + / \ 2 * / \ 3 4 in postorder (left child then right child then parent) yields 2, 3, 4, *, + which immediately yields the Java byte code iconst_2 iconst_3 iconst_4 imul iadd

A class for syntax trees of expressions 6 Exp / \ / \ / \ Constant Plus Times A constant is represented as an instance of class Constant extends Exp { private int value; public Constant (int value) { this.value = value;

An expression with + as root is represented as an instance of class Plus extends Exp { private Exp left, right; public Plus (Exp left, Exp right) { this.left = left; this.right = right; 7 An expression with * as root is represented as an instance of class Times extends Exp { private Exp left, right; public Times (Exp left, Exp right) { this.left = left; this.right = right;

Translation 8 Translate a syntax tree (an instance of Exp) into byte code A constant c is compiled into iconst c class Constant extends Exp { private int value; public Constant (int value) { this.value = value; public void compile() { System.out.println("iconst_" + value);

An expression e1 + e2 is compiled into 9 result of compiling e1 result of compiling e2 iadd class Plus extends Exp { private Exp left, right; public Plus (Exp left, Exp right) { this.left = left; this.right = right; public void compile() { left.compile(); right.compile(); System.out.println("iadd");

Lexical analysis 10 Convert the source into an array of tokens import java.util.*; class Tokens { String[] tokens; Tokens (String source) { StringTokenizer st = new StringTokenizer(source); int tokencount = st.counttokens(); tokens = new String[tokenCount]; for (int i = 0 ; i < tokencount ; i++) { tokens[i] = st.nexttoken();

Syntactic analysis 11 Parse the tokens into a syntax tree If input is 2 + 3 output should be the result of executing Exp e = new Plus(new Constant(2), new Constant(3)); A parser builds the syntax tree and simultaneously checks for errors. We first assume that no error checking is needed, and then modify the implementation to include it.

Recursive descent 12 Expressions are defined by the following rules Constant Any integer constant is an expression Plus If left and right are expressions then so is left + right Times If left and right are expressions then so is left * right Parentheses If subexp is an expression then so is (subexp) Closure Nothing else is an expression We assume that the parser receives as input The position of the first token that has not been processed yet A parsed subexpression (possibly null) The parser reads the next token; deducts the syntax rule that applies from the llist above; acts accordingly, calling itself recursively if necessary, and returns the parse tree of the result of applying the rule

13 An example Parser is called with parameters n and exp Next token is + Parser selects rule Plus; identifies exp with left; Parser calls itself recursively with parameters n+1 and null; it stores the result of the call in right Parser returns new Plus(exp,right) Reason for the name recursive descent : the parser recursively descends through the syntax rules

A first implementation 14 We add a current position as new field to Tokens, as well as some new methods. class Tokens { int pos; String[] tokens; Tokens (String source) {... boolean atend() {... boolean nextis(string target) {... String next() {...

15 The parser contains several cases according to the next token class Exp { Tokens tokens; public Exp parse(tokens tokens) { this.tokens = tokens; return parseexp(null); public Exp parseexp(exp exp) { if (tokens.atend()) { return exp; else if (tokens.nextis("+")) { tokens.eat("+"); Exp right = parseexp(null); return parseexp(new Plus(exp, right));

16 else if (tokens.nextis("(")) { tokens.eat("("); Exp subexp = parseexp(null); tokens.eat(")"); return parseexp(subexp); else if (tokens.nextis(")")) { return exp; else { String s = tokens.next(); int i = Integer.parseInt(s); return parseexp(new Constant(i)); public void compile() {

17 class Compiler { public static void main (String[] args) throws SyntaxErro Tokens tokens = new Tokens(args[0]); Exp e = new Exp(); e.parse(tokens).compile();

Adding error checking 18 We introduce a new class SyntaxError class SyntaxError extends Exception { String message; SyntaxError (String symbol) { this.message = "expected " + symbol; We throw syntax errors where necessary

Changes in Exp 19 if (tokens.atend()){ return exp; if (tokens.atend()){ if (exp == null) throw new SyntaxError("expression"); else return exp;

20 else { String s = tokens.next(); int i = Integer.parseInt(s); return parseexp(new Constant(i)); else { try { String s = tokens.next(); int i = Integer.parseInt(s); return parseexp(new Constant(i)); catch (NumberFormatException e) { throw new SyntaxError ("integer");

Changes in Tokens 21 void eat(string s) { pos++; void eat(string s) throws SyntaxError { if (pos!= tokens.length && tokens[pos].equals(s)) pos++; else throw new SyntaxError(s);

Changes in Compiler 22 class Compiler { public static void main (String[] args) { Tokens tokens = new Tokens(args[0]); Exp e = new Exp(); e.parse(tokens).compile(); class Compiler { public static void main (String[] args) { try { Tokens tokens = new Tokens(args[0]); Exp e = new Exp(); e.parse(tokens).compile(); catch (SyntaxError e) { System.out.println(e.message);