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
Unformatted text preview: Section Notes 7 CS51Spring 2009 Week of March 30, 2009 1 Outline 1. Dijkstras 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 wed otherwise have to write manually. 4. Understand how to implement Dijkstras algorithm in Scheme 2 Parsing Combinators For PS6, youll 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, heres 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 ps result doesnt 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 dont 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
- Spring '09