block-structured procedural languages lecture 11: dolores zage

26
Block-Structured Procedural Languages Lecture 11: Dolores Zage

Upload: aldous-floyd

Post on 26-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Block-Structured Procedural Languages Lecture 11: Dolores Zage

Block-Structured Procedural Languages

Lecture 11:

Dolores Zage

Page 2: Block-Structured Procedural Languages Lecture 11: Dolores Zage

Block-Structured• Class of languages• Pascal is the best representative• designed for programmer flexibility than absolute

run-time performance (procedural languages)• possess activation records but employ scope rules

and nested block structures for structured data.• There is some run-time penalty

Page 3: Block-Structured Procedural Languages Lecture 11: Dolores Zage

Pascal

• Designed in 1968-1970 by Nicklaus Wirth

• overcome the deficiencies of ALGOL

• From the late 70’s through late 80’s it was dominant language

• although use of Pascal is dropping many of its concepts are borrowed by language designers for newer languages

Page 4: Block-Structured Procedural Languages Lecture 11: Dolores Zage

History• Design a language that could be compiled in

one pass

• designed a recursive descent parsing algorithm for initial compile

• developed the now-famous “P code” interpreter

• P code - hypothetical machine language for a machine that was based on stack architecture

Page 5: Block-Structured Procedural Languages Lecture 11: Dolores Zage

History

• P code made Pascal relatively easy to transport to other computer systems.

• Pascal compiler was written in Pascal itself.

• This is called bootstrapping

• compile the compiler by hand into its P-code, then the P code execute this hand-translated compiler.

Page 6: Block-Structured Procedural Languages Lecture 11: Dolores Zage

Hello WorldProgram trivial (input, output);

var I: integer;

procedure printit;

begin

writeln(‘Hello World’)

end;

I := 2;

if I>= 2 then printit

end.

Page 7: Block-Structured Procedural Languages Lecture 11: Dolores Zage

Overview of Pascal

• Run-time structure like C

• it also has the ability to declare internal local procedures and create a full nested name hierarchy of data objects

• Pascal program formed form a single main program block, which contains within the definitions of the subprograms used.

Page 8: Block-Structured Procedural Languages Lecture 11: Dolores Zage

Each block

• Has a characteristic structure:– a header giving specification of parameters and

results– followed by constant definition, type

definitions, local variable declarations other nested subprogram definitions

– statements for the executable part

Page 9: Block-Structured Procedural Languages Lecture 11: Dolores Zage

Primitive and Structured Data Types• Integers

• reals• characters• enumerations• Booleans• arrays• records• sequential files• limited form of sets• pointer type

• Type statement

• allows the programmer to develop new types, but does not provide grouping and encapsulation with a set of subprograms for operating on new type data objects

Page 10: Block-Structured Procedural Languages Lecture 11: Dolores Zage

Subprograms

• Functions - if they return a single value

• procedures - if they act by modifying their parameters or global variables

• type specifications must be provided for all parameters

• complete static type checking for correspondence

Page 11: Block-Structured Procedural Languages Lecture 11: Dolores Zage

Data-Control Structure

• Used standard static scope rules and nested program format characteristic of block structure

• parameters may be transmitted either by value or by reference

• subprograms may be passed as parameters

Page 12: Block-Structured Procedural Languages Lecture 11: Dolores Zage

Execution

• Central stack used for subprogram activation records

• heap storage for data objects created directly for use with pointer variables

• and a static area for subprogram code segments and run-time support routines

• run-time support - procedures for I/O and for storage management

Page 13: Block-Structured Procedural Languages Lecture 11: Dolores Zage

Annotated Example

1 Program main(input,output,infile);

2 const size = 99;

3 type Vector = array [1..size] of real;

4 var infile: text;

5 a: Vector;

6 j, k: integer;

7 function sum (v: Vector; n: integer): real;

8 var temp: real;

9 I: integer;

Input- standard input (keyboard

output - standard output (terminal)

infile - data file used within the program

upper and lower bounds must always be specified (3)

4-6 globals

call by value (7) function returns real

8-9 local variables

Page 14: Block-Structured Procedural Languages Lecture 11: Dolores Zage

Annotated Example

10 {Body of function sum}

11 begin

12 temp:=o;

13 for I := 1 to n do temp := temp = v[I];

14 sum := temp

15 end; {sum}

16 begin {of main}

17 reset(infile,’sample.data’);

18 while(not(eof(infile))do

(13) if for statement had more than one line must have begin and end

functions return value through name of the function (14)

(17) reset opens a file for input, if data comes from standard input no reset needed, rewrite opens a file for output

Page 15: Block-Structured Procedural Languages Lecture 11: Dolores Zage

Annotated Example19 begin

20 read(infile, k);

21 for j := 1 to k do

22 begin

23 read(infile, a[j]);

24 write(a[j]: 10:2)

25 end;

26 writeln;

27 writeln(‘sum’,sum(a,k)6:4);

28 readln(infile)

29 end

30 end.

20. Read the first value of k from infile. This is a free-form input. Pascal will read characters up to a comma, blank and try to translate what was read into integer format.

24 since no file name was given writes to standard output. The 10:2 prints out 10 decimal digits with 2 being fractional digits write statements append to the same line

Page 16: Block-Structured Procedural Languages Lecture 11: Dolores Zage

Data Objects

• Pascal is mostly strongly typed

• array bounds being part declared as part of the type

Page 17: Block-Structured Procedural Languages Lecture 11: Dolores Zage

Data Objects• Primitive Data types

– variable and constants– numerical data types

• no exponentation)• enumeration( literal1 < literal2 … < literalk)• succ, pred and ord enumeration operations

– Boolean– Character

• uses the enumeration operators, also has chr -> takes an integer argument and returns the corresponding character type

– Pointers - variable_name : type (new and dispose)

Page 18: Block-Structured Procedural Languages Lecture 11: Dolores Zage

Data Objects• Array• record

– (variant also) variant record structure is regarded as one of the least secure features of Pascal

• sets - – example, [Fresh, Soph, Junior, Senior]– 0000 is empty set, 0010 is set [Junior], 1100 is the set

[Fresh, Soph] – union (+), intersection(*), and difference (-)

Page 19: Block-Structured Procedural Languages Lecture 11: Dolores Zage

Subprograms and Storage Management

• Designed for a straightforward batch-processing operating environment containing only sequential files

• no special provisions for separate compilation of subprograms

• programs are assumed to be assembled into complete units

• merging is done a source language level

Page 20: Block-Structured Procedural Languages Lecture 11: Dolores Zage

Subprograms and Storage Management

• Only the standard call-return structure

• no explicit return

• since once pass strategy may have to use forward declaration, this gives the compiler enough information to correctly compile a call even though the complete definition has not yet appeared

Page 21: Block-Structured Procedural Languages Lecture 11: Dolores Zage

Program anomaly(input, output);

procedure S; {1}

begin writeln(‘wrong one’) end;

procedure T;

{missing: procedure S; forward; here}

procedure U;

begin S {2} end;

procedure S; {3}

begin writeln(‘right’); end;

begin U end;

begin T end.

Page 22: Block-Structured Procedural Languages Lecture 11: Dolores Zage

Three Possible Interpretations

• Compilation fails, since anomaly is an illegal program. The procedure call S at location 2 is calling the procedure at location 3 and that is a forward reference without a forward declaration

• The procedure call at location 2 is calling the procedure S at location 1 which is what a simple one pass compiler would do, even though it is the wrong S within the scope of the call

• The program executes with the procedure call at location 2 calling the procedure S at location 3. This is the correct procedure to call, even though the superfluous, but required forward is missing

Page 23: Block-Structured Procedural Languages Lecture 11: Dolores Zage

Which One is Correct

• 1, is clearly correct, although 3 while wrong is a reasonable interpretation, Option 2 is clearly wrong and the worst choice

• Option 1 - 3 compilers

• Option 2 7 compilers

• Option 3 3 compilers

Page 24: Block-Structured Procedural Languages Lecture 11: Dolores Zage

Language Evaluation

• Forward - confuses language design with underlying implementation

• arrays are considered a type - makes general array processing algorithms difficult, since arrays of differing sizes cannot be passed into a common subprogram

• documentation and understanding of large programs is difficult

Page 25: Block-Structured Procedural Languages Lecture 11: Dolores Zage

Language Evaluation

• Features in a language should occur by NOT by omission of information, but by commission of that information– In Pascal, parameters are all call by value unless an

explicit var attribute is added to the parameter list, making the parameter call by reference

• implementation was defined as one monolithic compilation. No concept of independently compiled modules

Page 26: Block-Structured Procedural Languages Lecture 11: Dolores Zage

Language Evaluation

• While Pascal have user-defined types support, it has no real encapsulation and information-hiding attributes. However in the 70s the state of knowledge in languages did not support this kind of thinking