APT Session 7: Compilers

Similar documents
APT Session 6: Compilers

Syntax-Directed Translation. Introduction

APT Session 4: C. Software Development Team Laurence Tratt. 1 / 14

Higher-Order Functions (Part I)

APT Session 2: Python

Project 2 Interpreter for Snail. 2 The Snail Programming Language

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

CSE P 501 Exam 8/5/04

EDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised:

Parsing III. (Top-down parsing: recursive descent & LL(1) )

Parsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones

Higher-Order Functions

Code Genera*on for Control Flow Constructs

CSCI312 Principles of Programming Languages

Computing Inside The Parser Syntax-Directed Translation, II. Comp 412 COMP 412 FALL Chapter 4 in EaC2e. source code. IR IR target.

A Bison Manual. You build a text file of the production (format in the next section); traditionally this file ends in.y, although bison doesn t care.

CSE P 501 Compilers. Intermediate Representations Hal Perkins Spring UW CSE P 501 Spring 2018 G-1

CSE P 501 Exam 8/5/04 Sample Solution. 1. (10 points) Write a regular expression or regular expressions that generate the following sets of strings.

COP4020 Programming Assignment 2 - Fall 2016

ASTs, Objective CAML, and Ocamlyacc

CSE-304 Compiler Design

Semantic Analysis Attribute Grammars

Syntax and Grammars 1 / 21

CS2383 Programming Assignment 3

Intro to Bottom-up Parsing. Lecture 9

CS453 : JavaCUP and error recovery. CS453 Shift-reduce Parsing 1

Semantic actions for declarations and expressions

Parsing Scheme (+ (* 2 3) 1) * 1

Semantic actions for declarations and expressions. Monday, September 28, 15

Compilers. Intermediate representations and code generation. Yannis Smaragdakis, U. Athens (original slides by Sam

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

LL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012

Semantic Analysis. Lecture 9. February 7, 2018

Semantic actions for declarations and expressions

Alternatives for semantic processing

CS 314 Principles of Programming Languages. Lecture 9

Lexical and Syntax Analysis. Top-Down Parsing

CS 320: Concepts of Programming Languages

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

Intermediate Code Generation

ASTS, GRAMMARS, PARSING, TREE TRAVERSALS. Lecture 14 CS2110 Fall 2018

Summary: Direct Code Generation

A Simple Syntax-Directed Translator

syntax tree - * * * * * *

COMP 181. Prelude. Intermediate representations. Today. High-level IR. Types of IRs. Intermediate representations and code generation

CSE 5317 Midterm Examination 4 March Solutions

CSE 307: Principles of Programming Languages

Syntax-directed translation. Context-sensitive analysis. What context-sensitive questions might the compiler ask?

Syntax-Directed Translation. Lecture 14

Lecture 3 Local Optimizations, Intro to SSA

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

Today. Assignments. Lecture Notes CPSC 326 (Spring 2019) Operator Associativity & Precedence. AST Navigation. HW4 out (due next Thurs)

Plan for Today. Regular Expressions: repetition and choice. Syntax and Semantics. Context Free Grammars

Project Compiler. CS031 TA Help Session November 28, 2011

Compilers. Compiler Construction Tutorial The Front-end

What is a compiler? var a var b mov 3 a mov 4 r1 cmpi a r1 jge l_e mov 2 b jmp l_d l_e: mov 3 b l_d: ;done

Exceptions and Design

CIT Lecture 5 Context-Free Grammars and Parsing 4/2/2003 1

COP4020 Programming Assignment 2 Spring 2011

LL parsing Nullable, FIRST, and FOLLOW

Semantic Analysis. CSE 307 Principles of Programming Languages Stony Brook University

Class Information ANNOUCEMENTS

Syntax-Directed Translation. CS Compiler Design. SDD and SDT scheme. Example: SDD vs SDT scheme infix to postfix trans

CSE302: Compiler Design

Compilers. Predictive Parsing. Alex Aiken

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

A clarification on terminology: Recognizer: accepts or rejects strings in a language. Parser: recognizes and generates parse trees (imminent topic)

A language is a subset of the set of all strings over some alphabet. string: a sequence of symbols alphabet: a set of symbols

CS 4120 Introduction to Compilers

Static Checking and Intermediate Code Generation Pat Morin COMP 3002

It parses an input string of tokens by tracing out the steps in a leftmost derivation.

CISC220 Lab 2: Due Wed, Sep 26 at Midnight (110 pts)

The fringe of a binary tree are the values in left-to-right order. For example, the fringe of the following tree:

CISC-124. Casting. // this would fail because we can t assign a double value to an int // variable

Semantic Analysis. Role of Semantic Analysis

a. Determine the EPS, FIRST and FOLLOWs set for this grammar.

Monday, September 13, Parsers

Part 3. Syntax analysis. Syntax analysis 96

How do LL(1) Parsers Build Syntax Trees?

Object Code (Machine Code) Dr. D. M. Akbar Hussain Department of Software Engineering & Media Technology. Three Address Code

Cool Abstract Syntax Trees

The role of semantic analysis in a compiler

CSE 401/M501 Compilers

Outline CS412/413. Administrivia. Review. Grammars. Left vs. Right Recursion. More tips forll(1) grammars Bottom-up parsing LR(0) parser construction

LECTURE 18. Control Flow

A parser is some system capable of constructing the derivation of any sentence in some language L(G) based on a grammar G.

The story so far. Elements of Programming Languages. While-programs. Mutable vs. immutable

CMSC430 Spring 2014 Midterm 2 Solutions

Comp 204: Computer Systems and Their Implementation. Lecture 22: Code Generation and Optimisation

Compilers Project. Cyril Soldani. Mar 6th University of Liège

COSC160: Data Structures: Lists and Queues. Jeremy Bolton, PhD Assistant Teaching Professor

Today Winter Compiler Construction T5 AST Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv University

Left to right design 1

CSE 582 Autumn 2002 Exam Sample Solution

Semantics of programming languages

Problem Score Max Score 1 Syntax directed translation & type

Compiler construction lecture 3

B The SLLGEN Parsing System

Expressions and Assignment

Compiler Design. Code Shaping. Hwansoo Han

Transcription:

APT Session 7: Compilers Laurence Tratt Software Development Team 2015-12-02 1 / 12 http://soft-dev.org/

What to expect from this session 1 Building a compiler. 2 / 12 http://soft-dev.org/

Prerequisites 1 Have the programming language of your choice (e.g. Java, Python) installed and running on your computer. 2 For Java: clone and compile Sam White s parser. 3 For Python: download my simple parser. 4 Use Sam White s stack visualizer. 5 (Optionally, but highly recommended) use Sam White s go.py which allows you to pipe output straight from your compiler to the stack visualizer e.g.: python3 compiler.py ex4.hll python3 go.py 3 / 12 http://soft-dev.org/

Compilers (1) In previous sessions we ve written: an interpreter for a stack machine; and a parser for simple languages. 4 / 12 http://soft-dev.org/

Compilers (1) In previous sessions we ve written: an interpreter for a stack machine; and a parser for simple languages. A compiler is the link we need between the two to takes in human readable programs and convert them to stack format. 4 / 12 http://soft-dev.org/

Compilers (1) In previous sessions we ve written: an interpreter for a stack machine; and a parser for simple languages. A compiler is the link we need between the two to takes in human readable programs and convert them to stack format. Compilers for dynamically typed languages are simple. [Statically typed languages compilers, especially if they heavily optimise code, are superficially more complex, though they re still conceptually simple.] 4 / 12 http://soft-dev.org/

Compilers (1) In previous sessions we ve written: an interpreter for a stack machine; and a parser for simple languages. A compiler is the link we need between the two to takes in human readable programs and convert them to stack format. Compilers for dynamically typed languages are simple. [Statically typed languages compilers, especially if they heavily optimise code, are superficially more complex, though they re still conceptually simple.] Our starting grammar: Stmt ::= Assign While Print Assign ::= ID = Expr ; Expr ::= INT + Expr INT - Expr INT < Expr INT > Expr INT While ::= WHILE Expr { stmt* } Print ::= PRINT Expr ; Python and Java tree-creating parsers are provided for this grammar. 4 / 12 http://soft-dev.org/

Compilers (2) The transformation our compiler needs is fairly simple. For example print 2 + 3; should be transformed to: INT 2 INT 3 ADD PRINT EXIT The trick is to break this transformation into phases. The first is parsing (which we did in the last session). The second is code generation (which we ll do today). The (optional) third is optimisation (which we don t need). 5 / 12 http://soft-dev.org/

Parse trees Last time we wrote a yes/no parser. More commonly we want a parse tree as output. For example the parse tree for x = 2 + 3; is: ASSIGN: x BINOP: + INT: 2 INT: 3 which we will write as assign("x", binop("+", int(2), int(3)). The program print 2; is transformed to the parse tree print(int(2)) etc. 6 / 12 http://soft-dev.org/

Parse trees Last time we wrote a yes/no parser. More commonly we want a parse tree as output. For example the parse tree for x = 2 + 3; is: ASSIGN: x BINOP: + INT: 2 INT: 3 which we will write as assign("x", binop("+", int(2), int(3)). The program print 2; is transformed to the parse tree print(int(2)) etc. Exercises: 1 Write the program print 2; into a file ex1.hll and parse it. 6 / 12 http://soft-dev.org/

Traversing over a parse tree A compiler is a translater of parse tree nodes to code. 7 / 12 http://soft-dev.org/

Traversing over a parse tree A compiler is a translater of parse tree nodes to code. To make life easier, parse tree edges are labelled. So a print node has an exp, an int node has a val and so on. 7 / 12 http://soft-dev.org/

Traversing over a parse tree A compiler is a translater of parse tree nodes to code. To make life easier, parse tree edges are labelled. So a print node has an exp, an int node has a val and so on. We iterate over the tree, gradually converting it to stack-based instructions. For a print node: process the exp then add a PRINT instruction. For an int: simply create an INT instruction. 7 / 12 http://soft-dev.org/

Traversing over a parse tree A compiler is a translater of parse tree nodes to code. To make life easier, parse tree edges are labelled. So a print node has an exp, an int node has a val and so on. We iterate over the tree, gradually converting it to stack-based instructions. For a print node: process the exp then add a PRINT instruction. For an int: simply create an INT instruction. Exercises: 1 Write a simple compiler which reads a file in, parses it, converts ex1.hll to a stack-based format and prints the output to stdout. Initially you only need to handle parse trees of the form print(int(n)) (where n is an integer). Run the output in the stack visualiser. 7 / 12 http://soft-dev.org/

More sophisticated traversal Compilers mostly do a preorder traversal: process the node; process the Left Hand Side (LHS) of the node (all the way); process the Right Hand Side (RHS) of the node (all the way). For a BINOP (e.g. add/sub/etc.) node: process the lhs then the rhs then emit the appropriate binop instruction. 8 / 12 http://soft-dev.org/

More sophisticated traversal Compilers mostly do a preorder traversal: process the node; process the Left Hand Side (LHS) of the node (all the way); process the Right Hand Side (RHS) of the node (all the way). For a BINOP (e.g. add/sub/etc.) node: process the lhs then the rhs then emit the appropriate binop instruction. For each node type X, we need a function t_x which to process it. We also need a function preorder which takes in an arbitrary node and dispatches to the appropriate t_ function. 8 / 12 http://soft-dev.org/

More sophisticated traversal Compilers mostly do a preorder traversal: process the node; process the Left Hand Side (LHS) of the node (all the way); process the Right Hand Side (RHS) of the node (all the way). For a BINOP (e.g. add/sub/etc.) node: process the lhs then the rhs then emit the appropriate binop instruction. For each node type X, we need a function t_x which to process it. We also need a function preorder which takes in an arbitrary node and dispatches to the appropriate t_ function. Exercises: 1 Adjust your previous compiler to be a class with traversal functions t_ and a general preorder function. 2 Write the program print 2 + 3 into a file ex2.hll. 3 Add a binop traversal function (which need only cope with + ). 4 Run the output in the stack visualiser. 8 / 12 http://soft-dev.org/

Variables Working only with the stack is frustrating; we want a heap. Variables are named parts of the heap. The stack machine has two variable instructions: VAR_SET x VAR_LOOKUP y VAR_SET sets the variable x to the (popped) top of the stack. VAR_LOOKUP looks up a variable y and pushes it onto the stack. 9 / 12 http://soft-dev.org/

Variables Working only with the stack is frustrating; we want a heap. Variables are named parts of the heap. The stack machine has two variable instructions: VAR_SET x VAR_LOOKUP y VAR_SET sets the variable x to the (popped) top of the stack. VAR_LOOKUP looks up a variable y and pushes it onto the stack. Exercises: 1 Write the program x=2; y=2+3; print x+y; into a file ex3.hll. 2 Add variable traversal functions. 3 Run the output in the stack visualiser. 9 / 12 http://soft-dev.org/

Conditionals The stack machine defines LESS_THAN, MORE_THAN, and EQUALS which do: rhs = stack.pop() lhs = stack.pop() stack.push(lhs op rhs) where op is <, >, or ==. 0 is pushed for false, 1 for true. 10 / 12 http://soft-dev.org/

Conditionals The stack machine defines LESS_THAN, MORE_THAN, and EQUALS which do: rhs = stack.pop() lhs = stack.pop() stack.push(lhs op rhs) where op is <, >, or ==. 0 is pushed for false, 1 for true. Exercises: 1 Write the program print 2 < 3; into a file ex4.hll. 2 Add conditional traversal functions. 3 Run the output in the stack visualiser. 10 / 12 http://soft-dev.org/

Loops A while loop has a condition and a body. While the condition doesn t evaluate to 0, the body is executed. 11 / 12 http://soft-dev.org/

Loops A while loop has a condition and a body. While the condition doesn t evaluate to 0, the body is executed. Exercises: 1 Write the program i = 0; while i < 10 { print i; i = i + 1; } into a file ex5.hll. 2 Add a while loop traversal function. You will need labels and jumps. 3 Run the output in the stack visualiser. 11 / 12 http://soft-dev.org/

Post-session exercises Try these (no particular order): Extend the parser to handle functions and update the compiler accordingly. Read how difficult is it to write a compiler? in light of your experiences. 12 / 12 http://soft-dev.org/