• Keine Ergebnisse gefunden

2. 1 Structure of a Standard Pascal Program

Im Dokument PASCAL-a6 USER'S GUIDE (Seite 34-41)

Pascal is a block-structured language. Programs in such a language are composed of sections (called blocks) that perform logically related functions and may be nested inside one another. PL/M, PL/I, and Algol are also block-structured languages;

FORTRAN, BASIC, COBOL, and most assembly languages are not. Block struc-ture in a programming language has several advantages:

It permits you to concentrate on one part of a program at a time, isolating that part from the rest of the program.

It results in programs whose logical structure is easy to read and understand.

• It allows the compiler to impose certain rules and checks over the scope of varia-bles and the flow of control in a program, enabling you to discover and correct many types of logical errors at compile time.

Figure 2-1 illustrates the structure of a Pascal program. (Section 9.2 gives a more detailed explanation of the sample program in figure 2-1.)

A block in Pascal consists of the following three parts:

1. Definitions and declarations

2. Procedure and function declarations 3. Statements

The first part defines data items--constants, types, and variables-and labels used within the block. Definitions introduce items that have meaning only at compile time, and declarations introduce items that have meaning at run time as well. Procedure declarations and function declarations define subprograms, which are blocks nested, or contained, within the block in question. Statements specify the actions to be performed by the block. Of the three parts, only the statement part is required in every block.

Program Structure Pascal-86 User's Guide

program TreeTraversal (Input ,Output); •• - - - - - PROGRAM HEADING const MaxNumNodes = 20; •• _ _ _ _ _ _ _ _ _ _ _ __

ExpressionTree : Tree;

DataFile : text;

VARIABLE DECLARATIONS

BLOCK 2 (PROCEDURE DECLARATION)

C* ---*)

procedure BuildTree; C* build tree from user input *)

var FindRoot : booleCln; VARIABLE DECLARATION BLOCK 6 (PROCEDURE DECLARATION) • PROCEDURE HEADING

procedure Add Node; (* add a node to the tree *)

begin

write(NodeCharacter : 3, NodeIndex: 3);

with ExpressionTree[NodeIndex] do begin Symbol:=NodeCharacter;

readCOataFile,Left); writeCLeft : 3);

readCOataFile,Right); writeCRight : 3);

readlnCOataFile);

writelnC'IN~UT IS:'); writeln;

AddNode;

reoeat

readCwataFile,NodeCharacter,NodeIndex);

if NodeIndex = 1 then Find~oot := true else AddNode

until C~indRoot) or Ceof(OataFile»;

writeln

end; (* BuildTree *) BLOCK 3 (PROCEDURE DECLARATION)

,

with ExpressionTree[NodeIndex] do if Left <> 0 then begin

Figure 2-1. Sample Program 2A: Binary Tree Traversal in Standard Pascal

Pascal-86 User's Guide

BLOCK 1 (PROGRAM BLOCK) (CONTINUED) BLOCK 4 (PROCEDURE DECLARATION)

(* - - - - *) procedure Prefix(NodeIndex : Subscr); (* write out the

tree in prefix notation *) • PROCEDURE HEADING begin

with ExpressionTree(Nodelndex) do if Left <> 0 then begin

with ExpressionTree(Nodelndex) do if Left <> 0 then begin

Figure 2-1. Sample Program 2A: Binary Tree Traversal in Standard Pascal (Cont'd.)

Program Structure

Program Structure Pascal-86 User's Guide

2-4

The program block is the outer-level block in a program. The procedure and function declarations contained within the program block are also blocks. In standard Pascal, procedure and function blocks cannot stand alone; however, they may contain all three parts of a block, including other procedure and function declarations.

Figure 2-1 consists of a program block containing four second-level procedure decla-rations, marked BLOCK 2, BLOCK 3, BLOCK 4, and BLOCK 5. BLOCK 2 itself contains another procedure declaration, marked BLOCK 6. Thus the structure is hierarchical.

This block structure encourages top-down development and stepwise-refinement techniques of program design. The nesting level of a procedure or function may correspond to the level of that operation in a stepwise breakdown of the problem. For example, for the tree traversal program of figure 2-1, the programmer first divided the task into four main parts: build tree from user input, write out tree in infix notation, write out tree in prefix notation, and write out tree in postfix notation. The program may perform each of these tasks more than once. Next, within the "build tree from user input" task, he identified a sub-task: add a node to the tree. Larger programs may include many more levels of nesting.

The program block defines the main program, with which execution starts when you run the program. During execution, the outer statement part of the program block may make calls to subprogram blocks (procedures and functions) contained within the program block. Thus, the statement part specifies the order in which the program performs the sub-tasks.

Procedures and functions are similar structurally, but differ in how they are invoked and in their purpose. A procedure is invoked via a procedure statement (similar to a

"call" in some other languages). A/unction is invoked by giving its name and a list of arguments in an expression within a statement. The function returns a value to the calling program; this value replaces the function name and argument list in the expression. Thus procedures often perform actions that change many values, but the primary purpose of a function is to return a single value.

For example, the following function returns the absolute value of the argument corre-sponding to x: procedure is executing. In Pascal, all procedures and functions are automatically reentrant, as long as they do not change any global variables (defined in 4.1.2). Thus you do not need to make any special provisions for a procedure which may be inter-rupted during its execution, and subsequently invoked to process the interrupt.

Another important capability of Pascal is recursion. A recursive procedure or function is one that calls itself-either directly or indirectly. For instance, if procedure A contains a call to procedure A, it is (directly) recursive. If A calls Band B calls A, the two procedures are both indirectly (mutually) recursive. If A calls B, B calls C, C calls D, and D calls A, all four procedures are indirectly (mutually) recursive. A recursive procedure or function is frequently a natural way to express an algorithm.

In figure 2-1, the procedures Infix, Prefix, and Postfix (BLOCK 3, BLOCK 4, and BLOCK 5) are directly recursive.

Pascal-86 User's Guide Program Structure

Certain useful procedures and functions are included as part of the Pascal-86 language.

These include arithmetic and conversion functions, ordinal and Boolean functions, input/output procedures, procedures for allocating dynamic variables, and proce-dures and functions to communicate directly with microprocessor hardware. Some of these are from standard Pascal; others are Intel extensions. You need not declare these predefined procedures and predefined functions in your program; simply invoke them, using procedure statements or expressions, when needed. The program of figure 2-1 calls the predefined procedures read, readln, write, and writeln.

Program Structure

BLOCK 1 (PROGRAM BLOCK)

(* This program builds a binary tree of cha,.acters fro_ BLOCK 2 (PROCEDURE DECLARATION)

(*

---*)

procedure BuildTree; • PROCEDURE HEADING

var FindRoot : booleani. VARIABLE DECLARATION BL~;~~ r:uOrCeEDAUdRJNDoEdCeLf~ATlON) 1 PROCEDURE HEADING

begin

write(NodeCharacter : 3, NodeIndex: 3);

with ExpressionTree[N deIndex] do begin Symbol:=NodeCharacter;

read(OataFile,Left); write(Left : 3); STATEMENT read(OataFile,Right); write(Right : 3); PART readln(DataFile);

writeln("INPUT IS:"); write1n;

AddNode;

repeat

read(DataFile,NodeCharacter,Nodelndex);

if NodeIndex = 1 then FindRoot := true else AddNode

until (Find~oot) or (eof(OataFile»;

writeln

~ritelni write In; writeln;

read(OataFile,NodeCharacter,NodeIndex);

Figure 2-2. Sample Program 2B: Binary Tree Traversal Using Separately Compiled Modules

Pascal-86 User's Guide

MODULE 2 (NON-MAIN MODULE)

eodu.1. 8inaryTr •• -outpuU • MODULEttEAOtHG

p~.lie linaryTr . . Output;

procedure Infi.' . . . . ln4 •• : $~$cr);

procedure p,.efix(Nodelnd.. : Subscr);

procedure Pos1:fixC"od.lnde. : $ubscr);

public 8inaryTr .... ain; i

BLOCK 4 (PROCEDURE DECLARATION)

(* - - - - *)

procedure InfixCNodelndex : Subscr); (* write out the tree in infix notatio'n *) •• - - - - - PROCEDURE HEADING beQin

with ExpressionTree[Nodelndex) do if Left <> 0 then beQin

BLOCK 5 (PROCEDURE DECLARATION)

STATEMENT

with ExpressionTree[NodeIndex) do if Left <> 0 then begin

BLOCK 6 (PROCEDURE DECLARATION)

}

with ExpressionTree[Nodelndex) do if Left <> 0 then begin

Figure 2-2. Sample Program 2B: Binary Tree Traversal Using Separately Compiled Modules (Cont'd .)

Program Structure

CHAPTER 3 LANGUAGE ELEMENTS

Chapter 2 presented the overall structure of a Pascal-86 program. This chapter, and the chapters following it through Chapter 8, define the details of the Pascal-86 language.

Rules governing the coding of programs are of two types: syntax rules and semantic rules. The syntax of a programming language is the set of rules defining what sequences of symbols make up acceptable programs in the language. The semantics of a language is the set of rules for determining, given a syntactically acceptable program, what that program means-that is, what actions it will cause the processor to take. It is possible for a program to be syntactically correct but semantically meaningless.

In this manual, the syntax of each part of a program is generally defined using the syntax notation described in the preface (How To Use This Manual). In cases where such symbolic definitions would be cumbersome, the syntax is presented in prose.

Most of these cases occur in this chapter.

Along with the syntax definition of each language construct, the accompanying semantic rules are given in prose. The syntax and the semantic rules are generally followed by one or more examples of the language construct being defined.

The syntax of the entire Pascal-86 language, in syntax notation, is given in Appendix D. For those who prefer the syntax diagrams used in an appendix to the Pascal User Manual and Report by Kathleen Jensen and Nicklaus Wirth, and in a number of textbooks on Pascal, Appendix E provides the syntax of the language in that form.

This chapter defines the symbols that make up the building blocks, or "words," of a program. These elements, such as digits, blanks, keywords, identifiers, and special punctuation symbols, make up the larger "sentences" of the language as defined in the chapters that follow.

3.1 Basic Alphabet

The basic building blocks of a Pascal-86 program are:

• The upper-case and lower-case letters

• The digits 0 through 9

The following keywords (word symbols):

AIiD

Im Dokument PASCAL-a6 USER'S GUIDE (Seite 34-41)