2.2.2 Abstract Syntax Recall BNF definition of l-calculus expressions:

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 (-
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
1 The metacircular evaluator Names Extend the calculator to store intermediate results as named values (define x (+ 4 5)) store result as x (+ x.
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.
1 Lecture 18 Continue Evaluator. 2 z9 true#t + twice Representing procedures (eval '(define twice (lambda (x) (+ x x))) GE) symbol primitive scheme procedure.
1 Scheme Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition In Scheme, a function is defined.
מבוא מורחב - שיעור 10 1 Symbols Manipulating lists and trees of symbols: symbolic differentiation Lecture 10.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
SICP Variations on a Scheme Scheme Evaluator – A Grand Tour Techniques for language design: Interpretation: eval/apply Semantics vs. syntax Syntactic.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
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...
Cs784(tk)1 Implementing Recursion. Recap: Goals Experimenting with PL design alternatives Scoping, parameter passing, arrays,... Runnable prototype of.
1 The metacircular evaluator (Cont.) Defining new procedures (define (lambda? e) (tag-check e 'lambda)) (define (eval exp env) (cond ((number? exp)
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.
3.5 Procedures Recall procedures (functions) in Scheme: (let ((f (lambda(y z) (+ y (- z 5))) (f 2 28)) We would like something similar in our toy language:
Chapter 1: Inductive Sets of Data Recall definition of list  '() is a list.  If l is a list and a is any object, then (cons a l ) is a list More generally:
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
18-October-2002cse Symbols © 2002 University of Washington1 Symbols CSE 413, Autumn 2002 Programming Languages
מבוא מורחב 1 Lecture #9. מבוא מורחב 2 Symbol: a primitive type constructors: (quote alpha) ==> quote is a special form. One argument: a name. selectors.
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 6 한 태숙.
Comp 311 Principles of Programming Languages Lecture 4 The Scope of Variables Corky Cartwright September 3, 2008.
Inductively Defined Data Concrete and Abstract syntax Karl Lieberherr.
3.3 Conditional Evaluation --> if 1 then 2 else 3 2 Want to support if/then/else We'll represent false as 0, true as 1: Syntax (BNF): ::= if then else.
7.5 – Radical Expressions. Radical Radical – radical symbol.
Comp 311 Principles of Programming Languages Lecture 2 Syntax Corky Cartwright August 26, 2009.
Change to scientific notation: A. B. C. 289,800, x x x
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2.
Cs784 (Prasad)L6AST1 Abstract Syntax. cs784 (Prasad)L6AST2 Language of -expressions ::= | (lambda ( ) ) | ( ) E.g., concrete syntax Scheme S-expressions.
Abstract Syntax cs7100 (Prasad) L7AST.
Chapter 1: Inductive Sets of Data
Additional Scheme examples
Introduction to Scheme
Interpreters Study Semantics of Programming Languages through interpreters (Executable Specifications) cs7100(Prasad) L8Interp.
Corky Cartwright January 18, 2017
Programming Language Concepts
Syntax versus Semantics
Programming Languages 2nd edition Tucker and Noonan
Implementing Recursion
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
The Metacircular Evaluator
Abstract Syntax Prabhaker Mateti 1.
CSC 4181Compiler Construction Context-Free Grammars
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
Procedures App B: SLLGEN 1.
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:
Lecture #9 מבוא מורחב.
Abstract Syntax cs7100 (Prasad) L7AST.
3.6 Interpreter: Recursion
6.001 SICP Variations on a Scheme
CSC 4181 Compiler Construction Context-Free Grammars
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
topics interpreters meta-linguistic abstraction eval and apply
Recursive Procedures and Scopes
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Inductively Defined Data Concrete and Abstract syntax
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

2.2.2 Abstract Syntax Recall BNF definition of l-calculus expressions: <expression> ::= <identifier> ::= (lambda (<identifier>) <expression> ) ::= (<expression> <expression>) Uses concrete syntax: includes parens, and keywords like lambda We can use this to define an abstract datatype without these redundant items:

(define-datatype expression expression? (var-exp ::= <identifier> (id symbol?)) (lambda-exp ::= (lambda (<identifier>) <expression> (id symbol?) (body expression?)) (app-exp ::= (<expression> <expression>) (rator expression?) (rand expression?)))

First, we annotate the BNF with tree notation: 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: <expression> ::= <identifier> var-exp (id) ::= (lambda (<identifier>) <expression> lambda-exp (id body) ::= (<expression> <expression>) app-exp (rator rand)

Example: (lambda (x) (f (f x))) lambda-exp id body x app-exp rator rand var-exp app-exp id rator rand f var-exp var-exp id id f x

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))))))

Example: > (define e (lambda-exp 'a (var-exp 'b))) > (expression? e) #t > (occurs-free? 'a e) #f > (occurs-free? 'b e) But this is awkward. We want to parse actual expressions into the data structure....

(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 (eopl:error 'parse-expression "Syntax error: ~s" e))))) > (parse-expression '(lambda(a) (a b))) #(struct:lambda-exp a #(struct:app-exp #(struct:var-exp a) #(struct:var-exp b)))