Download presentation
Presentation is loading. Please wait.
Published byNeil Wilcox Modified over 8 years ago
1
Programming Languages Meeting 3 September 9/10, 2014
2
Homework Results Look for sequences. Every RHS is one. ::= 0|1|2|3|4|5|6|7|8|9 ::= | ::= ::= ( ) | λ ::= ( ) | | ::= |,
3
Homework (2) Spelling: it is grammar There are four different grammars cited in the slides from the first lecture. I expected four parse trees. You cannot include ( ) in the parse tree if they don’t appear in the input string.
4
From Last Time Example 2 (Scope): How many identifiers used? How many defining points? What is the program output?
5
Plan for Tonight A survey and acting on the results Assignment statements – Simple semantics – Making them more complex and testing the limits of our definitions – Reference values – Multiple assignment statements – Syntax and semantics of complex lhs expressions
6
Survey On the small piece of paper, write down all the programming languages you have installed on your computer because you have: – An IDE for the language – An interpreter for the language – A compiler for the language Hint: Check the Program Files folder or the Applications folder
7
On to Assignment Statements In addition to Env and Loc, which have been defined before, define Value, the set of storable values for a particular type V* = Value + {undefined} Store = Loc V*
8
Assignment (2) Investigating the semantics of an assignment statement, which stores a value in a named location: Abstract syntax shows we need – An l-expression: the target of the assignment – A value: to store in the target
9
Assignment (3) Possible l-expressions are – a simple identifier – an array component – a field in a record – a pointer image – and others
10
Assignment (4) At declaration time each identifier is associated with an attribute record, which contains – A type descriptor – An addressing function (the identity function for nonstructured types) – Storage requirements (# of words and layout)
11
Assignment (5) At instantiation, which occurs at procedure invocation, each identifier is mapped to a base location, which becomes an argument to the addressing function. Let M = Ide Loc + {unbound} V* with the understanding that {unbound} {undefined}
12
Assignment (6) In the simplest cases we can work with M = Ide V* bypassing consideration of locations. Ready for the formal semantics of an assignment statement:
13
Assignment (7) The meaning of the assignment statement A := E is a function that maps M to M, sometimes denoted by [A:=E] defined so that M goes to M’ where M is the original memory state: a function assigning identifiers to values M’ is the new memory state, defined by
14
Assignment (8) M(x) if x ≠ A M’(x) = v if x = A Note that this says: The previous value of A is irrelevant No other values are affected
15
Your Turn Exercise 1: Assume Ide = { a,b,c }, Value = { 0,1,2,3 } Find [a := 2] Caution: There are several sets of functions involved. Keep them straight.
16
Your Turn (2) Exercise 2: (Refer to the handout: Syntax Definition for REF) 1.Determine the four components of the context- free grammar specification for REF – start symbol, nonterminals, etc. 2.Write a small (no more than 5 lines) syntactically correct program in REF. Put your final version on the special sheet of paper.
17
Exercise 2 (2) 3.What is the output of your program? 4.How many different types does the language support? 5.Create the parse tree for the program you have been given.
18
Exercise 3 For the program fragment on the following slide, give the elements of Env, Loc, Store, and M that represent the program state after the fragment loads. If you need memory addresses, make them up. Hint: Figure out what the elements of each of the semantic domains look like.
19
Exercise 3 (2) program declare ONE : integer constant = 1; declare TWO : integer constant = 2; declare A,B : integer; declare RC,RD : ref integer; declare RRE, RRF : ref ref integer;
20
Semantics of REF Semantics of a REF assignment statement: First the LHS: It is an identifier with a type. – Type is integer constant: invalid assignment—type mismatch – Type is [ref…] integer: Assume j occurrences of ref, j = 0,1,2,…
21
Semantics (2) Now the RHS: three kinds of expressions a)+ operation included b)literal integer c)single identifier
22
Semantics (3) Generating a RHS value: Case a) RHS involves + – Find location bound to variable, perform operation as indicated in RHS dereferencing identifiers as necessary, obtaining value v Case b) RHS is an integer – The value v is the value of the integer
23
Semantics (4) Case c) RHS is a single identifier, with type – Integer constant: Value v is the value of the constant – [ref]… integer: There are 3 subcases, depending on the reference level k of the identifier Case 1: j < k+1 Case 2: j = k+1 Case 3: J > k+1
24
Semantics (5) Taking each of these subcases in turn j < k+1 : Dereference RHS identifier to level j. Set v to the value of the dereferenced identifier. j = k+1 : Set v to the location of the RHS identifier j > k+1 : Invalid statement—type mismatch
25
Semantics (6) Store v in location determined by LHS. – If v is an integer, the reference level of the LHS, j, must be 0. Otherwise, there is a type mismatch. – If v is a reference value, it is of the same level as the LHS, so is storable.
26
Exercise 4 Using the declaration fragment from before, execute the following statements and determine the memory function M : Ide -> Loc -> V* when the fragment completes.
27
Exercise 4 (2) A := ONE; B := A + ONE; RC := A; RD := B; RRE := RC; RRF := RD;
28
Exercise 5 Finally, assume that you have executed the 6 lines of code in Exercise 4. Make each of the statements below the 7 th line of code and determine how M changes. 1. A := RRF + B; 2. B := RRE; 3. RC := RRF + RD; 4. RRF : = RRE; 5. RRE : = RD;
29
Installing Systems Make sure that you have installed on your computer: ChucK (1.3.4.0) -- chuck.cs.princeton.edu Python (3.4.1) -- python.org Perl (5.20.0) – perl.org
30
For Next Time Install the systems noted on the previous slide. Execute the five statements in REF in Exercise 5. Post the results on piazza.com
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.