Document

advertisement
Programming Languages
Meeting 6
September 30, 2014
October 1, 2014
Short Exam Answers
• Problem 1: Grammar of expressions
• Problem 2: Semicolons, binding, statements
Short Exam Reprise
• Focused on Problem 2(c)
• Steps to developing a grammar for the
programming language of the problem
• Submit by 5:00 pm, Friday, October 3, 2014, to
my mailbox in MSC 159.
• Consult only with the instructor.
• Those following the instructions exactly and
getting the right answers will have 12 marks
add to their exam score.
SE Reprise (2)
The problem says that the first production in the
grammar is:
<program> ::= program <statement-sequence>
Notes:
<program> is the start symbol
program is a terminal symbol
SE Reprise (3)
Question 2(c)i asks you to create the second
production for the grammar
<statement-sequence> ::= _________________
Hint: Review slides 1.16, 1.17, 1.32, and the first
syntax exercise to discover how sequences are
constructed.
SE Reprise (4)
The third production is the answer to 2(c)ii. Fill
in the six blanks with the six options occurring in
the program. Give each option a meaningful
name.
<statement> ::= _______ | ______ | ______ |
_______ | ______| ______
SE Reprise (5)
The next six productions are the complete
answer to 2(c)iii. Each defines the syntax of a
particular statement type listed in the previous
production. Do not refine the productions by
including more productions to define other
syntactic categories. Use just one production for
each statement type.
SE Reprise (6)
Your complete answer should have exactly 9
productions, no more, no less. The first three
will describe the syntactic categories
<program>, <statement-sequence>,
<statement>. The other six will describe the
various statement types.
Program Functions
We’ll take some time to work on the homework
problem (PFE I.4) assuming range of
−maxint..maxint:
if y>0 then
x := x-1;
y := y-1;
x := x-y;
y := 0
fi
Program Functions (2)
And the related problem (PFE I.5) assuming
range of 0 .. maxint:
if y>0 then
x := x-1;
y := y-1;
fi;
x := x-y;
y := 0
Program Functions (3)
And the one with more moving parts, PFE II.5
with range 0..maxint
x := x + a;
while x <> b do
x := x + a
Program Functions (4)
And finally PFE II.4 with range −maxint..maxint
while x <> b do
x := x + a
The solution for this one is left for you to try again
and submit by Friday, October 10. Remember
homework problems are to be individual effort.
Consult only with the instructor.
Project 1
• Class will not meet on October 7/8
• Instead, the classes will divide into teams of 3
and investigate a common programming
language structure in a simplified
programming setting.
• Take a few minutes now and form your teams.
Choose someone from the team to send me
an email with the team member names and
email addresses.
Project 1 – Step 1
Syntax of a float:
<digit> ::= 0|1|2|3|4|5|6|7|8|9
<sign> ::= + | −
<digit string> ::= <digit>{<digit>}
<integer> ::= [<sign>]<digit string>
<float> ::= <integer>.<digit string>[E<integer>]
Step 1 (2)
• Step 1.0. Determine the terminal symbols of the
grammar for float.
• Step 1.1. For each possible path coming from the
BNF, give an example of a float defined by that
path.
• Step 1.2. Add a production (or several) to the
grammar to define a normalized integer and a
normalized float. Normalization removes leading
zeros except for a single zero before a decimal
point.
Step 1 (3)
• Step 1.3 Develop a grammar for a text file as a
sequence of lines, and a line as a sequence of
characters.
• Step 1.4 Verify that your grammar is correct by
using the Unix wc command or its equivalent on
various samples of text files.
• Submit your answers to Step 1.0 through 1.3 by
4:00 pm on October 7 either as a Word document
attached to an email message sent to me or as a
handwritten solution placed in my mailbox.
Project 1 – Step 2
• Choose one of three languages in which your
team will write its programs: C, Java, Python
3.4
• Assume that the only way to read information
from a file is one character at a time, using the
getc function defined as:
getc
int newline = 10; int endfile = -1; // declare and initialize
type character = -1..127;
// declare a new type
function getc(var c:character): character; // var = value/result
char ch;
// declare ch as char, the built-in character type
if eof
// eof is the text file end-of-file
then
c := endfile
else
if eoln
// eoln is the text file end-of-line
then
readln; // readln skips to the first character in the next line
c := newline
else
read(ch);
// read is the function that reads text files
c := ord(ch); // ord is the function that produces the
// ASCII value of a character
return( c )
Project 1 – Step 2 (2)
• Step 2. Code getc in your chosen language (C,
Java, Python 3.4) and test it thoroughly.
Note: You will have to figure out how to check
for end of file and end of line in your language.
You will also have to figure out how to move to
reading characters in the next line, how to read
a single character at a time, and how to convert
a character to its ASCII code.
Project 1 – Step 3
• Step 3. Design, code and thoroughly test a
function called getfloat that will read from a
text file using getc and return the value of the
first (normalized) real number, defined by the
syntax above, that it encounters on the
current line. The next slide contains more
detailed specifications.
Project 1 – Step 3 (2)
Assumptions and specifications:
• Assume there is white space preceding any
real number or that the real number starts at
the beginning of the line.
• Assume that the real number is terminated by
the first character encountered that does not
fit the specified syntax.
• If there is no real number found on a given
line, getfloat returns 0.0.
Project 1 – Step 4
• Step 4. Write a program that reads an
arbitrary text file using getfloat and prints the
list of real numbers it returns (one per text
line) and the sum of those numbers, labeled
as the sum.
Project 1 – Step 4 (2)
For example, when the input file is
0.81 9.45
quidditch
A 1.23
the printed output is
0.81
0.0
1.23
Sum = 2.04
Project 1 - Submission
Due date: October 21/22, 2014
What to submit:
• Source code for three programs
– getc
– getfloat
– main (the program that reads an arbitrary text file
looking for real numbers)
• Attach the source code to an email. I will attempt
to compile the source code and execute it against
several input files.
Project 1 - Hints
• Comments in source code are very helpful
• One team member could take on the role of
tester, trying to break the program.
• 3.02450 is a fine float
• 0.13E-4 is a fine float
• 1.08E03 is not a float
Functions, Methods, etc.
Segmenting programs is an old technique
producing program parts known by many names:
• Function
• Subroutine
• Procedure
• Method
• Operator
• Subprogram
Functions (2)
All segmentation strategies involve:
• Input values
• Local variables
• Output values
We look in detail at the mechanisms provided by
various programming languages using the
semantic categories from before: Env, Ide,
Loc, Store, V*, M, …
Functions (3)
Syntax issues:
• Defining the function
• Using (invoking) the function
Defining Functions
Need to specify:
• Name
– Kind: function, procedure, operator, …
– Type of output
• Parameters
– Name
– Type
– Mode
Output Values
Produced in several ways
• Appear as parameter values:
– foofun(x,y) takes x and computes y
– Modes of y: result, reference
– Can handle arbitrary type as output
Output Values (2)
• Return in function name
– Follows structure in mathematics
• log(x)
– Easy to work with in expressions
– Implemented with
• RETURN statement
• Assignment to function name
Output Values (3)
• Side effect to global variable
• Action but no state change
– Print
– Write to file
Simple Functions
Specialize to
• One or two arguments (parameters) of same
type
• One value of same type as arguments
• Monadic or dyadic operators
Simple Functions (2)
Implementation strategies
• Mix operators and functions
– Typical, mimics mathematics: 1+log(4)
• Operators only
– Use symbols for functions
• Monadic functions only
– Use list structure for several arguments
Subprogram Calls and Returns
Need to specify for calls:
1. Parameter passing mechanism (mode)
2. Allocation of storage for local variables and binding to
names
3. Making global variables visible
4. Saving execution status of calling program unit
5. Transfer of control to subprogram code
Calls and Returns (2)
Need to specify for return
1. Moving values to output parameters
2. Deallocating storage
3. Resetting variable access
4. Returning control to calling program
Semantics of Parameter Passing
Consider five ways of parameter passing:
1. Value
2. Result
3. Value-Result
4. Reference (aka Location)
5. Name
Semantics (2)
In each case, we’ll assume a parameter list
specified in the header of the procedure
as (x1, x2, …). These are the formal parameters.
We’ll assume a call foofun(a1,a2, …). The a’s are
the actual parameters.
Here’s what happens in each scenario.
Pass by Value
1. Bind local parameters to locations:
Element of Env becomes
x1 -> loc 1
x2 -> loc 2
If x1 or x2 or … were bound to locations before
invocation, those bindings are saved as part of the
transfer of control
Pass by Value (2)
2. Bind values of actual parameters to these
locations.
Assume a1 -> loc 50 -> v1
Then bind loc 1 to v1, changing the
element of the Store
3. Execute subprogram
Pass by Value (3)
4. Break the bindings of the local parameters to
locations.
Either the element of Env shows
x1 -> unbound
or
x1 -> location saved on entry
Pass by Result
1. Bind local parameters to locations:
Element of Env becomes
x1 -> loc 1
x2 -> loc 2
If x1 or x2 or … were bound to locations before
invocation, those bindings are saved as part of the
transfer of control
Pass by Result (2)
2. Execute subprogram
3. Copy values of local parameters to locations
of actual parameters
x1 -> loc 1 -> v1
a1 -> loc 50
Bind loc 50 -> v1
Pass by Result (3)
4. Break the bindings of the local parameters to
locations.
Either the element of Env shows
x1 -> unbound
or
x1 -> location saved on entry
Pass by Value-Result
1. Bind local parameters to locations:
Element of Env becomes
x1 -> loc 1
x2 -> loc 2
If x1 or x2 or … were bound to locations before
invocation, those bindings are saved as part of the
transfer of control
Pass by Value-Result (2)
2. Bind values of actual parameters to these
locations.
Assume a1 -> loc 50 -> v1
Then bind loc 1 to v1, changing the
element of the Store
3. Execute subprogram
Pass by Value-Result (3)
3. Copy values of local parameters to locations
of actual parameters
x1 -> loc 1 -> v1
a1 -> loc 50
Bind loc 50 -> v1
Pass by Value-Result (4)
4. Break the bindings of the local parameters to
locations.
Either the element of Env shows
x1 -> unbound
or
x1 -> location saved on entry
Pass by Reference
1. Bind local parameters to locations of actual
parameters:
If a1 -> loc 50, a2 -> loc 51, … then
Element of Env becomes
x1 -> loc 50
x2 -> loc 51, etc
If x1 or x2 or … were bound to locations before
invocation, those bindings are saved as part of the
transfer of control
Pass by Reference (2)
2. Execute subprogram
Pass by Reference (3)
3. Break the bindings of the local parameters to
locations.
Either the element of Env shows
x1 -> unbound
or
x1 -> location saved on entry
Pass by Name
1. Bind local parameters to rexp of actual
parameters
If a1 is given as string1 then
x1 is assumed to be string1
etc
If x1 or x2 or … were bound to locations before
invocation, those bindings are saved as part of the
transfer of control.
Pass by Name (2)
2. Execute subprogram
Pass by Name (3)
3. Break the bindings of the local parameters to
strings (of code)
Either the element of Env shows
x1 -> unbound
or
x1 -> location saved on entry
Example 1
Your turn:
For the following procedure and calling
program, determine the elements of Env and
Store at the four points indicated by the
comments. Make this determination for each of
the five ways of passing parameters from calling
programs to procedures.
Example 1 (2)
procedure swap(int x, int y);
int temp;
// Show Env element and Store element
temp := x;
x := y;
y := temp;
// Show Env element and Store element
end swap;
Example 1 (3)
int I;
int A[10];
I := 3;
A[I] := 6;
// Show Env element and Store element
swap(I, A[I]);
// Show Env element and Store element
Task Summary
• Due 5:00 pm, Friday, October 3. Written solution to
Short Exam problem 2(c) put in my mailbox in MSC
159.
• Due 4:00 pm, Tuesday, October 7. Team solution to
Step 1 of Project 1 either as a Word document sent as
an attachment or as a printed copy put in my mailbox
in MSC 159.
• Due 5:00 pm, Friday, October 10. Revised solution to
Program Function exercise II.4 assuming range
−maxint..maxint put in my mailbox in MSC 159.
• Due October 21/22. Project 1 code. See project specs
for details.
Download