Lecture 4 Abstract syntax

Similar documents
Note first midterm date: Wed. evening, Feb. 23 Today's class: Types in OCaml and abstract syntax

(Not Quite) Minijava

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division

Decaf Language Reference

COMP520 - GoLite Type Checking Specification

IC Language Specification

CMSC 330: Organization of Programming Languages. Operational Semantics

CS 360 Programming Languages Interpreters

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

Lecture 2: Big-Step Semantics

MiniJava Parser and AST. Winter /27/ Hal Perkins & UW CSE E1-1

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

Syntax and Grammars 1 / 21

Here redirection. Case statement. Advanced Unix Tools Lecture 6 CS214 Spring 2004 Friday March 5, 2004

Parsing. Zhenjiang Hu. May 31, June 7, June 14, All Right Reserved. National Institute of Informatics

The SPL Programming Language Reference Manual

CSE P 501 Exam 8/5/04

LECTURE 17. Expressions and Assignment

A Short Summary of Javali

These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without

Introduction to Programming Using Java (98-388)

MP 3 A Lexer for MiniJava

Computer Science II Lecture 1 Introduction and Background

Semantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413

CS 320: Concepts of Programming Languages

A Simple Syntax-Directed Translator

Hard deadline: 3/28/15 1:00pm. Using software development tools like source control. Understanding the environment model and type inference.

CIS 194: Homework 3. Due Wednesday, February 11, Interpreters. Meet SImPL

COMP520 - GoLite Type Checking Specification

Operators. Java operators are classified into three categories:

CMSC 330: Organization of Programming Languages. OCaml Expressions and Functions

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Chapter 3. Section 3.10 Type of Expressions and Automatic Conversion. CS 50 Hathairat Rattanasook

Operators & Expressions

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

Formal Methods. Seminar 2

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

More Assigned Reading and Exercises on Syntax (for Exam 2)

CPS 506 Comparative Programming Languages. Syntax Specification

GOLD Language Reference Manual

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Boolean expressions. Elements of Programming Languages. Conditionals. What use is this?

Variables and Operators 2/20/01 Lecture #

Comp 311: Sample Midterm Examination

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

Interpreters. Prof. Clarkson Fall Today s music: Step by Step by New Kids on the Block

The Warhol Language Reference Manual

List Functions, and Higher-Order Functions

COMP Primitive and Class Types. Yi Hong May 14, 2015

CSCI 3155: Homework Assignment 4

The Decaf Language. 1 Lexical considerations

Decaf Language Reference

COMP520 - GoLite Type Checking Specification

L3 Programming September 19, OCaml Cheatsheet

VARIABLES & ASSIGNMENTS

Compilers. Compiler Construction Tutorial The Front-end

Meeting14:Denotations

ASTs, Objective CAML, and Ocamlyacc

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

Chapter 2: Using Data

Homework 3 COSE212, Fall 2018

SECTION II: LANGUAGE BASICS

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture)

DaMPL. Language Reference Manual. Henrique Grando

The Substitution Model

Full file at

SPARK-PL: Introduction

Ruby: Introduction, Basics

The Substitution Model. Nate Foster Spring 2018

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

Chapter 2: Introduction to C++

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

The Decaf language 1

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

CS 360: Programming Languages Lecture 10: Introduction to Haskell

CMSC 330: Organization of Programming Languages. Rust Basics

Decaf Language Reference Manual

Overview of Assignment 5

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Semantic actions for declarations and expressions

LECTURE 3 C++ Basics Part 2

CSE 115. Introduction to Computer Science I

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

CA4003 Compiler Construction Assignment Language Definition

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

CIS 110: Introduction to Computer Programming

1.3b Type Conversion

Language Reference Manual simplicity

Implementing Classes and Assignments

CHAPTER 3 BASIC INSTRUCTION OF C++

Functional Programming

Dialects of ML. CMSC 330: Organization of Programming Languages. Dialects of ML (cont.) Features of ML. Functional Languages. Features of ML (cont.

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square)

Transcription:

Lecture 4 Abstract syntax Abstract syntax examples for various languages: A simple expression language OCaml MiniJava (a subset of Java) CS 421 Class 4, 1/26/12 1

Ex: Abstract syntax of simple expressions Abstract syntax is a tree representation of the basic syntactic structure of a program. For a simple example, consider the language of ordinary arithmetic expressions, with numbers, unary and binary operators, and parentheses. For example, expression 3 + 4*5 + -7 would be represented by this tree: CS 421 Class 4, 1/26/12 2

Abstract syntax of simple expressions (cont.) Here is an abstract syntax for simple arithmetic expressions as an OCaml data type: type expr = Int of int Plus of expr*expr Times of expr*expr Negate of expr The tree given above would be written as an OCaml expression of type expr like this: CS 421 Class 4, 1/26/12 3

Exercises using expr Show the abstract syntax tree for expression 4+-(7*-8+4): Give the OCaml expression of type expr for that tree: CS 421 Class 4, 1/26/12 4

Exercises using expr (cont.) Write the function countpluses: expr int, which counts the number of Plus operations in an expr: let rec countpluses e = match e with Int i -> Plus(e1, e2) -> Times(e1, e2) -> Negate e -> CS 421 Class 4, 1/26/12 5

Exercises using expr (cont.) Write the function eval: expr int, which evaluates its argument, e.g. eval (Times(Negate(Int 5), Int 6)) = -30. CS 421 Class 4, 1/26/12 6

Exercises using expr (cont.) For a little more practice, consider this slightly different definition of type expr: type expr = Int of int Binop of bop*expr*expr Unop of uop*expr and bop = Plus Times and uop = Negate Define eval for this definition of expr: CS 421 Class 4, 1/26/12 7

Abstract syntax of OCaml Here s a (partial) abstract syntax for OCaml: type ocamlexpr = Int of int Binop of bop * ocamlexpr * ocamlexpr Var of string App of ocamlexpr * ocamlexpr Let of def * ocamlexpr Letrec of def * ocamlexpr and def = string * string list * ocamlexpr and bop = Plus Times Write the ocamlexpr corresponding to let f a = let x = 0 in a+x in f 7 CS 421 Class 4, 1/26/12 8

Ex: Abstract syntax of OCaml occursin: string ocamlexpr bool checks if a variable or function name is used in an expression (in its scope), e.g. a occurs in let x = a in... a occurs in let x = 0 in a+x a does not occur in let a = 0 in a f occurs in let g x = f (x+1) in g 0 f does not occur in let rec f x = f (x+1) in f 0 let rec occursin v e = match e with Int i -> Binop(b, e1, e2) -> Var(s) -> App(e1, e2) -> Let(d, e) -> Letrec(d, e) -> CS 421 Class 4, 1/26/12 9

Abstract syntax of MiniJava In the first half of the semester, we will build a compiler for a Java-like language called MiniJava. Over the new few weeks, we will build the front end of that compiler, whose primary purpose is to transform source files into abstract syntax trees. In MP 2, you will write some functions on the abstract syntax for MiniJava. That abstract syntax is given here: type program = Program of (class_decl list) and class_decl = Class of id * id * (var_decl list) * (method_decl list) and method_decl = Method of exp_type * id * ((exp_type * id) list) CS 421 Class 4, 1/26/12 10

* (var_decl list) * (statement list) * exp and var_decl = Var of var_kind * exp_type * id and var_kind = Static NonStatic and statement = Block of (statement list) If of exp * statement * statement While of exp * statement Println of exp Assignment of id * exp ArrayAssignment of id * exp * exp Break Continue Switch of exp * ((int * (statement list)) list) (* cases *) * (statement list) (* default *) CS 421 Class 4, 1/26/12 11

and exp = Operation of exp * binary_operation * exp Array of exp * exp Length of exp MethodCall of exp * id * (exp list) Integer of int True False Id of id This NewArray of exp_type * exp NewId of id Not of exp Null String of string Float of float and binary_operation = And Or LessThan Plus CS 421 Class 4, 1/26/12 12

Minus Multiplication Division and exp_type = ArrayType of exp_type BoolType IntType ObjectType of id StringType FloatType and id = string; CS 421 Class 4, 1/26/12 13

Ex: pretty-print expressions Write pp : exp string, that produces a printed version of its argument, such that, if it were parsed, it would produce the same abstract syntax tree. (pp stands for pretty-print.) Use parentheses freely (even though some will be unnecessary). let rec pp_bop binop = match binop with And -> "&&" LessThan -> "<"... end rec pp e = match e with Operation(e1, binop, e2) -> Array(e1, e2) -> CS 421 Class 4, 1/26/12 14