Download presentation
Presentation is loading. Please wait.
Published byGinger Wright Modified over 8 years ago
1
PPL Syntax & Formal Semantics Lecture Notes: Chapter 2
3
Midterm 2007 (let ((x 4) (y 5)) ( + x y (let ((x y) (y x) (+ -)) (+ x y)))) מה הערך של הביטוי ?
4
Syntax Concrete Abstract
5
Concrete Syntax Defines the actual syntax In Scheme: – Small and simple – Context free grammar We use BNF notation – Formal way to describe formal languages – Next slide…
6
Concrete Syntax Non-terminals are denoted as Terminals (tokens) are surrounded with ’ Optional items are denoted as […] Items repeating 0 or more times end with * Items repeating 1 or more times end with + Alternative choices are separated by the | symbol Grouped items are in parentheses
7
Concrete Syntax -> | ’(’ ’)’ -> | | -> | -> Numbers -> ’#t’ | ’#f’ -> valid variable name -> | | | … -> + -> ’define’ -> ’lambda’ ’(’ * ’)’ + … See full syntax in lecture notes! Really. See lecture notes.
8
-> ( ) -> ( if )-> ( if #f ) -> ( if #f number) -> ( if #f 2) -> ( if #f ( ) 2) -> ( if #f ( / ) 2) -> ( if #f ( / 1 ) 2) -> ( if #f ( / 1 0 ) 2) Partial syntax: -> | ’(’ ’)’ -> | | -> | -> Numbers -> ’#t’ | ’#f’ -> valid variable name -> | | … -> + -> ’if’
9
Abstract Syntax Emphasizes the components Ignores syntactic parts irrelevant to semantics (like order of components, for example) Kinds – Kind of category Components – Elements that compose a component
10
Abstract Syntax : Kinds:, : Kinds:, : Kinds:,, … : Components: Variable: Expression: See notes for full syntax
11
Abstract Syntax Feature Diagram Can be represented by an Abstract-Syntax- Feature-Diagram (ASD or AS-FD) – Actually an and/or tree: kinds are or-branch and components are and-branch
12
Build ASD from Concrete Syntax -> | ‘(‘ ‘)’
13
: Kinds:, : Kinds:,, : Components: Variable: Expression:
14
Abstract Syntax Tree (AST) Representations of language expression For an expression e, AST(e) is a tree representing e. The ASD defines the AST representation of expressions
15
Possible AST for the expression: (lambda (x y) (+ x y))
16
Operational Semantics Specified by a set of evaluation rules Actually an eval(exp) algorithm for Scheme expressions In order to formally define eval(exp) we recall some concepts and introduce new ones
17
Binding Instance (declaration) a variable occurrence to which other occurrences refer. In Scheme: 1.Variables occurring as λ parameters are binding instances (recall let ) 2.Top level (non-nested) defined variables (in a define form) are binding instances (declarations). No internal define s are allowed!
18
Scope The region of the program in which variable occurrences refer to the value that is bound by the variable declaration (where the variable declaration is recognized) In Scheme: 1.Scope of λ parameters is the entire lambda expression (let). 2.Scope of define variables is the entire program, from the define expression and on: Universal / global scope.
19
Bound Occurrence Occurrence of variable x that is not a binding instance and is contained within the scope of x. The binding instance (declaration) that binds an occurrence of x is the most nested declaration
20
Free Occurrence An occurrence of variable x that is not a binding instance, and is not bound.
21
Examples (lambda (x) (+ x 5)) ; x is bound by its declaration ; in the parameter list. (define y 3) ;A binding y instance. (+ x y) ; x is free, y is bound ; (considering the above ; evaluations). (+ x ( (lambda (x) (+ x 3)) 4)) ; the 1st x occurrence is free, ; the 2nd is a binding instance, ; the 3rd is bound by the declaration ; in the lambda parameters. (lambda (y) ;a binding instance (+ x y ;the x occurrence is free ;the y occurrence is bound by the outer declaration ((lambda (x y) (+ x y)) ;the x and y occurrences are bound by the internal declarations. 3 4) ))
22
Renaming Bound variables can be renamed by new variable without changing the semantics (lambda (x) x) (lambda (y) y) Bad renaming: ((+ x ( (lambda (x) (+ x y)) 4))) ==> ((+ x ( (lambda (y) (+ y y)) 4))) New definition!
23
Substitution New definition!
24
Composition of Substitution New definition!
25
Application of Substitution New definition!
26
Application of Substitution No renaming; no replacement 10◦{x = 5} = 10 No renaming; just replacement (+ x y)◦{x = 5} = (+ 5 y) Renaming and replacement ((+ x ((λ (x) (+ x 3)) 4)))◦{x = 5} = ((+ 5 ((λ (x 1 ) (+ x 1 3)) 4))) Another renaming and replacement (λ (y) (((λ (x) x) y) x))◦{x = } (rename ) => (λ (y 2 ) (((λ (x 3 ) x 3 ) y 2 ) x))◦{x = } (replace ) => (λ (y 2 ) (((λ (x 3 ) x 3 ) y 2 ) ))
27
Reduce Get the body part of a closure. x New definition!
28
The Substitution Model: Applicative Order Evaluation Now we have all the definitions we need to define eval(e).
29
29 Applicative order evaluation rules Combination... ( …… ) 1.Evaluate to get the procedure 2.Evaluate to get the arguments 3.If is primitive: do whatever magic it does 4.If is user-defined (e.i. lambda) apply: 1.Substitute 2.Reduce Eager!
30
Before We See the Pseudo-code… Predicates atom?, composite?, number?, boolean? and variable? Are used to classify expressions Predicates primitive-procedure? And procedure? are used to classify procedures Predicate value? Identifies Scheme values.
32
Another Approach: Normal Order Evaluation Also called ‘lazy’ Main idea: argument evaluation is deferred until needed: – Branch – Primitive procedure
33
33 Normal order evaluation Combination … ( …… ) Evaluate to get the procedure evaluate to get the arguments If is primitive: do whatever it does If is compound: evaluate body with formal parameters replaced by arguments Lazy…
35
35 The Difference Applicative ((lambda (x) (+ x x)) (* 3 4)) (+ 12 12) 24 Normal ((lambda (x) (+ x x)) (* 3 4)) (+ (* 3 4) (* 3 4)) (+ 12 12) 24 This may matter in some cases: ((lambda (x y) (+ x 2)) 3 (/ 1 0)) How can you find out how DrRacket works?
36
Comparison If both stop, they compute the same value If applicative stops, normal stops. Not the other way around. Normal may repeat computations
37
Another Example (define f (lambda (x) (f x))) (define g (lambda (x) 5)) (g (f 0)) What will happen with applicative evaluation? What about Normal?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.