{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture12 - Today COMPUTER SCIENCE 51 Spring 2009...

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

View Full Document Right Arrow Icon
3/10/2009 1 COMPUTER SCIENCE 51 Spring 2009 cs51.seas.harvard.edu Prof. Greg Morrisett Prof. Ramin Zabih Today Check your email: do the survey!!! Parsing how to describe syntax (BNF) how to implement a recognizer (later, a parser ) from the syntax description. using lambda to generate code for you. Some Motivation Consider a service like Google search: www.google.com/search?q=Morrisett In essence, calling a search procedure with a parameter of “Morrisett”. www.google.com/search?q=Morrisett+Zabih Now searching for pages with “Morrisett” and “Zabih”. More Google Image search: www.google.com/images?q=Greg Return first 100 links instead of default: www.google.com/search?q=Greg&num=100 Where the pages are in Chinese: www.google.com/search?q=Greg&lr=lang_z h-CN There are Many Options... Search Terms conjunction: ?q=Greg+Morrisett disjunction: &oq=Amy+John unwanted terms: &eq=Tanya Results per page: &num=100 Page Language: &lr=eng File Type: &filetype=pdf Restrict search to domain: &site=harvard.edu ... Order doesn’t matter. What Has to Happen? Google gets URL as a request: search?q=Zabih&num=100 It must turn this string of characters into procedure calls to its search engine. This is proprietary to Google But for many web services, the URL gets converted into a SQL query for a database. e.g., “SELECT * FROM table WHERE ...” perform post-processing (e.g., get only 100) print the results as HTML
Background image of page 1

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

View Full Document Right Arrow Icon
3/10/2009 2 Imagine... You have a “database” of papers. title, authors, journal, date, etc. perhaps represented as a list of structs. You have written a set of procedures: add entries to the list sort the list by some field filter the list by some predicate And now you want to turn this into a web service like Google’s search. accept URL’s as commands perform commands specified by the URL print any results as HTML Recognition & Parsing Taking a string of characters like: search?author=Morrisett or add?author=Zabih&year=2008& title=Kittens&journal=J.of.Cute recognize: checking that the string is legal i.e., it is a proper command parse: figuring out what command the string represents i.e., call filter and sort with appropriate arguments i.e., create new struct, and add it to the list First Task: Recognition We can describe the syntax of commands using something like Backus-Naur Form (BNF): com ::= ? add fieldarg+ | ? search fieldarg+ sortarg? fieldarg ::= & field = id+ | & year = number sortarg ::= & sort = field | & sort = year field ::= author | title | journal id ::= [ a - zA - Z ]+ number := [ 1 - 9 ][ 0 - 9 ]* Some abbreviations: * is zero or more, + is one or more, ? is 0 or 1, [0-9] = 0 | 1 | 2 | 3 | ... | 8 | 9 How Not to Write a Parser ;; com ::= ? add fieldarg+ | ? search fieldarg+ sortarg? (define (check_com cs) (if (or (empty? cs) (not (equal? (head cs) #\?))) (error ...) (cond [(empty (cdr cs)) (error ...)] [(equal? (cadr cs) #\a) (cond [(empty? (cddr cs)) (error ...)] [(equal? (caddr cs) #\d) ...] [else (error ...)]] [(equal? (cadr cs) #\s) (cond [(empty? (cddr cs)) (error ...)] [(equal? (caddr s) #\e) ...] [else (error ...)]])) Instead...
Background 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 ]}