section07

# section07 - Section Notes 7 CS51—Spring 2009 Week of 1...

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

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Section Notes 7 CS51—Spring 2009 Week of March 30, 2009 1 Outline 1. Dijkstra’s Algorithm review/implementation discussion 2. Parsing Combinators review/discussion 3. Midterm return/common mistakes At the end of this section, you should be able to do the following: 1. Be able to understand/modify a BNF grammar 2. Understand how to use parsing combinators 3. Appreciate how combinators generate code that we’d otherwise have to write manually. 4. Understand how to implement Dijkstra’s algorithm in Scheme 2 Parsing Combinators For PS6, you’ll need to understand how the parsers and parser combinators defined in lecture work, so we decided that the most useful thing to do in section is to go over the ones we gave you. First, here’s the example grammar from before break, just so we have some context: 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 And here are the contents of the parse.ss file: ; The main idea: a parser is a function that takes a list of chars, ; and tries to match some of them. If it fails, it returns a fail ; struct (make-fail); if it succeeds, it returns an ans struct, with ; the first field being a result of the parse (a number, string, ; etc...), and the second being the unconsumed characters from the 1 ; list. (define-struct ans (result unconsumed)) (define-struct fail ()) ; Try to parse string s with parser p. The parser must successfully ; consume all of s. (define (parse p s) (let ([a (p (string->list s))]) (if (or (fail? a) (not (empty? (ans-unconsumed a)))) (error ’match "parse failed") ; Since the parser matched the whole string, can just return ; the result of the parse (ans-result a)))) ; The parser that always succeeds without consuming any characters. ; Note that the result of the answer is the empty list. (This is ; useful in defining cats below) (define always (lambda (cs) (make-ans empty cs))) ; The parser that always fails (define never (lambda (cs) (make-fail))) ; (failwhen f p) is a parser that behaves the same as the parser p, ; except fails whenever p’s result doesn’t satisfy the predicate f. ; For example, we can pass a predicate like ; ; (lambda (x) (not (or (equal? x "name") (equal? x "age")))) ; ; and a general alphabetic string parser to reject any parses ; that don’t return "name" or "age". ; (Note that our parsers are greedy by default, so if we used ; such a combinator to parse "ageforty-two", it would read ; "ageforty", conclude that it was neither "name" nor "age", ; and therefore fail.) (define (failwhen f p) (lambda (cs) (let ([a (p cs)]) (if (or (fail? a) (f (ans-result a))) (make-fail) a)))) ;(char c) is a parser that parses the single character c, returning c ;as the result....
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 / 10

section07 - Section Notes 7 CS51—Spring 2009 Week of 1...

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

View Full Document
Ask a homework question - tutors are online