1 / 7

Chapter 1 Review: BNF

Chapter 1 Review: BNF. Grammar for lists: <list> ::= ({<datum>}*) <dotted-datum> ::= ({<datum>}+ . <datum>) <vector> ::= #({<datum>}*) <datum> ::= <number> | <symbol> | <boolean> | <string> ::= <list> | <dotted-datum> | <vector>

cara
Télécharger la présentation

Chapter 1 Review: BNF

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. Chapter 1 Review: BNF Grammar for lists: <list> ::= ({<datum>}*) <dotted-datum> ::= ({<datum>}+ . <datum>) <vector> ::= #({<datum>}*) <datum> ::= <number> | <symbol> | <boolean> | <string> ::= <list> | <dotted-datum> | <vector> Let's derive (a “mixed” # (bag (of . data))) from this grammar....

  2. Chapter 1 Review: Free and Bound Variables A variable xoccurs free in a l-calculus expression E iff: 1. E is a variable reference and E = x; or 2. E is of the form (l (y) E') where y≠ x and x occurs free in E'; or 3. E is of the form (E1E2) and x occurs free in E1 or E2 Let's prove that b occurs free in (l (a) (a b))

  3. Chapter 1 Review: Free and Bound Variables A variable xoccurs bound in a l-calculus expression E iff: 1. E is of the form (l (y) E') where x occurs bound in E' or x = y and y occurs free in E'; or 2. E is of the form (E1E2) and x occurs bound in E1 or E2 Let's prove that a occurs bound in (l (a) (a b))

  4. Coding It Up: Follow the Definition (define occurs-free? (lambda (var exp) (cond 1. E is a variable reference and E = x; or ((symbol? exp) (eqv? var exp)) 2. E is of the form (l (y) E') where y≠ x and x occurs free in E'; or ((eqv? (car exp) 'lambda) (and (not (eqv? (caadr exp) var)) (occurs-free? var (caddr exp)))) 3. E is of the form (E1E2) and x occurs free in E1 or E2 ((else (or (occurs-free? var (car exp)) (occurs-free? var (cadr exp)))))))

  5. (define occurs-bound? (lambda (var exp) (cond 0. symbol by itself isn't bound ((symbol? exp) #f) 1. E is of the form (l (y) E') where x occurs bound in E' ((eqv? (car exp) 'lambda) (or (occurs-bound? var (caddr exp)) or x = y and y occurs free in E'; or (and (eqv? (caadr exp) var) (occurs-free? var (caddr exp))))) 2. E is of the form (E1E2) and x occurs bound in E1 or E2 (else (or (occurs-bound? var (car exp)) (occurs-bound? var (cadr exp)))))))

  6. Use helper functions! (define (lambda-exp? exp) (eqv? (car exp?) 'lambda)) (define (lambda-arg exp) (caadr exp)) (define (lambda-body exp) (caddr exp)) (define (app-fun exp) (car exp)) (define (app-arg exp) (cadr exp))

  7. (define occurs-free? (lambda (var exp) (cond 1. E is a variable reference and E = x; or ((symbol? exp) (eqv? var exp)) 2. E is of the form (l (y) E') where y≠ x and x occurs free in E'; or ((lambda-exp? exp) (and (not (eqv? (lambda-arg exp) var) (occurs-free? var (lambda-body exp)))) 3. E is of the form (E1E2) and x occurs free in E1 or E2 ((else (or (occurs-free? var (app-fun exp)) (occurs-free? var (app-arg exp)))))))

More Related