You are on page 1of 50

PLI Training

DAY 5

PL/I Training - Day 5 1


PL/I Training

% Statements

PL/I Training - Day 5 2


% Statements

 Statements that direct the operation of the compiler, begin with a percent
(%) symbol. These statements must not have label or condition prefixes,
and cannot be a "unit" of a compound statement.
 The % statements allow you to control the source program listing and to
include external strings in the source program. These control statements,
%INCLUDE, %PRINT, %NOPRINT, %PAGE, and %SKIP

PL/I Training - Day 5 3


% Statements - %INCLUDE

 Directs the compiler to incorporate external strings of characters and/or


graphics into the source program.

>>__%INCLUDE__member________ _________;_______________><
|_dataset__(__member__)_|

 Dataset specifies the ddname used in the FILEDEF command for the VM
library, or in the ddname of the appropriate DD statement for MVS. The
default ddname is SYSLIB. The ddname must refer to a partitioned data set.
 Dataset is optional.
 Member specifies the name of the library member to be incorporated in VM
or the name of the data set member to be incorporated in MVS.

PL/I Training - Day 5 4


% Statements - %INCLUDE

 %INCLUDE statements can be nested. In other words, included text can


contain %INCLUDE statements.
 For example, if the source program contains the following statement:

% INCLUDE PAYRL;

PL/I Training - Day 5 5


%Statements - %PRINT

 Directs the compiler to resume printing the source and in-source listings.

>>__%PRINT__;_____________________________________><

 %PRINT is in effect at the onset of both the in-source and the source
listings, provided that the relevant compile-time options are specified.
 The %PRINT statement must be on a line with no other statements. It must
not appear within another statement.

PL/I Training - Day 5 6


%Statement - %NOPRINT

 The %NOPRINT statement causes printing of the source and in-source


listings to be suspended until a %PRINT statement is encountered. The
syntax for the %NOPRINT statement is:

>>__%NOPRINT__;_______________________________________><

 The %NOPRINT statement must be on a line with no other statements. It


must not appear within another statement.

PL/I Training - Day 5 7


%Statement - %PAGE

 The statement following a %PAGE statement in the program listing is


printed on the first line (after the page headings) of the next page. The
syntax for the %PAGE statement is:

>>__%PAGE__;_________________________________________><

 This statement controls both the in-source and the source listing.
 For paging to take place, the %PAGE statement must be on a line with no
other statements. When paging takes place, %PAGE does not appear in the
formatted listing

PL/I Training - Day 5 8


%Statement - %SKIP

 The specified number of lines following a %SKIP statement in the program


listing are left blank. The syntax for the %SKIP statement is:

>>__ %SKIP_____ __;___________________________________><

|_(__n__)_|

 n Specifies the number of lines to be skipped. It must be an integer in the


range 1 through 999. If n is omitted, the default is 1. If n is greater than the
number of lines remaining on the page, the equivalent of a %PAGE
statement is executed in place of the %SKIP statement.
 This statement controls both the in-source and the source listing. For
skipping to take place, the %SKIP statement must be on a line with no other
statements. When skipping takes place, %SKIP does not appear in the
formatted listing.

PL/I Training - Day 5 9


PL/I Training

Storage Classes

PL/I Training - Day 5 10


Storage Classes

Data names represent locations in main storage where the data items are
recorded.
When a locations in the main storage has been associated with a data name,
storage is said to be allocated.
The time at which storage is to be allocated depends on the storage class of the
data.

Storage Classes

Automatic Static Based Controlled

PL/I Training - Day 5 11


Storage – AUTOMATIC Storage

 Unless otherwise declared variables (most of them) would have the


AUTOMATIC attribute.
 The AUTOMATIC attribute results in dynamic storage allocation.
 Automatic variables are allocated on entry to the block in which they are
declared. They can be reallocated many times during the execution of a
program.
 New data gets over-layed over the storage released by declarations of a
deactivated block.
 The total storage requirements of the program may be reduced by this
automatic overlay feature.

AUTOMATIC
AUTO

PL/I Training - Day 5 12


Storage – AUTOMATIC Storage

A:PROC; Each time procedure B is invoked, the


variables X and Y are allocated
.
storage. When B terminates the
. storage is released, and the values
CALL B; they contain are lost. The storage that
is freed is available for allocation to
B:PROC;
other variables. Thus, whenever a
DECLARE (X,Y) AUTO; block (procedure or begin) is active,
. storage is allocated for all variables
declared automatic within that block.
. Whenever a block is inactive, no
END B; storage is allocated for the automatic
. variables in that block.

CALL B;

PL/I Training - Day 5 13


Storage – STATIC Storage

 You use static storage when the variable is local to the procedure and the
value it contains must be saved between successive invocations.
 Variables declared with the STATIC attribute are allocated prior to running a
program.
 They remain allocated until the program terminates (STATIC storage in
fetched procedures is an exception).
 The program has no control on the allocation of static variables during
execution.

DCL TABLEA(10,2) STATIC FIXED DEC(5);

PL/I Training - Day 5 14


Storage – BASED Storage

DCL X(2,2) FIXED DEC(5) BASED (P)

Based Variable
Pointer

PL/I Training - Day 5 15


Storage – BASED Storage

 Declaration of a based variable is a description of the generation; that is, the


amount of storage required and its attributes.
 A locator value identifies the location of the generation. Any reference to the
value of a based variable that is not allocated produces undefined results.
 In BASED storage the address of the identifier is contained in a pointer
variable.

DCL P1 POINTER;
DCL A(10,2) CHAR(10) BASED (P1);

 A pointer variable is a special variable that can be used to locate data – it


points data in the main storage. In other words, a pointer variable is an
address.

PL/I Training - Day 5 16


Storage – BASED Storage

 BASED (P1) – The address of array is determined by the storage address of


P.
 Before a reference is made to a based variable a value must be given to the
pointer associated with it.
 A value can be given to a pointer by:
 Assignment of the value returned by the ADDR built-in function.
 Assignment of the value of another pointer.
 With the SET option of a READ or LOCATE statement.
 By an ALLOCATE statement

PL/I Training - Day 5 17


Storage – BASED Storage

ADDR DCL X1 BIT(32) BASED (P);


X2 FLOAT(6);
P = ADDR(VALUE2);
Assignment DCL (P,Q)POINTER;
P = ADDR(AREA);
Q=P
SET READ FILE(INFIL1) SET(X);
LOACTE FILE(OUTFILE) SET(Y);
ALLOCATE DCL (X,Y) POINTER;
DCL AREA FIXED DEC(15,2) BASED(X);
ALLOCATE AREA;
ALLOCATE AREA SET(Y);

PL/I Training - Day 5 18


Storage – BASED is different from DEFINED Attribute

 Allows overlay of one storage area over another.

DCL B CHAR (100);


DCL A CHAR(50) DEFINED B;

 Both A and B reference the same storage area.


 Only identifiers of the same base, scale and precision may be over-layed.
 With a based variable the same area can be made available to store
different types of data at different times in the program depending on the
program logic.

PL/I Training - Day 5 19


Storage – BASED Storage

DCL A(100) FIXED BIN(31);


DCL B(50) FLOAT DEC(6) BASED(P);

DCL P POINTER;
P=ADDR(A);

 It is possible for more than one variable to be based on the same pointer.

PL/I Training - Day 5 20


Storage – LOCATE Mode

In the MOVE mode:

READ READ

Dataset Buffer Program

WRITE WRITE

READ – Record is moved from the dataset to the buffer and from the buffer to
the program (the record specified in the INTO).
WRITE – Record is moved from the program (the record specified in FROM) to
the buffer and from the buffer to the dataset.

PL/I Training - Day 5 21


Storage – LOCATE Mode

 The locate mode allows the programmer to process the data directly from
the buffer without moving it to the program.
 Here the data transmission statement identifies the location of the storage
allocated to a record in the buffer.
 Locate mode requires the files to be declared as BUFFERED
SEQUENTIAL.

PL/I Training - Day 5 22


Storage – LOCATE MODE – Input Example

The READ statement causes a block of


DCL P POINTER;
data to be read from the dataset to the
DCL 1 IN_REC BASED (P), buffer and sets the pointer variable P in the
2 A PIC’99’, SET option to point to the location in the
buffer of the next record.
2 B CHAR(8),
2 C FIXED DEC(7,2), The data is then processed with reference
to the based variable (IN_REC) associated
2 REST CHAR(6); with pointer variable.
READ FILE(TAPEIN) SET(P); The record is only available until the
execution of the next READ statement that
refers to the same file.
The SET option of the READ statement
causes the pointer variable P to be set to
the starting address of the next record in
the internal buffer

PL/I Training - Day 5 23


Storage – LOCATE Mode - Input

 When data is processed in an input buffer, improved results can be obtained


if two buffers (at least) are reserved.

DCL INFIL1 FILE INPUT RECORD BUFFERED ENV(BLKSIZE(800)


RECSIZE(80) BUFFERS(2));

PL/I Training - Day 5 24


Storage – LOCATE Mode - Input

DCL P POINTER; READ FILE(TAPE) SET(P);


DCL INFILE ……………. SELECT(HEADREC.CODE);
DCL 1 HEADREC BASED(P),
2 CODE CHAR(1), WHEN(‘H’) CALL HEADY;
2 COUNTRY CHAR(20), WHEN(‘F’) CALL FOOTY;
2 FILL1 CHAR(59);
END;
DCL 1 DETREC BASED(P),
2 CODE CHAR(1),
2 PLAYER CHAR(20),
2 RUNS PIC’(5)9’,
2 WICKETS PIC’(3)9’,
2 CATCHES PIC’(3)9’,
2 FILL2 CHAR(48);

PL/I Training - Day 5 25


Storage – LOCATE Mode - Output

LOCATE OUTREC FILE(OUTFIL) SET(Q);

Instead of WRITE Based Variable File Name Pointer Variable

PL/I Training - Day 5 26


Storage – LOCATE Mode - Output

 Using a LOCATE statement, the structure of the based variable is


superimposed on the data in the output buffer so that any reference to that
allocation of the based variable is a reference to the data.
 The output record area once located is available. Data may be moved to it
by assignment statements.

PL/I Training - Day 5 27


Storage – LOCATE Mode - Example

PGM: PROC OPTIONS(MAIN);


DCL INPUT FILE INPUT RECORD SEQUENTIAL BUFFERED
ENV(F BLKSIZE(800) RECSIZE(80) BUFFERS(2));
DCL OUTPUT FILE OUTPUT RECORD SEQL BUFFERED
ENV(F BLKSIZE(400) RECSIZE(80) BUFFERS(2));
DCL 1 INPUT_REC BASED(P),
2 FIELD1 CHAR(20),
2 FIELD2 CHAR(60);
DCL 1 OUTPUT_REC BASED(Q),
2 FIELD1 CHAR(20),
2 FIELD2 CHAR(60);
DCL FLAG1 BIT(1) INIT(‘1’B);
DCL FLAG2 BIT(1) INIT(‘0’B);
DCL (P,Q) POINTER;
ON ENDFILE(INPUT)
FLAG1 = NO
READ FILE(INPUT) SET(P);
DO WHILE(FLAG1)
LOCATE OUTPUT_REC FILE(OUTPUT) SET(Q);
OUTPUT_REC = INPUT_REC;
READ DILE(INPUT) SET(P);
END
END PGM;

PL/I Training - Day 5 28


Storage – CONTROLLED Storage

 Variables declared as CONTROLLED are allocated only when they are


specified in an ALLOCATE statement. You have individual control over each
controlled variable.
 A controlled variable remains allocated until a FREE statement that names
the variable is encountered or until the end of the program in which it is
allocated.
 Controlled Storage is used to create a stack.

PL/I Training - Day 5 29


Storage – CONTROLLED Storage

A:PROC;
DCL X CONTROLLED;
CALL B;
.
.
.
B:PROC;
ALLOCATE X;
.
.
END B;
END A;

PL/I Training - Day 5 30


Storage – CONTROLLED Storage

 The ALLOCATE statement may be used to specify the amount


of storage required for arrays if the array size is to
be established during program execution rather than at
compile-time.

DCL ARRAY(*,*) FIXE DEC(5) CONTROLLED;


GET LIST(I,J);
ALLOCATE ARRAY(I,J);

PL/I Training - Day 5 31


Storage – CONTROLLED Storage - Stacks

 Controlled attribute stores the allocations in a stack with the most recent
one at the top.

DCL MSSG CHAR(100) CONTROLLED; PRINT_ST: PROC;

. DO WHILE (ALLOCATION(MSSG) >0

. PUT SKIP LIST (MSSG);

IF ERROR THEN FREE MSSG;

DO; END;

ALLOCATE MSSG; END PRINT_ST;

MSSG = ‘ERROR MEGGAGES …’


END;

PL/I Training - Day 5 32


Storage – CONTROLLED Storage – ALLOCATION
Function

A = ALLOCATION(Y);

Returns ‘1’B if the storage has been allocated to X


‘0’B if storage is not allocated.

PL/I Training - Day 5 33


Storage – List Processing

 List processing is the name for a number of techniques to help manipulate


collections of data.
 Although arrays and structures in PL/I are also used for manipulating
collections of data, list processing techniques are more flexible since they
allow collections of data to be indefinitely reordered and extended during
program execution.
 In list processing, a number of based variables with many generations can
be included in a list. Members of the list are linked together by one or more
pointers in one member identifying the location of other members or lists.

PL/I Training - Day 5 34


Storage – List Processing

LISTS

Sequential Linear Circular Bi-directional Binary Tree Stacks

PL/I Training - Day 5 35


Storage – List Processing – Sequential List

 Most basic form of a list is an array.

PL/I Training - Day 5 36


Storage – List Processing – Linear List

 A linear list is a collection of elements in which each element is a structure


(also called data structure). A pointer variable is included in this structure
and this pointer variable contains the storage address of the next item in the
list.

DCL 1 CRICKET,
2 NAME CHAR(20),
2 NEXT_PTR POINTER;

Gavaskar Richards …… Vengsarkar


Pointer Pointer Pointer Null

PL/I Training - Day 5 37


Storage – List Processing – Linear List

 The elements in the list do not have be contiguous in the main storage.
This helps in more efficient use of the main storage.
 The main storage need only be allocated for those items that are currently in
the list and subsequently freed when specific items need no longer remain
in the list.

DCL NULL BUILTIN;


DCL NEXT_PTR POINTER;
NEXT_PTR = NULL;

 New items can be added and existing items can be deleted in a linear list
without resorting the list – you simple change the pointers.

PL/I Training - Day 5 38


Storage – List Processing – Circular List

 A circular list is a linear list in which the last element contains the address
for the first element in the list rather than the end-of-list indicator (null value).

PL/I Training - Day 5 39


Storage – List Processing – Bidirectional List

 A bidirectional list contains two points per element. One pointer points to
the preceding element in the list and the other to the succeeding element.
 Useful in applications that need processing in forward and backward
directions.

PL/I Training - Day 5 40


Storage – List Processing – Binary Tree

 A binary tree is a collection of structures that are linked together so that


every item can be reached by a sequence of pointers.
 Each element in the binary tree is called a node.
 The first element or node is referred to as the top of the tree.
 Each node has two pointers associated with it: a left pointer and a right
pointer.

DCL 1 CRICKET BASED,


2 NAME CHAR(20),
2 LEFT POINTER,
2 RIGHT POINTER;

PL/I Training - Day 5 41


Storage – List Processing – Binary Tree
GAVASKAR

BORDER TENDULKAR

AZHARUDDIN FLEMING RICHARDS VISWANATH


* * *
* *

VENGSARKAR
BOON
*
*
*
*

PL/I Training - Day 5 42


Storage – List Processing – Binary Tree

 The advantage of creating this type of list structure is that the items entered
into the binary tree did not have to be sorted.
 Search time to locate an item is reduced.

PL/I Training - Day 5 43


Storage – List Processing – Allocating Storage

 Each element is in a list is allocated storage by a PL/I program at the time storage is needed for
that item.
 One method for allocating storage is a based structure and associated pointer variable.

Declare
DCL 1 CRICKET BASED(P),
2
Allocate Storage
ALLOCATE CRICKET

PL/I Training - Day 5 44


Storage – List Processing – Allocating Storage

Declare
DCL 1 CRICKET BASED,
2
Allocate Storage
ALLOCATE CRICKET SET P

 If the pointer variable has not been declared with the based variable, then
the ALLOCATE statement must indicate the name of the pointer to be set.
 However CRICKET must always be referenced by a locator qualifier.

P -> CRICKET

 If P was set by the above ALLOCATE statement (2), the reference is to the
current generation of CRICKET

PL/I Training - Day 5 45


Storage – List Processing – Example of a Linear List

Linear List Example

PL/I Training - Day 5 46


PL/I Training

Miscellaneous Topics

PL/I Training - Day 5 47


Miscellaneous – DEFAULT Statement

 The DEFAULT statement specifies data-attribute defaults (when attribute


sets are not complete). Any attributes not applied by the DEFAULT
statement for any partially complete explicit or contextual declarations, and
for implicit declarations, are supplied by language-specified defaults.

PL/I Training - Day 5 48


Miscellaneous - INTERNAL and EXTERNAL Attributes

 INTERNAL specifies that the name can be known only in the declaring
block. Any other explicit declaration of that name refers to a new object with
a different, non-overlapping scope.
 A name with the EXTERNAL attribute can be declared more than once,
either in different external procedures or within blocks contained in external
procedures. All declarations of the same name with the EXTERNAL
attribute refer to the same data. The scope of each declaration of the name
(with the EXTERNAL attribute) includes the scopes of all the declarations of
that name (with EXTERNAL) within the program.

PL/I Training - Day 5 49


Miscellaneous – CONNECTED Attribute

 Elements, arrays, and major structures are always allocated in connected


storage. References to unconnected storage arise only when you refer to an
aggregate that is made up of noncontiguous items from a larger aggregate.

PL/I Training - Day 5 50

You might also like