section06 - Section Notes 6 CS51—Spring 2009 Week of...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Section Notes 6 CS51—Spring 2009 Week of March 9, 2009 Outline 1. Combinators and Parsing 2. Dijkstra’s Algorithm 3. Impeccable Style 1 Goals for today: At the end of today’s section, you should be able to do the following: 1. Combinate. Yep, it’s a verb 1 . 2. Find the cheapest way to get to Miami for Spring break 3. Eliminate those pesky unnecessary lambdas from your programs. 2 Combinators and Parsing We’ve already encountered the parsing problem in project 1: how to understand a string in terms of the grammar that generated it. Furthermore, in lecture, we saw a surprisingly elegant way to do this using higher-order parsing functions, or parser combinators . In this example, we’ll be applying those ideas to write a recognizer for valid arithmetic expressions. 2 Our recognizer will take expressions, like (4+1)*9-3 or 4++*/193 , and tell us whether they are valid arithmetic expressions (true in the first case, false in the second). 3 Before writing this recognizer, though, let’s write out a BNF 4 grammar for our language of valid arithmetic expressions. We have to be somewhat careful in doing this. In particular, consider the following grammar. exp ::= exp + exp | exp - exp | exp * exp | exp / exp | (exp) | num num ::= digit | digit num digit ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 1 In Italian. 2 Later, we’ll see how to turn this into something more useful, like a calculator for said expressions. 3 And the expressions will be infix, rather than fully-parenthesized-prefix. Truly, this is the best of all possible worlds! 4 Backus-Naur Form. 1 There are a few problems with this grammar. One problem is intrinsic to the grammar, and involves ambiguity . Can you come up with a string that can be parsed two or more ways according to this grammar? Two other problems pertain to our usual style of implementation as applied to this grammar, and have to do with left-recursion 5 and search order , respectively. Can you identify them? Which of those three issues are solved by the following grammar? exp ::= num op exp | num | (exp) op ::= + | - | * | / num ::= digit num | digit digit ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 And the following grammar? exp ::= term + term | term - term | term term ::= factor * factor | factor / factor | factor factor ::= num | (exp) num ::= digit num | digit digit ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 For simplicity, we’ll use the second grammar above, though we’ll want to use the third later, in lecture (when we set out to parse rather than just recognize ). Now, how do we go about writing our recognizer? Recall from lecture that a pattern p is a function taking a list of characters and returning either a result or some non-list failure value (hackily taken to be 0), where a result is the remainder of the list after the pattern has matched as many of the initial characters as possible....
View Full Document

This note was uploaded on 07/26/2009 for the course COMPUTERSC CS51 taught by Professor Gregmorrisett during the Spring '09 term at Harvard.

Page1 / 9

section06 - Section Notes 6 CS51—Spring 2009 Week of...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online