1 / 27

Abstract Syntax

Abstract Syntax. Different Levels of Syntax. Lexical syntax Basic symbols (names, values, operators, etc.) Concrete syntax Rules for writing expressions, statements, programs Input to compilers/interpreters Abstract syntax “Internal” representation Captures semantics. Overview.

madge
Download Presentation

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. Abstract Syntax

  2. Different Levels of Syntax • Lexical syntax • Basic symbols (names, values, operators, etc.) • Concrete syntax • Rules for writing expressions, statements, programs • Input to compilers/interpreters • Abstract syntax • “Internal” representation • Captures semantics

  3. Overview Parse-expression Concrete Syntax Abstract Syntax Unparse-expression Interpreter Results

  4. Concrete vs. Abstract Syntax • Expressions with common meaning (should) have the same abstract syntax. • C: a+b*c • Assumes certain operator precedence (why?) • Forth: bc*a+ (reverse Polish) This expression tree represents the meaning of expression Not the same as parse tree (why?) Does the value depend on traversal order?abc*+ (or is this it?)

  5. Parse tree vs. AST expr + expr expr expr + expr expr expr 1 + ( 2 ) + ( 3 ) 1 2 3

  6. Abstract Syntax Tree • More useful representation of syntax tree • Less clutter • Actual level of detail depends on your design • Basis for semantic analysis • Later annotated with various information • Type information • Computed values

  7. Compilation in a Nutshell Source code (character stream) if (b == 0) a = b; Lexical analysis if ( b == 0 ) a = b ; Token stream Parsing if ; == = Abstract syntax tree (AST) b 0 a b Semantic Analysis if boolean int == = ; Decorated AST int b int 0 int a lvalue intb

  8. λ-expressions • <exp> ::= • <identifier> • | (lambda (<identifier> ) <exp>) • | (<exp> <exp>) • Compare with Scheme S-expressions. • EOPL3 p52: Lc-exp

  9. Lc-exp ::= • Identifier • var-exp (var) • (lambda (Identifier) Lc-exp) • lambda-exp (bound-var body) • (Lc-exp Lc-exp) • app-exp (rator rand) • Abstract syntax

  10. EOPL3 Scheme: define-datatype • Syntax definition: (define-datatype type-name type-predicate-name {(variant-name {(field-name predicate)}* )} + ) • An Example: (define-datatype environment environment? (empty-env-record) (extended-env-record (syms (list-of symbol?)) (vals (list-of scheme-value?)) (env environment?))) • Data types built by define-datatype may be mutually recursive.

  11. Syntax Driven Representation (define-datatype expression expression? (var-exp (id symbol?)) (lambda-exp (id symbol?) (body expression?)) (app-exp (rator expression?) (rand expression?)))

  12. Figure 2.2: (lambda (x) (f (f x)))

  13. Concrete to Abstract Syntax (define parse-expression (lambda (datum) (cond ((symbol? datum) (var-exp datum)) ((pair? datum) (if (eqv? (car datum) 'lambda) (lambda-exp (caadr datum) (parse-expression (caddr datum))) (app-exp (parse-expression (car datum)) (parse-expression (cadr datum)))) ) (else (eopl:error 'parse-expression "Invalid concrete syntax ~s" datum)) )))

  14. DrRacket

  15. Unparse: Abstract to Concrete Syntax (define unparse-expression (lambda (exp) (cases expression exp (var-exp (id) id) (lambda-exp (id body) (list 'lambda (list id) (unparse-expression body)) ) (app-exp (rator rand) (list (unparse-expression rator) (unparse-expression rand)) ))))

  16. Role of Induction and Recursion • Define data structures (infinite values) by induction. • Seed elements. • Closure operations. • Define functions (operations) by recursion. • Boundary/Basis case. • Composite/Recursive case. • Prove properties using structural induction. • Basis case. • Inductive step.

  17. The Environment • Environment: • table of variable-value pairs • Chronologically later var-value pair overrides • Constructors • empty-env • extend-env • Observer • apply-env

  18. The Environment Spec • (empty-env) = ∅ • (apply-envfvar) = f (var) • (extend-envvar v  f] ) = g, • where g(var1 ) = v, if var1 = var = f (var1). otherwise • from EOPL3 p36

  19. An Example Env e • (define e (extend-env ’d 6 (extend-env ’y 8 (extend-env ’x 7 (extend-env ’y 14 (empty-env)))))) • e(d)=6, e(x)=7, e(y)=8

  20. Representing Environment • Many representations are possible • Speedy access • Memory frugal • Change in the interface: Syms x Vals (define extend-env (lambda (symsvalsenv) … ))

  21. Alt-1: env is a List of Ribs • left rib: list of variables • right rib: corresponding list of values. • Exercise 2.11 EOPL3 (Ribcage)

  22. Alt-1: List of Ribs (Ribcage) (define empty-env (lambda () '())) (define extend-env (lambda (symsvalsenv) (cons (list symsvals) env) )) (define apply-env (lambda (env sym) (if (null? env) (eopl:error 'apply-env "No binding for ~s" sym) (let ((syms (car (car env))) (vals (cadr (car env))) (env (cdrenv))) (let ((pos (rib-find-position sym syms))) (if (number? pos) (list-ref vals pos) (apply-envenv sym)))))))

  23. Alt-2: env is a Unary Function (define empty-env (lambda () (lambda (sym) (eopl:error 'apply-env "No binding for ~s" sym)) )) (define extend-env (lambda (syms vals env) (lambda (sym) (let ((pos (list-find-position sym syms))) (if (number? pos) (list-ref vals pos) (apply-env env sym)))) )) (define apply-env (lambda (env sym) (env sym) ))

  24. Alt-3: Tagged Records (define-datatype environment environment? (empty-env-record) (extended-env-record (syms (list-of symbol?)) (vals (list-of scheme-value?)) (env environment?))) (define scheme-value? (lambda (v) #t)) (define empty-env (lambda () (empty-env-record) )) (define extend-env (lambda (symsvalsenv) (extended-env-record symsvalsenv)))

  25. Alt-3: Tagged records (define apply-env (lambda (env sym) (cases environment env (empty-env-record () (eopl:error 'apply-env "No binding for ~s" sym)) (extended-env-record (symsvalsenv) (let ((pos (list-find-position sym syms))) (if (number? pos) (list-ref vals pos) (apply-envenv sym)))))))

  26. Queue (define reset (lambda (q) (vector-ref q 0))) (define empty? (lambda (q) (vector-ref q 1))) (define enqueue (lambda (q) (vector-ref q 2))) (define dequeue (lambda (q) (vector-ref q 3))) (define Q (create-queue)) ((enqueue Q) 55) ((empty? Q)) ((dequeue Q)) ((empty? Q)) ((reset Q)) ((dequeue Q))

  27. (define create-queue • (lambda () • (let ((q-in '()) (q-out '())) • (letrec • ((reset-queue • (lambda () • (set! q-in '()) (set! q-out '())) ) • (empty-queue? • (lambda () • (and (null? q-in) (null? q-out))) ) • (enqueue • (lambda (x) • (set! q-in (cons x q-in))) ) • (dequeue • (lambda () • (if (empty-queue?) • (eopl:error 'dequeue "Not on an empty queue") • (begin • (if (null? q-out) • (begin • (set! q-out (reverse q-in)) (set! q-in '()))) • (let ((ans (car q-out))) • (set! q-out (cdr q-out)) • ans))))) ) • (vector reset-queue empty-queue? enqueue dequeue)) • )))

More Related