58
Programming Languages Meeting 6 September 30, 2014 October 1, 2014

Programming Languages Meeting 6 September 30, 2014 October 1, 2014

Embed Size (px)

Citation preview

Page 1: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

Programming Languages

Meeting 6September 30, 2014

October 1, 2014

Page 2: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

Short Exam Answers

• Problem 1: Grammar of expressions• Problem 2: Semicolons, binding, statements

Page 3: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

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.

Page 4: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

SE Reprise (2)

The problem says that the first production in the grammar is:

<program> ::= program <statement-sequence>

Notes:<program> is the start symbolprogram is a terminal symbol

Page 5: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

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.

Page 6: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

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> ::= _______ | ______ | ______ | _______ | ______| ______

Page 7: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

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.

Page 8: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

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.

Page 9: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

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

Page 10: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

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

Page 11: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

Program Functions (3)

And the one with more moving parts, PFE II.5 with range 0..maxintx := x + a;while x <> b do

x := x + a

Page 12: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

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.

Page 13: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

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.

Page 14: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

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>]

Page 15: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

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.

Page 16: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

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.

Page 17: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

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:

Page 18: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

getcint newline = 10; int endfile = -1; // declare and initializetype character = -1..127; // declare a new typefunction 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 )

Page 19: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

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.

Page 20: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

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.

Page 21: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

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.

Page 22: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

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.

Page 23: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

Project 1 – Step 4 (2)For example, when the input file is

0.81 9.45quidditchA 1.23

the printed output is

0.810.01.23Sum = 2.04

Page 24: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

Project 1 - Submission

Due date: October 21/22, 2014What 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.

Page 25: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

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

Page 26: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

Functions, Methods, etc.

Segmenting programs is an old technique producing program parts known by many names:• Function• Subroutine• Procedure• Method• Operator• Subprogram

Page 27: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

Functions (2)

All segmentation strategies involve:• Input values• Local variables• Output valuesWe look in detail at the mechanisms provided by various programming languages using the semantic categories from before: Env, Ide, Loc, Store, V*, M, …

Page 28: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

Functions (3)

Syntax issues:• Defining the function• Using (invoking) the function

Page 29: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

Defining Functions

Need to specify:• Name– Kind: function, procedure, operator, …– Type of output

• Parameters– Name– Type– Mode

Page 30: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

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

Page 31: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

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

Page 32: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

Output Values (3)

• Side effect to global variable• Action but no state change– Print– Write to file

Page 33: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

Simple Functions

Specialize to• One or two arguments (parameters) of same

type• One value of same type as arguments• Monadic or dyadic operators

Page 34: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

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

Page 35: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

Subprogram Calls and Returns

Need to specify for calls:1. Parameter passing mechanism (mode)2. Allocation of storage for local variables and binding to

names3. Making global variables visible4. Saving execution status of calling program unit5. Transfer of control to subprogram code

Page 36: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

Calls and Returns (2)

Need to specify for return1. Moving values to output parameters2. Deallocating storage3. Resetting variable access4. Returning control to calling program

Page 37: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

Semantics of Parameter Passing

Consider five ways of parameter passing:1. Value2. Result3. Value-Result4. Reference (aka Location)5. Name

Page 38: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

Semantics (2)

In each case, we’ll assume a parameter list specified in the header of the procedureas (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.

Page 39: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

Pass by Value

1. Bind local parameters to locations:Element of Env becomes

x1 -> loc 1x2 -> loc 2

If x1 or x2 or … were bound to locations before invocation, those bindings are saved as part of the transfer of control

Page 40: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

Pass by Value (2)

2. Bind values of actual parameters to these locations.

Assume a1 -> loc 50 -> v1Then bind loc 1 to v1, changing the

element of the Store

3. Execute subprogram

Page 41: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

Pass by Value (3)

4. Break the bindings of the local parameters to locations.

Either the element of Env showsx1 -> unbound

orx1 -> location saved on entry

Page 42: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

Pass by Result

1. Bind local parameters to locations:Element of Env becomes

x1 -> loc 1x2 -> loc 2

If x1 or x2 or … were bound to locations before invocation, those bindings are saved as part of the transfer of control

Page 43: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

Pass by Result (2)

2. Execute subprogram3. Copy values of local parameters to locations

of actual parametersx1 -> loc 1 -> v1a1 -> loc 50

Bind loc 50 -> v1

Page 44: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

Pass by Result (3)

4. Break the bindings of the local parameters to locations.

Either the element of Env showsx1 -> unbound

orx1 -> location saved on entry

Page 45: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

Pass by Value-Result

1. Bind local parameters to locations:Element of Env becomes

x1 -> loc 1x2 -> loc 2

If x1 or x2 or … were bound to locations before invocation, those bindings are saved as part of the transfer of control

Page 46: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

Pass by Value-Result (2)

2. Bind values of actual parameters to these locations.

Assume a1 -> loc 50 -> v1Then bind loc 1 to v1, changing the

element of the Store

3. Execute subprogram

Page 47: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

Pass by Value-Result (3)

3. Copy values of local parameters to locations of actual parameters

x1 -> loc 1 -> v1a1 -> loc 50

Bind loc 50 -> v1

Page 48: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

Pass by Value-Result (4)

4. Break the bindings of the local parameters to locations.

Either the element of Env showsx1 -> unbound

orx1 -> location saved on entry

Page 49: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

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 50x2 -> 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

Page 50: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

Pass by Reference (2)

2. Execute subprogram

Page 51: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

Pass by Reference (3)

3. Break the bindings of the local parameters to locations.

Either the element of Env showsx1 -> unbound

orx1 -> location saved on entry

Page 52: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

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

etcIf x1 or x2 or … were bound to locations before invocation, those bindings are saved as part of the transfer of control.

Page 53: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

Pass by Name (2)

2. Execute subprogram

Page 54: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

Pass by Name (3)

3. Break the bindings of the local parameters to strings (of code)

Either the element of Env showsx1 -> unbound

orx1 -> location saved on entry

Page 55: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

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.

Page 56: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

Example 1 (2)

procedure swap(int x, int y);int temp;

// Show Env element and Store elementtemp := x;x := y;y := temp;

// Show Env element and Store elementend swap;

Page 57: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

Example 1 (3)

int I;int A[10];I := 3;A[I] := 6;// Show Env element and Store elementswap(I, A[I]);// Show Env element and Store element

Page 58: Programming Languages Meeting 6 September 30, 2014 October 1, 2014

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.