Upload
oswald-lewis
View
214
Download
0
Embed Size (px)
Citation preview
CS 152: Programming Language Paradigms
February 3 Class Meeting
Department of Computer ScienceSan Jose State University
Spring 2014Instructor: Ron Mak
www.cs.sjsu.edu/~mak
SJSU Dept. of Computer ScienceSpring 2014: February 3
CS 152: Programming Language Paradigms© R. Mak
2
Small Teams
You will learn better by working in small teams.
Form your own teams of 2 or 3 students each.
Choose your team members wisely!
Be sure you’ll be able to meet and communicate with each other and work together well.
After forming a team, no moving to another team.
Each team member will receive the same score on the team assignments.
Email me your team name and the list of team members and email addressesby Monday, February 3: [email protected]_
Use Piazza tofind teammates!
SJSU Dept. of Computer ScienceSpring 2014: February 3
CS 152: Programming Language Paradigms© R. Mak
3
Team Assignment #1: Algebra 1 Nightmare
Due Monday, February 10 at 11:59 PM
Each team writes a FORTRAN IV program. Get experience writing in a classic programming language. Appreciate the limitations that programmers faced in the 1960s. Understand where today’s languages come from.
Recall the quadratic formula:
a
acbbx
2
42
SJSU Dept. of Computer ScienceSpring 2014: February 3
CS 152: Programming Language Paradigms© R. Mak
4
Assignment #1, cont’d
The formula produces two complex roots, x1 and x2: (x1real, x1imaginary) and (x2real, x2imaginary)
The discriminant b2 – 4ac determines the nature of the two roots:
Discriminant positive:
a
acbbx
2
42
a
acbbx real 2
42
1
a
acbbx real 2
42
2
021 imaginaryimaginary xx
SJSU Dept. of Computer ScienceSpring 2014: February 3
CS 152: Programming Language Paradigms© R. Mak
5
Assignment #1, cont’d
The discriminant b2 – 4ac determines the nature of the two roots:
Discriminant zero:
a
bxx realreal 221
021 imaginaryimaginary xx
a
acbbx
2
42
SJSU Dept. of Computer ScienceSpring 2014: February 3
CS 152: Programming Language Paradigms© R. Mak
6
Assignment #1, cont’d
The discriminant b2 – 4ac determines the nature of the two roots:
Discriminant negative:a
bxx realreal 221
a
acbx imaginary 2
42
1
imaginaryimaginary xx 12
a
acbbx
2
42
SJSU Dept. of Computer ScienceSpring 2014: February 3
CS 152: Programming Language Paradigms© R. Mak
7
Assignment #1, cont’d
Your FORTRAN program quadratic.for should read the following 21 “punched cards” as input data (text file quadratic.in).
Each card contains one set of values for a, b, and c.
Hint: Use the input format specification 3F10.0 to read the three values from each card.
The 21st card is an “end of deck” marker.
1.0 -2.0 1.01.0 -10.0 25.01.0 -3.0 2.02.0 -6.0 4.01.0 1.0 -2550.010.0 -20.0 10.01000.0 -2000.0 1000.00.02 -0.04 0.021.432 9.876 -4.5678.813 -1.31 0.02.3 1.9917 0.01.0 0.0 -1.01.0 0.0 1.09.0 0.0 36.01.0 2.0 5.0535.0 22.0 1583.0-9.0 23.0 37.061.0 2.0 87.061.0 159.0 87.0100.0 99.0 98.00.0
SJSU Dept. of Computer ScienceSpring 2014: February 3
CS 152: Programming Language Paradigms© R. Mak
8
Assignment #1, cont’d
For each set of input values a, b, and c, use the quadratic formula to compute the two roots x1 and x2. Compute the quadratic formula in a FORTRAN subroutine.
For each set of input values, your program should output the values of a, b, c, x1real, x1imaginary, x2real, and x2imaginary. Also print the card count.
Hint: Use the format specification 1PE15.4 to output each real number. You can specify a repeat factor such as 1P3E15.4. The E means exponential notation, and the 1P moves the decimal point to the right by one (e.g., print -2.5500E+03 instead of the default -0.2550E+04)_
SJSU Dept. of Computer ScienceSpring 2014: February 3
CS 152: Programming Language Paradigms© R. Mak
9
Assignment #1, cont’d
Sample output. Note the blanked out items in the last four columns
for values that are purely real or purely imaginary. COUNT A B C X1 REAL X1 IMG X2 REAL X2 IMG
1: 1.0000E+00 -2.0000E+00 1.0000E+00 1.0000E+00 1.0000E+00 2: 1.0000E+00 -1.0000E+01 2.5000E+01 5.0000E+00 5.0000E+00 3: 1.0000E+00 -3.0000E+00 2.0000E+00 2.0000E+00 1.0000E+00 4: 2.0000E+00 -6.0000E+00 4.0000E+00 2.0000E+00 1.0000E+00 5: 1.0000E+00 1.0000E+00 -2.5500E+03 5.0000E+01 -5.1000E+01 6: 1.0000E+01 -2.0000E+01 1.0000E+01 1.0000E+00 1.0000E+00 7: 1.0000E+03 -2.0000E+03 1.0000E+03 1.0000E+00 1.0000E+00 8: 2.0000E-02 -4.0000E-02 2.0000E-02 1.0000E+00 1.0000E+00 9: 1.4320E+00 9.8760E+00 -4.5670E+00 4.3500E-01 -7.3316E+00 10: 8.8130E+00 -1.3100E+00 0.0000E+00 1.4864E-01 0.0000E+00 11: 2.3000E+00 1.9917E+00 0.0000E+00 0.0000E+00 -8.6596E-01 12: 1.0000E+00 0.0000E+00 -1.0000E+00 1.0000E+00 -1.0000E+00 13: 1.0000E+00 0.0000E+00 1.0000E+00 1.0000E+00 -1.0000E+00 14: 9.0000E+00 0.0000E+00 3.6000E+01 2.0000E+00 -2.0000E+00 15: 1.0000E+00 2.0000E+00 5.0000E+00 -1.0000E+00 2.0000E+00 -1.0000E+00 -2.0000E+00 16: 5.3500E+02 2.2000E+01 1.5830E+03 -2.0561E-02 1.7200E+00 -2.0561E-02 -1.7200E+00 17: -9.0000E+00 2.3000E+01 3.7000E+01 -1.1189E+00 3.6744E+00 18: 6.1000E+01 2.0000E+00 8.7000E+01 -1.6393E-02 1.1941E+00 -1.6393E-02 -1.1941E+00 19: 6.1000E+01 1.5900E+02 8.7000E+01 -7.8145E-01 -1.8251E+00 20: 1.0000E+02 9.9000E+01 9.8000E+01 -4.9500E-01 8.5731E-01 -4.9500E-01 -8.5731E-01
END OF PROGRAM: 20 CARDS READ.
SJSU Dept. of Computer ScienceSpring 2014: February 3
CS 152: Programming Language Paradigms© R. Mak
10
Assignment #1, cont’d
Do not use modern FORTRAN features. Use only FORTRAN IV (also known as FORTRAN 66). A short reference:
http://www.math-cs.gordon.edu/courses/cps323/FORTRAN/fortran.html
A “C” in column 1 is for a comment. Statement numbers are restricted to
columns 1 through 5. Statements are in columns 7 through 72. A non-blank character in column 6 means what follows
is a continuation of the previous line. Keep your statements short and column 6 is always blank.
_
SJSU Dept. of Computer ScienceSpring 2014: February 3
CS 152: Programming Language Paradigms© R. Mak
11
Assignment #1, cont’d
There were no lower-case characters with punched cards, so everything is in upper case.
Variable names are limited to 6 characters.
You can declare variables with the INTEGER, REAL, DOUBLE PRECISION, and LOGICAL (boolean) statements. Example: INTEGER I, J, K
However, variable declarations are optional! By default, any variable that starts with the letters I through N are type integer, and the rest are type real. You still have to declare DOUBLE PRECISION and LOGICAL variables.
SJSU Dept. of Computer ScienceSpring 2014: February 3
CS 152: Programming Language Paradigms© R. Mak
12
Assignment #1, cont’d
The LOGICAL values are .TRUE. and .FALSE. (note the surrounding periods).
The relational operators are: .EQ. .NE. .LT. .LE. GT. .GE. .NOT. .AND. .OR. (more surrounding periods).
SJSU Dept. of Computer ScienceSpring 2014: February 3
CS 152: Programming Language Paradigms© R. Mak
13
Assignment #1, cont’d
There are two types of IF statements, the logical IF and the arithmetic IF.
The logical IF tests a logical (boolean) expression. You can have only one statement as the true part.
There is no else part. Examples:
IF (A .GT. B) A = 3.2 IF ((A .LE. B) .AND. (C .NE. 0.0)) GO TO 7
The arithmetic IF tests an arithmetic expression. Guess how the arithmetic IF works. Example: IF (A-B) 110, 201, 3
SJSU Dept. of Computer ScienceSpring 2014: February 3
CS 152: Programming Language Paradigms© R. Mak
14
Assignment #1, cont’d
There is also the computed GO TO statement, the precursor to the select or case statement in today's languages. Guess how this example works:
GO TO (43, 182, 672, 12, 8), K
You will undoubtedly stumble unto some unexpected syntax limitations. Remember, this is a nearly 60 years old language!
SJSU Dept. of Computer ScienceSpring 2014: February 3
CS 152: Programming Language Paradigms© R. Mak
15
Assignment #1, cont’d
Windows
Download the Open Watcom FORTRAN development environment:http://www.openwatcom.org/index.php/Main_Page
It implements FORTRAN 77, which is backwards compatible with FORTRAN IV._
SJSU Dept. of Computer ScienceSpring 2014: February 3
CS 152: Programming Language Paradigms© R. Mak
16
Assignment #1, cont’d
Mac
First, you must get gcc (the GNU C compiler) by downloading and installing the command-line tools for Xcode: http://www.mkyong.com/mac/how-to-install-gcc-compiler-on-mac-os-x/
Next, download g77-intel-bin.tar.gz (g77 3.4) from http://hpc.sourceforge.net/. It will install the FORTRAN compiler into /usr/local.
Then you should then be able to compile and run with the commands
g77 -o quadratic quadratic.for./quadratic < quadratic.in
SJSU Dept. of Computer ScienceSpring 2014: February 3
CS 152: Programming Language Paradigms© R. Mak
17
Assignment #1, cont’d
This is a team assignment. Each team turns in one assignment. Each member of the team will get the same score.
Email the source of the FORTRAN program and the text output to [email protected].
Subject line: CS 152 Assignment #1, team name Example: CS 152 Assignment #1, SuperCoders
Be sure to cc all members of your team so that when I send you your score, I can just do a “Reply all”._
SJSU Dept. of Computer ScienceSpring 2014: February 3
CS 152: Programming Language Paradigms© R. Mak
18
Programming Language Abstractions
Two types of programming language abstractions: Data abstraction Control abstraction
Data abstractions Simplify the behavior and attributes of data. Examples: numbers, character strings, search trees
Control abstractions Simplify properties of the transfer of control. Examples: loops, conditional statements, procedure calls
_
SJSU Dept. of Computer ScienceSpring 2014: February 3
CS 152: Programming Language Paradigms© R. Mak
19
Programming Language Abstractions, cont’d
Abstractions can also be categorized by levels. How much information is contained or hidden
in the abstraction? An orthogonal axis to data and control.
Basic abstractions Collect the most localized machine information.
Structured abstractions Collect intermediate information about
the structure of a program.
Unit abstractions Collect large-scale information in a program.
SJSU Dept. of Computer ScienceSpring 2014: February 3
CS 152: Programming Language Paradigms© R. Mak
20
Programming Language Abstractions, cont’d
data control
basic
structured
unit
basic data
structured data
unit data
basic control
structured control
unit control
TYPE
LEV
EL
SJSU Dept. of Computer ScienceSpring 2014: February 3
CS 152: Programming Language Paradigms© R. Mak
21
Basic Data Abstractions
Hide the internal representation of common data values. Values are also called “primitive” or “atomic”. The programmer normally cannot access the component parts or
bits of the internal representation.
Variables Symbolic names hide computer memory locations
containing data values.
Data types Names given to kinds of data values. Determine the valid operations on the data values.
Declarations Give variables their names and specify their data types.
_
SJSU Dept. of Computer ScienceSpring 2014: February 3
CS 152: Programming Language Paradigms© R. Mak
22
Structured Data Abstractions
Data structure Collects related data values into a single entity. Hides component parts. Parts can be accessed and modified.
Examples: An array is a sequence of individually indexed items
(all the same data type). An employee record contains name, address, phone, salary
(different data types). A text file is a sequence of characters transferred
to and from an external storage device._
SJSU Dept. of Computer ScienceSpring 2014: February 3
CS 152: Programming Language Paradigms© R. Mak
23
Unit Data Abstractions
Abstract data type (ADT) A set of data values and the operations on those values. Information hiding: How the values are stored and how the
operations are implemented.
Separate the interface from the implementation. Interface: The set of operations available to the user Implementation: The internal representation of the data values
and their operations.
Examples Java package Python module
SJSU Dept. of Computer ScienceSpring 2014: February 3
CS 152: Programming Language Paradigms© R. Mak
24
Unit Data Abstractions, cont’d
Data unit abstraction also provides reusability.
Typically, components are entered into a library. Applications can combine components into larger entities. Interoperability allows different applications
to share components.
An application programming interface (API) gives information about a library’s components._
SJSU Dept. of Computer ScienceSpring 2014: February 3
CS 152: Programming Language Paradigms© R. Mak
25
Basic Control Abstractions
Statements that combine a few machine instructions into an abstract statement that is easier to understand.
Example: sum = first + second What is abstracted away?
Syntactic sugar allows you to replace some notation with a simpler, shorthand notation.
Example: x += 10replaces: x = x + 10_
SJSU Dept. of Computer ScienceSpring 2014: February 3
CS 152: Programming Language Paradigms© R. Mak
26
Structured Control Abstractions
Divide a program into groups of instructions nested within tests that govern their execution.
Express the logic of primary control structures of sequencing, selection, and iteration._
SJSU Dept. of Computer ScienceSpring 2014: February 3
CS 152: Programming Language Paradigms© R. Mak
27
Structured Control Abstractions, cont’d
Example: A Java for loop operating on an array:
Use of an iterator and the syntactic sugar of a Java enhanced for loop:
SJSU Dept. of Computer ScienceSpring 2014: February 3
CS 152: Programming Language Paradigms© R. Mak
28
Structured Control Abstractions, cont’d
Procedures AKA subroutines, subprograms
Functions Procedures that return values
Higher-order functions Functions that can accept other functions as arguments
and return functions as values. Combine functions into higher-level abstractions. Example: The Lisp map and reduce functions:
(reduce + (map abs (list 33 -10 66 68 -4)))
(33 10 66 68 4)
201
(map abs (list 33 -10 66 68 -4))
SJSU Dept. of Computer ScienceSpring 2014: February 3
CS 152: Programming Language Paradigms© R. Mak
29
Unit Control Abstractions
Unit A standalone collection of procedures. Provides logically related services to other parts of a program. Allows a program to be understood as a whole.
Don’t need to know the details of the services provided by the unit.
Threads Separately executed control paths within the Java system. Threads must be synchronized.
Processes Other programs executing outside the Java system. Interprocess communication Process synchronization
_
SJSU Dept. of Computer ScienceSpring 2014: February 3
CS 152: Programming Language Paradigms© R. Mak
30
Computational Paradigms
Paradigm: a pattern or model
Imperative paradigm Instructions executed sequentially. Variables represent memory locations. Assignment statements change the values of variables.
The von Neumann bottleneck A program must be described as a sequence of instructions. Restricts the ability of a programming language to provide:
parallel computation nondeterministic computation (does not depend on order)
_
SJSU Dept. of Computer ScienceSpring 2014: February 3
CS 152: Programming Language Paradigms© R. Mak
31
Computational Paradigms
Functional paradigm Based on the abstract notion of a function in the
lambda calculus.
Logic paradigm Based on symbolic logic.
Both functional and logic paradigms correspond to mathematical foundations. Easier to determine if a program will execute correctly.
Object-oriented paradigm Reusable code that operates in a way that
mimic the behaviors of real-world objects.
SJSU Dept. of Computer ScienceSpring 2014: February 3
CS 152: Programming Language Paradigms© R. Mak
32
Language Definition
How can we formally define a programming language?
Grammar What sequence of characters is correct within the language? Examples: What are valid expressions?
Assignment statements? If statements? How can we specify the language’s syntax.
Meaning and behavior What does an assignment statement mean? How does an if statement behave at run time? How can we specify the language’s semantics.
_
SJSU Dept. of Computer ScienceSpring 2014: February 3
CS 152: Programming Language Paradigms© R. Mak
33
Language Definition
Formal language definition allows you to:
Reason mathematically about programs.
Promote standardization for machine or implementation independence.
Define program behavior and interaction.
Ensure discipline when a language is designed._
SJSU Dept. of Computer ScienceSpring 2014: February 3
CS 152: Programming Language Paradigms© R. Mak
34
Language Syntax
Grammar: The formal definition of a language’s syntax. Similar to the grammar of a natural language. Precisely specifies what are valid constructs in the language.
Tokens: Elements of the language identifiers numbers reserved words: if else for while do etc. special symbols: + - = ( ) >= etc.
Lexical structure Similar to spelling rules in a natural language. The rules that define what are valid language tokens.
SJSU Dept. of Computer ScienceSpring 2014: February 3
CS 152: Programming Language Paradigms© R. Mak
35
Language Syntax
Example: Syntax of the Java if statement:
This syntax rule is written in English. Later, we’ll use a more precise notation called
Backus Naur Form (BNF)._
An if statement consists of the reserved word iffollowed by a left parenthesis followed by anexpression followed by a right parenthesisfollowed by a statement. Optionally, this is
followed by the else part which consists of thereserved word else followed by a statement.
SJSU Dept. of Computer ScienceSpring 2014: February 3
CS 152: Programming Language Paradigms© R. Mak
36
Language Semantics
Semantics: The meaning of a language’s statements. What is the effect of executing the statement at run time? It can be difficult to provide a comprehensive description
of meaning in all contexts.
Example: Semantics of the Java if statement:
First evaluate the expression following thereserved word if. If the expression’s value is true,
then execute the statement following the expression,otherwise skip over the statement. If there is anelse part, execute the statement following the
reserved word else if the expression’s value is false,otherwise skip over that statement.
SJSU Dept. of Computer ScienceSpring 2014: February 3
CS 152: Programming Language Paradigms© R. Mak
37
Language Semantics
There isn’t a generally accepted formal method for describing semantics.
Several notational systems have been developed:
Operational semantics Denotational semantics Axiomatic semantics
_
SJSU Dept. of Computer ScienceSpring 2014: February 3
CS 152: Programming Language Paradigms© R. Mak
38
Syntax and Semantics
How are a language’s syntax and semantics used?
A well-specified syntax allows programs in the language to be automatically read and translated.
Well-understood semantics allows the program to be translated into a equivalent program. Equivalent programs have similar runtime behaviors.
_