Programming Languages - PowerPoint PPT Presentation

1 / 82
About This Presentation
Title:

Programming Languages

Description:

Must conform to the machine's register configuration and instruction set. Low level primitives ... Describe the strings that conforms to the structure Chacha ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 83
Provided by: shyhka
Category:

less

Transcript and Presenter's Notes

Title: Programming Languages


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

2
Generations of programming languages
3
Machine Instructions and Mnemonic Techniques
156C 166D 5056 306E C000
LD R5, PRICE LD R6, TAX ADDI R0, R5 R6 ST R0,
TOTAL HLT
4
Assemblers 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

5
Assembly 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

6
Design 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

7
Third-Generation Languages
  • High-level primitives
  • Machine independent if standard is followed
  • FORTRAN
  • FORmula TRANslator
  • COBOL
  • COmmon Business-Oriented Language
  • BASIC

8
Compilers 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

9
Machine 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

10
Evolution of Programming Paradigms
11
Imperative 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

12
Declarative 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

13
Functional 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

14
Functional Paradigm Example
15
Examples of LISP Expressions
  • (Find_diff
  • (Find_Sum Old_balance Credits) (Find_Sum
    Debits))
  • (First (Sort List))

16
Advantages 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

17
Object-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

18
Statements 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

19
Composition of a Typical Imperative Program or
Program Unit
20
Variables, Literals, Constants
  • EffectiveAlt ? Altimeter 645
  • Variables
  • Example EffectiveAlt, Altimeter
  • Literals
  • Example 645
  • Constants
  • Example const int AirportAlt 645

21
Data Type
  • Refers to
  • Interpretation of data
  • Operations that can be performed on the data
  • Common types
  • integer, real, character, Boolean

22
Variable 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

23
Data Structure
  • Conceptual shape of data
  • Common data structure
  • Homogeneous array
  • Heterogeneous array

24
Declaration of a 2D Array
  • C
  • int Scores29
  • Java
  • int Scoresnew int 29
  • Pascal
  • Scores array3..4, 12..20 of integer

25
A two-dimensional array
26
Declaration 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

27
Assignment Statements and Operators
  • C, C, Java
  • Total Price Tax
  • Ada, Pascal
  • Total Price Tax
  • APL
  • Total lt- Price Tax
  • Operator precedence
  • Operator overloading

28
Control 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
29
Control Structure (1)
30
Control Structure (2)
31
Control Structures (3)
32
Comments
  • 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

33
Procedures
  • 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

34
Procedures (1)
35
Procedures (2)
  • Local variables
  • Global variables
  • Procedures header
  • Parameters
  • Formal parameters
  • Actual parameters

36
A C Procedure
37
Pass by Value
38
Pass by Reference
39
Functions
  • A program unit similar to procedure unit except
    that a value is transferred back to the calling
    unit
  • Example
  • Cost 2 TotalCost( Price, TaxRate )

40
A C Function
41
Input/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

42
A Formatted Output Example in C
43
Translation Process
44
Lexical 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

45
Parsing
  • 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

46
Syntax Diagram
  • Pictorial representations of a programs
    grammatical structure
  • Nonterminals
  • Requires further description
  • Terminals

47
Syntax Diagram
48
Parse 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

49
Parse Tree
50
Dangling else Problem
  • if B1
  • then ( if B2 then S1 )
  • else S2
  • if B1
  • then ( if B2 then S1
  • else S2 )

51
Parse Tree (1)
52
Parse Tree (2)
53
Mini Review
  • Draw the parse tree for the expression
  • based on the given syntax diagrams

54
Answer
Expression
Expression
Term
Factor
Factor
Term
Expression
Term
Factor
Factor
55
Mini 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
56
Other Translation Topics
  • Symbol tables
  • Coercion
  • Strongly typed language
  • Code generation
  • Code optimization
  • x ? y z
  • w ? x z

57
Object-Oriented Approach to the Translation
Process
58
Linker
  • 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

59
Loader
  • 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)

60
Program Preparation Processes
61
Object-Oriented Programming
  • Objects and classes
  • Abstract data type
  • Example (in C) LaserClass laser1, laser2
  • Inheritance
  • Example (in Java)
  • class RechargeableLaser extends
  • LaserClass
  • Polymorphism
  • Encapsulation

62
A Class Example
63
Constructor
64
An Example for Encapsulation in Java or C
65
Concurrent 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

66
Interaction 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

67
Spawning processes
68
Additional Reading for Logical Programming
  • S. Russell and P. Norvig, Artificial
    Intelligence A Modern Approach, Englewood
    Cliffs, NJ Prentice Hall, 1995

69
Logical Deduction
  • Either Kermit is on stage or Kermit is sick
  • Kermit is not on stage
  • Kermit is sick
  • Resolution Principle

70
Truth Table
71
Implication
U
P
Q
72
Resolving (P OR Q) and (R OR ? Q) to Produce (P
OR R)
73
Clause Form
  • First-order predicate logic vs. Higher-order
    predicate logic
  • Clause form

74
Confirming the Inconsistency of a Set of
Inconsistent Clauses
75
Interpretation (1)
  • P Kermit is on stage
  • Q Kermit has got SARS for two weeks
  • R Kermit coughs

76
Interpretation (2)
contradiction
77
Automatic 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

78
Unification
  • The process of assigning values to variables so
    that resolution can be performed

79
Prolog (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

80
Prolog (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

81
Prolog Queries
82
Exercise
  • 2, 5, 8, 22, 28, 29, 31, 37, 49, 53
Write a Comment
User Comments (0)
About PowerShow.com