MIT 6.001 Fall, 1997 Instructor: A. Meyer Recitation #7, Notes for Wed., 12/10/97 LAST CLASS. We discussed final exam topics and then reviewed the desired behavior and types of a LEXER taking a string to a list of TOKENS, a PARSER taking a list of tokens to a tree of ATOMS, and a PRINTER taking a tree of atoms to a string. ;;LEXICAL ANALYSIS TOKEN = Atom + {*LPARAN*} + {*RPARAN*} Atom = Symbol + Number + Bool (define *LPARAN* (LIST '*LPARAN*)) (define *RPARAN* (list '*RPARAN*)) Example: (string->tokens "a be c#d () 12 s #t aa 123#") ;Value: (a be c#d (*lparan*) (*rparan*) 12 s #t aa 123#) ;;PARSING ;PARSE-TOKENS: List(Token) --> Tree(Atom) EXAMPLES: (parse-tokens (list *LPARAN* *LPARAN* 1 'symb *RPARAN* '+ *RPARAN*)) ;Value: ((1 symb) +) (parse-tokens (string->tokens "(+ (- 2x 3yy#t) (if #t 1 0))")) ;Value: (+ (- 2x 3yy#t) (if #t 1 0)) ;;PRINTING ;TREE->STRING: Tree(Atom) --> String ;CONTRACT: ;(equal? TREE (parse-tokens (string->tokens (tree->string TREE)))) ==> #t ;(tree->string '(+ a#2 3 (* 12 x))) ;;Value: "(+ a#2 3 (* 12 x))" We did not develop code for these since this is not required material. If you're interested in seeing and/or running these procedures, look at the file READ-PRINT.SCM in the section directory.