Title: Prepared by
1Extending Tiny
Programming Language Principles Lecture 27
- Prepared by
- Manuel E. Bermúdez, Ph.D.
- Associate Professor
- University of Florida
2Tinys Denotational Semantics in RPAL
- let EQ x y Istruthvalue x Istruthvalue y
- - (x y) or (not x not y)
- Isstring x Isstring y
- or Isinteger x Isinteger y
- - x eq y false
- in
- let COMP f g x let R f x in
- R _at_EQ 'error' - 'error' g R
- in
3Tinys Denotational Semantics in RPAL
- let PIPE x f x _at_EQ 'error'
- - 'error' (f x)
- in
- let Return v s (v,s)
- in
- let Check Dom (v,s)
- Dom eq 'Num' -
- Isinteger v - (v,s) 'error'
- Dom eq 'Bool' -
- Istruthvalue v - (v,s) 'error'
- 'error'
- in
4Tinys Denotational Semantics in RPAL
- let Dummy s s
- in
- let Cond F1 F2 (v,s)
- s _at_PIPE (v - F1 F2)
- in
- let Replace m i v x x _at_EQ i - v m x
- in
- let Head i i 1
- in
- let Tail T rtail T (Order T)
- where rec rtail T N
- N eq 1 - nil
- (rtail T (N-1) aug (T N))
- in
5Tinys Denotational Semantics in RPAL
- let rec EE E (m,i,o)
- Isinteger E - Return E (m,i,o)
- Isstring E -
- ( E eq 'true' - Return true (m,i,o)
- E eq 'false' - Return false (m,i,o)
- E eq 'read' - Null i - 'error'
- (Head i,(m,Tail i,o))
- (let R m E in R _at_EQ 'undef'
- - 'error' (R,(m,i,o))
- )
- )
6Tinys Denotational Semantics in RPAL
- Istuple E -
- ( (E 1) _at_EQ 'not' -
- (m,i,o) _at_PIPE EE(E 2)
- _at_PIPE (Check 'Bool')
- _at_PIPE (fn(v,s).(not v,s))
7Tinys Denotational Semantics in RPAL
- (E 1) _at_EQ ' (m,i,o)
- _at_PIPE EE(E 2)
- _at_PIPE (Check 'Num')
- _at_PIPE (fn(v1,s1). s1
- _at_PIPE EE(E 3)
- _at_PIPE (Check 'Num')
- _at_PIPE
- (fn(v2,s2).(v1 le v2,s2))
- )
8Tinys Denotational Semantics in RPAL
- (E 1) _at_EQ '' - (m,i,o)
- _at_PIPE EE(E 2)
- _at_PIPE (Check 'Num')
- _at_PIPE (fn(v1,s1). S1
- _at_PIPE EE(E 3)
- _at_PIPE (Check 'Num')
- _at_PIPE
- (fn(v2,s2).(v1 v2,s2))
- )
- 'error' // not 'not', '
- )
- 'error' // not a tuple
- in
9Tinys Denotational Semantics in RPAL
- let rec CC C s
- not (Istuple C) - 'error'
- (C 1) _at_EQ ''
- - s _at_PIPE EE (C 3)
- _at_PIPE (fn(v,s).
- (Replace (s 1) (C 2) v,s 2,s 3))
- (C 1) _at_EQ 'print'
- - s _at_PIPE EE (C 2)
- _at_PIPE (fn(v,s).
- (s 1,s 2,s 3 aug v))
10Tinys Denotational Semantics in RPAL
- (C 1) _at_EQ 'if'
- - s _at_PIPE EE (C 2)
- _at_PIPE (Check 'Bool')
- _at_PIPE (Cond (CC(C 3)) (CC(C 4)))
- (C 1) _at_EQ 'while'
- - s _at_PIPE EE (C 2)
- _at_PIPE (Check 'Bool')
- _at_PIPE Cond (CC('',C 3,C)) Dummy
-
11Tinys Denotational Semantics in RPAL
- (C 1) _at_EQ ''
- - s _at_PIPE CC (C 2) _at_PIPE CC (C 3)
- 'error' // not '', 'if', ...
- in
- let PP P
- not (Istuple P) - (fn i. 'error')
- not ((P 1) _at_EQ 'program')
- - (fn i. 'error')
- ((fn i. CC (P 2)
- ((fn i.'undef'),i,nil) //start state!
- ) _at_COMP (fn s.(s 3)) )
- in
12Tinys Denotational Semantics in RPAL
- Print
- ( PP ('program', // test program
- ('',
- ('', 'x',3),
- ('print', 'x')
- )
- ) (nil aug 3) // the input
- )
- Whew ! Now, RUN IT !!
13Tinys Denotational Semantics in RPAL
- Executable semantic specification of Tiny.
- Add a parser, and voilà ... Tiny is implemented !
- Could even write the parser in RPAL ... ?
- Inefficient, but who cares ...
- 'error' (and others) should probably be
'', so we allow those as variable names in
Tiny. - Subject to change
- Alter order of evaluation of operands.
- Allow comparison of booleans.
14Extending Tiny
- First, add more comparison operators, and lots of
arithmetic operators (easy). Example
EE EEE1 o (Check Num) o
(?(v1,s1). s1 EEE2 (Check Num)
(?(v2,s2).(v1 - v2,s2) )
15Extending Tiny
- Lets add the '' comparison operator. Allow for
Num and Bool. This allows type mixing ! - EE
- EEE1
- o (Check Num)
- o (?(v1,s1). s1 EEE2
- (Check Num)
- (?(v2,s2).(v1 eq v2,s2)
- )
16Add Conditional Expression
- Need a new auxiliary function Econd.
- ECond (State ? Val x State)
- ? (State ? Val x State)
- ? (Val x State)
- ? (Val x State)
-
- Econd ?EF1. ?EF2. ?(v,s). s (v ? EF1 EF2)
- EE EEE1
- o (Check Bool)
- o (Econd EEE2 EEE3)
17Add prefix auto-increment operator
- EE
- EEI o (Check Num)
- (?(v,(m,i,o)). v eq ? ? error
- (v1, (Replace m I (v1)),
i, o) - )
- For postfix (n), change this to v !
18Adding the one-armed if to Tiny
- CC EEE
- o (Check Bool)
- o (Cond CCC Dummy)
- Of course, for most of these, the syntactic
domains need to be updated.
19Adding a repeat statement to Tiny
- CC
- CCC o EEE o (Check Bool) o
- (Cond Dummy (CC))
- or better yet,
- CC
- CCC o CC C
20Adding a read statement to Tiny
- CC
- ?(m,i,o). Null i ? error
- (Replace m I (Head i), Tail i,
o) - Would need to remove the read expression.
21Adding the Pascal for loop to Tiny
- CC EEL o (Check Num) o
- (?(l,s). S EEF (Check Num)
- (?(f,(m,i,o)). (Replace m I f, i,
o) - CC
- I 1
-
- )
- ) o
- (?(m,i,o). (Replace m I ?, i, o))
- Yuck. Cant enforce lots of rules.
22Adding the C for loop to Tiny
- CC
- CCF o CC
- or
- CC
- Remarkably simple, eh ?
- Of course, Tiny has no continue statement to get
in the way. - We assume default values have been added for any
missing parts, e.g. true for E.
23Adding a case statement to Tiny
- CC
- EEE o (Check Num) o
- C_CCCC1 ... C_CCCCn o (?(v,s).s)
- Need a new syntactic domain, for case-clauses
- C_C
- Also, a new semantic function to process them
- C_CC C_C ? (Val x State) ? (Val x State)
24Adding a case statement to Tiny
- To process one case clause
- C_CC
- ?(v,s). v eq ? ? (v,s)
- v ne n ? (v,s)
- (? , s CCC)
- Aborts all subsequent case clauses.
- To process them all, change this to v !
25Remarks on Denotational Semantics
- Exercise implement these in RPAL ! (see medium
on website) - Can this be done for real programming languages
? Yes, but ... - We now have three formalisms for specifying the
semantics of programming languages - Operational (RPAL)
- Attribute grammars (Tiny)
- Denotational (Tiny)
26Remarks on Semantic Specifications
- Remember, parsing was easy ?
- Reason one formalism (CFGs) good for everyone
- Language user.
- Language implementer.
- Language designer.
- Not so in the world of semantics.
27Remarks on Semantic Specifications
User
Designer
Implementer
Operational
Denotational
Attribute Grammar
E Excellent, G Good F Fair, T -
Terrible
28Extending Tiny
Programming Language Principles Lecture 27
- Prepared by
- Manuel E. Bermúdez, Ph.D.
- Associate Professor
- University of Florida