270 likes | 545 Views
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.
E N D
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 Parse-expression Concrete Syntax Abstract Syntax Unparse-expression Interpreter Results
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?)
Parse tree vs. AST expr + expr expr expr + expr expr expr 1 + ( 2 ) + ( 3 ) 1 2 3
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
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
λ-expressions • <exp> ::= • <identifier> • | (lambda (<identifier> ) <exp>) • | (<exp> <exp>) • Compare with Scheme S-expressions. • EOPL3 p52: Lc-exp
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
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.
Syntax Driven Representation (define-datatype expression expression? (var-exp (id symbol?)) (lambda-exp (id symbol?) (body expression?)) (app-exp (rator expression?) (rand expression?)))
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)) )))
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)) ))))
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.
The Environment • Environment: • table of variable-value pairs • Chronologically later var-value pair overrides • Constructors • empty-env • extend-env • Observer • apply-env
The Environment Spec • (empty-env) = ∅ • (apply-envfvar) = f (var) • (extend-envvar v f] ) = g, • where g(var1 ) = v, if var1 = var = f (var1). otherwise • from EOPL3 p36
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
Representing Environment • Many representations are possible • Speedy access • Memory frugal • Change in the interface: Syms x Vals (define extend-env (lambda (symsvalsenv) … ))
Alt-1: env is a List of Ribs • left rib: list of variables • right rib: corresponding list of values. • Exercise 2.11 EOPL3 (Ribcage)
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)))))))
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) ))
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)))
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)))))))
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))
(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)) • )))