1 / 7

2.2.2 Abstract Syntax

2.2.2 Abstract Syntax. Recall BNF definition of l -calculus expressions: <expression> ::= <identifier> ::= (lambda ( <identifier> ) <expression> ) ::= ( <expression> <expression> ) Uses concrete syntax: includes parens, and keywords like lambda

Télécharger la présentation

2.2.2 Abstract Syntax

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 2.2.2 Abstract Syntax • Recall BNF definition of l-calculus expressions: • <expression> ::= <identifier> • ::= (lambda (<identifier>) <expression> ) • ::= (<expression> <expression>) • Uses concrete syntax: includes parens, and keywords like lambda • We can use this to define an abstract datatype without these redundant items:

  2. (define-datatype expression expression? (var-exp ::= <identifier> (id symbol?)) (lambda-exp ::= (lambda (<identifier>) <expression> (id symbol?) (body expression?)) (app-exp ::= (<expression> <expression>) (rator expression?) (rand expression?)))

  3. We can use the abstract data type together with the BNF definition, to make an abstract syntax tree • First, we annotate the BNF with tree notation: <expression> ::= <identifier> var-exp (id) ::= (lambda (<identifier>) <expression> lambda-exp (id body) ::= (<expression> <expression>) app-exp (rator rand)

  4. Example: (lambda (x) (f (f x))) lambda-exp id body app-exp x rator rand app-exp var-exp id rator rand f var-exp var-exp id id f x

  5. We can use the defined datatype to simplify functions: (define occurs-free? (lambda (var exp) (cases expression exp (var-exp (id) (eqv? id var)) (lambda-exp (id body) (and (not (eqv? id var) (occurs-free? var body))) (app-exp (rator rand) (or (occurs-free? var rator) (occurs-free? var rand))))))

  6. Example: > (define e (lambda-exp 'a (var-exp 'b))) > (expression? e) #t > (occurs-free? 'a e) #f > (occurs-free? 'b e) #t But this is awkward. We want to parse actual expressions into the data structure....

  7. (define parse-expression (lambda (e) (cond ((symbol? e) (var-exp e)) ((pair? e) (if (eqv? (car e) 'lambda) (lambda-exp (caadr e) (parse-expression (caddr e))) (app-exp (parse-expression (car e)) (parse-expression (cadr e))))) (else (eopl:error 'parse-expression "Syntax error: ~s" e))))) > (parse-expression '(lambda(a) (a b))) #(struct:lambda-exp a #(struct:app-exp #(struct:var-exp a) #(struct:var-exp b)))

More Related