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

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

View Full Document Right Arrow Icon
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
Image of page 1

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

View Full Document Right Arrow Icon
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. 6 As a warmup, let’s write the never and always patterns: the former always fails to match, while the latter just matches the empty string (so will always succeed, even if the character list being matched against is empty).
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern