+ All Categories
Home > Documents > Writing a Program in a High- level Language Figure out what you want to do –Understand the rules...

Writing a Program in a High- level Language Figure out what you want to do –Understand the rules...

Date post: 19-Dec-2015
Category:
View: 214 times
Download: 0 times
Share this document with a friend
Popular Tags:
57
Writing a Program in a High-level Language • Figure out what you want to do – Understand the rules that guide the process you are automating • Make sure that your rules are complete • Translate the rules into the computer language – Build structures to hold your data – Build tools to manipulate the structures • Make sure that the program does what the rules say
Transcript
Page 1: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

Writing a Program in a High-level Language

• Figure out what you want to do– Understand the rules that guide the process you

are automating• Make sure that your rules are complete

• Translate the rules into the computer language– Build structures to hold your data– Build tools to manipulate the structures

• Make sure that the program does what the rules say

Page 2: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

Elements of Programming

• We looked at machine language– Single instructions that tell the hardware what to do

– Primitive• Arithmetic, simple branching, communication with memory

• We built state machines– States using memory– Transitions modeling tasks– A “hardwired” program

Page 3: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

Elements of Programming

• We’ve seen– Truth tables– Logic gates– States and transitions in a state machine– Machine language

• Now, higher level programming language

Page 4: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

To build a computer program

• Figure out what you want to do– Understand the rules that guide the process you

are automating• Make sure that your rules are complete

• Translate the rules into the computer language– Build structures to hold your data– Build tools to manipulate the structures

• Make sure that the program does what the rules say

Page 5: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

Figuring out the rules

• For traffic lights:– We stored data that told us the current color of lights

– We read input from sensors

– We had rules that told us whether to change state

– We had rules that told us how to change state

Page 6: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

Light

ATraffic Light Behavior

IF A=1

AND B=0

Always

IF A=0

AND B=1

Otherwise

Light BOtherwise

Always

Page 7: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

Turn Memory, Inputs and Outputs Into Variables

• Store data to tell current color of lights– Dim LightA, LightB as Integer

• 0 for red, 1 for yellow, 2 for green

• Read input from sensors– Dim SensorA, SensorB as Integer

• tell if cars are waiting

Page 8: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

Turn Rules Into Statements

• Decide whether to change state– If LightA = 0 And LightB = 2 And SensorA = 1 And

SensorB = 0 Then

• here we want to specify that the colors change

– If LightA = 2 And LightB= 0 And SensorA = 0 And SensorB = 1 Then

• again, we want to specify that the colors change

Page 9: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

Build shell of program

Dim LightA, LightB as Integer Dim SensorA, SensorB as Integer If LightA = 2 And LightB = 0 And SensorA = 0 And SensorB = 1 Then

ChangeGreenToYellow(LightA)ChangeYellowToRed(LightA)ChangeRedToGreen(LightB)

If LightA = 0 and LightB = 2 And SensorA = 1 And SensorB = 0 ThenChangeGreenToYellow(LightB)ChangeYellowToRed(LightB)ChangeRedToGreen(LightA)

Page 10: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

Some Rules

• Statements have to be in blocks• How does the computer know that the instructions are

If LightA = 2 And LightB = 0 And SensorA = 0 And SensorB = 1 ThenChangeGreenToYellow(LightA)ChangeYellowToRed(LightA)ChangeRedToGreen(LightB)

• And not …If LightA = 2 And LightB = 0 And SensorA = 0 And SensorB = 1 Then

ChangeGreenToYellow(LightA)

ChangeYellowToRed(LightA)ChangeRedToGreen(LightB)

Page 11: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

Some Rules

• Statements have to be in blocksIf LightA = 2 And Light B = 0 And SensorA = 0 And

SensorB = 1 Then

ChangeGreenToYellow(LightA)

ChangeYellowToRed(LightA)

ChangeRedToGreen(LightB)

End If

Page 12: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

More rules: Loops

• Don’t want just a single state change

• Program should run ‘forever,’until we want it to stop

• Can do this with a “while” loop, which is governed by a termination/continuation condition

Page 13: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

More Rules

• We have to tell the program to loopDo While condition

action Loop

Page 14: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

More Rules

• We have to tell the program to loop

Do While StillWantToControlTraffic RunMyTrafficControlProgram

Loop

Page 15: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

Procedures

• We said: ChangeGreenToYellow (LightA)• Could also say: ChangeGreenToYellow(LightB)• Must write a procedure to change lights

Private Sub ChangeGreenToYellow(Light As Integer)

Light = 1

End Sub

• When called with an argument, such as LightA, substitutes it for “Light” parameter internally– So effect is to set LightA to 1

Page 16: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

Procedures

• SimilarlyPrivate Sub ChangeYellowToRed(Light As Integer)

Light = 2

End Sub

Private Sub ChangeRedToGreen(Light As Integer)

Light = 0

End Sub

• Procedure parameters and runtime arguments

Page 17: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

Could build Procedure of Procedures

ChangeGreenToYellow(LightA)ChangeYellowToRed(LightA)ChangeRedToGreen(LightB)

Could become the command ChangeLights(LightA,LightB)

Private Sub ChangeLights(Light1 As Integer, Light2 As Integer)

ChangeGreenToYellow(Light1)

ChangeYellowToRed(Light1)ChangeRedToGreen(Light2)

End Sub

Page 18: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

Using the procedure

ChangeLights(LightB,LightA) then does

ChangeGreenToYellow(LightB)ChangeYellowToRed(LightB)ChangeRedToGreen(LightA)

i.e. Light1 is LightB, and Light2 is LightA

Similarly, ChangeLights(LightA, LightC) does

ChangeGreenToYellow(LightA)ChangeYellowToRed(LightA)ChangeRedToGreen(LightC)

Page 19: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

The Program: Procedure Definitions

Private Sub ChangeGreenToYellow(Light As Integer)Light = 1

End SubPrivate Sub ChangeYellowToRed(Light As Integer)

Light = 2End SubPrivate Sub ChangeRedToGreen(Light As Integer)

Light = 0End SubPrivate Sub ChangeLights(Light1 As Integer, Light2 As Integer)

ChangeGreenToYellow(Light1)

ChangeYellowToRed(Light1)ChangeRedToGreen(Light2)

End Sub

Page 20: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

The Program (cont.)

Dim LightA, LightB as Integer

Dim SensorA, SensorB as Integer

If LightA = 2 And LightB = 0 And SensorA = 0 And SensorB = 1 Then

ChangeLights(LightA,LightB)

End If

If LightA = 0 And LightB = 2 And SensorA = 1 And SensorB = 0 Then

ChangeLights(LightB,LightA)

Page 21: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

Make it happen forever

Dim LightA, LightB as Integer

Dim SensorA, SensorB as Integer

Dim StillWantToControlTraffic as Integer

StillWantToControlTraffic = 1

Do While StillWantToControlTraffic If LightA = 2 And LightB = 0 And SensorA = 0 And SensorB = 1 Then

ChangeLights(LightA,LightB)

End If

If LightA = 0 And LightB = 2 And SensorA = 1 And SensorB = 0 Then

ChangeLights(LightB,LightA)

End If

Loop

Page 22: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

What could go wrong?• Program could get confused

– Check for consistency

• ReplacePrivate Sub ChangeGreenToYellow(Light As Integer)

Light = 1End Sub

• WithPrivate Sub ChangeGreenToYellow(Light As Integer)

If (Light = 0) ThenLight = 1

ElseReportInconsistency()

End IfEnd Sub

Page 23: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

Building a bigger program

• Could write this as a subroutine– Private sub

ControlTrafficLight(light1,light2,sensor1,sensor2)

• Could reuse the subroutine to do a whole string of lights.

• But how would we keep track of hundreds of lights?

Page 24: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

Arrays

• Build arrays– LightNS[1], LightNS[2], LightNS[3], …– LightEW[1]. LightEW[2]. LightEW[3], …– SensorNS[1], SensorNS[2], SensorNS[3], …– SensorEW[1], SensorEW[2], SensorEW[3], …

Page 25: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

Arrays (con’t).

• Access particular lights/sensors in array:ControlTrafficLight(lightNS[i],lightEW[i],sensorNS[i],sensorEW[i])

• Control 100 traffic lights:For i = 1 To 100

ControlTrafficLight(lightNS[i],lightEW[i],sensorNS[i],sensorEW[i]) Next i

• But:– Lights may want to communicate

– Lights affected by neighboring lights, not just sensors

Page 26: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

Object oriented programming

• Figure out characteristics of your data– Objects

• Figure out operations you’ll want to perform – Methods

• A modern programming style

Page 27: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

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– Neighboring Lights

• May affect change as much as sensors

– …

Page 28: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

Methods

• Method of querying color of light

• Method of changing color of light

• Method of scheduling a color change later

• …

Page 29: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

What happens to the program?

• It is either compiled or interpreted– Eventually it gets translated into machine

language

• If compiled– Can store executable and run again

• If interpreted– Interpret from original language each time it is

executed

Page 30: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

What does the compiler do?

• Identifies variables– These need space in RAM– Uses stores and loads to get values to registers

• Parses commands– Turns each program statement into a string of

machine language commands

• Sets things up for execution

Page 31: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

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

Page 32: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

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)

Page 33: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

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

Page 34: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

Code fragment (lex’ed)

Dim low As Integer, high As IntegerDim fahrenheit As Double, celsius As DoubleFor fahrenheit = low to high

celsius = 5 / 9 * (fahrenheit - 32) print fahrenheit , celsius Next fahrenheit

End

Keywords, variables, constants, operators, functions, separators

Page 35: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

Code fragment (cont.)

Dim <tag1> As Integer, <tag2> As IntegerDim <tag3> As Double, <tag4> As DoubleFor <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 (i.e. the types of the variables) determines:how much RAM they needhow operations on them work

Page 36: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

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> + 1If <tag3> <= <tag2> go back

Page 37: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

<tag3> = <tag1><tag4> = 5 / 9 * (<tag3> -

32) print <tag3> , <tag4> <tag3> = <tag3> + 1If <tag3> <= <tag2> go

back

The unwound loop can be translated into machine language

Store 32 in R3Store 5/9 in R4Store 1 in R5Load <tag1> into R1L1: Store R1 into <tag3>Load <tag3> into R2Subtract R3 from R2 and store in R2Multiply R4 by R2 and store in R2

Store R2 in <tag4>Print R1,R2Add R5 to R1 and store in R1Store R5 in <tag3>Load <tag2> into R6Subtract R6 from R5 and store in R5Go back to L1 if R6 > 0

Page 38: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

How is the unwinding etc done: Parsing

• Language is defined by a grammar

• Grammar is defined by production rules

• Parsing is done by unwinding

• Grammars and rules for parsing languages are complex, so let’s look at something simpler: A grammar for generating numbers (instead of computer programs)

Page 39: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

How do we specify a grammar?

• 2 aspects to a language– Symbols– Rewriting rules (also called productions)

• Simple language for generating numbers– Symbols

• Non-terminals– <number>, <digits>, <sign>, <digit>

• Terminals– + - . 1 2 3 4 5 6 7 8 9

Page 40: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

Simple rewriting rules

• <number> <sign> <digit><digits> . <digits>• <sign> + | -• <digits> <digit><digits> | <digit> | • digit

Page 41: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

An example• <number> <sign> <digit><digits> . <digits>• <sign> + | -• <digits> <digit><digits> | <digit> | • digit

<number> <sign><digit><digits>.<digits> <sign><digit><digit><digits>.<digits> <sign><digit><digit>.<digits> <sign><digit><digit>.<digit><digits> <sign><digit><digit>.<digit><digit> …+98.65

Page 42: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

Alternative rules

• <number> <sign> <digit><digits> . <digits>• <sign> + | -• <digits> <digit><digits> | <digit> | • digit

• <number> <sign><digits>.<digits>| <sign><digits>

• <sign> +|-

• <digits> <digit><digits> | <digit>

• <digit> 0|1|2|3|4|5|6|7|8|9

Page 43: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

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?

Page 44: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

What about real languages?

• The complete grammar for C – about 400 lines long– 58 tokens (based on keywords)– 65 basic productions (each with many options)

– Only a few complex situations

Page 45: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

Some programming language issues

• 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?

Page 46: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

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

Page 47: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

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,

Page 48: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

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

Page 49: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

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

Page 50: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

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 that could be stored in a 16 bit signed integer, and so the conversion failed.

Page 51: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

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

Page 52: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

Syntax “typo” bugs

• 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. – Essentially a period instead of a comma in a

FORTRAN DO-Loop

Page 53: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

Control flow bug

• AT&T long distance service fails for nine hours(Wrong BREAK statement in C code)

• 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).

Page 54: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

• 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.

Data structure management bug

Page 55: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

Summary• Programming is difficult

– Have to thoroughly understand the task– Have to anticipate all possibilities– Code is written at a fairly primitive level– Impossible to anticipate what users might do

• Programming languages allow the user to use tools to build code

• But everything still has bugs• The cost of a bug can be very large

• There is no Moore’s Law for software.

Page 56: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

Where are we

• We’ve built a computer

• We’ve built programs– And looked under the hood

Page 57: Writing a Program in a High- level Language Figure out what you want to do –Understand the rules that guide the process you are automating Make sure that.

What’s next• Algorithms• Networking: The Internet, Email, the Web Operating Systems (Mon,

Apr 7)• Sound and Graphics (Wed, Apr 9 and Mon, Apr 14, resp.)• Distributed Systems (Wed, Apr 16)

– Sharing files, sharing cycles, distributed computing

• Complexity theory– Undecidable problems, unsolvable (in practice) problems

• Applications of hard problems• Social impacts

– Digital rights management– Access to information (Digital Divide)– Artificial intelligence


Recommended