2.2.2 Abstract Syntax - PowerPoint PPT Presentation

1 / 7
About This Presentation
Title:

2.2.2 Abstract Syntax

Description:

2.2.2 Abstract Syntax Recall BNF definition of l-calculus expressions: ::= ::= (lambda () )::= ( – PowerPoint PPT presentation

Number of Views:89
Avg rating:3.0/5.0
Slides: 8
Provided by: wlu81
Learn more at: http://home.wlu.edu
Category:
Tags: abstract | syntax

less

Transcript and Presenter's Notes

Title: 2.2.2 Abstract Syntax


1
2.2.2 Abstract Syntax
  • Recall BNF definition of l-calculus
    expressions
  • ltexpressiongt ltidentifiergt
  • (lambda (ltidentifiergt) ltexpressiongt )
  • (ltexpressiongt ltexpressiongt)
  • 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 ltidentifiergt (id
symbol?)) (lambda-exp (lambda
(ltidentifiergt) ltexpressiongt (id
symbol?) (body expression?))
(app-exp (ltexpressiongt ltexpressiongt)
(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

ltexpressiongt ltidentifiergt
var-exp (id) (lambda
(ltidentifiergt) ltexpressiongt lambda-exp (id
body) (ltexpressiongt ltexpressiongt) a
pp-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
gt (define e (lambda-exp 'a (var-exp 'b))) gt
(expression? e) t gt (occurs-free? 'a e) f gt
(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
(eoplerror 'parse-expression
"Syntax error s" e)))))
gt (parse-expression '(lambda(a) (a
b))) (structlambda-exp a (structapp-exp
(structvar-exp a) (structvar-exp b)))
Write a Comment
User Comments (0)
About PowerShow.com