V02 Parsing, Conditionals, Names
V02 Parsing, Conditionals, Names
1
COPL
PARSING
How to go from concrete syntax to abstract syntax?
2
Parsing
Concrete syntax definition
— defines program text; recipe for
parsing and disambiguation
Program in concrete
syntax
In general, the process
of converting the
parsing + concrete syntax into
disambiguation ASTs is called parsing
4+5*6
+ *
4 * or + 6
5 6 4 5
(4 + (5 * 6)) ((4 + 5) * 6)
4
Parsing — A Subject of Study on its Own
Parsing algorithms
GLR SGLR LALR(1)
LL(k) LR(1) Early
…
5
No parsing in the rest of this course
6
S-expressions
4+5*6 ⇒ (+ 4 (* 5 6))
An S-expression is either:
enum SExp:
• A literal case SList(list: List[SExp])
• Symbol, Number, String case SSym(symbol: String)
• A list of s-expressions case SNum(num: Int)
• In parenthesis, separated by spaces case SString(string: String)
7
S-expressions
Num(1) ⇒ 1
Add(
Num(1) ⇒ (+ 1 2)
Num(2))
Add(
Num(1)
Add( ⇒ (+ 1 (+ 2 3))
Num(2)
Num(3))
8
Concrete Syntax as s-expressions
enum Exp:
case Num(n: Int)
case Plus(l: Exp, r: Exp)
case Mult(l: Exp, r: Exp)
10
S-Expression Parsing
SList(List(
SSym("+"),
Plus(
SNum(23),
Num(23),
(+ 23
(* 5 6))
⇒ SList(List(
SSym("*"),
⇒ Mult(
Num(5),
SNum(5),
Num(6)))
SNum(6)))))
11
Lisp
12 source: https://en.wikipedia.org/wiki/John_McCarthy_(computer_scientist)
Syntax versus Semantics
Semantics
Abstract
Syntax parse desugar Core interpret Value
Syntax
Syntax
13
COPL
CONDITIONALS
There can be very complex conditional expressions in real languages.
For now we’ll keep it simple and will only consider a simple if-then-else.
14
How Proceed to Add Conditionals
15
How Proceed to Add Conditionals
16
1. Extending the Abstract Representation
17
2. Extending the Interpreter…
What is the meaning of the following?
if (e) { e1 } else { e2 }
what‘s the
result of
interp(c)?
19
The Test-Expression
20
2. Extending the Interpreter
What about this?
21
2. Extending the Interpreter
But one could say that all we’ve done is (mostly) deferring to
Scala to handle these.
Do you (dis)agree?
22
Let’s Add Proper Boolean Expressions
23
Let’s Add Proper Boolean Expressions
1.
3. Extend the parser if we have one to produce the new representations
24
Extending the Program Representation
… Like This?
26
Extending the Interpreter …
… Like This?
Found: Bool
Required: Int
Found: Int
Required: Bool
27
Design Decision: The Range of Values
28
Implementing the Decision About Values
We first define a datatype that reflects the different kinds of values that
an interpreter can produce. Convention: call the return type constructors
...V to distinguish from the inputs of the interpreter
???
29
Implementing the Decision About Values
31
Encapsulating Design Decision Points
Build an abstraction:
• encapsulate the decision about what can change, e.g., what
values can be added, in one place for facilitating
experimentation with language design
• keep what is a fixed decision in the interpreter body
32
Encapsulating Design Decision Points
33
Encapsulating Design Decision Points
34
COPL
NAMES
35
Motivational Example 1
36
Motivational Example 2
37
Declarations and References
declaration
reference
38
Binding, Bound, Free Occurrences
bound free
occurrences occurrence
x is free in (x + x)
let x = 5 in x + x
x is bound in (let x = 5 in x + x)
39
Let’s Extend the Language with Let and Ids
40
Concrete Syntax for the Language with Identifiers
Example program:
val x = 3 + 4 (let x (+ 3 4)
x * x (* x x))
41
Concrete s-expr Syntax
42
Abstract Syntax
43
Semantics of Let
44
Defining Substitution
Wanted: A definition of the process of substitution
Here is one: Definition (Substitution):
To substitute identifier i in e with expression v, replace
all identifier sub-expressions of e named i with v.
45
Defining Substitution
Wanted: A definition of the process of substitution
Here is one: Definition (Substitution):
To substitute identifier i in e with expression v, replace
all identifier sub-expressions of e named i with v.
let x = 5 in x + x
let x = 5 in
x * (let x = 3 in x)
47
Defining Substitution
let x = 5 in
x + (let x = 3 in
x + x)
What‘s wrong here?
➔ 5 + (5 + 5)
➔ 15
48
Defining Substitution 2
49
Implementing Substitution
50
Implementing Substitution
51
Implementing Substitution
52
Implementing Substitution
54
Substitution: Corner Case 1
56
Substitution: Corner Case 2
57
Substitution: Take 3
def subst(id: String, bound: Exp, body: Exp): Exp = body match
…
case Let(letId, boundE, bodyE) =>
if (id == letId) Let(y, subst(id, bound, boundE), bodyE)
else
if ( !freeVars(bound).contains(letId) )
Let(letId, subst(id, bound, boundE), subst(id, bound, bodyE))
else
val z = freshName(freeVars(bound) ++ freeVars(bodyE))
Let(z, subst(id, bound, boundE), subst(id, bound, subst(letId, Id(z), bodyE))
)
id = x,
bound = y,
body = (let y 5 (\ x y))
(let x y (let y 5 (/ x y))
=>(let x y (let z 5 (/ x z))
letId = y,
=>(let z 5 (/ y z))
boundE = 5,
=>(/ y 5)
58 bodyE = (\ x y)
Capture-Avoiding Substitution
59
Alpha-Equivalence
Alpha-equivalence:
Two terms are identical up to consistent renaming of variables.
• terms have the same structure
• references resolve to the same declarations
• but names used for references and declarations may differ
60
Interpreting Ids
61
Semantics of Id
Any Id in the scope of a let-expr is replaced when the interpreter
encounters that identifier’s binding instance.
− subst replaces identifiers before the interpreter ever “sees”
them.
− the interpreter can’t replace a free name - it halts with an error
62
Two Substitution Regimes
Eager substitution (static1 {let {x {+ 5 5}} {let {y {- x 3}} {+ y y}}}
and dynamic reduction): = {let {x 10} {let {y {- x 3}} {+ y y}}}
= {let {y {- 10 3}} {+ y y}}
avoids re-computing the = {let {y 7} {+ y y}}
same value several times. = {+ 7 7}
= 14
Lazy substitution 2
{let {x {+ 5 5}} {let {y {- x 3}} {+ y y}}}
(Static reduction): the = {let {y {- {+ 5 5} 3}} {+ y y}}
expression may be = {+ {- {+ 5 5} 3} {- {+ 5 5} 3}}
= {+ {- 10 3} {- {+ 5 5} 3}}
evaluated multiple times. = {+ {- 10 3} {- 10 3}}
= {+ 7 {- 10 3}}
= {+ 7 7}
= 14
63
Two Substitution Regimes: Questions
Home work
{let {x {+ 5 5}}
{let {y 4} {+ y y}}}
{let {x {+ z 4}}
{let {y 4} {+ y y}}}
64
Notes on the Practicality of Substitution
=> (/ (+ (* 3 4) 1) 12)
65