Programming Languages
-
Upload
ethan-boyer -
Category
Documents
-
view
23 -
download
1
description
Transcript of 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
22
Generations of programming Generations of programming languageslanguages
33
Machine Instructions and Machine Instructions and Mnemonic TechniquesMnemonic Techniques
156C166D5056306EC000
LD R5, PRICELD R6, TAXADDI R0, R5 R6ST R0, TOTALHLT
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
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
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
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
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
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
1010
Evolution of Programming Evolution of Programming ParadigmsParadigms
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
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
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
1414
Functional Paradigm Functional Paradigm ExampleExample
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))
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
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
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
1919
Composition of a Typical Composition of a Typical Imperative Program or Imperative Program or
Program UnitProgram Unit
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;
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
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
2323
Data StructureData Structure
Conceptual shape of dataConceptual shape of data Common data structureCommon data structure
Homogeneous arrayHomogeneous array Heterogeneous arrayHeterogeneous array
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;
2525
A two-dimensional arrayA two-dimensional array
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;};};
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
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
2929
Control Structure (1)Control Structure (1)
3030
Control Structure (2)Control Structure (2)
3131
Control Structures (3)Control Structures (3)
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;
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
3434
Procedures (1)Procedures (1)
3535
Procedures (2)Procedures (2) Local variablesLocal variables Global variablesGlobal variables Procedure’s headerProcedure’s header ParametersParameters
Formal parametersFormal parameters Actual parametersActual parameters
3636
A C ProcedureA C Procedure
3737
Pass by ValuePass by Value
5 5
5 6
5
3838
Pass by ReferencePass by Reference
5
6
6
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 );
4040
A C FunctionA C Function
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;
4242
A Formatted Output Example A Formatted Output Example in Cin C
4343
Translation ProcessTranslation Process
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
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
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
4747
Syntax DiagramSyntax Diagram
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
4949
Parse TreeParse Tree
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 )
5151
Parse Tree (1)Parse Tree (1)
5252
Parse Tree (2)Parse Tree (2)
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
5454
AnswerAnswer
x y
z
Expression
ExpressionTerm
Factor Factor Term Expression
Factor Term
Factor
x
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
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;
5757
Object-Oriented Approach to Object-Oriented Approach to the Translation Processthe Translation Process
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
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)
6060
Program Preparation Program Preparation ProcessesProcesses
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
6262
A Class ExampleA Class Example
6363
ConstructorConstructor
6464
An Example for Encapsulation An Example for Encapsulation in Java or C#in Java or C#
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
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
6767
Spawning processesSpawning processes
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
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
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
7171
ImplicationImplication
QORPQP
P
U
Q
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)
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
7474
Confirming the Inconsistency Confirming the Inconsistency of a Set of Inconsistent Clausesof a Set of Inconsistent Clauses
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
7676
Interpretation (2)Interpretation (2)
PQ QR R P
PR
P
contradiction
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
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
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
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
8181
Prolog QueriesProlog Queries
).,(
).,(
).,(
).,(
).,(
).,(
WVfaster
Wrabbitfaster
snailWfaster
snailrabbitfaster
turtlerabbitfaster
snailturtlefaster
8282
ExerciseExercise
2, 5, 8, 22, 28, 29, 31, 37, 49, 2, 5, 8, 22, 28, 29, 31, 37, 49, 5353