Abstract Syntax Prabhaker Mateti 1.

Slides:



Advertisements
Similar presentations
Plt /7/ Data Abstraction Programming Language Essentials 2nd edition Chapter 2.2 An Abstraction for Inductive Data Types.
Advertisements

Cs784(Prasad)L10Rec1 Implementing Recursion. cs784(Prasad)L10Rec2 let insufficient for recursion ( (let((fact(lambda (n) (if (zero? n) 1 (* n (fact (-
Semantics of PLs via Interpreters: Getting Started CS784: Programming Languages Prabhaker Mateti.
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Attribute Grammars Prabhaker Mateti ACK: Assembled from many sources.
Assignments and Procs w/Params EOPL3 Chapter 4. Expressible vs. Denotable values Expressible Values –the language can express and compute these –represented.
Recap 1.Programmer enters expression 2.ML checks if expression is “well-typed” Using a precise set of rules, ML tries to find a unique type for the expression.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Cs784(TK)1 Semantics of Procedures and Scopes. Kinds of Scope Static or Lexical scope –determined by structure of program –Scheme, C++, Java, and many.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
SICP Interpretation part 1 Parts of an interpreter Arithmetic calculator Names Conditionals and if Store procedures in the environment.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
EOPL3: Section 3.3 PROC and App B: SLLGEN
Environment: Data type associating symbols with values { a: 4s:'foof: x.x } Interface: (empty-env) = [ Ø ] (apply-env [f] s ) = f(s) (extend-env '( s 1...
Semantic Analysis (Generating An AST) CS 471 September 26, 2007.
Cs784(tk)1 Implementing Recursion. Recap: Goals Experimenting with PL design alternatives Scoping, parameter passing, arrays,... Runnable prototype of.
Cs7100(Prasad)L8Proc1 Procedures. cs7100(Prasad)L8Proc2 Primitive procedures  etc User-defined procedures –Naming a sequence of operations.
Plt /12/ Data Abstraction Programming Language Essentials 2nd edition Chapter 2.3 Representation Strategies for Data Types.
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
CPS 506 Comparative Programming Languages Syntax Specification.
Scheme Profs Tim Sheard and Andrew Black CS 311 Computational Structures.
Comp 311 Principles of Programming Languages Lecture 4 The Scope of Variables Corky Cartwright September 3, 2008.
1 Programming Languages (CS 550) Lecture 2 Summary Mini Language Interpreter Jeremy R. Johnson.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
1 Vectors, binary search, and sorting. 2 We know about lists O(n) time to get the n-th item. Consecutive cons cell are not necessarily consecutive in.
Cs784 (Prasad)L6AST1 Abstract Syntax. cs784 (Prasad)L6AST2 Language of -expressions ::= | (lambda ( ) ) | ( ) E.g., concrete syntax Scheme S-expressions.
Operational Semantics of Scheme
Abstract Syntax cs7100 (Prasad) L7AST.
Edited by Original material by Eric Grimson
6.001 SICP Variations on a Scheme
Introduction to Scheme
Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications) cs7100(Prasad) L8Interp.
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Corky Cartwright January 18, 2017
Implementing Recursion
Env. Model Implementation
Original material by Eric Grimson
5.4.1: Implementation Method Invocation
2.3 Representation Strategies for Data Types
Semantics of PLs via Interpreters: Getting Started
Mini Language Interpreter Programming Languages (CS 550)
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
Programming Languages (CS 550) Mini Language Semantics
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
Procedures App B: SLLGEN 1.
CS 36 – Chapter 11 Functional programming Features Practice
3.7 Variable Assignment Recall instance variables in Python:
3.4 Local Binding Recall Scheme's let: > (let ((x 5)‏ (y 6))
The Metacircular Evaluator (Continued)
Lecture 26: The Metacircular Evaluator Eval Apply
6.001 SICP Further Variations on a Scheme
Chapter 1 Review: BNF Grammar for lists:
Streams, Delayed Evaluation and a Normal Order Interpreter
Lecture #9 מבוא מורחב.
Abstract Syntax cs7100 (Prasad) L7AST.
2.2.2 Abstract Syntax Recall BNF definition of l-calculus expressions:
3.6 Interpreter: Recursion
6.001 SICP Variations on a Scheme
2.2.2 Abstract Syntax Recall BNF definition of l-calculus expressions:
Chapter 3: Environment-Passing Interpreters
6.001 SICP Interpretation Parts of an interpreter
Assignments and Procs w/Params
topics interpreters meta-linguistic abstraction eval and apply
Recursive Procedures and Scopes
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

Abstract Syntax Prabhaker Mateti 1

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 CS784(PM) 2 2

Overview Concrete Syntax Abstract Syntax Interpreter Results Parse-expression Concrete Syntax Abstract Syntax Unparse-expression Interpreter Potentially one can have several concrete syntax. Specifically, the text uses two concrete syntaxes in examples: Scheme Symbolic Expressions Scheme Character Strings Results CS784(PM) 3 3

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?) abc*+ (or is this it?) CS784(PM) 4 4

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

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 CS784(PM) 6 6

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

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

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

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. CS784(PM) 10 10

Syntax Driven Representation (define-datatype expression expression? (var-exp (id symbol?)) (lambda-exp (id symbol?) (body expression?)) (app-exp (rator expression?) (rand expression?))) Resembles signature declarations var-exp : symbol? -> expression? lambda-exp : symbol? x expression? -> expression? app-exp : expression? x expression? -> expression? (caadr X) = (car (car (cdr X))) caadr = first of second of caddr = third CS784(PM) 11 11

Figure 2.2: (lambda (x) (f (f x))) CS784(PM) 12 12

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)) ))) CS784(PM) 13 13

DrRacket CS784(PM) 14 14

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)) )))) CS784(PM) 15 15

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. Example: Natural number Constructors zero, succ Operations add,mul Properties identity, commutativity Chapter 1 + Scoping CS784(PM) 16 16

The Environment Environment: Constructors Observer table of variable-value pairs Chronologically later var-value pair overrides Constructors empty-env extend-env Observer apply-env Constructors (empty-env, extend-env)+ Lookup (apply-env) together capture table behavior. They apportion the overall workload differently in the different implementations. Extend-env introduces a collection of bindings simultaneously. CS784(PM) 17 17

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

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 CS784(PM) 19 19

Representing Environment Many representations are possible Speedy access Memory frugal Change in the interface: Syms x Vals (define extend-env (lambda (syms vals env) … )) Constructors (empty-env, extend-env)+ Lookup (apply-env) together capture table behavior. They apportion the overall workload differently in the different implementations. Extend-env introduces a collection of bindings simultaneously. CS784(PM) 20 20

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

Alt-1: List of Ribs (Ribcage) (define empty-env (lambda () '())) (define extend-env (lambda (syms vals env) (cons (list syms vals) 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 (cdr env))) (let ((pos (rib-find-position sym syms))) (if (number? pos) (list-ref vals pos) (apply-env env sym))))))) Ribcage = list of list of pairs impl. CS784(PM) 22 22

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) (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) )) Env value – unary function – (lambda (sym) …) CS784(PM) 23 23

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 (syms vals env) (extended-env-record syms vals env))) CS784(PM) 24 24

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

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)) ((reset Q)) Queue represented as a pair of lists with the second list in reverse order to enable constant time access to both ends in practice. When the first list is empty, the second list is reversed --- “inefficient” dequeue. (Cf. CS776 Queue) (Data invariant – normal form) Assignment operation in Scheme + encapsulation (static scoping) + (infinite lifetime). CS784(PM) 26 26

(define create-queue (lambda () (let ((q-in '()) (q-out '())) (letrec ((reset-queue (set! q-in '()) (set! q-out '())) ) (empty-queue? (and (null? q-in) (null? q-out))) ) (enqueue (lambda (x) (set! q-in (cons x q-in))) ) (dequeue (if (empty-queue?) (eopl:error 'dequeue "Not on an empty queue") (begin (if (null? q-out) (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)) ))) Queue represented as a pair of list with the second list in reverse order to enable constant time access to both ends in practice. When the first list is empty, the second list is reversed --- “inefficient” dequeue. (Cf. CS776 Queue) (Data invariant – normal form) Assignment operation in Scheme + encapsulation (static scoping) + (infinite lifetime). CS784(PM) 27 27