Administrivia • • Comments about assignment 5 • Assignment 6

Download Report

Transcript Administrivia • • Comments about assignment 5 • Assignment 6

Administrivia
•
•
•
•
First pieces of assignment 5
Comments about assignment 5
Assignment 6
Labs 5 and 6
Some bytes
• 0000000 5089 474e 0a0d 0a1a
0000 0d00 4849 5244
• 0000020 0000 9001 0000 2c01
0208 0000 6200 72d5
• 0000040 0095 2000 4900 4144
7854 ccda 79fd 24d4
• 0000000 5089 474e 0a0d 0a1a
0000 0d00 4849 5244
• 0000020 0000 9001 0000 2c01
0208 0000 6200 72d5
• 0000040 0095 2000 4900 4144
7854 d4da 61fd 648c
Object oriented programming
• Figure out characteristics of your data
– objects
• Figure out operations you will want to
perform
– Methods
• Modern idea in programming.
Objects
• Traffic light at intersection involves
– Lights in each direction
• Call them red, yellow and green and not 0,1,2
– Sensors in each direction
– Timing (rate of change in each direction)
• Timings needn’t be the same
– Neighboring Lights
• May affect change as much as sensors
Methods
•
•
•
•
Method of querying color of light
Method of changing color of light
Method of scheduling a color change later
…
What happens to the program?
• Compiled or interpreted
– Eventually it gets translated into machine
language
• If compiled
– Can store executable and run again
• If interpreted
– Interpret each time it is executed
What does the compiler do?
• Identifies variables (need space in RAM)
– Uses stores and loads to get values to registers
• Parses commands
– Turns each command into a string of machine
language commands
• Sets things up for execution
Steps in compilation
• Lexical analysis
–
–
–
–
Identify all keywords
Identify all operators
Identify all variables
Make everything into tokens
• Parsing
– Turn the tokens into operations
– Build a computation tree
• Code generation
– Generate machine code
Lexical analysis
• Keywords
–
–
–
–
–
–
If … Then .. End If
If .. Then .. Else … End If
Do While … … Loop
Private sub ….
End sub
Dim … as Integer
• Operators
– = (in 2 contexts)
• If (Light = 0)
• Light = 1
– +-*/
• - also in 2 contexts (unary or binary)
Simple code fragment
Sub print_ftoc(low As Integer, high As Integer)
Dim fahrenheit As Double, celsius As Double
For fahrenheit = low to high
celsius = 5 / 9 * (fahrenheit - 32)
print fahrenheit, celsius
Next fahrenheit
End Sub
Simplified code fragment
Dim low As Integer, high As Integer
Dim fahrenheit As Double, celsius As Double
For fahrenheit = low to high
celsius = 5 / 9 * (fahrenheit - 32)
print fahrenheit, celsius
Next fahrenheit
End
Code fragment (lex’ed)
Dim low As Integer, high As Integer
Dim fahrenheit As Double, celsius As Double
For fahrenheit = low to high
celsius = 5 / 9 * (fahrenheit - 32)
print fahrenheit , celsius
Next fahrenheit
End
Keywords, variables, constants, operators, functions,
separators
Code fragment (cont.)
Dim <tag1> As Integer, <tag2> As Integer
Dim <tag3> As Double, <tag4> As Double
For <tag3> = <tag1> to <tag2>
<tag4> = 5 / 9 * (<tag3> - 32)
print <tag3> , <tag4>
Next <tag3>
End
Replace variables by tags
these are really locations in RAM
How things are defined determines
how much RAM they need
how operations on them work
Code fragment (cont.)
For <tag3> = <tag1> to <tag2>
<tag4> = 5 / 9 * (<tag3> - 32)
print <tag3> , <tag4>
Next <tag3>
The instructions in the loop must be unwound
<tag3> = <tag1>
<tag4> = 5 / 9 * (<tag3> - 32)
print <tag3> , <tag4>
<tag3> = <tag3> + 1
If <tag3> > <tag2> go back
The unwound loop can be translated into machine language
<tag3> = <tag1>
<tag4> = 5 / 9 * (<tag3> - 32)
print <tag3> , <tag4>
<tag3> = <tag3> + 1
If <tag3> > <tag2> go back
Store 32 in R3
Store 5/9 in R4
Store 1 in R5
Load <tag1> into R1
***Store R1 into <tag3>
Load <tag3> into R2
Subtract R3 from R2 and store in R2
Multiply R4 by R2 and store in R2
Store R2 in <tag4>
Print R1,R2
Add R5 to R1 and store in R1
Store R5 in <tag3>
Load <tag2> into R6
Subtract R6 from R5 and store in R5
Go back to *** if R6 > 0
Parsing
• Language is defined by a grammar
• Grammar is defined by production rules
• Parsing is done by unwinding
How do we specify a grammar?
• 2 aspects to a language
– Symbols
– Rewriting rules
• Simple language for generating numbers
– Symbols
• Non-terminals
– <number>, <digits>, <sign>, <digit>
• Terminals
– +-.123456789
Simple rewriting rules
•
•
•
•
<number>  <sign> <digit><digits> . <digits>
<sign>  + | <digits>  <digit><digits> | <digit> | e
<digit>  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
An example
•
•
•
•
<number>  <sign> <digit><digits> . <digits>
<sign>  + | <digits>  <digit><digits> | <digit> | e
<digit>  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
<number> 
<sign><digit><digits>.<digits> 
<sign><digit><digit><digits>.<digits> 
<sign><digit><digit>.<digits> 
<sign><digit><digit>.<digit><digit> 
…
+98.65
Simplifying the rules
•
•
•
•
<number>  <sign> <digit><digits> . <digits>
<sign>  + | <digits>  <digit><digits> | <digit> | e
<digit>  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
•
•
•
•
<number>  <sign><digits>.<digits>| <sign><digits>
<sign>  +|<digits>  <digit><digits> | <digit>
<digit>  0|1|2|3|4|5|6|7|8|9
Parsing
•
•
•
•
<number>  <sign><digits>.<digits>| <sign><digits>
<sign>  +|<digits>  <digit><digits> | <digit>
<digit>  0|1|2|3|4|5|6|7|8|9
What rules were applied to get 123.45?
What about real languages?
• The complete grammar for C
– around 400 lines long
– 58 tokens (based on keywords)
– 65 basic productions (each with many options)
– Only a few complex situations
Programming language tradeoffs
• Branching vs. locality
– Should the program be in blocks or look like spaghetti
• Type declarations
– If you Dim something as an integer and then try to
make it hold a double, what should happen?
• Verification
– How do you tell if your specification is right?
– How do you tell if your program meets your
specification?
History of Programming Languages
• Fortran (1954) for scientific
• Cobol (1959) for business
• Algol (1958) more universal Fortran
• Lisp (1958) string/concept oriented
• APL (1960) formula oriented
History of Programming Languages
• PL/1 (1964) from Algol + Fortran
• Basic (1964) for everyone to use
• Simula (1967) combines with Algol to yield
Smalltalk (1969) – object oriented
• BCPL  B  C (1971)
• Algol  Pascal (1971)  Modula 1,2,3,
History of Programming Languages
• C++ (1983) C with object oriented features
– Often C is still used
• Awk (1978)  Perl (1987) report generators
– Web programming language
• Java (1991) object oriented and portable
– Web applets, devices
• Visual Basic(1991) macros and programs
– Core of Microsoft systems
What makes a good language
•
•
•
•
Does the task you want
Keeps you from making mistakes
Supports debugging when you need it
Has a strong tool kit
Big number bug
On June 4, 1996 an unmanned Ariane 5 rocket launched by the European
Space Agency exploded just forty seconds after its lift-off from Kourou,
French Guiana. The rocket was on its first voyage, after a decade of
development costing $7 billion. The destroyed rocket and its cargo were
valued at $500 million. A board of inquiry investigated the causes of the
explosion and in two weeks issued a report. It turned out that the cause of
the failure was a software error in the inertial reference system.
Specifically a 64 bit floating point number relating to the horizontal
velocity of the rocket with respect to the platform was converted to a 16
bit signed integer. The number was larger than 32,768, the largest integer
storeable in a 16 bit signed integer, and thus the conversion failed.
Pentium II bug
• Software bug encoded in hardware
• Division algorithm uses a lookup table of 1066
entries
• Only 1061 of the entries are downloaded to the
PLA (programmed logic array from which the data
are used)
•
• Intel had to recall all versions of the chip
• NASA Mariner 1 , Venus probe (1992)
• Intended to be the first US spacecraft to visit
another planet, it was destroyed by a range
officer on 22 July 1962 when it behaved
erratically four minutes after launch.
– The alleged missing `hyphen' was really a missing
`bar'.
– (period instead of comma in FORTRAN DO-Loop)
• AT&T long distance service fails for nine hours
(Wrong BREAK statement in C-Code, 1990)
• January 15, 1990:
• 70 million of 138 million long distance customers
in the US lost long distance service.
• Cost to ATT was between $ 75 Million and $100
Million (plus the loss of good will).
• E-mail buffer overflow (1998)
• Several E-mail systems suffer from a "buffer
overflow error", when extremely long e-mail
addresses are received. The internal buffers
receiving the addresses do not check for
length and allow their buffers to overflow
causing the applications to crash. Hostile
hackers use this fault to trick the computer
into running a malicious program in its place.
Everything has bugs
• Bug lists
Summary
• Programming is hard
–
–
–
–
Have to thoroughly understand the task
Have to anticipate all possibilities
Code is written at a fairly primitive level
Is impossible to anticipate what users might do
• Programming languages allow the user to use
tools to build things
• The cost of a bug can be very large
• There is no Moore’s Law for software.
Where are we
•
•
•
•
We’ve built a computer
We’ve looked at operating systems
We’ve looked at the network
We’ve built programs
– And looked under the hood
What’s next
• One more piece of networking
– Sharing files, sharing cycles, distributed computing
• Algorithms
– Ideas of how to design processes
• Complexity theory
– Undecidable problems
– Unsolvable (in practice) problems
• Applications of hard problems
• Social impacts
– Digital rights management
– Artificial intelligence