INFOB3TC Solutions for the Exam

Similar documents
INFOB3TC Solutions for Exam 1

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

Talen en Compilers. Jurriaan Hage , period 2. November 13, Department of Information and Computing Sciences Utrecht University

Syntax and Grammars 1 / 21

Lexical and Syntax Analysis. Top-Down Parsing

Part I: Multiple Choice Questions (40 points)

PROGRAMMING IN HASKELL. CS Chapter 6 - Recursive Functions

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

Specifying Syntax. An English Grammar. Components of a Grammar. Language Specification. Types of Grammars. 1. Terminal symbols or terminals, Σ

([1-9] 1[0-2]):[0-5][0-9](AM PM)? What does the above match? Matches clock time, may or may not be told if it is AM or PM.

Lecture 2: Big-Step Semantics

CSCE 314 Programming Languages

CSCE 314 Programming Languages. Functional Parsers

CSCI312 Principles of Programming Languages!

Compilers Course Lecture 4: Context Free Grammars

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08013 INFORMATICS 1 - FUNCTIONAL PROGRAMMING

Defining Languages GMU

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

Lexical and Syntax Analysis

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Functional Parsers

Syntax-Directed Translation

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

CSCE 314 Programming Languages

University of Utrecht. 1992; Fokker, 1995), the use of monads to structure functional programs (Wadler,

MIT Specifying Languages with Regular Expressions and Context-Free Grammars

Defining syntax using CFGs

Shell CSCE 314 TAMU. Higher Order Functions

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

Lecture 19: Functions, Types and Data Structures in Haskell

Theorem Proving Principles, Techniques, Applications Recursion

7. Introduction to Denotational Semantics. Oscar Nierstrasz

CMPS Programming Languages. Dr. Chengwei Lei CEECS California State University, Bakersfield

A simple syntax-directed

Exercise 1: Balanced Parentheses

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

WARNING for Autumn 2004:

CSCE 314 Programming Languages

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

Introduction to Parsing. Lecture 8

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

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

Logic - CM0845 Introduction to Haskell

Introduction to Parsing Ambiguity and Syntax Errors

The List Datatype. CSc 372. Comparative Programming Languages. 6 : Haskell Lists. Department of Computer Science University of Arizona

Lexical Analysis. COMP 524, Spring 2014 Bryan Ward

Building a Parser III. CS164 3:30-5:00 TT 10 Evans. Prof. Bodik CS 164 Lecture 6 1

Introduction to Parsing Ambiguity and Syntax Errors

CS 320: Concepts of Programming Languages

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

announcements CSE 311: Foundations of Computing review: regular expressions review: languages---sets of strings

CS 314 Principles of Programming Languages

INFOB3TC Exam. Johan Jeuring. Monday, 31 January 2011, 09:00 11:30, EDUC-GAMMA

CS Lecture 2. The Front End. Lecture 2 Lexical Analysis

Habanero Extreme Scale Software Research Project

MIT Specifying Languages with Regular Expressions and Context-Free Grammars. Martin Rinard Massachusetts Institute of Technology

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS

Exercise 1 (2+2+2 points)

EECS 700 Functional Programming

CS 360: Programming Languages Lecture 10: Introduction to Haskell

Lexical Analysis. Introduction

University of Technology Department of Computer Sciences. Final Examination st Term. Subject:Compilers Design

CS1622. Today. A Recursive Descent Parser. Preliminaries. Lecture 9 Parsing (4)

Talen en Compilers. Johan Jeuring , period 2. January 17, Department of Information and Computing Sciences Utrecht University

Introduction to Functional Programming in Haskell 1 / 56

Defining Functions. CSc 372. Comparative Programming Languages. 5 : Haskell Function Definitions. Department of Computer Science University of Arizona

Syntax. In Text: Chapter 3

CPS 506 Comparative Programming Languages. Syntax Specification

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Compiler Construction

Chapter 3. Describing Syntax and Semantics ISBN

Type-indexed functions in Generic Haskell

Problem Set CVO 103, Spring 2018

A general introduction to Functional Programming using Haskell

Programs as data Parsing cont d; first-order functional language, type checking

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFR08013 INFORMATICS 1 - FUNCTIONAL PROGRAMMING

CMSC 330: Organization of Programming Languages. Context Free Grammars

ADTS, GRAMMARS, PARSING, TREE TRAVERSALS

CS 360: Programming Languages Lecture 12: More Haskell

02157 Functional Programming Interpreters for two simple languages including exercises

Syntax-Directed Translation. Lecture 14

Derivations vs Parses. Example. Parse Tree. Ambiguity. Different Parse Trees. Context Free Grammars 9/18/2012

If you are going to form a group for A2, please do it before tomorrow (Friday) noon GRAMMARS & PARSING. Lecture 8 CS2110 Spring 2014

Where We Are. CMSC 330: Organization of Programming Languages. This Lecture. Programming Languages. Motivation for Grammars

CS 164 Programming Languages and Compilers Handout 8. Midterm I

Bottom-Up Parsing. Lecture 11-12

Lambda Calculus and Type Inference

Compiler Construction

Chapter 3: Lexing and Parsing

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

Denotational Semantics. Domain Theory

Lecture 4: Higher Order Functions

CVO103: Programming Languages. Lecture 5 Design and Implementation of PLs (1) Expressions

Programming Language Features. CMSC 330: Organization of Programming Languages. Turing Completeness. Turing Machine.

CMSC 330: Organization of Programming Languages

Introduction to Bottom-Up Parsing

Languages and Compilers

COP 3402 Systems Software Top Down Parsing (Recursive Descent)

Programming Language Concepts, cs2104 Lecture 04 ( )

Transcription:

Department of Information and Computing Sciences Utrecht University INFOB3TC Solutions for the Exam Johan Jeuring Monday, 13 December 2010, 10:30 13:00 lease keep in mind that often, there are many possible solutions, and that these example solutions may contain mistakes. Context-free grammars 1 (10 points). Let A = {x, y}. Give context-free grammars for the following languages over the alphabet A: (a) L 1 = {w w A, #(x, w) = #(y, w)} (b) L 2 = {w w A, #(x, w) > #(y, w)} Here, #(c, w) denotes the number of occurrences of a terminal c in a word w. Solution 1. (a) A context-free grammar for L 1 is given in the solution to exercise 2.10 in the lecture notes. Establish an inductive definition for L 1. An example of a grammar that can be derived from the inductive definition is: E ε xey yex EE There are many other solutions. (b) Using E, we define the the following grammar for L 2 : L xe Ex ExE L xl Lx LL Again, there are many other solutions. 1

Grammar analysis and transformation Consider the following context-free grammar G over the alphabet {a, b} with start symbol S: S Sab Sa A A aa as 2 (5 points). Is the word aababab in L(G)? If yes, give a parse tree. If not, argue informally why the word cannot be in the language. Solution 2. The word aababab is not in L(G), because L(G) does not contain any finite words. Any production has a non-terminal in the right-hand side, so it is impossible to produce a word that only contains terminals. 3 (10 points). Simplify the grammar G by transforming it in steps. erform as many as possible of the following transformations: removal of left recursion, left factoring, and removal of unreachable productions. Solution 3. This is the original grammar: S Sab Sa A A aa as We can first left-factor the grammar: S SaR A R b ε A at T A S Now, we remove left recursion: S AZ? Z arz? R b ε A at T A S Of course, it is possible to remove left recursion first and perform left factoring later. New parser combinators 4 (4 points). A price in dollars can be written in two ways: $10, or 10$. So the currency may appear either before, or after the amount. Write a parser combinator beforeorafter that takes two parsers p and q as argument, and parses p either before or after q: beforeorafter :: arser Char a arser Char b arser Char (a, b) 2

Solution 4. beforeorafter p q = (, ) <$> p < > q < > flip (, ) <$> q < > p testboal = parse (beforeorafter (symbol $ ) natural) "$10" testboar = parse (beforeorafter (symbol $ ) natural) "10$" 5 (6 points). Binding constructs are used in many languages, here are some examples: x := 3 (x, y) pairs f = λx x A binding consists of a pattern to the left of a binding token, and then a value to which the pattern is bound. I want to define a parser pbind that parses such bindings. pbind takes a parser for patterns, a parser for the binding token, and a parser for the value to which the pattern is bound, and returns the pair of values consisting of the pattern and the value. For the above examples, this would be (x, 3), ((x, y), pairs), and (f, λx x), respectively. Define the parser pbind. Solution 5. pbind :: arser s a arser s b arser s c arser s (a, c) pbind lhs btoken rhs = (, ) <$> lhs < btoken < > rhs testbind = parse (pbind identifier (token ":=") natural) "x:=3" An efficient choice combinator 6 (10 points). The choice parser combinator is defined by (< >) :: arser s a arser s a arser s a (p < > q) xs = p xs ++ qxs The ++ in the right-hand side of this definition is a source of inefficiency, and might lead to rather slow parsers. We will use a standard approach to removing this efficiency: replace append (++) by composition (.). To do this, we need to turn our parser type into a so-called accumulating parser type. The new parser type looks as follows: type Accarser s a = [s] [(a, [s])] [(a, [s])] Define the parser combinators symbol and < > using the Accarser type: symbol :: Char Accarser Char Char (< >) :: Accarser s a Accarser s a Accarser s a 3

Solution 6. symbol :: Char Accarser Char Char symbol c [] = id symbol c (y : ys) = if c = = y then ((c, ys):) else id (< >) :: Accarser s a Accarser s a Accarser s a p < > q = λxs p xs. q xs testacc = (symbol a < > symbol b ) "ab" [] arsing polynomials In secondary school mathematics you have encountered polynomials. A polynomial is an expression of finite length constructed from variables and constants, using only the operations of addition, subtraction, multiplication, and non-negative integer exponents. Here are some examples of polynomials: x + 2, x 2 + 3x 4, (x + 2) 2, and x 5 2y 7 + z. In this exercise, you will develop a parser for polynomials. Since it is hard to represent superscripts in a string, we assume that before the above polynomials are parsed, they are transformed into the following expressions: x+2, x^2+3x-4, (x+2)^2, and x^5-2y^7+z. So integer exponents are turned into normal constants preceded by a ^. Here is a grammar for polynomials with start symbol : Nat Identifier + - ^Nat () olynomials can be composed from constant naturals (described by means of the nonterminal Nat), variables (identifiers, described by means of the nonterminal Identifier), by using addition, subtraction and multiplication (which is invisible), by exponentiation with a natural number, and parentheses for grouping. A corresponding abstract syntax in Haskell is: data = Const Int Var String Add Sub Mul Exp Int deriving Show 4

7 (5 points). Is the context-free grammar for polynomials ambiguous? Motivate your answer. Solution 7. The grammar is ambiguous: we can construct two different parse trees for the sentence x+y+2. + Identifier x Identifier y + Nat 2 Identifier + Identifier + Nat 2 x y 8 (10 points). Resolve the operator priorities in the grammar as follows: exponentiation (^) binds stronger than (invisible) multiplication, which in turn binds stronger than addition + and subtraction -. Furthermore, multiplication, addition and subtraction associate to the left. Give the resulting grammar. Solution 8. We split into several nonterminals, corresponding to the different priority levels: 1 1 1 + 2 1-2 2 2 2 3 3 3 4^Nat 4 4 Nat Identifier () 9 (10 points). Give a parser that recognizes the grammar from Task 8 and produces a value of type : parse :: arser Char 5

You can use chainl and chainr, but if you want more advanced abstractions such as gen from the lecture notes, you have to define them yourself. You may assume that spaces are not allowed in the input. Remember to not define left-recursive parsers. Solution 9. This is a rather direct transcription of the grammar as a parser using chainl: parse = p 1 p 1 = chainl p 2 ((Add <$ symbol + ) < > (Sub <$ symbol - )) p 2 = chainl p 3 (Mul <$ epsilon) p 3 = Exp <$> p 4 < symbol ^ < > natural < > p 4 p 4 = Const <$> natural < > Var <$> identifier < > parenthesised parse Using identifier, many 1 or greedy 1 for the Var case is also ok. The test-cases work as expected: ex0 = "x+2" ex1 = "x^2+3x-4" ex2 = "(x+2)^2" ex3 = "x^5-2y^7+z" tex0 = parse parse ex0 tex1 = parse parse ex1 tex2 = parse parse ex2 tex3 = parse parse ex3 10 (10 points). Define an algebra type and a fold function for type. Solution 10. We apply the systematic translation: type Algebra r = (Int r, String r, r r r, r r r, r r r, r Int r) Const Var Add Sub Mul Exp fold :: Algebra r r fold (const, var, add, sub, mul, exp) = f where f (Const x ) = const x f (Var x ) = var x f (Add x 1 x 2 ) = add (f x 1 ) (f x 2 ) f (Sub x 1 x 2 ) = sub (f x 1 ) (f x 2 ) f (Mul x 1 x 2 ) = mul (f x 1 ) (f x 2 ) f (Exp x i ) = exp (f x) i No surprises here. 6

11 (5 points). A constant polynomial is a polynomial in which no variables appear. So 4 + 2 2 is constant, but x + y and x 2 x 2 are not. Using the algebra and fold, determine whether or not a polynomial is constant: Solution 11. isconstant :: Bool isconstant = fold (const True, const False, ( ), ( ), ( ), λx i x ) testisconstantf = parse (isconstant <$> parse) ex0 testisconstantt = parse (isconstant <$> parse) "3+4^2" 12 (5 points). Using the algebra and fold (or alternatively directly), define an evaluator for polynomials: eval :: Env Int The environment of type Env should map free variables to integer values. You can either use a list of pairs or a finite map with the following interface to represent the environment: data Map k v abstract type, maps keys of type k to values of type v empty :: Map k v (!) :: Ord k Map k v k v insert :: Ord k k v Map k v Map k v delete :: Ord k k Map k v Map k v member :: Ord k k Map k v Bool fromlist :: Ord k [(k, v)] Map k v Solution 12. We assume type Env = Map String Int The algebra is similar to the evaluator for expressions discussed in the lectures: 7

evalalgebra :: Algebra (Env Int) evalalgebra = (λx e x, λx e e! x, λx 1 x 2 e x 1 e + x 2 e, λx 1 x 2 e x 1 e x 2 e, λx 1 x 2 e x 1 e x 2 e, λx i e (x e) i ) eval = fold evalalgebra The environment never changes, so defining evalalgebra :: Env Algebra Int evalalgebra e = (id, (e!), (+), ( ), ( ), ( )) is simpler and ok as well. 8