# CS 842 Ben Cassell University of Waterloo

Size: px
Start display at page:

Transcription

1 CS 842 Ben Cassell University of Waterloo

2 Recursive Descent Re-Cap Top-down parser. Works down parse tree using the formal grammar. Built from mutually recursive procedures. Typically these procedures represent the production rules of the grammar. Lend themselves very well to representation in both functional languages and simple, nonfunctional equivalents.

3 Sample Recursive Descent Parser Consider the language defined as follows: A a + B B A B a Generates words such as: a + a a + a + a a + a + a + a Based on samples available at

4 parsea([parse, input]) if (isfirst(input, a ) and isfirst(rest(input), + )) then return parseb([parse, rest(rest(input))]) else return [false, ] parseb([parse, input]) if (isfirst(input, ( )) then let [parse2, input2] = parsea([parse, rest(input)]) if (parse2 and isfirst(input2, ) )) then return [parse, rest(input2)] else return [false, ] else if (isfirst(input, a )) then return [parse, rest(input)] else return [false, ]

5 Types of Recursive Descent Parsers At a high level there are two primary types of recursive descent parsers: Predictive parsers. Backtracking parsers.

6 Predictive Parsing Parse the class of LL(k) grammars. Use the k token lookahead to determine which production rule to choose. Cannot handle some ambiguous grammars (depending on lookahead) nor left recursion Which is ok because LL(k) does not include any of these grammars! Should be noted that you don t necessarily create an LL(k) grammar by removing left recursion. Predictive parsing on LL(k) grammars runs in linear time.

7 Breaking Predictive Parsing Left recursion: A Aa A ϵ Ambiguous grammar: A A + A A a

8 Backtracking Parsers Although k-lookahead parsers are very fast, they are also limited. Backtracking parsers attempts production rules in turn, rewinding given an error and trying other alternatives. Backtracking parsers can handle a much larger variety of languages, but might not terminate on non-ll(k) languages. Even when they do terminate, backtracking parsers can require exponential time to run if implemented naively.

9 A Potentially Slow Example Consider the ambiguous grammar: A aaa A ϵ This will return an exponential number of valid parses for any series of a s!

10 Memoization Consider the following implementation of the Fibonacci numbers in Haskell: fib Int Integer fib 0 = 0 fib 1 = 1 fib n = fib n 2 + fib n 1 As calls to this function get larger, they will become significantly slower.

11 Now, consider this implementation instead: fib Int Integer fib = map fibrec 0..!! where fibrec 0 = 0 fibrec 1 = 1 fibrec n = fib n 2 + fibrec n 1 Similar techniques can be manually applied to backtracking parsers to significantly improve performance. Why run the same production rule multiple times if you already know what it will produce? Based on samples available at

12 Extending Memoization Wouldn t it be great if the technique for memoization could be extended out into a general form that could simply be pluggedin to our other functions? Maybe we could even use similar techniques to solve some of the problems with recursive descent parsers if applied correctly!

13 Papers Techniques for Automatic Memoization with Applications to Context-Free Parsing Peter Norvig (UC Berkley), 1991 Memoization in Top-Down Parsing Mark Johnson (Brown), 1995

14 Techniques for Automatic Memoization Claims that an algorithm similar to Earley s algorithm can be generated with a backtracking parser using memoization. Earley s algorithm: Top-down dynamic programming algorithm. Set of states to examine. Starts with only the top rule, and as input is processed, new rules are added to the set of states by prediction, scanning and completion. Has O n 3 time complexity where n is the length of the string, and O n 2 time complexity when the grammar is unambiguous.

15 Memoizing Functions in General Consider the following code from the paper: (defun memo (fn) (let ((table (make-hash-table))) # (lambda (x) (multiple-value-bind (val found) (gethash x table) (if found val (setf (gethash x table) (funcall fn x)))))))

16 Problems with the Implementation The function fn being memoized is required to both take and return one value. This is probably too restrictive to be useful. Also, more importantly, what if fn makes any recursive calls? Recursive calls will go to the original version of fn, and not the memoized version. This mostly defeats the point of using the current version, especially for functional parsing.

17 One Possible Solution Globally rebind what fn points to: (defun memoize (fn-name) (setf (symbol-function fn-name) (memo (symbol-function fn-name)))) This is highly useful, but still has a limitation: Memoized functions can only have one argument. It should accept arbitrary arguments, and we should be able to index on arbitrary combinations of them.

18 Updated Version (defun memo (fn &key (key # first) (test # eql) name) (let ((table (make-hash-table :test test))) (setf (get name memo) table) # (lambda (&rest args) let ((k funcall key args))) (multiple-value-bind (val found) (gethash k table) (if found val (setf (gethash k table) (apply fn args))))))))

19 Updated Version (Continued) (defun memoize (fn-name &key (key # first) (test # eql)) (setf (symbol-function fn-name) (memo (symbol-function fn-name) :name fn-name :key key :test test)))

20 Notes About the New Version The hash table is stored on the property list for the function name, meaning it can be inspected, cleared or otherwise modified. Useful when the working set changes. The default key function is first. This is fine for a single argument. In lisp, identity can be used to hash on all the arguments. The test by default uses eql. Can be changed to equal or other tests as desired. equal, for instance, requires more computational overhead but will prevent duplicated lists.

21 Using Memoize to Parse: A Simple Top- Down Parser (defun parse (tokens start-symbol) (if (eq (first tokens) start-symbol) (list (make-parse :tree (first tokens) :rem (rest tokens))) (mapcan # (lambda (rule) (extend-parse (lhs rule) nil tokens (rhs rule))) (rules-for start-symbol) (defun extend-parse (lhs rhs rem needed) (if (null needed) (list (make-parse :tree (cons lhs rhs) :rem rem)) (mapcan # (lambda (p) (extend-parse lhs (append rhs (list (parse-tree p))) (parse-rem p) (rest needed))) (parse rem (first needed)))))

22 Adding Memoization (memoize rules-for) (memoize parse :test # equal :key # identity) (defun parser (tokens start-symbol) (clear-memoize parse) (mapcar # parse-tree (remove-if-not # null (parse tokens start-symbol) :key # parse-rem))) Parse returned all valid parses of all prefixes of the input. Parser looks for completeness.

23 Limitations The algorithm is equivalent to Earley s (not proved), but with O n 4 complexity. The asymptotic complexity is worse because of the use of equal over remaining tokens. O n 3 is achieved by adding a different type of hash table (compromising between eql and equal), and a memoize that allows a user-specified hash getter and putter functions. Explicitly cannot handle left-recursion (the authors mention this directly). Hash tables that are not very carefully implemented can result in very poor performance.

24 Silver Linings The parser is exceedingly simple (15 lines of code or less). The technique applies beyond parsing. The authors show automatic memoization applied across various languages: Scheme. Pascal.

25 Memoization in Top-Down Parsing Goal: Discover why left-recursion fails for memoized parsers and present a memoization technique that can handle it. Takeaway: Instead of returning a set of positions as a single value for the right string positions of a category, return them incrementally.

26 Symbols Used Throughout Uses symbols similar to The Functional Treatment of Parsing (Leermakers, 1993): S: Sentence (S NP VP) N: Noun ( student, professor ) V: Verb ( likes, knows ) Det: Determinant ( every, no ) PN: Person Name ( Kim, Sandy ) NP: Noun phrase, (NP PN Det N) VP: Verb phrase, (VP V NP V S) This rule in Figure 1 of the paper has a typo. (seq (V S)) should read (seq V S) to properly represent VP V S.

27 Formalizing Grammars Johnson creates a recursive descent parser quite similar to Norvig s, and defines higher order functions to simplify the process. reduce: Recursively applies a function across a list of arguments. For example, (reduce f x (1 2 3)) would reduce to (f (f (f x 1) 2) 3). union: Construct a unique list from two lists. terminal: Map a substring to a terminal if it matches, otherwise empty. seq: Recognize a concatenation of substrings recognized by two functions.

28 Formalizing Grammars (Continued) alt: Recognize the union of substrings recognized by two functions. epsilon: Recognizes the empty string. opt: Recognizes optional elements. k*: Recognize the Kleene star of an element. recognize: Return true if the string passed in can be parsed from the start symbol.

29 Language Problem Consider the following examples: (define S (seq NP VP)) (define VP (alt (seq V NP) (seq V S))) In Scheme, the way these are written incurs a mutual recursion issue (the binding will fail). The fix for this is fairly straight-forward: (define-syntax vacuous (syntax-rules () ((vacuous fn) (lambda args (apply fn args))))) (define S (vacuous (seq NP VP)))

30 More Involved Problems The provided rules act as a top-down parser. Results are returned as a list of suffixes. We get our usual left-recursive problems. There is a non-trivial amount of re-computation that occurs by default. Memoization can prevent the re-computation. The presented memo function is a Scheme version of the Norvig technique. Now we can write: (define S (memo (vacuous (seq NP VP)))) As in the Norvig paper however, this still doesn t allow the parsing of left-recursive grammars.

31 What s the Fundamental Problem? To memoize a result, it first needs to be fully computed, which is impossible. Instead, memoize calls as they are made and lazily evaluate the results as needed. To do this, Johnson uses a technique called Continuation-Passing Style with memoization. Provide a function to return a result to. This effectively reverses the direction of computation from bottom-up to top-down.

32 Sample CPS Function Traditional definition: (define (square x) (* x x)) CPS definition: (define (square cont x) (cont (* x x))) (square display 10)

33 How do the Parsing Functions Change? Rules (A) are now represented by functions g A c l that reduce in a way such that (c r) only reduces if A can derive the string from positions l to r. In other words, (c r) is evaluated zero or more times with r bound to right string positions. This implies that instead of returning a set of string positions, we re simply calling the continuation for each result position instead. The terminal will could now be written as: (define (future-aux continuation pos) (if (and (pair? pos) (eq? (car pos) will)) (continuation (cdr pos))) The rule VP V NP V S could be written: (define (VP continuation pos) (begin (V (lambda (pos1) (NP continuation pos1)) pos) (V (lambda (pos1) (S continuation pos1)) pos)))

34 Simplifying and Recognizing In the previous example, the lambda expression tells the function V to pass a parsed V s right string position into NP and S. Johnson redefines alt, seq and terminal to simplify this process. The recognize function now simply passes a continuation that marks whether or not a word is successfully parsed. The example uses a set! but this is not necessary. Problem: This still fails to terminate, even in the memoized version!

35 The Secret Sauce: Updating Memo Memo table entries can t record reduced results. In CPS the results are what are passed forwards to the continuation. New memo function stores a set of argument values that maps to a list of caller continuations and a list of result values. Result values are propagated and updated as new values are returned. Values that are not subsumed by previous ones are added to the list of associated entries. When a function is called it looks up whether or not values already exist to pass to the continuation based on the provided arguments.

36 Left Recursion is Fixed! Unmemoized functions are never called more than once on the same arguments. They can fall back on the lazily-evaluated continuation list stored in the hash table. Even a left-recursive grammar can look up the continuation and result list and pass computation forwards as the CPS-style calls enumerate through the parse of the input string. Progress is always made because the left-recursive call does not need to drill down again to produce a concrete result, proving that the lazy shall inherit the Earth!

37 The End

### Functional programming techniques

Functional programming techniques o Currying o Continuations o Streams. Lazy evaluation Currying o Haskell B. Curry. o Second-order programming: o Functions that return other functions. o Example: A two-arguments

### Squibs and Discussions Memoization in Top-Down Parsing

Squibs and Discussions Memoization in Top-Down Parsing Mark Johnson" Brown University 1. Introduction In a paper published in this journal, Norvig (1991) pointed out that memoization of a top-down recognizer

### INF4820. Common Lisp: Closures and Macros

INF4820 Common Lisp: Closures and Macros Erik Velldal University of Oslo Oct. 19, 2010 Erik Velldal INF4820 1 / 22 Topics for Today More Common Lisp A quick reminder: Scope, binding and shadowing Closures

### Lisp Basic Example Test Questions

2009 November 30 Lisp Basic Example Test Questions 1. Assume the following forms have been typed into the interpreter and evaluated in the given sequence. ( defun a ( y ) ( reverse y ) ) ( setq a (1 2

### UMBC CMSC 331 Final Exam

UMBC CMSC 331 Final Exam Name: UMBC Username: You have two hours to complete this closed book exam. We reserve the right to assign partial credit, and to deduct points for answers that are needlessly wordy

### Principles of Programming Languages 2017W, Functional Programming

Principles of Programming Languages 2017W, Functional Programming Assignment 3: Lisp Machine (16 points) Lisp is a language based on the lambda calculus with strict execution semantics and dynamic typing.

### Functional programming with Common Lisp

Functional programming with Common Lisp Dr. C. Constantinides Department of Computer Science and Software Engineering Concordia University Montreal, Canada August 11, 2016 1 / 81 Expressions and functions

### Homework & Announcements

Homework & nnouncements New schedule on line. Reading: Chapter 18 Homework: Exercises at end Due: 11/1 Copyright c 2002 2017 UMaine School of Computing and Information S 1 / 25 COS 140: Foundations of

### Syntactic Analysis. CS345H: Programming Languages. Lecture 3: Lexical Analysis. Outline. Lexical Analysis. What is a Token? Tokens

Syntactic Analysis CS45H: Programming Languages Lecture : Lexical Analysis Thomas Dillig Main Question: How to give structure to strings Analogy: Understanding an English sentence First, we separate a

### Racket: Macros. Advanced Functional Programming. Jean-Noël Monette. November 2013

Racket: Macros Advanced Functional Programming Jean-Noël Monette November 2013 1 Today Macros pattern-based macros Hygiene Syntax objects and general macros Examples 2 Macros (According to the Racket Guide...)

### SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

SCHEME 7 COMPUTER SCIENCE 61A October 29, 2015 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

### COP4020 Spring 2011 Midterm Exam

COP4020 Spring 2011 Midterm Exam Name: (Please print Put the answers on these sheets. Use additional sheets when necessary or write on the back. Show how you derived your answer (this is required for full

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

Predictive Parsers LL(k) Parsing Can we avoid backtracking? es, if for a given input symbol and given nonterminal, we can choose the alternative appropriately. his is possible if the first terminal of

### Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003 CS 314, LS, LTM: Functional Programming 1 Scheme A program is an expression to be evaluated (in

### Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

CS 6A Scheme Fall 208 Discussion 8: October 24, 208 Solutions Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write

### Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

CS 6A Scheme Fall 207 Discussion 7: October 25, 207 Solutions Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write

### 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).

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

Building a Parser III CS164 3:30-5:00 TT 10 Evans 1 Overview Finish recursive descent parser when it breaks down and how to fix it eliminating left recursion reordering productions Predictive parsers (aka

### User-defined Functions. Conditional Expressions in Scheme

User-defined Functions The list (lambda (args (body s to a function with (args as its argument list and (body as the function body. No quotes are needed for (args or (body. (lambda (x (+ x 1 s to the increment

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

Scheme Tutorial Introduction Scheme is an imperative language with a functional core. The functional core is based on the lambda calculus. In this chapter only the functional core and some simple I/O is

### Top-Down Parsing and Intro to Bottom-Up Parsing. Lecture 7

Top-Down Parsing and Intro to Bottom-Up Parsing Lecture 7 1 Predictive Parsers Like recursive-descent but parser can predict which production to use Predictive parsers are never wrong Always able to guess

### Functional Programming. Pure Functional Languages

Functional Programming Pure functional PLs S-expressions cons, car, cdr Defining functions read-eval-print loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure

### Symbolic Programming. Dr. Zoran Duric () Symbolic Programming 1/ 89 August 28, / 89

Symbolic Programming Symbols: +, -, 1, 2 etc. Symbolic expressions: (+ 1 2), (+ (* 3 4) 2) Symbolic programs are programs that manipulate symbolic expressions. Symbolic manipulation: you do it all the

### Parsing. Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice.

Parsing Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice. Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students

### Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

CS 6A Scheme Summer 207 Discussion 0: July 25, 207 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

### Streams, Delayed Evaluation and a Normal Order Interpreter. CS 550 Programming Languages Jeremy Johnson

Streams, Delayed Evaluation and a Normal Order Interpreter CS 550 Programming Languages Jeremy Johnson 1 Theme This lecture discusses the stream model of computation and an efficient method of implementation

### Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

CS 61A Scheme Spring 2018 Discussion 7: March 21, 2018 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme

### Syntax Analysis, III Comp 412

COMP 412 FALL 2017 Syntax Analysis, III Comp 412 source code IR Front End Optimizer Back End IR target code Copyright 2017, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp

### Comp 411 Principles of Programming Languages Lecture 3 Parsing. Corky Cartwright January 11, 2019

Comp 411 Principles of Programming Languages Lecture 3 Parsing Corky Cartwright January 11, 2019 Top Down Parsing What is a context-free grammar (CFG)? A recursive definition of a set of strings; it is

### The Earley Parser

The 6.863 Earley Parser 1 Introduction The 6.863 Earley parser will work with any well defined context free grammar, including recursive ones and those containing empty categories. It can use either no

### SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

SCHEME 8 COMPUTER SCIENCE 61A March 2, 2017 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

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

Parsing III (Top-down parsing: recursive descent & LL(1) ) Roadmap (Where are we?) Previously We set out to study parsing Specifying syntax Context-free grammars Ambiguity Top-down parsers Algorithm &

### CSCI 3155: Principles of Programming Languages Exam preparation #1 2007

CSCI 3155: Principles of Programming Languages Exam preparation #1 2007 Exercise 1. Consider the if-then-else construct of Pascal, as in the following example: IF 1 = 2 THEN PRINT X ELSE PRINT Y (a) Assume

### CSE 3302 Programming Languages Lecture 2: Syntax

CSE 3302 Programming Languages Lecture 2: Syntax (based on slides by Chengkai Li) Leonidas Fegaras University of Texas at Arlington CSE 3302 L2 Spring 2011 1 How do we define a PL? Specifying a PL: Syntax:

### Lisp. Versions of LISP

Lisp Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks is based on Common Lisp Scheme is one of the major

### Modern Programming Languages. Lecture LISP Programming Language An Introduction

Modern Programming Languages Lecture 18-21 LISP Programming Language An Introduction 72 Functional Programming Paradigm and LISP Functional programming is a style of programming that emphasizes the evaluation

### Syntax Analysis, III Comp 412

Updated algorithm for removal of indirect left recursion to match EaC3e (3/2018) COMP 412 FALL 2018 Midterm Exam: Thursday October 18, 7PM Herzstein Amphitheater Syntax Analysis, III Comp 412 source code

### Algorithms for NLP. Chart Parsing. Reading: James Allen, Natural Language Understanding. Section 3.4, pp

11-711 Algorithms for NLP Chart Parsing Reading: James Allen, Natural Language Understanding Section 3.4, pp. 53-61 Chart Parsing General Principles: A Bottom-Up parsing method Construct a parse starting

### 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)

### Functional Programming. Big Picture. Design of Programming Languages

Functional Programming Big Picture What we ve learned so far: Imperative Programming Languages Variables, binding, scoping, reference environment, etc What s next: Functional Programming Languages Semantics

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

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division Fall, 2001 Prof. R. Fateman SUGGESTED S CS 164 Final Examination: December 18, 2001, 8-11AM

### Parsing II Top-down parsing. Comp 412

COMP 412 FALL 2018 Parsing II Top-down parsing Comp 412 source code IR Front End Optimizer Back End IR target code Copyright 2018, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled

### Topic 3: Syntax Analysis I

Topic 3: Syntax Analysis I Compiler Design Prof. Hanjun Kim CoreLab (Compiler Research Lab) POSTECH 1 Back-End Front-End The Front End Source Program Lexical Analysis Syntax Analysis Semantic Analysis

### Functional Programming. Pure Functional Languages

Functional Programming Pure functional PLs S-expressions cons, car, cdr Defining functions read-eval-print loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure

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

Derivations vs Parses Grammar is used to derive string or construct parser Context ree Grammars A derivation is a sequence of applications of rules Starting from the start symbol S......... (sentence)

### 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

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

CS1622 Lecture 9 Parsing (4) CS 1622 Lecture 9 1 Today Example of a recursive descent parser Predictive & LL(1) parsers Building parse tables CS 1622 Lecture 9 2 A Recursive Descent Parser. Preliminaries

### Chapter 4. Lexical and Syntax Analysis. Topics. Compilation. Language Implementation. Issues in Lexical and Syntax Analysis.

Topics Chapter 4 Lexical and Syntax Analysis Introduction Lexical Analysis Syntax Analysis Recursive -Descent Parsing Bottom-Up parsing 2 Language Implementation Compilation There are three possible approaches

### Streams and Evalutation Strategies

Data and Program Structure Streams and Evalutation Strategies Lecture V Ahmed Rezine Linköpings Universitet TDDA69, VT 2014 Lecture 2: Class descriptions - message passing ( define ( make-account balance

### Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

Functional Languages CSE 307 Principles of Programming Languages Stony Brook University http://www.cs.stonybrook.edu/~cse307 1 Historical Origins 2 The imperative and functional models grew out of work

### Algorithms for NLP. Chart Parsing. Reading: James Allen, Natural Language Understanding. Section 3.4, pp

-7 Algorithms for NLP Chart Parsing Reading: James Allen, Natural Language Understanding Section 3.4, pp. 53-6 Chart Parsing General Principles: A Bottom-Up parsing method Construct a parse starting from

### Top-Down Parsing and Intro to Bottom-Up Parsing. Lecture 7

Top-Down Parsing and Intro to Bottom-Up Parsing Lecture 7 1 Predictive Parsers Like recursive-descent but parser can predict which production to use Predictive parsers are never wrong Always able to guess

### Introduction to Scheme

How do you describe them Introduction to Scheme Gul Agha CS 421 Fall 2006 A language is described by specifying its syntax and semantics Syntax: The rules for writing programs. We will use Context Free

### UMBC CMSC 331 Final Exam Section 0101 December 17, 2002

0 / 0 1 / 20 UMBC CMSC 331 Final Exam Section 0101 December 17, 2002 Name: Student ID#: 2 / 25 3 / 20 4 / 25 5 / 20 6 /40 7 /40 You will have two hours to complete this closed book exam. We reserve the

### UMBC CMSC 331 Final Exam

UMBC CMSC 331 Final Exam Name: UMBC Username: You have two hours to complete this closed book exam. We reserve the right to assign partial credit, and to deduct points for answers that are needlessly wordy

### Introduction to Artificial Intelligence CISC-481/681 Program 1: Simple Lisp Programs

CISC-481/681 Program 1: Simple Lisp Programs 1 Introduction to Artificial Intelligence CISC-481/681 Program 1: Simple Lisp Programs Reading: Handouts Summarizing Common Lisp Functions Paul Graham s ANSI

### Programming Languages. Thunks, Laziness, Streams, Memoization. Adapted from Dan Grossman s PL class, U. of Washington

Programming Languages Thunks, Laziness, Streams, Memoization Adapted from Dan Grossman s PL class, U. of Washington You ve been lied to Everything that looks like a function call in Racket is not necessarily

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

COMP 412 FALL 2017 Computing Inside The Parser Syntax-Directed Translation Comp 412 source code IR IR target Front End Optimizer Back End code Copyright 2017, Keith D. Cooper & Linda Torczon, all rights

### 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.

### CSE431 Translation of Computer Languages

CSE431 Translation of Computer Languages Top Down Parsers Doug Shook Top Down Parsers Two forms: Recursive Descent Table Also known as LL(k) parsers: Read tokens from Left to right Produces a Leftmost

### SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

SCHEME COMPUTER SCIENCE 6A October 29th, 202 In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs, we will eventually

### Introduction to Lexical Analysis

Introduction to Lexical Analysis Outline Informal sketch of lexical analysis Identifies tokens in input string Issues in lexical analysis Lookahead Ambiguities Specifying lexers Regular expressions Examples

### The role of semantic analysis in a compiler

Semantic Analysis Outline The role of semantic analysis in a compiler A laundry list of tasks Scope Static vs. Dynamic scoping Implementation: symbol tables Types Static analyses that detect type errors

### CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).

CS 2210 Sample Midterm 1. Determine if each of the following claims is true (T) or false (F). F A language consists of a set of strings, its grammar structure, and a set of operations. (Note: a language

### 6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

6.184 Lecture 4 Interpretation Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson 1 Interpretation Parts of an interpreter Arithmetic calculator

### CS 415 Midterm Exam Spring 2002

CS 415 Midterm Exam Spring 2002 Name KEY Email Address Student ID # Pledge: This exam is closed note, closed book. Good Luck! Score Fortran Algol 60 Compilation Names, Bindings, Scope Functional Programming

### CS1 Recitation. Week 2

CS1 Recitation Week 2 Sum of Squares Write a function that takes an integer n n must be at least 0 Function returns the sum of the square of each value between 0 and n, inclusive Code: (define (square

### Grammars & Parsing. Lecture 12 CS 2112 Fall 2018

Grammars & Parsing Lecture 12 CS 2112 Fall 2018 Motivation The cat ate the rat. The cat ate the rat slowly. The small cat ate the big rat slowly. The small cat ate the big rat on the mat slowly. The small

### CSCI337 Organisation of Programming Languages LISP

Organisation of Programming Languages LISP Getting Started Starting Common Lisp \$ clisp i i i i i i i ooooo o ooooooo ooooo ooooo I I I I I I I 8 8 8 8 8 o 8 8 I \ `+' / I 8 8 8 8 8 8 \ `-+-' / 8 8 8 ooooo

### CS 275 Name Final Exam Solutions December 16, 2016

CS 275 Name Final Exam Solutions December 16, 2016 You may assume that atom? is a primitive procedure; you don t need to define it. Other helper functions that aren t a standard part of Scheme you need

### Common LISP-Introduction

Common LISP-Introduction 1. The primary data structure in LISP is called the s-expression (symbolic expression). There are two basic types of s-expressions: atoms and lists. 2. The LISP language is normally

### Question Points Score

CS 453 Introduction to Compilers Midterm Examination Spring 2009 March 12, 2009 75 minutes (maximum) Closed Book You may use one side of one sheet (8.5x11) of paper with any notes you like. This exam has

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

Parsing III (Top-down parsing: recursive descent & LL(1) ) (Bottom-up parsing) CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones Copyright 2003, Keith D. Cooper,

### Parsing Algorithms. Parsing: continued. Top Down Parsing. Predictive Parser. David Notkin Autumn 2008

Parsing: continued David Notkin Autumn 2008 Parsing Algorithms Earley s algorithm (1970) works for all CFGs O(N 3 ) worst case performance O(N 2 ) for unambiguous grammars Based on dynamic programming,

### Normal Order (Lazy) Evaluation SICP. Applicative Order vs. Normal (Lazy) Order. Applicative vs. Normal? How can we implement lazy evaluation?

Normal Order (Lazy) Evaluation Alternative models for computation: Normal (Lazy) Order Evaluation Memoization Streams Applicative Order: evaluate all arguments, then apply operator Normal Order: pass unevaluated

### Common Lisp. Blake McBride

Contents Common Lisp Blake McBride (blake@mcbride.name) 1 Data Types 2 2 Numeric Hierarchy 3 3 Comments 3 4 List Operations 4 5 Evaluation and Quotes 5 6 String Operations 5 7 Predicates 6 8 Math Predicates

### CS 480. Lisp J. Kosecka George Mason University. Lisp Slides

CS 480 Lisp J. Kosecka George Mason University Lisp Slides Symbolic Programming Symbols: +, -, 1, 2 etc. Symbolic expressions: (+ 1 2), (+ (* 3 4) 2) Symbolic programs are programs that manipulate symbolic

### Top down vs. bottom up parsing

Parsing A grammar describes the strings that are syntactically legal A recogniser simply accepts or rejects strings A generator produces sentences in the language described by the grammar A parser constructs

### INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. More Common Lisp

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing More Common Lisp Stephan Oepen & Murhaf Fares Language Technology Group (LTG) September 6, 2017 Agenda 2 Previously Common

### Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Outline Semantic Analysis The role of semantic analysis in a compiler A laundry list of tasks Scope Static vs. Dynamic scoping Implementation: symbol tables Types Static analyses that detect type errors

### Data Types The ML Type System

7 Data Types 7.2.4 The ML Type System The following is an ML version of the tail-recursive Fibonacci function introduced Fibonacci function in ML in Section 6.6.1: EXAMPLE 7.96 1. fun fib (n) = 2. let

### Object-oriented Compiler Construction

1 Object-oriented Compiler Construction Extended Abstract Axel-Tobias Schreiner, Bernd Kühl University of Osnabrück, Germany {axel,bekuehl}@uos.de, http://www.inf.uos.de/talks/hc2 A compiler takes a program

### A Small Interpreted Language

A Small Interpreted Language What would you need to build a small computing language based on mathematical principles? The language should be simple, Turing equivalent (i.e.: it can compute anything that

### Syntactic Analysis. Top-Down Parsing

Syntactic Analysis Top-Down Parsing Copyright 2017, Pedro C. Diniz, all rights reserved. Students enrolled in Compilers class at University of Southern California (USC) have explicit permission to make

### Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs Lexical addressing The difference between a interpreter and a compiler is really two points on a spectrum of possible

### CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages Context Free Grammars and Parsing 1 Recall: Architecture of Compilers, Interpreters Source Parser Static Analyzer Intermediate Representation Front End Back

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

CS 4240: Compilers and Interpreters Project Phase 1: Scanner and Parser Due Date: October 4 th 2015 (11:59 pm) (via T-square) Introduction This semester, through a project split into 3 phases, we are going

### Chapter 4: LR Parsing

Chapter 4: LR Parsing 110 Some definitions Recall For a grammar G, with start symbol S, any string α such that S called a sentential form α is If α Vt, then α is called a sentence in L G Otherwise it is

### Defining Languages GMU

Defining Languages CS463 @ GMU How do we discuss languages? We might focus on these qualities: readability: how well does a language explicitly and clearly describe its purpose? writability: how expressive

### CIS4/681 { Articial Intelligence 2 > (insert-sort '( )) ( ) 2 More Complicated Recursion So far everything we have dened requires

1 A couple of Functions 1 Let's take another example of a simple lisp function { one that does insertion sort. Let us assume that this sort function takes as input a list of numbers and sorts them in ascending

### LR Parsing Techniques

LR Parsing Techniques Introduction Bottom-Up Parsing LR Parsing as Handle Pruning Shift-Reduce Parser LR(k) Parsing Model Parsing Table Construction: SLR, LR, LALR 1 Bottom-UP Parsing A bottom-up parser

### Administrativia. WA1 due on Thu PA2 in a week. Building a Parser III. Slides on the web site. CS164 3:30-5:00 TT 10 Evans.

Administrativia Building a Parser III CS164 3:30-5:00 10 vans WA1 due on hu PA2 in a week Slides on the web site I do my best to have slides ready and posted by the end of the preceding logical day yesterday,

### Notes on Higher Order Programming in Scheme. by Alexander Stepanov

by Alexander Stepanov August 1986 INTRODUCTION Why Scheme? Because it allows us to deal with: 1. Data Abstraction - it allows us to implement ADT (abstact data types) in a very special way. The issue of

### 8 Parsing. Parsing. Top Down Parsing Methods. Parsing complexity. Top down vs. bottom up parsing. Top down vs. bottom up parsing

8 Parsing Parsing A grammar describes syntactically legal strings in a language A recogniser simply accepts or rejects strings A generator produces strings A parser constructs a parse tree for a string

### Public-Service Announcement

Public-Service Announcement Hi CS 61A students! Did you know that the university has a farm? The UC Gill Tract Community Farm is located just two miles from campus and is a community space, open to all.

### FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 4. LISP: PROMĚNNÉ, DALŠÍ VLASTNOSTI FUNKCÍ, BLOKY, MAPOVACÍ FUNKCIONÁLY, ITERAČNÍ CYKLY,

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 4. LISP: PROMĚNNÉ, DALŠÍ VLASTNOSTI FUNKCÍ, BLOKY, MAPOVACÍ FUNKCIONÁLY, ITERAČNÍ CYKLY, 2011 Jan Janoušek MI-FLP Evropský sociální fond Praha & EU: Investujeme do vaší

### Scheme in Scheme: The Metacircular Evaluator Eval and Apply

Scheme in Scheme: The Metacircular Evaluator Eval and Apply CS21b: Structure and Interpretation of Computer Programs Brandeis University Spring Term, 2015 The metacircular evaluator is A rendition of Scheme,