Programming Languages

82
1 Programming Languages Programming Languages Shyh-Kang Jeng Shyh-Kang Jeng Department of Electrical Engineering/ Department of Electrical Engineering/ Graduate Institute of Communication Graduate Institute of Communication Engineering Engineering National Taiwan University National Taiwan University

description

Programming Languages. Shyh-Kang Jeng Department of Electrical Engineering/ Graduate Institute of Communication Engineering National Taiwan University. Generations of programming languages. Machine Instructions and Mnemonic Techniques. 156C 166D 5056 306E C000. LD R5, PRICE LD R6, TAX - PowerPoint PPT Presentation

Transcript of Programming Languages

Page 1: Programming Languages

11

Programming LanguagesProgramming Languages

Shyh-Kang JengShyh-Kang JengDepartment of Electrical Engineering/Department of Electrical Engineering/

Graduate Institute of Communication Graduate Institute of Communication EngineeringEngineering

National Taiwan UniversityNational Taiwan University

Page 2: Programming Languages

22

Generations of programming Generations of programming languageslanguages

Page 3: Programming Languages

33

Machine Instructions and Machine Instructions and Mnemonic TechniquesMnemonic Techniques

156C166D5056306EC000

LD R5, PRICELD R6, TAXADDI R0, R5 R6ST R0, TOTALHLT

Page 4: Programming Languages

44

Assemblers and Assembly Assemblers and Assembly LanguagesLanguages

Programs to translate other programs Programs to translate other programs written in mnemonic form into machine written in mnemonic form into machine compatible formcompatible form

Assemble machine instructions out of Assemble machine instructions out of the op-codes and operands obtained by the op-codes and operands obtained by translating mnemonics and identifierstranslating mnemonics and identifiers

Mnemonic systems for representing Mnemonic systems for representing programs are recognized as programs are recognized as programming languages called programming languages called assembly languagesassembly languages

Page 5: Programming Languages

55

Assembly LanguagesAssembly Languages

Second-generation languageSecond-generation language Machine dependentMachine dependent

Must conform to the machine’s register Must conform to the machine’s register configuration and instruction setconfiguration and instruction set

Low level primitivesLow level primitives A programmer is still forced to think in A programmer is still forced to think in

terms of the small, incremental steps of terms of the small, incremental steps of the machine’s languagethe machine’s language

Page 6: Programming Languages

66

Design Process of a ProductDesign Process of a Product

Better suited to the use of high-level Better suited to the use of high-level primitives, each representing a primitives, each representing a concept associated with a major concept associated with a major feature of the productfeature of the product

Once the design is complete, the Once the design is complete, the primitives can be translated to lower-primitives can be translated to lower-level concepts relating to the details level concepts relating to the details of implementationof implementation

Page 7: Programming Languages

77

Third-Generation LanguagesThird-Generation Languages High-level primitivesHigh-level primitives Machine independent if standard is foMachine independent if standard is fo

llowedllowed FORTRANFORTRAN

FORmula TRANslatorFORmula TRANslator COBOLCOBOL

COmmon Business-Oriented LanguageCOmmon Business-Oriented Language BASICBASIC

Page 8: Programming Languages

88

Compilers vs. InterpretersCompilers vs. Interpreters CompilersCompilers

Compile several machine instructions into Compile several machine instructions into short sequences to simulate the activity short sequences to simulate the activity requested by a single high-level primitiverequested by a single high-level primitive

Produce a machine-language copy of a Produce a machine-language copy of a program that would be executed laterprogram that would be executed later

InterpretersInterpreters Execute the instructions as they were Execute the instructions as they were

translatedtranslated

Page 9: Programming Languages

99

Machine Independence and Machine Independence and BeyondBeyond

Restrictions of the machinesRestrictions of the machines Size of registers and memory cellsSize of registers and memory cells

Dialects and standard languagesDialects and standard languages ANSI (American National Standards ANSI (American National Standards

Institute) standardInstitute) standard ISO (International Organization of ISO (International Organization of

Standardization) standardStandardization) standard Language extensionsLanguage extensions

Page 10: Programming Languages

1010

Evolution of Programming Evolution of Programming ParadigmsParadigms

Page 11: Programming Languages

1111

Imperative ParadigmImperative Paradigm Procedural paradigmProcedural paradigm Develops a sequence of commands Develops a sequence of commands

that when followed, manipulate that when followed, manipulate data to produce the desired resultdata to produce the desired result

Approaches a problem by trying to Approaches a problem by trying to find an algorithm for solving itfind an algorithm for solving it

Page 12: Programming Languages

1212

Declarative ParadigmDeclarative Paradigm EmphasizesEmphasizes

““What is the problem?”What is the problem?” Rather than “What algorithm is required Rather than “What algorithm is required

to solve the problem?”to solve the problem?” Implemented a general problem-Implemented a general problem-

solving algorithmsolving algorithm Develops a statement of the problem Develops a statement of the problem

compatible with the algorithm and compatible with the algorithm and then applies the algorithm to solve itthen applies the algorithm to solve it

Page 13: Programming Languages

1313

Functional ParadigmFunctional Paradigm Views the process of program Views the process of program

development as connecting development as connecting predefined “black boxes,” each of predefined “black boxes,” each of which accepts inputs and produces which accepts inputs and produces outputsoutputs

Mathematicians refer to such Mathematicians refer to such “boxes” as functions“boxes” as functions

Constructs functions as nested Constructs functions as nested complexes of simpler functionscomplexes of simpler functions

Page 14: Programming Languages

1414

Functional Paradigm Functional Paradigm ExampleExample

Page 15: Programming Languages

1515

Examples of LISP Examples of LISP ExpressionsExpressions

(Find_diff (Find_diff

(Find_Sum Old_balance Credits) (Find_Sum Old_balance Credits) (Find_Sum Debits))(Find_Sum Debits))

(First (Sort List))(First (Sort List))

Page 16: Programming Languages

1616

Advantages of Functional Advantages of Functional ParadigmParadigm

Constructing complex software from Constructing complex software from predefined primitive functions leads to predefined primitive functions leads to well-organized systemswell-organized systems

Provides an environment in which Provides an environment in which hierarchies of abstraction are easily hierarchies of abstraction are easily implemented, enabling new software implemented, enabling new software to be constructed from large to be constructed from large predefined components rather than predefined components rather than from scratchfrom scratch

Page 17: Programming Languages

1717

Object-Oriented ParadigmObject-Oriented Paradigm OOPOOP Encapsulation of data and Encapsulation of data and

proceduresprocedures Lists and sortingLists and sorting Graphic user interface (GUI)Graphic user interface (GUI)

Natural modular structure and Natural modular structure and program reuseprogram reuse

Communication between modules is Communication between modules is accomplished by passing messagesaccomplished by passing messages

CORBA and software componentsCORBA and software components

Page 18: Programming Languages

1818

Statements in Programming Statements in Programming LanguagesLanguages

Declarative statementsDeclarative statements Define customized terminology that is Define customized terminology that is

used later in the programused later in the program Imperative statementsImperative statements

Describe steps in the underlying Describe steps in the underlying algorithmsalgorithms

CommentsComments Enhance the readability of a programEnhance the readability of a program

Page 19: Programming Languages

1919

Composition of a Typical Composition of a Typical Imperative Program or Imperative Program or

Program UnitProgram Unit

Page 20: Programming Languages

2020

Variables, Literals, Variables, Literals, ConstantsConstants

EffectiveAlt EffectiveAlt Altimeter + 645 Altimeter + 645 VariablesVariables

Example: EffectiveAlt, AltimeterExample: EffectiveAlt, Altimeter LiteralsLiterals

Example: 645Example: 645 ConstantsConstants

Example: const int AirportAlt = 645;Example: const int AirportAlt = 645;

Page 21: Programming Languages

2121

Data TypeData Type

Refers toRefers to Interpretation of dataInterpretation of data Operations that can be performed on Operations that can be performed on

the datathe data Common typesCommon types

integer, real, character, Booleaninteger, real, character, Boolean

Page 22: Programming Languages

2222

Variable DeclarationsVariable Declarations PascalPascal

Length, width: real;Length, width: real;Price, Tax, Total: integer;Price, Tax, Total: integer;

C, C++, Java, C#C, C++, Java, C#float Length, width;float Length, width;int Price, Tax, Total;int Price, Tax, Total;

FORTRANFORTRANREAL Length, WidthREAL Length, WidthINTEGER Price, Tax, TotalINTEGER Price, Tax, Total

Page 23: Programming Languages

2323

Data StructureData Structure

Conceptual shape of dataConceptual shape of data Common data structureCommon data structure

Homogeneous arrayHomogeneous array Heterogeneous arrayHeterogeneous array

Page 24: Programming Languages

2424

Declaration of a 2D ArrayDeclaration of a 2D Array

CCint Scores[2][9];int Scores[2][9];

JavaJavaint Scores[][]=new int [2][9];int Scores[][]=new int [2][9];

PascalPascalScores: array[3..4, 12..20] of integeScores: array[3..4, 12..20] of intege

r;r;

Page 25: Programming Languages

2525

A two-dimensional arrayA two-dimensional array

Page 26: Programming Languages

2626

Declaration of a Declaration of a Heterogeneous ArrayHeterogeneous Array

PascalPascalvar Employee : recordvar Employee : record

Name: packed array[1..8] of char;Name: packed array[1..8] of char; Age: integer;Age: integer; SkillRating: real;SkillRating: real;endend

CCstruct Employee {struct Employee { char Name[8];char Name[8]; int Age;int Age; float SkillRating;float SkillRating;};};

Page 27: Programming Languages

2727

Assignment Statements and Assignment Statements and OperatorsOperators

C, C++, JavaC, C++, JavaTotal = Price + TaxTotal = Price + Tax;;

Ada, PascalAda, PascalTotal := Price + Tax;Total := Price + Tax;

APLAPLTotal <- Price + TaxTotal <- Price + Tax;;

Operator precedenceOperator precedence Operator overloadingOperator overloading

Page 28: Programming Languages

2828

Control StatementsControl Statements

Alter the execution sequence of the programAlter the execution sequence of the program gotogoto is the simplest control statement is the simplest control statement ExampleExample

goto 40goto 4020 Total = price + 1020 Total = price + 10

goto 70goto 7040 if Price < 50 goto 6040 if Price < 50 goto 60

goto 20goto 2060 Total = Price + 560 Total = Price + 570 stop70 stop

if( Price < 50 ) then Total = Price + 5else Total = Price + 10endifstop

Page 29: Programming Languages

2929

Control Structure (1)Control Structure (1)

Page 30: Programming Languages

3030

Control Structure (2)Control Structure (2)

Page 31: Programming Languages

3131

Control Structures (3)Control Structures (3)

Page 32: Programming Languages

3232

CommentsComments For inserting explanatory statements For inserting explanatory statements

(internal documentation)(internal documentation) C++ and JavaC++ and Java

/* This is/* This is a comment a comment

*/*/// This is a comment// This is a comment

Explain the program, not to repeat itExplain the program, not to repeat it Example: Example: Total = Price + Tax;Total = Price + Tax;

Page 33: Programming Languages

3333

ProceduresProcedures A procedure is a set of instructions for A procedure is a set of instructions for

performing a task that can be used as an performing a task that can be used as an abstract tool by other program unitsabstract tool by other program units

Control is transferred to the procedure at Control is transferred to the procedure at the time its services are required and then the time its services are required and then returned to the original program unit returned to the original program unit (calling unit) after the procedure is finished(calling unit) after the procedure is finished

The process of transferring control to a The process of transferring control to a procedure is often referred to as calling or procedure is often referred to as calling or invoking the procedureinvoking the procedure

Page 34: Programming Languages

3434

Procedures (1)Procedures (1)

Page 35: Programming Languages

3535

Procedures (2)Procedures (2) Local variablesLocal variables Global variablesGlobal variables Procedure’s headerProcedure’s header ParametersParameters

Formal parametersFormal parameters Actual parametersActual parameters

Page 36: Programming Languages

3636

A C ProcedureA C Procedure

Page 37: Programming Languages

3737

Pass by ValuePass by Value

5 5

5 6

5

Page 38: Programming Languages

3838

Pass by ReferencePass by Reference

5

6

6

Page 39: Programming Languages

3939

FunctionsFunctions A program unit similar to procedure uA program unit similar to procedure u

nit except that a value is transferred bnit except that a value is transferred back to the calling unitack to the calling unit

ExampleExampleCost = 2 * TotalCost( Price, TaxRate );Cost = 2 * TotalCost( Price, TaxRate );

Page 40: Programming Languages

4040

A C FunctionA C Function

Page 41: Programming Languages

4141

Input/Output StatementsInput/Output Statements I/O statements are often not primitiveI/O statements are often not primitive

s of programming languagess of programming languages Most programming languages implemMost programming languages implem

ent I/O operations as procedures or fuent I/O operations as procedures or functionsnctions

ExamplesExamplesprintf( “%d %d\n”, value1, value2 printf( “%d %d\n”, value1, value2 ););

cout << value << endl;cout << value << endl;

Page 42: Programming Languages

4242

A Formatted Output Example A Formatted Output Example in Cin C

Page 43: Programming Languages

4343

Translation ProcessTranslation Process

Page 44: Programming Languages

4444

Lexical AnalyzerLexical Analyzer Reads the source program symbol by Reads the source program symbol by

symbol, identifying which groups of symbol, identifying which groups of symbols represent single units, and symbols represent single units, and classifying those units classifying those units

As each unit is classified, the lexical As each unit is classified, the lexical analyzer generates a bit pattern analyzer generates a bit pattern known as a token to represent the known as a token to represent the unit and hands the token to the unit and hands the token to the parserparser

Page 45: Programming Languages

4545

ParsingParsing Group lexical units (tokens) into Group lexical units (tokens) into

statementsstatements Identify the grammatical structure of Identify the grammatical structure of

the program the program Recognize the role of each componentRecognize the role of each component Fixed-format vs. free-format Fixed-format vs. free-format

languageslanguages Key words and reserved wordsKey words and reserved words

Page 46: Programming Languages

4646

Syntax DiagramSyntax Diagram Pictorial representations of a program’Pictorial representations of a program’

s grammatical structures grammatical structure NonterminalsNonterminals

Requires further descriptionRequires further description TerminalsTerminals

Page 47: Programming Languages

4747

Syntax DiagramSyntax Diagram

Page 48: Programming Languages

4848

Parse TreeParse Tree Pictorial form which represents a Pictorial form which represents a

particular string conforming to a set particular string conforming to a set of syntax diagramsof syntax diagrams

The process of parsing a program is The process of parsing a program is essentially that of constructing a essentially that of constructing a parse tree for the source programparse tree for the source program

A parse tree represents the parser’s A parse tree represents the parser’s understanding of the programmer’s understanding of the programmer’s grammatical compositiongrammatical composition

Page 49: Programming Languages

4949

Parse TreeParse Tree

Page 50: Programming Languages

5050

Dangling Dangling elseelse Problem Problem if B1if B1

then ( if B2 then S1 )then ( if B2 then S1 )

else S2else S2

if B1if B1

then ( if B2 then S1then ( if B2 then S1

else S2 )else S2 )

Page 51: Programming Languages

5151

Parse Tree (1)Parse Tree (1)

Page 52: Programming Languages

5252

Parse Tree (2)Parse Tree (2)

Page 53: Programming Languages

5353

Mini ReviewMini Review

Draw the parse tree for the expressionDraw the parse tree for the expression

based on the given syntax diagramsbased on the given syntax diagrams

zxyx

Page 54: Programming Languages

5454

AnswerAnswer

x y

z

Expression

ExpressionTerm

Factor Factor Term Expression

Factor Term

Factor

x

Page 55: Programming Languages

5555

Mini ReviewMini Review Describe the strings that conforms to the strucDescribe the strings that conforms to the struc

ture Chacha according to the following syntax ture Chacha according to the following syntax diagramsdiagrams

Step

Turn

ChachaChacha

forward

backward

backward

forwardCha Cha Cha

Step

swingright

leftCha Cha Cha

Turn

Page 56: Programming Languages

5656

Other Translation TopicsOther Translation Topics Symbol tablesSymbol tables CoercionCoercion Strongly typed languageStrongly typed language Code generationCode generation Code optimizationCode optimization

x x y + z; y + z; w w x + z; x + z;

Page 57: Programming Languages

5757

Object-Oriented Approach to Object-Oriented Approach to the Translation Processthe Translation Process

Page 58: Programming Languages

5858

LinkerLinker Most programming environments Most programming environments

allow the modules of a program to be allow the modules of a program to be developed and translated as developed and translated as individual units at different timesindividual units at different times

Linker links several object programs, Linker links several object programs, operating system routines, and other operating system routines, and other utility software to produce a utility software to produce a complete, executable program (load complete, executable program (load module) that is in turn stored as a module) that is in turn stored as a file in the mass storage systemfile in the mass storage system

Page 59: Programming Languages

5959

LoaderLoader Places the load module in memoryPlaces the load module in memory Often part of the operating system’s Often part of the operating system’s

scheduler scheduler Important in multitasking systemsImportant in multitasking systems

Exact memory area available to the Exact memory area available to the programs is not known until it is time to programs is not known until it is time to execute itexecute it

Loader also makes any final adjustments Loader also makes any final adjustments that might needed once the exact memory that might needed once the exact memory location of the program is known (e.g. location of the program is known (e.g. dealing with the JUMP instruction)dealing with the JUMP instruction)

Page 60: Programming Languages

6060

Program Preparation Program Preparation ProcessesProcesses

Page 61: Programming Languages

6161

Object-Oriented Object-Oriented ProgrammingProgramming

Objects and classesObjects and classes Abstract data typeAbstract data type

Example (in C++): Example (in C++): LaserClass laser1, laser2;LaserClass laser1, laser2; InheritanceInheritance

Example (in Java): Example (in Java): class RechargeableLaser extends class RechargeableLaser extends LaserClassLaserClass PolymorphismPolymorphism EncapsulationEncapsulation

Page 62: Programming Languages

6262

A Class ExampleA Class Example

Page 63: Programming Languages

6363

ConstructorConstructor

Page 64: Programming Languages

6464

An Example for Encapsulation An Example for Encapsulation in Java or C#in Java or C#

Page 65: Programming Languages

6565

Concurrent ProcessingConcurrent Processing True parallel processingTrue parallel processing

Requires multiple CPU’sRequires multiple CPU’s Ada task and Java threadAda task and Java thread Basic actionsBasic actions

Creating new processCreating new process Handling communication between processeHandling communication between processe

ss Mutually exclusive accessMutually exclusive access Critical regionCritical region

A data item augmented with the ability to cA data item augmented with the ability to control access to itself is called a monitorontrol access to itself is called a monitor

Page 66: Programming Languages

6666

Interaction between Interaction between processesprocesses

Mutual exclusionMutual exclusion = a method for ensuri = a method for ensuring that data can be accessed by only onng that data can be accessed by only one process at a timee process at a time

MonitorMonitor = a data item augmented with t = a data item augmented with the ability to control access to itselfhe ability to control access to itself

Page 67: Programming Languages

6767

Spawning processesSpawning processes

Page 68: Programming Languages

6868

Additional Reading for Logical Additional Reading for Logical ProgrammingProgramming

S. Russell and P. Norvig, Artificial IntelligS. Russell and P. Norvig, Artificial Intelligence: A Modern Approach, Englewood Clence: A Modern Approach, Englewood Cliffs, NJ: Prentice Hall, 1995iffs, NJ: Prentice Hall, 1995

Page 69: Programming Languages

6969

Logical DeductionLogical Deduction Either Kermit is on stage or Kermit Either Kermit is on stage or Kermit

is sickis sick Kermit is not on stageKermit is not on stage Kermit is sickKermit is sick Resolution PrincipleResolution Principle

PQ

PQQORP

Page 70: Programming Languages

7070

Truth TableTruth Table

P Q P QP QP QP QP

False False

False

False

False

False False

False

False False

False False

False

True

True

True True

True

True True

True

True

True

True

True

True

True

True

Page 71: Programming Languages

7171

ImplicationImplication

QORPQP

P

U

Q

Page 72: Programming Languages

7272

Resolving (P OR Q) and (R OR Resolving (P OR Q) and (R OR Q) to Produce (P OR R)Q) to Produce (P OR R)

Page 73: Programming Languages

7373

Clause FormClause Form First-order predicate logic vs. First-order predicate logic vs.

Higher-order predicate logicHigher-order predicate logic

Clause formClause form

)()()( 2211 NN QORPANDANDQORPANDQORP

Page 74: Programming Languages

7474

Confirming the Inconsistency Confirming the Inconsistency of a Set of Inconsistent Clausesof a Set of Inconsistent Clauses

Page 75: Programming Languages

7575

Interpretation (1)Interpretation (1)

P: Kermit is on stageP: Kermit is on stage Q: Kermit has got SARS for two weeksQ: Kermit has got SARS for two weeks R: Kermit coughsR: Kermit coughs

PRRORP

QRQORR

PQQORP

Page 76: Programming Languages

7676

Interpretation (2)Interpretation (2)

PQ QR R P

PR

P

contradiction

Page 77: Programming Languages

7777

Automatic Reasoning Automatic Reasoning using reduction ad absurdumusing reduction ad absurdum

Want to confirm a collection of Want to confirm a collection of statements implies the statement statements implies the statement

Same as contradicting the statementSame as contradicting the statement Apply resolution to the original Apply resolution to the original

statements and the statement until statements and the statement until an empty clause occursan empty clause occurs

With inconsistent with the original With inconsistent with the original statements, the original statements statements, the original statements must imply must imply

PP

P

P

P

Page 78: Programming Languages

7878

UnificationUnification

homeatisMary

XatislambsMaryXatisMary )'()(

)(

)'()(

homeatisMary

XatislambsMaryORXatisMary

)(

)'()(

homeatisMary

homeatislambsMaryORhomeatisMary

)'( homeatislambsMary

The process of assigning values to The process of assigning values to variables so that resolution can be variables so that resolution can be performedperformed

Page 79: Programming Languages

7979

Prolog (1)Prolog (1) PROgramming in LOGicPROgramming in LOGic A Prolog program consists of a collectiA Prolog program consists of a collecti

on of initial statements upon which thon of initial statements upon which the underlying algorithm bases its dedue underlying algorithm bases its deductive reasoningctive reasoning

PredicatePredicate A fact about its argumentA fact about its argument ExamplesExamples

).,(

).,(

).,(

turtlerabbitfaster

snailturtlefaster

marybillparent

Page 80: Programming Languages

8080

Prolog (2)Prolog (2)

RuleRule

GoalGoal Proposed to the system and the system Proposed to the system and the system

applies the resolution to try to confirm applies the resolution to try to confirm that the goal is a consequence of the that the goal is a consequence of the initial statementsinitial statements

).,(),,(:),( ZYfasterYXfasterZXfaster

Page 81: Programming Languages

8181

Prolog QueriesProlog Queries

).,(

).,(

).,(

).,(

).,(

).,(

WVfaster

Wrabbitfaster

snailWfaster

snailrabbitfaster

turtlerabbitfaster

snailturtlefaster

Page 82: Programming Languages

8282

ExerciseExercise

2, 5, 8, 22, 28, 29, 31, 37, 49, 2, 5, 8, 22, 28, 29, 31, 37, 49, 5353