Consider the following EBNF definition of a small language:

Size: px
Start display at page:

Download "Consider the following EBNF definition of a small language:"

Transcription

1 xercise 1: Consider the following BNF definition of a small language: program ::= <decl>+ decl ::= <vardecl> <procdecl> vardecl ::= "var" <name> ":" <typename> "=" <expr> ";" procdecl ::= "procedure" <name> "(" <formalparamlist>? ")" "" <blockbody> "" formalparamlist ::= <formalparameter> ( "," <formalparameter> )* formalparameter ::= <name> ":" <typename> blockbody ::= <vardecl>* <statement>* statement ::= <printstatement> <callstatement> <assignstatment> <ifstatement> printstatement ::= "print" <expr> ";" callstatement ::= "call" <name> "(" <actualparamlist> ")" ";" assignstatment ::= <name> ":=" <expr> ";" ifstatement ::= "if" <expr> "" <blockbody> "" actualparamlist ::= <expr> ( "," <expr> )* expr ::= <name> <integer> "true" "false" In the productions above, nonterminals are enclosed within angled brackets (< and >), and terminals are quoted. Plus (+) is used for one or more repetitions, asterisk (*) for zero or more, and question mark (?) for zero or one. Regular parentheses are used for grouping symbols in the grammar. The nonterminals name, typename and integer are not specified in any further detail, but it will suffice to say that name and typename are strings of letters and numbers, and integer denotes ordinary integer numbers. 1a) Below is a small program in the language defined by the grammar above:

2 var x : int = 0; var y : int = 1; procedure M(x : int) var y : int = 42; x := 1; call N(x); procedure N(x : int) var z : int = 2; if true var z : int = 42; print x; print y; print z; procedure Main() call M(x); xplain what this program does; describe what will be printed, and why. Is there more than one reasonable interpretation? xplain your reasoning. Assume that the program execution starts with the execution of the procedure Main(), and that print <expr> will print the value of the expression to the console/terminal. Furthermore, assume that the program is legal and valid (it has no errors) in the language in question. Solution: in this exercise, the student should show some maturity, and be able to discuss some alternatives for semantics for this language. To gain full score, the student should mention, and show that he/she understands, the following: - Static scope - Dynamic scope - Variable hoisting.

3 Furthermore, there should be an explanation of what will be printed in each case. - Static scope: x = 1, y = 1, z = 2 - Dynamic scope: x = 1, y = 42, z = 2 - Variable hoisting, static function scope: x = 1, y = 1, z = 42 - Variable hoisting, dynamic function scope: x = 1, y = 42, z = 42 - Variable hoisting, program scope: x = 1, y = 42, z = 42 The double declaration of z should be mentioned. If not the above, one could also discuss static typing vs dynamic checking, but since the language has types specified explicitly, we better not take that too far. Also, different mechanism of variable passing (by value, by ref, by name, etc) could be discussed. 1b) Draw an abstract syntax tree for the procedure M from 1a) Solution: Variants of this, e.g. with a little less details, are OK. 1c)

4 Draw a runtime stack for the program from 1a) with activation records containing variables, parameters, access links and control links when the program has just executed the print z statement. xplain briefly the assumptions you make about the semantics of the language. Solution: Assuming static scoping, and no variable hoisting: For dynamic scoping, access link will be equal to the control link. Values will then be as stated in the solution for 1a. 1d)

5 xt the grammar provided at the top of this exercise to allow for procedures within procedures..g., the following should be allowed, where we have introduced new procedures P and Q: procedure N(x : int) var z : int = 2; procedure Q(v : int) print v; if true var z : int = 42; procedure P() print z; call P(); print x; print y; print z; call Q(y); You only need to write out the productions that you modify. Solution: We modify the blockbody production: blockbody ::= ( <vardecl> <statement> <procdecl> ) * Note that we cannot just add <procdecl> to the original setup, e.g. like this: blockbody ::= <vardecl>* <statement>* <procdecl>* as this involves an ordering of the decls and statements that does not fit with the program. Putting <procdecl>* before <statement>* in the above does work with the example, but is clearly not a very elegant solution. 1e)

6 Assume now, that in addition to procedures within procedures, we also allow procedures as parameters to other procedures. xplain briefly how this can be implemented in the runtime system. Solution: The students should know how this is done through the use of closures, where the closure is a pair pointing to the procedure s code and the procedure s environment (access link). A call to the parameter procedure will then utilize this closure in order to be able to find it s environment through its access link, regardless of where the call site actually is. 1f) Modify the procedure named N from 1d) so that this procedure now has a new formal parameter FP that is a procedure, and so that N performs a recursive call to itself with the procedure P as the actual parameter. Write down the new program, and the runtime stack right after the first recursive call to N. (As in 1a), we assume that program execution starts with the Main() procedure.) You only need to write down the parts of the program you modify. You do not need to write any modified version of the grammar of the language in this exercise, just assume a suitable syntax for this purpose. If the program s up with infinite recursion (non-termination), that is OK. Briefly state any further assumptions that you make about the language. Solution: First, we invent some syntax for formal and actual procedure parameters, and modify the procedure N accordingly, e.g. procedure N(x : int, proc FP(int)) var z : int = 2; procedure Q(v : int) print v; if true var z : int = 42; procedure P() print z; call P();

7 call N(z, P); print x; print y; print z; call Q(y); We also need to modify M, that calls N procedure M(x : int) var y : int = 42; x := 1; proc Dummy(d : int) call N(x, Dummy); Then, we need to draw the runtime stack Note how there are two closures for P, one for each call to N. One could also make an extra activation block for the if-statement in the procedure N, in between the last and the second-to-last block above.

8 Question 2. ML (weight 40%) 2a valuate the following ML expressions: a) List.foldr (-) 4 [3,2,1] -2 b) List.foldr (fn (y,x) => 2*x + y) 4 [1,2,3] 49 2b Assume the standard definition of foldl: fun foldl (f: 'a*'b->'b) (acc: 'b) (l: 'a list): 'b = case l of [] => acc x::xs => foldl f (f(x,acc)) xs With the help of foldl, define the function val myreverse = fn : 'a list -> 'a list which returns the input list in reverse order. 2c fun myrev zs = List.foldl (fn (x,xs) => x::xs) [] zs; 1) Define a function that sums up all values in a list of integers (and consider also question (4) below): val sum = fn : int list -> int 2) Define a function that finds the largest element in a list of integers: val max = fn : int list -> int 3) Define the function val summax = fn : int list list -> int which takes a list of lists of integers and sums up the largest values from each list. xample: summax [[1,2],[4,2],[3,6,5,9]] = 15 4) Give function sum from the question (2c1) above as a tail-recursive function (if you have not done so immediately you do not have to answer both questions separately if you directly give the tail-recursive version)! If you cannot answer question (2c1), explain the difference between a tail-recursive and a naïve implementation of a recursive function in a few sentences.

9 NO SOLUTION b/c easy J 2d 1) Define a datatype for binary trees, 'a tree, with a constructor mpty, and a constructor Node with a value of type 'a and two subtrees. 2) Define a function val mirror = fn : 'a tree -> 'a tree that returns a mirrored version of the tree passed as an argument. That is, for a node, each subtree must be mirrored. xamples ( for mpty): ) Define a function val sym = fn : 'a tree -> 'a tree -> bool which returns true, if the two trees are symmetric (that is, one is a mirror of the other), or false otherwise. datatype 'a tree = m Br of ('a * 'a tree * 'a tree); fun mir m = m mir (Br (a, l, r)) = Br (a, (mir r), (mir l)); fun sym m m = true sym (Br (a, l1, r1)) (Br (b, l2, r2)) = (a = b) andalso (mir l1 = r2) andalso (mir r1 = l2) sym = false; -- or: fun sym2 t1 t2 = (mir t1) = t2; 2e Calculate the type for the following expression according to the ML type inference algorithm: fn x => fn y => x (y x). Use the provided type variables in the parse graph below. Derive the corresponding equations for the parse graph, and solve the resulting equation system to obtain the type of the root node R.

10 λ:r x:x R = X -> A A=Y->B X=C->B Y=X->C ~ Y = (C->B)->C ~ R = (C->B) -> ((C->B)->C) -> B

11 Question 3. Prolog (weight 20%) 3a Give PROLOG s answer (that is, the substitutions for all variables in a query, if the terms can be unified) for each of the queries below, or write no if no solution exists and give the reason. Be careful with the last question, the == is no typo! 1. g(a,x,y) = g(z,c,f(z)). 2. g(a,x,y) = g(y,c,f(x)). 3. f(x,y,d) == f(y,h(x),z).?- g(a,x,y) = g(z,c,f(z)). X = c Y = f(a) Z = a?- g(a,x,y) = g(y,c,f(x)). no?- f(x,y,d) == f(y,h(x),z). no 3b Define the predicate rotate(l,n,r) which rotates cyclically a list L with N positions to the left, i.e. the following queries succeed: rotate([a,b,c,d], 1, [b,c,d,a]). rotate([a,b,c,d], 6, [c,d,a,b]). rotate([a,b,c,d], 3, [d,a,b,c]). rotate([],[],_). rotate(l,l,0). rotate([x H],Z,N) :- N is 1, app(h,[x],z). rotate(y,z,n) :- N > 1, X is N-1, rotate(y,z1,1), rotate(z1,z,x).

UNIVERSITETET I OSLO

UNIVERSITETET I OSLO xam in INF3110, December 12, 2016 Page 1 UNIVRSITTT I OSLO Det matematisk-naturvitenskapelige fakultet xam in: INF3110 Programming Languages Day of exam: December 12, 2016 xam hours: 14:30 18:30 This examination

More information

UNIVERSITETET I OSLO

UNIVERSITETET I OSLO Exam in INF3110, November 29, 2017 Page 1 UNIVERSITETET I OSLO Det matematisk-naturvitenskapelige fakultet Exam in: INF3110 Programming Languages Day of exam: November 29, 2017 Exam hours: 14:30 18:30

More information

CSE3322 Programming Languages and Implementation

CSE3322 Programming Languages and Implementation Monash University School of Computer Science & Software Engineering Sample Exam 2004 CSE3322 Programming Languages and Implementation Total Time Allowed: 3 Hours 1. Reading time is of 10 minutes duration.

More information

CSE3322 Programming Languages and Implementation

CSE3322 Programming Languages and Implementation Monash University School of Computer Science & Software Engineering Exam 2005 CSE3322 Programming Languages and Implementation Total Time Allowed: 3 Hours 1. Reading time is of 10 minutes duration. 2.

More information

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II)

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II) Processadors de Llenguatge II Functional Paradigm Pratt A.7 Robert Harper s SML tutorial (Sec II) Rafael Ramirez Dep Tecnologia Universitat Pompeu Fabra Paradigm Shift Imperative Paradigm State Machine

More information

Programming Languages Third Edition

Programming Languages Third Edition Programming Languages Third Edition Chapter 12 Formal Semantics Objectives Become familiar with a sample small language for the purpose of semantic specification Understand operational semantics Understand

More information

COSC252: Programming Languages: Semantic Specification. Jeremy Bolton, PhD Adjunct Professor

COSC252: Programming Languages: Semantic Specification. Jeremy Bolton, PhD Adjunct Professor COSC252: Programming Languages: Semantic Specification Jeremy Bolton, PhD Adjunct Professor Outline I. What happens after syntactic analysis (parsing)? II. Attribute Grammars: bridging the gap III. Semantic

More information

Inductive Data Types

Inductive Data Types Inductive Data Types Lars-Henrik Eriksson Functional Programming 1 Original slides by Tjark Weber Lars-Henrik Eriksson (UU) Inductive Data Types 1 / 42 Inductive Data Types Today Today New names for old

More information

CSE 341 Section 5. Winter 2018

CSE 341 Section 5. Winter 2018 CSE 341 Section 5 Winter 2018 Midterm Review! Variable Bindings, Shadowing, Let Expressions Boolean, Comparison and Arithmetic Operations Equality Types Types, Datatypes, Type synonyms Tuples, Records

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Operational Semantics CMSC 330 Summer 2018 1 Formal Semantics of a Prog. Lang. Mathematical description of the meaning of programs written in that language

More information

CSCE 531 Spring 2009 Final Exam

CSCE 531 Spring 2009 Final Exam CSCE 531 Spring 2009 Final Exam Do all problems. Write your solutions on the paper provided. This test is open book, open notes, but no electronic devices. For your own sake, please read all problems before

More information

Chapter 6 Abstract Datatypes

Chapter 6 Abstract Datatypes Plan Chapter 6 Abstract Datatypes (Version of 17 November 2005) 1. Application: correctly parenthesised texts...... 6.2 2. An abstract datatype for stacks................ 6.6 3. Realisation of the stack

More information

1 Lexical Considerations

1 Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2013 Handout Decaf Language Thursday, Feb 7 The project for the course is to write a compiler

More information

UNIVERSITY OF TORONTO Faculty of Arts and Science. Midterm Sample Solutions CSC324H1 Duration: 50 minutes Instructor(s): David Liu.

UNIVERSITY OF TORONTO Faculty of Arts and Science. Midterm Sample Solutions CSC324H1 Duration: 50 minutes Instructor(s): David Liu. UNIVERSITY OF TORONTO Faculty of Arts and Science Midterm Sample s CSC324H1 Duration: 50 minutes Instructor(s): David Liu. No Aids Allowed Name: Student Number: Please read the following guidelines carefully.

More information

Standard ML. Data types. ML Datatypes.1

Standard ML. Data types. ML Datatypes.1 Standard ML Data types ML Datatypes.1 Concrete Datatypes The datatype declaration creates new types These are concrete data types, not abstract Concrete datatypes can be inspected - constructed and taken

More information

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine. 1. true / false By a compiler we mean a program that translates to code that will run natively on some machine. 2. true / false ML can be compiled. 3. true / false FORTRAN can reasonably be considered

More information

CSC324- TUTORIAL 5. Shems Saleh* *Some slides inspired by/based on Afsaneh Fazly s slides

CSC324- TUTORIAL 5. Shems Saleh* *Some slides inspired by/based on Afsaneh Fazly s slides CSC324- TUTORIAL 5 ML Shems Saleh* *Some slides inspired by/based on Afsaneh Fazly s slides Assignment 1 2 More questions were added Questions regarding the assignment? Starting ML Who am I? Shems Saleh

More information

Functional Programming Mid-term exam Tuesday 3/10/2017

Functional Programming Mid-term exam Tuesday 3/10/2017 Functional Programming Mid-term exam Tuesday 3/10/2017 Name: Student number: Before you begin: Do not forget to write down your name and student number above. If necessary, explain your answers in English.

More information

Lexical Considerations

Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Fall 2005 Handout 6 Decaf Language Wednesday, September 7 The project for the course is to write a

More information

CS 457/557: Functional Languages

CS 457/557: Functional Languages CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University 1 Why Lists? Lists are a heavily used data structure in many functional programs Special syntax is

More information

Syntax and Grammars 1 / 21

Syntax and Grammars 1 / 21 Syntax and Grammars 1 / 21 Outline What is a language? Abstract syntax and grammars Abstract syntax vs. concrete syntax Encoding grammars as Haskell data types What is a language? 2 / 21 What is a language?

More information

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

These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without previous written authorization. 1 2 The simplest way to create

More information

Formal Semantics. Chapter Twenty-Three Modern Programming Languages, 2nd ed. 1

Formal Semantics. Chapter Twenty-Three Modern Programming Languages, 2nd ed. 1 Formal Semantics Chapter Twenty-Three Modern Programming Languages, 2nd ed. 1 Formal Semantics At the beginning of the book we saw formal definitions of syntax with BNF And how to make a BNF that generates

More information

CSE341 Spring 2016, Midterm Examination April 29, 2016

CSE341 Spring 2016, Midterm Examination April 29, 2016 CSE341 Spring 2016, Midterm Examination April 29, 2016 Please do not turn the page until 10:30. Rules: The exam is closed-book, closed-note, etc. except for one side of one 8.5x11in piece of paper. Please

More information

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

More Assigned Reading and Exercises on Syntax (for Exam 2) More Assigned Reading and Exercises on Syntax (for Exam 2) 1. Read sections 2.3 (Lexical Syntax) and 2.4 (Context-Free Grammars) on pp. 33 41 of Sethi. 2. Read section 2.6 (Variants of Grammars) on pp.

More information

Haskell Introduction Lists Other Structures Data Structures. Haskell Introduction. Mark Snyder

Haskell Introduction Lists Other Structures Data Structures. Haskell Introduction. Mark Snyder Outline 1 2 3 4 What is Haskell? Haskell is a functional programming language. Characteristics functional non-strict ( lazy ) pure (no side effects*) strongly statically typed available compiled and interpreted

More information

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia CPL 2016, week 10 Clojure functional core Oleg Batrashev Institute of Computer Science, Tartu, Estonia April 11, 2016 Overview Today Clojure language core Next weeks Immutable data structures Clojure simple

More information

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

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics Recall Architecture of Compilers, Interpreters CMSC 330: Organization of Programming Languages Source Scanner Parser Static Analyzer Operational Semantics Intermediate Representation Front End Back End

More information

CSE341 Spring 2016, Midterm Examination April 29, 2016

CSE341 Spring 2016, Midterm Examination April 29, 2016 CSE341 Spring 2016, Midterm Examination April 29, 2016 Please do not turn the page until 10:30. Rules: The exam is closed-book, closed-note, etc. except for one side of one 8.5x11in piece of paper. Please

More information

The PCAT Programming Language Reference Manual

The PCAT Programming Language Reference Manual The PCAT Programming Language Reference Manual Andrew Tolmach and Jingke Li Dept. of Computer Science Portland State University September 27, 1995 (revised October 15, 2002) 1 Introduction The PCAT language

More information

Chapter 3. Describing Syntax and Semantics

Chapter 3. Describing Syntax and Semantics Chapter 3 Describing Syntax and Semantics Chapter 3 Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Attribute Grammars Describing the Meanings of Programs:

More information

ML Built-in Functions

ML Built-in Functions ML Built-in Functions Since ML is a functional programming language, many of its built-in functions are concerned with function application to objects and structures. In ML, built-in functions are curried

More information

Programming Language Concepts, cs2104 Lecture 04 ( )

Programming Language Concepts, cs2104 Lecture 04 ( ) Programming Language Concepts, cs2104 Lecture 04 (2003-08-29) Seif Haridi Department of Computer Science, NUS haridi@comp.nus.edu.sg 2003-09-05 S. Haridi, CS2104, L04 (slides: C. Schulte, S. Haridi) 1

More information

A Short Summary of Javali

A Short Summary of Javali A Short Summary of Javali October 15, 2015 1 Introduction Javali is a simple language based on ideas found in languages like C++ or Java. Its purpose is to serve as the source language for a simple compiler

More information

UNIVERSITETET I OSLO

UNIVERSITETET I OSLO Exam in INF3110, December 12, 2013 Page 1 UNIVERSITETET I OSLO Det matematisk-naturvitenskapelige fakultet Exam in: INF3110 Programming Languages Day of exam: December 12, 2013 Exam hours: 14:30 18:30

More information

Lexical Considerations

Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2010 Handout Decaf Language Tuesday, Feb 2 The project for the course is to write a compiler

More information

Sample Exam; Solutions

Sample Exam; Solutions Sample Exam; Solutions Michael P. Fourman February 2, 2010 1 Introduction This document contains solutions to the sample questions given in Lecture Note 10 Short Question 5 marks Give the responses of

More information

Programming Paradigms Written Exam

Programming Paradigms Written Exam Programming Paradigms Written Exam 17.06.2014 First name Student number Last name Signature Instructions for Students Write your name and student number on the exam sheet and on every solution sheet you

More information

This book is licensed under a Creative Commons Attribution 3.0 License

This book is licensed under a Creative Commons Attribution 3.0 License 6. Syntax Learning objectives: syntax and semantics syntax diagrams and EBNF describe context-free grammars terminal and nonterminal symbols productions definition of EBNF by itself parse tree grammars

More information

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013 CSE341: Programming Languages Lecture 9 Function-Closure Idioms Dan Grossman Winter 2013 More idioms We know the rule for lexical scope and function closures Now what is it good for A partial but wide-ranging

More information

Introduction to OCaml

Introduction to OCaml Fall 2018 Introduction to OCaml Yu Zhang Course web site: http://staff.ustc.edu.cn/~yuzhang/tpl References Learn X in Y Minutes Ocaml Real World OCaml Cornell CS 3110 Spring 2018 Data Structures and Functional

More information

Lecture 19: Functions, Types and Data Structures in Haskell

Lecture 19: Functions, Types and Data Structures in Haskell The University of North Carolina at Chapel Hill Spring 2002 Lecture 19: Functions, Types and Data Structures in Haskell Feb 25 1 Functions Functions are the most important kind of value in functional programming

More information

Midterm 2 Solutions Many acceptable answers; one was the following: (defparameter g1

Midterm 2 Solutions Many acceptable answers; one was the following: (defparameter g1 Midterm 2 Solutions 1. [20 points] Consider the language that consist of possibly empty lists of the identifier x enclosed by parentheses and separated by commas. The language includes { () (x) (x,x) (x,x,x)

More information

Functional Programming. Pure Functional Programming

Functional Programming. Pure Functional Programming Functional Programming Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends only on the values of its sub-expressions (if any).

More information

Contents. Chapter 1 SPECIFYING SYNTAX 1

Contents. Chapter 1 SPECIFYING SYNTAX 1 Contents Chapter 1 SPECIFYING SYNTAX 1 1.1 GRAMMARS AND BNF 2 Context-Free Grammars 4 Context-Sensitive Grammars 8 Exercises 8 1.2 THE PROGRAMMING LANGUAGE WREN 10 Ambiguity 12 Context Constraints in Wren

More information

CSE3322 Programming Languages and Implementation

CSE3322 Programming Languages and Implementation Monash University School of Computer Science & Software Engineering Sample Exam 2003 CSE3322 Programming Languages and Implementation Total Time Allowed: 3 Hours 1. Reading time is of 10 minutes duration.

More information

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

n   n Try tutorial on front page to get started! n   spring13/ n Stack Overflow! Announcements n Rainbow grades: HW1-6, Quiz1-5, Exam1 n Still grading: HW7, Quiz6, Exam2 Intro to Haskell n HW8 due today n HW9, Haskell, out tonight, due Nov. 16 th n Individual assignment n Start early!

More information

Syntax Analysis/Parsing. Context-free grammars (CFG s) Context-free grammars vs. Regular Expressions. BNF description of PL/0 syntax

Syntax Analysis/Parsing. Context-free grammars (CFG s) Context-free grammars vs. Regular Expressions. BNF description of PL/0 syntax Susan Eggers 1 CSE 401 Syntax Analysis/Parsing Context-free grammars (CFG s) Purpose: determine if tokens have the right form for the language (right syntactic structure) stream of tokens abstract syntax

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Winter 2017 Lecture 7b Andrew Tolmach Portland State University 1994-2017 Values and Types We divide the universe of values according to types A type is a set of values and

More information

COS 320. Compiling Techniques

COS 320. Compiling Techniques Topic 5: Types COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer 1 Types: potential benefits (I) 2 For programmers: help to eliminate common programming mistakes, particularly

More information

CSCE 314 Programming Languages. Functional Parsers

CSCE 314 Programming Languages. Functional Parsers CSCE 314 Programming Languages Functional Parsers Dr. Hyunyoung Lee 1 What is a Parser? A parser is a program that takes a text (set of tokens) and determines its syntactic structure. String or [Token]

More information

Higher-Order Functions

Higher-Order Functions Higher-Order Functions Tjark Weber Functional Programming 1 Based on notes by Pierre Flener, Jean-Noël Monette, Sven-Olof Nyström Tjark Weber (UU) Higher-Order Functions 1 / 1 Tail Recursion http://xkcd.com/1270/

More information

Programs as data first-order functional language type checking

Programs as data first-order functional language type checking Programs as data first-order functional language type checking Copyright 2013-18, Peter Sestoft and Cesare Tinelli. Created by Cesare Tinelli at the University of Iowa from notes originally developed by

More information

Principles of Programming Languages COMP251: Syntax and Grammars

Principles of Programming Languages COMP251: Syntax and Grammars Principles of Programming Languages COMP251: Syntax and Grammars Prof. Dekai Wu Department of Computer Science and Engineering The Hong Kong University of Science and Technology Hong Kong, China Fall 2006

More information

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

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree. Lecture 09: Data Abstraction ++ Parsing Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree. program text Parser AST Processor Compilers (and some interpreters)

More information

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type. OCaml The PL for the discerning hacker. ML Flow Expressions (Syntax) Compile-time Static 1. Enter expression 2. ML infers a type Exec-time Dynamic Types 3. ML crunches expression down to a value 4. Value

More information

Lecture 3: Recursion; Structural Induction

Lecture 3: Recursion; Structural Induction 15-150 Lecture 3: Recursion; Structural Induction Lecture by Dan Licata January 24, 2012 Today, we are going to talk about one of the most important ideas in functional programming, structural recursion

More information

Context-free grammars (CFG s)

Context-free grammars (CFG s) Syntax Analysis/Parsing Purpose: determine if tokens have the right form for the language (right syntactic structure) stream of tokens abstract syntax tree (AST) AST: captures hierarchical structure of

More information

Begin at the beginning

Begin at the beginning Begin at the beginning Expressions (Syntax) Exec-time Dynamic Values (Semantics) Compile-time Static Types 1. Programmer enters expression 2. ML checks if expression is well-typed Using a precise set of

More information

Lists. Michael P. Fourman. February 2, 2010

Lists. Michael P. Fourman. February 2, 2010 Lists Michael P. Fourman February 2, 2010 1 Introduction The list is a fundamental datatype in most functional languages. ML is no exception; list is a built-in ML type constructor. However, to introduce

More information

CSE341: Programming Languages Lecture 7 First-Class Functions. Dan Grossman Winter 2013

CSE341: Programming Languages Lecture 7 First-Class Functions. Dan Grossman Winter 2013 CSE341: Programming Languages Lecture 7 First-Class Functions Dan Grossman Winter 2013 What is functional programming? Functional programming can mean a few different things: 1. Avoiding mutation in most/all

More information

Principles of Programming Languages

Principles of Programming Languages Principles of Programming Languages Lesson 14 Type Checking Collaboration and Management Dana Fisman www.cs.bgu.ac.il/~ppl172 1 Type Checking We return to the issue of type safety we discussed informally,

More information

Functional Paradigm II

Functional Paradigm II Processadors de Llenguatge II Functional Paradigm II Pratt A.7 Robert Harper s SML tutorial (Sec II) Rafael Ramirez Dep Tecnologia Universitat Pompeu Fabra User-Defined Types How to define the new type.

More information

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1 A First Look at ML Chapter Five Modern Programming Languages, 2nd ed. 1 ML Meta Language One of the more popular functional languages (which, admittedly, isn t saying much) Edinburgh, 1974, Robin Milner

More information

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

Hard deadline: 3/28/15 1:00pm. Using software development tools like source control. Understanding the environment model and type inference. CS 3110 Spring 2015 Problem Set 3 Version 0 (last modified March 12, 2015) Soft deadline: 3/26/15 11:59pm Hard deadline: 3/28/15 1:00pm Overview In this assignment you will implement several functions

More information

CSci 4223 Principles of Programming Languages

CSci 4223 Principles of Programming Languages CSci 4223 Principles of Programming Languages Lecture 11 Review Features learned: functions, tuples, lists, let expressions, options, records, datatypes, case expressions, type synonyms, pattern matching,

More information

Lambda Calculus and Type Inference

Lambda Calculus and Type Inference Lambda Calculus and Type Inference Björn Lisper Dept. of Computer Science and Engineering Mälardalen University bjorn.lisper@mdh.se http://www.idt.mdh.se/ blr/ October 13, 2004 Lambda Calculus and Type

More information

COP 3402 Systems Software Syntax Analysis (Parser)

COP 3402 Systems Software Syntax Analysis (Parser) COP 3402 Systems Software Syntax Analysis (Parser) Syntax Analysis 1 Outline 1. Definition of Parsing 2. Context Free Grammars 3. Ambiguous/Unambiguous Grammars Syntax Analysis 2 Lexical and Syntax Analysis

More information

Functional Programming. Overview. Topics. Definition n-th Fibonacci Number. Graph

Functional Programming. Overview. Topics. Definition n-th Fibonacci Number. Graph Topics Functional Programming Christian Sternagel Harald Zankl Evgeny Zuenko Department of Computer Science University of Innsbruck WS 2017/2018 abstract data types, algebraic data types, binary search

More information

Parsing a primer. Ralf Lämmel Software Languages Team University of Koblenz-Landau

Parsing a primer. Ralf Lämmel Software Languages Team University of Koblenz-Landau Parsing a primer Ralf Lämmel Software Languages Team University of Koblenz-Landau http://www.softlang.org/ Mappings (edges) between different representations (nodes) of language elements. For instance,

More information

10/18/18. Outline. Semantic Analysis. Two types of semantic rules. Syntax vs. Semantics. Static Semantics. Static Semantics.

10/18/18. Outline. Semantic Analysis. Two types of semantic rules. Syntax vs. Semantics. Static Semantics. Static Semantics. Outline Semantic Analysis In Text: Chapter 3 Static semantics Attribute grammars Dynamic semantics Operational semantics Denotational semantics N. Meng, S. Arthur 2 Syntax vs. Semantics Syntax concerns

More information

Exercises on ML. Programming Languages. Chanseok Oh

Exercises on ML. Programming Languages. Chanseok Oh Exercises on ML Programming Languages Chanseok Oh chanseok@cs.nyu.edu Dejected by an arcane type error? - foldr; val it = fn : ('a * 'b -> 'b) -> 'b -> 'a list -> 'b - foldr (fn x=> fn y => fn z => (max

More information

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

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF) Chapter 3: Describing Syntax and Semantics Introduction Formal methods of describing syntax (BNF) We can analyze syntax of a computer program on two levels: 1. Lexical level 2. Syntactic level Lexical

More information

COMPUTER SCIENCE TRIPOS

COMPUTER SCIENCE TRIPOS CST.2011.3.1 COMPUTER SCIENCE TRIPOS Part IB Monday 6 June 2011 1.30 to 4.30 COMPUTER SCIENCE Paper 3 Answer five questions. Submit the answers in five separate bundles, each with its own cover sheet.

More information

CMSC330 Fall 2016 Midterm #1 2:00pm/3:30pm

CMSC330 Fall 2016 Midterm #1 2:00pm/3:30pm CMSC330 Fall 2016 Midterm #1 2:00pm/3:30pm Name: Discussion Time: 10am 11am 12pm 1pm 2pm 3pm TA Name (Circle): Alex Austin Ayman Brian Damien Daniel K. Daniel P. Greg Tammy Tim Vitung Will K. Instructions

More information

Intro to semantics; Small-step semantics Lecture 1 Tuesday, January 29, 2013

Intro to semantics; Small-step semantics Lecture 1 Tuesday, January 29, 2013 Harvard School of Engineering and Applied Sciences CS 152: Programming Languages Lecture 1 Tuesday, January 29, 2013 1 Intro to semantics What is the meaning of a program? When we write a program, we use

More information

PL Revision overview

PL Revision overview PL Revision overview Course topics Parsing G = (S, P, NT, T); (E)BNF; recursive descent predictive parser (RDPP) Lexical analysis; Syntax and semantic errors; type checking Programming language structure

More information

CS 3360 Design and Implementation of Programming Languages. Exam 1

CS 3360 Design and Implementation of Programming Languages. Exam 1 1 Spring 2017 (Thursday, March 9) Name: CS 3360 Design and Implementation of Programming Languages Exam 1 This test has 8 questions and pages numbered 1 through 7. Reminders This test is closed-notes and

More information

CMSC 330: Organization of Programming Languages. Operational Semantics

CMSC 330: Organization of Programming Languages. Operational Semantics CMSC 330: Organization of Programming Languages Operational Semantics Notes about Project 4, Parts 1 & 2 Still due today (7/2) Will not be graded until 7/11 (along with Part 3) You are strongly encouraged

More information

COMPUTER SCIENCE TRIPOS

COMPUTER SCIENCE TRIPOS CST.2014.3.1 COMPUTER SCIENCE TRIPOS Part IB Monday 2 June 2014 1.30 to 4.30 pm COMPUTER SCIENCE Paper 3 Answer five questions. Submit the answers in five separate bundles, each with its own cover sheet.

More information

Chapter 3 Linear Structures: Lists

Chapter 3 Linear Structures: Lists Plan Chapter 3 Linear Structures: Lists 1. Lists... 3.2 2. Basic operations... 3.4 3. Constructors and pattern matching... 3.5 4. Polymorphism... 3.8 5. Simple operations on lists... 3.11 6. Application:

More information

Programming Language Syntax and Analysis

Programming Language Syntax and Analysis Programming Language Syntax and Analysis 2017 Kwangman Ko (http://compiler.sangji.ac.kr, kkman@sangji.ac.kr) Dept. of Computer Engineering, Sangji University Introduction Syntax the form or structure of

More information

General Computer Science (CH ) Fall 2016 SML Tutorial: Practice Problems

General Computer Science (CH ) Fall 2016 SML Tutorial: Practice Problems General Computer Science (CH08-320101) Fall 2016 SML Tutorial: Practice Problems November 30, 2016 Abstract This document accompanies the traditional SML tutorial in GenCS. It contains a sequence of simple

More information

CA4003 Compiler Construction Assignment Language Definition

CA4003 Compiler Construction Assignment Language Definition CA4003 Compiler Construction Assignment Language Definition David Sinclair 2017-2018 1 Overview The language is not case sensitive. A nonterminal, X, is represented by enclosing it in angle brackets, e.g.

More information

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

Parsing. Zhenjiang Hu. May 31, June 7, June 14, All Right Reserved. National Institute of Informatics National Institute of Informatics May 31, June 7, June 14, 2010 All Right Reserved. Outline I 1 Parser Type 2 Monad Parser Monad 3 Derived Primitives 4 5 6 Outline Parser Type 1 Parser Type 2 3 4 5 6 What

More information

CSE-321: Assignment 8 (100 points)

CSE-321: Assignment 8 (100 points) CSE-321: Assignment 8 (100 points) gla@postech.ac.kr Welcome to the final assignment of CSE-321! In this assignment, you will implement a type reconstruction algorithm (the algorithm W discussed in class)

More information

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m.

Note that in this definition, n + m denotes the syntactic expression with three symbols n, +, and m, not to the number that is the sum of n and m. CS 6110 S18 Lecture 8 Structural Operational Semantics and IMP Today we introduce a very simple imperative language, IMP, along with two systems of rules for evaluation called small-step and big-step semantics.

More information

CSCE 314 Programming Languages

CSCE 314 Programming Languages CSCE 314 Programming Languages Final Review Part I Dr. Hyunyoung Lee 1 Programming Language Characteristics Different approaches to describe computations, to instruct computing devices E.g., Imperative,

More information

COMPUTER SCIENCE TRIPOS

COMPUTER SCIENCE TRIPOS CST.0.. COMPUTER SCIENCE TRIPOS Part IA NATURAL SCIENCES TRIPOS Part IA (Paper CS/) POLITICS, PSYCHOLOGY, AND SOCIOLOGY TRIPOS Part I (Paper 9) Monday June 0.0 to 4.0 COMPUTER SCIENCE Paper Answer five

More information

n Closed book n You are allowed 5 cheat pages n Practice problems available in Course Materials n Check grades in Rainbow grades

n Closed book n You are allowed 5 cheat pages n Practice problems available in Course Materials n Check grades in Rainbow grades Announcements Announcements n HW12: Transaction server n Due today at 2pm n You can use up to 2 late days, as always n Final Exam, December 17, 3-6pm, in DCC 308 and 318 n Closed book n You are allowed

More information

A Second Look At ML. Chapter Seven Modern Programming Languages, 2nd ed. 1

A Second Look At ML. Chapter Seven Modern Programming Languages, 2nd ed. 1 A Second Look At ML Chapter Seven Modern Programming Languages, 2nd ed. 1 Outline Patterns Local variable definitions A sorting example Chapter Seven Modern Programming Languages, 2nd ed. 2 Two Patterns

More information

Chapter 3 Linear Structures: Lists

Chapter 3 Linear Structures: Lists Plan Chapter 3 Linear Structures: Lists 1. Two constructors for lists... 3.2 2. Lists... 3.3 3. Basic operations... 3.5 4. Constructors and pattern matching... 3.6 5. Simple operations on lists... 3.9

More information

CSE341 Autumn 2017, Midterm Examination October 30, 2017

CSE341 Autumn 2017, Midterm Examination October 30, 2017 CSE341 Autumn 2017, Midterm Examination October 30, 2017 Please do not turn the page until 2:30. Rules: The exam is closed-book, closed-note, etc. except for one side of one 8.5x11in piece of paper. Please

More information

CMSC330 Spring 2017 Midterm 2

CMSC330 Spring 2017 Midterm 2 CMSC330 Spring 2017 Midterm 2 Name (PRINT YOUR NAME as it appears on gradescope ): Discussion Time (circle one) 10am 11am 12pm 1pm 2pm 3pm Discussion TA (circle one) Aaron Alex Austin Ayman Daniel Eric

More information

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology exam Compiler Construction in4020 July 5, 2007 14.00-15.30 This exam (8 pages) consists of 60 True/False

More information

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

Semantic analysis and intermediate representations. Which methods / formalisms are used in the various phases during the analysis? Semantic analysis and intermediate representations Which methods / formalisms are used in the various phases during the analysis? The task of this phase is to check the "static semantics" and generate

More information

CSE 311 Lecture 21: Context-Free Grammars. Emina Torlak and Kevin Zatloukal

CSE 311 Lecture 21: Context-Free Grammars. Emina Torlak and Kevin Zatloukal CSE 311 Lecture 21: Context-Free Grammars Emina Torlak and Kevin Zatloukal 1 Topics Regular expressions A brief review of Lecture 20. Context-free grammars Syntax, semantics, and examples. 2 Regular expressions

More information

CSE 130, Fall 2006: Final Examination

CSE 130, Fall 2006: Final Examination CSE 130, Fall 2006: Final Examination Name: ID: Instructions, etc. 1. Write your answers in the space provided. 2. Wherever it says explain, write no more than three lines as explanation. The rest will

More information

CS 320: Concepts of Programming Languages

CS 320: Concepts of Programming Languages CS 320: Concepts of Programming Languages Wayne Snyder Computer Science Department Boston University Lecture 04: Basic Haskell Continued o Polymorphic Types o Type Inference with Polymorphism o Standard

More information

6.821 Programming Languages Handout Fall MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Compvter Science

6.821 Programming Languages Handout Fall MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Compvter Science 6.821 Programming Languages Handout Fall 2002 MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Compvter Science Problem Set 6 Problem 1: cellof Subtyping Do exercise 11.6

More information