• 沒有找到結果。

Programming Language

N/A
N/A
Protected

Academic year: 2022

Share "Programming Language"

Copied!
40
0
0

加載中.... (立即查看全文)

全文

(1)

Programming Languages

National Chiao Tung University Chun-Jen Tsai 05/4/2012

(2)

2/40

Programming Language

 Programming Language (PL) is a language that can

“precisely describe” an algorithm to a computer so that it can execute the algorithm:

Algorithm

data output

program

(3)

3/40

Design Considerations

 There are two extremes in designing a PL:

 Use human language

 Use machine code

 Human languages as programming languages

 Imprecise

 Inefficient (for computer as well as human)

 Easy to use

 Hard to debug

 Machine instruction code as programming languages

 Precise

 Efficient for computers

 Verbose to use

 Hard to debug

(4)

4/40

Assembly Language

 Since machine codes are too hard to remember, each processor manufacture designs an “easy-to- remember” names for each op-code

 Assembly language – a mnemonic system for representing machine instruction codes

 Mnemonic names for op-codes

 Names for all registers

 Identifiers: descriptive names for memory locations, chosen by the programmers

 Assembly language is referred to as the 2nd generation of programming language

(5)

5/40

Assembly Language Characteristics

 One-to-one correspondence between machine instructions and assembly instructions

 Programmer must think like the machine

 Inherently machine-dependent

 Before execution by a computer, we must translate a machine language program into machine codes by an assembler

(6)

6/40

Assembly Language Example

Machine language program Assembly language program

assembler

Definition of mnemonics:

LD means “load”

ADDI means “Integer addition”

ST means “store”

HLT means “halt”

ORG means “origin”

db means “define byte”

156C 166D 5056 30CE C000

LD R5, [Price]

LD R6, [ShippingCharge]

ADDI R0, R5 R6

ST R0, [TotalCost]

HLT

;

ORG 6Ch

Price db 25 ShippingCharge db 5 TotalCost db 00

(7)

7/40

Third Generation Languages

 Uses high-level primitives

 Machine independent (mostly)

 Early examples:

 FORTRAN – for numerical computations

 COBOL – for financial computations and database systems

 Each primitive corresponds to a short sequence of machine instruction codes

 Can be translated into machine codes by a compiler

(8)

8/40

Language Translators

 There are several kinds of programming language translators

 Assemblers

 perform one-to-one mapping from assembly code to machine code

 Compilers

 perform translation from a high-level (machine-independent) statement to an equivalent short sequence of machine codes

 Interpreters

 perform translation and execution of high-level statements at the same time; note that there is no intermediate machine code being generated

(9)

9/40

Formal Languages

 Programming languages are “formal languages”

since they are artificial languages defined precisely by grammars

 Natural (human) languages are not precisely defined by grammars, instead, grammars are created

afterwards to “summarize” the language usage

 Esperanto is an “formal” human language artificially developed in late 1870s.

(10)

10/40

Timeline of Programming Languages

(11)

11/40

Programming Paradigms (1/2)

 Imperative (procedural) programming language

 A program is a sequence of commands

 Earliest way of programming

 Functional programming language

 A program is a description of a data flow (connections of functional units)

sum sum

diff old

balance credits debits

new balance

(diff (sum old_balance credits) (sum debits)) An “algorithm”

A program in LISP programming language:

(12)

12/40

Programming Paradigms (2/2)

 Declarative programming language

 Describes conditions that satisfy the intended solution;

the specific steps needed to arrive at that solution are up to an unspecified interpreter

 Only works for a specific domain of problems (e.g. for knowledge-based inference)

 Object-oriented programming language

 A “data-centric” programming language

 Operations are attached to data

 A program is composed of a list of objects, each annotated by a list of permissible operations of that object

(13)

13/40

Imperative Programming Language

 The imperative programming paradigm is the most intuitive and effective way of expressing our

commands to computers

Data

Commands Program

The first part consists of declaration statements describing the data that is manipulated by the program.

The second part consists of imperative statements describing the action to be performed.

(14)

14/40

Example of Data Declaration

 Variable (data) declarations in C, C++, C#, and Java are as follows:

 Scalar data declaration:

 Aggregate data declarations:

float Length, Width;

int Price, Tax, Total;

char Symbol;

int Scores[2][9];

Struct {

char Name[8];

int Age;

float SkillRating;

} Employee;

array

structure (heterogeneous array)

(15)

15/40

Memory Layout of Aggregate Data

 A two-dimensional array with two rows and nine columns:

 A structure:

(16)

16/40

Elements of an Imperative PL

 An imperative programming language provides statements to:

 Express constants and literals

 Assign values to variables

 Control the execution sequence of the program

 Conditional control

 Looping control

 Commenting the program

 Call procedural units

(17)

17/40

Procedural Calls (1/2)

 Procedural calls for imperative languages:

(18)

18/40

Procedural Calls (2/2)

 Description of a procedure in C:

(19)

19/40

Parameter Passing Methods

 There are several ways to pass a parameter from the calling program unit to the called procedure:

 Call-by-value (passed by value in the textbook)

 Call-by-reference (passed by reference in the textbook)

 Call-by-name

 not mentioned in the textbook, and not popular anymore

 similar to macro expansion in C/C++, but it’s a real function call

int x = 1, y = 2;

my_func() {

f1(x, x+y);

}

f1(p, q) {

int s;

p = q;

s = q;

}

This is equal to x = x+y;

and 3 will be assigned to p and x.

Here, 5 will be assigned to s.

(20)

20/40

Call by Value & Call by Reference

 Call by value  Call by reference

(21)

21/40

Function Calls

 A function is a special type of procedure that returns a value:

(22)

22/40

Translating Program to Executable

 A compiler translates a program into machine codes via the following steps:

Lexical analyzer converts alpha-numerical symbols in the source program to

tokens; for example, if each token is specified by a 16-bit number, a lexical analyzer may perform the following conversion:

position = x_coord + y_coord * 7 → 0003 1001 0001 1002 0002 1003 2001

The first byte specifies the type of token,

0 – variables, 1 – operators, 2 – constants The remaining bytes compose an index to the token value tables

(23)

23/40

Syntax Diagram

 The parsing process is based on a set of rules that define the syntax of the programming language

 The rules are called grammar

 The rules can be expressed by syntax diagrams

 A syntax diagram of the “if-then-else” statement is as follows:

(24)

24/40

Algebraic Expression Syntax

(25)

25/40

Example of Parsing An Expression

 The parser generates a parse tree for a statements x + y × z:

(26)

26/40

Ambiguous Parse Trees

 For “if B1 then if B2 then S1 else S2” we could have two possible parse trees:

(27)

27/40

Code Generation and Optimization

 Once the parse tree is done, one must generate machine codes for each

sub-tree or node, for example, in

bottom-up manner

 Code optimization is a

technique for finding the best way to generate codes

load z into R0

load y into R1

multiply R0 with R1 and save the result into R2

(28)

28/40

Concurrent Programming

 Concurrent programming is the simultaneous execution of multiple processes/threads

 If the computing system has only one CPU, simultaneous execution can be simulated using time-sharing techniques

 If the computing system has multiple CPUs, each

process/threads will be assigned to one CPU for execution

 The difference between processes and threads can be loosely defined as follows:

 Program (static) → Process (runtime)

 Procedure (static) → Thread (runtime)

(29)

29/40

Spawning A Thread (in a Process)

Main code

data

Proc.

Both threads access/modify the same data space

This whole thing is still considered as a single process space

thread 1 thread 2

(30)

30/40

Spawning A Process

 Spawning (or forking) of a process is done as follows:

code 1

data

code 2

data

Both processes are running in the main memory at the same time



exact copy

(31)

31/40

Object-Oriented Programming

 An object-oriented (OO) language composed of a hierarchical structure of objects

 Class: the static definition of an objects

 Object: an active substance inside a running process

 An OO program is composed of the declaration of

different types of static description of substances (i.e.

classes), and how these substances are created (become active) and interact with each others

 In OO terminology, an object is an instance of a class

(32)

32/40

OO View of Physical World

lecture room

desk

globe

human

chalk eraser blackboard

writing tools mouth

body

speak

An object-oriented

description of the lecturing process

teacher

pen

write

(33)

33/40

Object-Oriented Terminologies

 Data Encapsulation

 Access to the internal components of an object are restricted

 You can use an object, but you cannot modify its behavior and internal data

 Inheritance

 Define new classes in terms of previously defined classes

 Facilitate hierarchical structure of an object-oriented process

 Polymorphism

 Implementation details of the behaviors (or operators) of an object are interpreted by the object that perform that

behavior

(34)

34/40

Functional Programming

 Principle of functional programming:

 The value of an expression depends only on the values of its sub-expressions, if any

 Any language must be defined in some sort of

notation, called meta-language or defining language

 Meta-language tends to be a functional description

 Functional programming becomes popular due to the invention of LISP, a list processing language, by John McCarthy in 1958

(35)

35/40

Features of a Functional Language

 In functional language, program and data can be treated almost the same:

 (it seems that you liked me)

 Unification of code and data is an important concept in many modern languages

 Lots of parentheses are used to modify the structure of a program:

 (it seems that you liked me) and

((it seems that) you (liked) me) are different

 Some people jokingly call LISP: Lots of Silly Parentheses

(36)

36/40

Example: Differentiation

 Differentiation can be computed in LISP as follows:

 The function “d” is defined using the rules:

(define s (make-sum '(u v w))) (d 'v 'v)

(d 'v 'w) (d 'v 's)

(d 'v '(* v (+ u v w)))

1

0

(+ 0 1 0)

(+ (* 1 (+ u v w)) (* v (+ 0 1 0))))

d(x, x) = 1

d(x, not x) = 0

d(x, E1 + E2) = d(x, E1) + d(x, E2)

d(x, E1 * E2) = d(x, E1)*E2 + E1 * d(x, E2)

(37)

37/40

Declarative Programming

 Declarative programming is also referred to as Logic programming:

 The use of facts and rules to represent information

 The use of deduction to answer queries

 In declarative programming, the programmer supplies facts and rules; while the computer use deduction to find the answer

 The language that makes declarative programming well-known is Prolog, developed in 1972

 The application domain for Prolog is similar to that for LISP:

artificial intelligence, expert systems, etc.

(38)

38/40

Prolog Language Elements

 In Prolog, all statements must be facts or rules

 Fact:

 predicateName(arguments)

 Example: parent(Bill, Mary)

 Rule:

 conclusion :- premise (note that :- stands for “if”)

 Example: wise(x) :- old(x)

 Example: faster(x,z) :- faster(x,y),faster(y,z)

(39)

39/40

Deduction Methods

 Resolution

 Combining two or more statements to produce a new, logically equivalent statement

 Unification

 Assigning a value to a variable in a statement

(40)

40/40

Example of Deduction

 Resolving the statements:

(P OR Q), (R OR ¬Q), ¬R, ¬P

參考文獻

相關文件

If the students are very bright and if the teachers want to help prepare these students for the English medium in 81, teachers can find out from the 81 curriculum

9 The pre-S1 HKAT is conducted in all secondary schools in July every year to assess the performance of students newly admitted to S1 in Chinese Language, English Language

In this paper, we extended the entropy-like proximal algo- rithm proposed by Eggermont [12] for convex programming subject to nonnegative constraints and proposed a class of

It is well known that second-order cone programming can be regarded as a special case of positive semidefinite programming by using the arrow matrix.. This paper further studies

The relationship between these extra type parameters, and the types to which they are associated, is established by parameteriz- ing the interfaces (Java generics, C#, and Eiffel)

There are existing learning resources that cater for different learning abilities, styles and interests. Teachers can easily create differentiated learning resources/tasks for CLD and

Generic methods allow type parameters to be used to express dependencies among the types of one or more arguments to a method and/or its return type.. If there isn’t such a

• Java is one of general-purpose programming languages, supporting the object-oriented programming (OOP) paradigm.. • Java was first released by Sun Microsystems back in 1995 and is