Title: Programming Languages
1Programming Languages
- Shyh-Kang Jeng
- Department of Electrical Engineering/
- Graduate Institute of Communication Engineering
- National Taiwan University
2Generations of programming languages
3Machine Instructions and Mnemonic Techniques
156C 166D 5056 306E C000
LD R5, PRICE LD R6, TAX ADDI R0, R5 R6 ST R0,
TOTAL HLT
4Assemblers and Assembly Languages
- Programs to translate other programs written in
mnemonic form into machine compatible form - Assemble machine instructions out of the op-codes
and operands obtained by translating mnemonics
and identifiers - Mnemonic systems for representing programs are
recognized as programming languages called
assembly languages
5Assembly Languages
- Second-generation language
- Machine dependent
- Must conform to the machines register
configuration and instruction set - Low level primitives
- A programmer is still forced to think in terms of
the small, incremental steps of the machines
language
6Design Process of a Product
- Better suited to the use of high-level
primitives, each representing a concept
associated with a major feature of the product - Once the design is complete, the primitives can
be translated to lower-level concepts relating to
the details of implementation
7Third-Generation Languages
- High-level primitives
- Machine independent if standard is followed
- FORTRAN
- FORmula TRANslator
- COBOL
- COmmon Business-Oriented Language
- BASIC
8Compilers vs. Interpreters
- Compilers
- Compile several machine instructions into short
sequences to simulate the activity requested by a
single high-level primitive - Produce a machine-language copy of a program that
would be executed later - Interpreters
- Execute the instructions as they were translated
9Machine Independence and Beyond
- Restrictions of the machines
- Size of registers and memory cells
- Dialects and standard languages
- ANSI (American National Standards Institute)
standard - ISO (International Organization of
Standardization) standard - Language extensions
10Evolution of Programming Paradigms
11Imperative Paradigm
- Procedural paradigm
- Develops a sequence of commands that when
followed, manipulate data to produce the desired
result - Approaches a problem by trying to find an
algorithm for solving it
12Declarative Paradigm
- Emphasizes
- What is the problem?
- Rather than What algorithm is required to solve
the problem? - Implemented a general problem-solving algorithm
- Develops a statement of the problem compatible
with the algorithm and then applies the algorithm
to solve it
13Functional Paradigm
- Views the process of program development as
connecting predefined black boxes, each of
which accepts inputs and produces outputs - Mathematicians refer to such boxes as functions
- Constructs functions as nested complexes of
simpler functions
14Functional Paradigm Example
15Examples of LISP Expressions
- (Find_diff
- (Find_Sum Old_balance Credits) (Find_Sum
Debits)) - (First (Sort List))
16Advantages of Functional Paradigm
- Constructing complex software from predefined
primitive functions leads to well-organized
systems - Provides an environment in which hierarchies of
abstraction are easily implemented, enabling new
software to be constructed from large predefined
components rather than from scratch
17Object-Oriented Paradigm
- OOP
- Encapsulation of data and procedures
- Lists and sorting
- Graphic user interface (GUI)
- Natural modular structure and program reuse
- Communication between modules is accomplished by
passing messages - CORBA and software components
18Statements in Programming Languages
- Declarative statements
- Define customized terminology that is used later
in the program - Imperative statements
- Describe steps in the underlying algorithms
- Comments
- Enhance the readability of a program
19Composition of a Typical Imperative Program or
Program Unit
20Variables, Literals, Constants
- EffectiveAlt ? Altimeter 645
- Variables
- Example EffectiveAlt, Altimeter
- Literals
- Example 645
- Constants
- Example const int AirportAlt 645
21Data Type
- Refers to
- Interpretation of data
- Operations that can be performed on the data
- Common types
- integer, real, character, Boolean
22Variable Declarations
- Pascal
- Length, width real
- Price, Tax, Total integer
- C, C, Java, C
- float Length, width
- int Price, Tax, Total
- FORTRAN
- REAL Length, Width
- INTEGER Price, Tax, Total
23Data Structure
- Conceptual shape of data
- Common data structure
- Homogeneous array
- Heterogeneous array
24Declaration of a 2D Array
- C
- int Scores29
- Java
- int Scoresnew int 29
- Pascal
- Scores array3..4, 12..20 of integer
25A two-dimensional array
26Declaration of a Heterogeneous Array
- Pascal
- var Employee record
- Name packed array1..8 of char
- Age integer
- SkillRating real
- end
- C
- struct Employee
- char Name8
- int Age
- float SkillRating
-
27Assignment Statements and Operators
- C, C, Java
- Total Price Tax
- Ada, Pascal
- Total Price Tax
- APL
- Total lt- Price Tax
- Operator precedence
- Operator overloading
-
28Control Statements
- Alter the execution sequence of the program
- goto is the simplest control statement
- Example
- goto 40
- 20 Total price 10
- goto 70
- 40 if Price lt 50 goto 60
- goto 20
- 60 Total Price 5
- 70 stop
if( Price lt 50 ) then Total Price 5 else
Total Price 10 endif stop
29Control Structure (1)
30Control Structure (2)
31Control Structures (3)
32Comments
- For inserting explanatory statements (internal
documentation) - C and Java
- / This is
- a comment
- /
- // This is a comment
- Explain the program, not to repeat it
- Example Total Price Tax
33Procedures
- A procedure is a set of instructions for
performing a task that can be used as an abstract
tool by other program units - Control is transferred to the procedure at the
time its services are required and then returned
to the original program unit (calling unit) after
the procedure is finished - The process of transferring control to a
procedure is often referred to as calling or
invoking the procedure
34Procedures (1)
35Procedures (2)
- Local variables
- Global variables
- Procedures header
- Parameters
- Formal parameters
- Actual parameters
36A C Procedure
37Pass by Value
38Pass by Reference
39Functions
- A program unit similar to procedure unit except
that a value is transferred back to the calling
unit - Example
- Cost 2 TotalCost( Price, TaxRate )
40A C Function
41Input/Output Statements
- I/O statements are often not primitives of
programming languages - Most programming languages implement I/O
operations as procedures or functions - Examples
- printf( d d\n, value1, value2 )
- cout ltlt value ltlt endl
42A Formatted Output Example in C
43Translation Process
44Lexical Analyzer
- Reads the source program symbol by symbol,
identifying which groups of symbols represent
single units, and classifying those units - As each unit is classified, the lexical analyzer
generates a bit pattern known as a token to
represent the unit and hands the token to the
parser
45Parsing
- Group lexical units (tokens) into statements
- Identify the grammatical structure of the program
- Recognize the role of each component
- Fixed-format vs. free-format languages
- Key words and reserved words
46Syntax Diagram
- Pictorial representations of a programs
grammatical structure - Nonterminals
- Requires further description
- Terminals
47Syntax Diagram
48Parse Tree
- Pictorial form which represents a particular
string conforming to a set of syntax diagrams - The process of parsing a program is essentially
that of constructing a parse tree for the source
program - A parse tree represents the parsers
understanding of the programmers grammatical
composition
49Parse Tree
50Dangling else Problem
- if B1
- then ( if B2 then S1 )
- else S2
- if B1
- then ( if B2 then S1
- else S2 )
51Parse Tree (1)
52Parse Tree (2)
53Mini Review
- Draw the parse tree for the expression
- based on the given syntax diagrams
54Answer
Expression
Expression
Term
Factor
Factor
Term
Expression
Term
Factor
Factor
55Mini Review
- Describe the strings that conforms to the
structure Chacha according to the following
syntax diagrams
Step
Chacha
Chacha
Turn
Step
forward
backward
Cha
Cha
Cha
backward
forward
Turn
right
swing
Cha
Cha
Cha
left
56Other Translation Topics
- Symbol tables
- Coercion
- Strongly typed language
- Code generation
- Code optimization
- x ? y z
- w ? x z
57Object-Oriented Approach to the Translation
Process
58Linker
- Most programming environments allow the modules
of a program to be developed and translated as
individual units at different times - Linker links several object programs, operating
system routines, and other utility software to
produce a complete, executable program (load
module) that is in turn stored as a file in the
mass storage system
59Loader
- Places the load module in memory
- Often part of the operating systems scheduler
- Important in multitasking systems
- Exact memory area available to the programs is
not known until it is time to execute it - Loader also makes any final adjustments that
might needed once the exact memory location of
the program is known (e.g. dealing with the JUMP
instruction)
60Program Preparation Processes
61Object-Oriented Programming
- Objects and classes
- Abstract data type
- Example (in C) LaserClass laser1, laser2
- Inheritance
- Example (in Java)
- class RechargeableLaser extends
- LaserClass
- Polymorphism
- Encapsulation
62A Class Example
63Constructor
64An Example for Encapsulation in Java or C
65Concurrent Processing
- True parallel processing
- Requires multiple CPUs
- Ada task and Java thread
- Basic actions
- Creating new process
- Handling communication between processes
- Mutually exclusive access
- Critical region
- A data item augmented with the ability to control
access to itself is called a monitor
66Interaction between processes
- Mutual exclusion a method for ensuring that
data can be accessed by only one process at a
time - Monitor a data item augmented with the ability
to control access to itself
67Spawning processes
68Additional Reading for Logical Programming
- S. Russell and P. Norvig, Artificial
Intelligence A Modern Approach, Englewood
Cliffs, NJ Prentice Hall, 1995
69Logical Deduction
- Either Kermit is on stage or Kermit is sick
- Kermit is not on stage
- Kermit is sick
- Resolution Principle
70Truth Table
71Implication
U
P
Q
72Resolving (P OR Q) and (R OR ? Q) to Produce (P
OR R)
73Clause Form
- First-order predicate logic vs. Higher-order
predicate logic - Clause form
74Confirming the Inconsistency of a Set of
Inconsistent Clauses
75Interpretation (1)
- P Kermit is on stage
- Q Kermit has got SARS for two weeks
- R Kermit coughs
76Interpretation (2)
contradiction
77Automatic Reasoning using reduction ad absurdum
- Want to confirm a collection of statements
implies the statement - Same as contradicting the statement
- Apply resolution to the original statements and
the statement until an empty clause occurs - With inconsistent with the original
statements, the original statements must imply
78Unification
- The process of assigning values to variables so
that resolution can be performed
79Prolog (1)
- PROgramming in LOGic
- A Prolog program consists of a collection of
initial statements upon which the underlying
algorithm bases its deductive reasoning - Predicate
- A fact about its argument
- Examples
80Prolog (2)
- Rule
- Goal
- Proposed to the system and the system applies the
resolution to try to confirm that the goal is a
consequence of the initial statements
81Prolog Queries
82Exercise
- 2, 5, 8, 22, 28, 29, 31, 37, 49, 53