Tải bản đầy đủ (.pdf) (43 trang)

Nguyên lý các ngôn ngữ lập trình Lập trình chỉ thị

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (613.35 KB, 43 trang )

IT6020
(c) PGS.TS. H.Q.Thắng, Dep. of SE
1
IT6020 Nguyên l cc ngôn ng
lp trnh
Lecturer: Dr. Huỳnh Quyết Thắng,
Assoc. Prof at Dept. of Software Engineering,
SoICT, HUST
Email:
Mobile: 0913536752
Chương 2 – Lp trnh ch th
AT in PMT
2
Content of Lesson
1. Imperative Programming Model
2. Memory Management
3. Some Typical Programming Techniques
4. Some exercises
(c) PGS.TS. H.Q.Thắng, Dep. of SE
AT in PMT (c) PGS.TS. H.Q.Thắng, Dep. of SE
3
1. Imperative Programming model
Von Neumann machine has a memory, which
contains both programming instruction (the
program store) and data values (data store)
Imperative languages have variable
declaration, expression and command
 Declaration assign names to memory allocation
and associate types with stores values
 Expression are interpreted in term of the current
value in the data store


 Commands are normally executed in the ordr they
appear in the program store, although conditional
or unconditional jumps can change the flow of
execution
AT in PMT (c) PGS.TS. H.Q.Thắng, Dep. of SE
4
1. Imperative Programming model
Command in an imperative language were
simple abstraction of the instruction in
standard con Neumann machines
These included assignment statement,
conditional statement, branch statement, loop
statement
AT in PMT (c) PGS.TS. H.Q.Thắng, Dep. of SE
5
1.1. Feature of imperative programming languages
Data type for integer, real, characters, string,
booleans and their operator
Control structures: statement sequencing,
conditionals, branching statements, for and while
loop, case (switch) statement
Assignment statement
Array and element assignment
Record structures and element assignment
Input and output commands
Pointer
Procedure and function
AT in PMT (c) PGS.TS. H.Q.Thắng, Dep. of SE
6
1.2. Naming and Variables

The model uses a single-assignment store, which is a
set of variables that are initially unbound and that can
be bound to one value.
AT in PMT (c) PGS.TS. H.Q.Thắng, Dep. of SE
7
1.2. Naming and Variables
Declarative variables: Variables in the single-assignment
store are called declarative variables. We use this term
whenever there is a possible confusion with other kinds of
variables. Later we will also call these variables dataflow
variables because of their role in dataflow execution.
Once bound, a declarative variable stays bound throughout the
computation and is indistinguishable from its value. What this
means is that it can be used in calculations as if it were the
value.
Doing the operation x + y is the same as doing 11 + 22, if the
store is {x = 11, y = 22}.
AT in PMT (c) PGS.TS. H.Q.Thắng, Dep. of SE
8
1.2. Naming and Variables
Value store: A store where all variables are bound
to values is called a value store. A value is a
mathematical constant.
Value creation: The basic operation on a store is
binding a variable to a newly-created value. We will
write this as x=value. Here, x refers directly to a
variable in the store (and is not the variable’s textual
name in a program!) and value refers to a value, e.g.,
314 or [1 2 3].
AT in PMT (c) PGS.TS. H.Q.Thắng, Dep. of SE

9
1.2. Naming and Variables
Variable identifiers: So far, we have looked at a
store that contains variables and values, i.e., store
entities, with which calculations can be done. It
would be nice if we could refer to a store entity from
outside the store. This is the role of variable
identifiers. A variable identifier is a textual name
that refers to a store entity from outside the store.
The mapping from variable identifiers to store
entities is called an environment.
AT in PMT (c) PGS.TS. H.Q.Thắng, Dep. of SE
10
1.2. Naming and Variables
Variable-variable binding: Variables can be bound to
variables. For example, consider two unbound variables x1
and y1 referred to by the identifiers X and Y.
 x1=X
 y1=Y
Dataflow variables: In the declarative model, creating a
variable and binding it are done separately. What happens if
we try to use the variable before it is bound? We call this a
variable use error.
AT in PMT (c) PGS.TS. H.Q.Thắng, Dep. of SE
11
1.3. Elementary Types, Value and Expression
Elementary type:
 Byte
 Integer
 Real number

 Character
 Boolean
 Pointer
AT in PMT (c) PGS.TS. H.Q.Thắng, Dep. of SE
12
1.3. Elementary Types, Value and Expression
Expression in real language:
 Scope resolution
 Member Selection
 Unary negation, complement. increment, address-
of, de-reference, allocate, de-allocate
 Multiply, divide, Modulo
 Add, Subtract
 Left Shift, Right Shift
 Less than , Less or Equal, Greater than, Greater or
Equal
AT in PMT (c) PGS.TS. H.Q.Thắng, Dep. of SE
13
1.3. Elementary Types, Value and Expression
Expression in real language (continue):
 Equal, not Equal
 Bitwise and
 Bitwise or
 Logical and
 Logical or
 Conditional expression
 Assignment
 Exception
Conversion anf Type casting:
 A conversion is just the translation of a data value from

one type to an equivalent value in another type
AT in PMT (c) PGS.TS. H.Q.Thắng, Dep. of SE
14
1.4. Syntax and Semantics of statements in real
language
For loops
 ForStatement->for (assigntment1; Expression;
Assigntment2) Statement
Do Statement
 DoStatement-> do Statement while (Expression)
Switch (Case) statements
 SwitchStatement->switch (Expression) {cases}
 Cases->Case Cases | Default
 Case->CaseHead Cases | CaseHead Statement
 CaseHead-> case Literal;
 Default->default : statement
Break, continue
AT in PMT (c) PGS.TS. H.Q.Thắng, Dep. of SE
15
1.4. Scope, Visibility and Lifetime
The Scope of a variable is that collection of
statements which can access that variable
Static scoping: variable reference can be resolved by
inspections of the source text of the program without
regard to its execution history. The static scope of a
global variable includes all statement in the program
Local and global variable:
Dynamic and static allocation:
AT in PMT (c) PGS.TS. H.Q.Thắng, Dep. of SE
16

2. Memory Management
Memory consists of a sequence of words. This
sequence is divided up into blocks, where a block
consists of a sequence of one or more words used to
store a language entity or part of a language entity.
Blocks are the basic unit of memory allocation.
Each block of memory continuously cycles through
three states: active, inactive, and free. Memory
management is the task of making sure that memory
circulates correctly along this cycle.
A running program that needs a block of memory
will allocate it from a pool of free memory blocks.
AT in PMT (c) PGS.TS. H.Q.Thắng, Dep. of SE
17
2. Memory Management
AT in PMT (c) PGS.TS. H.Q.Thắng, Dep. of SE
18
2. Memory Management
Values in programming language can be assigned to
any of three different categories of memory: static
memory, the runtime-stack and the heap.
Static memory is allocated to values whose storage
requirement are known before run time and remain
constant throughout the life of the running program.
The run-time stack is the center of control for
dispatching active methods (procedures and
functions), their local variables, their parameters
linkages and they gain relinquish control to the
caller.
AT in PMT

(c) PGS.TS. H.Q.Thắng, Dep. of SE
19
The
Structure
of Run-
Time
Memory
2. Memory Management
AT in PMT
(c) PGS.TS. H.Q.Thắng, Dep. of SE
20
Structure of a
Called
Method’s
Stack Frame
2.1 Method, Locals, Parameters and the Runtime Stack
AT in PMT (c) PGS.TS. H.Q.Thắng, Dep. of SE
21
Whenever a method is called a new set of “local”
declaration is allocated space on the run-time stack.
This space is part of a block called stack frame
The stack frame contains spaces for the called
methods argument value, local variables, a static
link to the static ares and dynamic link to the stack
frame for the method that called this method
2.1 Method, Locals, Parameters and the Runtime Stack
Example Program with
Methods and Parameters
Run-Time Stack with Stack Frames for Method Invocations
Passing an Argument

by Reference Example
AT in PMT (c) PGS.TS. H.Q.Thắng, Dep. of SE
25
2.2 Pointer
A Pointer is implemented as a memory address or
reference
A Simple C-Style Linked List in the Heap
struct Node {
int key;
Node *next;
}
Node * head;

×