Block-Structured Procedural Languages Lecture 11: Dolores Zage.

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms
Advertisements

CSI 3120, Implementing subprograms, page 1 Implementing subprograms The environment in block-structured languages The structure of the activation stack.
CS0007: Introduction to Computer Programming Console Output, Variables, Literals, and Introduction to Type.
CS 415: Programming Languages Algol Aaron Bloomfield Fall 2005.
Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
Chapter 9 Subprograms Specification: name, signature, actions Signature: number and types of input arguments, number and types of output results –Book.
Subprogram Control - Data sharing Mechanisms to exchange data Arguments - data objects sent to a subprogram to be processed. Obtained through  parameters.
Kernighan/Ritchie: Kelley/Pohl:
Elementary Data Types Prof. Alamdeep Singh. Scalar Data Types Scalar data types represent a single object, i.e. only one value can be derived. In general,
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
Chapter Four Data Types Pratt 2 Data Objects A run-time grouping of one or more pieces of data in a virtual machine a container for data it can be –system.
(1) ICS 313: Programming Language Theory Chapter 10: Implementing Subprograms.
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
Names, Bindings, Type Checking, and Scopes
1 Chapter 7: Runtime Environments. int * larger (int a, int b) { if (a > b) return &a; //wrong else return &b; //wrong } int * larger (int *a, int *b)
C Programming Language 4 Developed in 1972 by Dennis Ritchie at AT&T Bell Laboratories 4 Used to rewrite the UNIX operating system 4 Widely used on UNIX.
Chapter3: Language Translation issues
G. Levine Chapter 6 Chapter 6 Encapsulation –Why do we want encapsulation? Programmer friendliness- programmer need not know about these details –Easier.
Encapsulation by Subprograms and Type Definitions
Run time vs. Compile time
Chapter 2: Impact of Machine Architectures What is the Relationship Between Programs, Programming Languages, and Computers.
Elementary Data Types Scalar Data Types Numerical Data Types Other
Chapter 9: Subprogram Control
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
1 Contents. 2 Run-Time Storage Organization 3 Static Allocation In many early languages, notably assembly and FORTRAN, all storage allocation is static.
Chapter 3: Introduction to C Programming Language C development environment A simple program example Characters and tokens Structure of a C program –comment.
MT311 Java Application Programming and Programming Languages Li Tak Sing ( 李德成 )
1 Chapter 5: Names, Bindings and Scopes Lionel Williams Jr. and Victoria Yan CSci 210, Advanced Software Paradigms September 26, 2010.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Universal Concepts of Programming Creating and Initializing local variables on the stack Variable Scope and Lifetime Stack Parameters Stack Frames Passing.
5-1 Chapter 5: Names, Bindings, Type Checking, and Scopes Variables The Concept of Binding Type Checking Strong Typing Type Compatibility Scope and Lifetime.
18-2 Understand “Scope” of an Identifier Know the Storage Classes of variables and functions Related Chapter: ABC 5.10, 5.11.
Simple Procedural Languages: FORTRAN and C Lecture 9: Dolores Zage.
Names and Scope. Scope Suppose that a name is used many times for different entities in text of the program, or in the course of execution. When the name.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
Compiler Construction
1 Names, Scopes and Bindings Aaron Bloomfield CS 415 Fall
Chapter 6: User-Defined Functions
CS 153: Concepts of Compiler Design October 5 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CSC3315 (Spring 2008)1 CSC 3315 Subprograms Hamid Harroud School of Science and Engineering, Akhawayn University
Ch. 5 Ch. 51 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (more notes) Dr. Carter Tiernan.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
COMP3190: Principle of Programming Languages
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
Structure of Programming Languages Names, Bindings, Type Checking, and Scopes.
Ch. 5 Ch. 51 jcmt Summer 2003Programming Languages CSE3302 Programming Languages (more notes) Summer 2003 Dr. Carter Tiernan.
Concepts of programming languages Chapter 5 Names, Bindings, and Scopes Lec. 12 Lecturer: Dr. Emad Nabil 1-1.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
10-1 Chapter 10: Implementing Subprograms The General Semantics of Calls and Returns Implementing “Simple” Subprograms Implementing Subprograms with Stack-Dynamic.
ISBN Chapter 10 Implementing Subprograms.
Implementing Subprograms
Names, Scope, and Bindings Programming Languages and Paradigms.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Operational Semantics (Slides mainly.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
Chapter 1: Preliminaries Lecture # 2. Chapter 1: Preliminaries Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation.
1 Compiler Construction Run-time Environments,. 2 Run-Time Environments (Chapter 7) Continued: Access to No-local Names.
Data Types In Text: Chapter 6.
Zuse’s Plankalkül – 1945 Never implemented Problems Zuse Solved
Implementing Subprograms
Type Checking, and Scopes
CMPE 152: Compiler Design October 2 Class Meeting
Subprograms and Programmer Defined Data Type
Names and Binding In Text: Chapter 5.
CS 432: Compiler Construction Lecture 11
CMPE 152: Compiler Design March 7 Class Meeting
SPL – PS1 Introduction to C++.
Presentation transcript:

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

Pascal Designed in 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

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

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.

Hello World Program trivial (input, output); var I: integer; procedure printit; begin writeln(‘Hello World’) end; I := 2; if I>= 2 then printit end.

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.

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

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

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

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

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

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

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

Annotated Example 19 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

Data Objects Pascal is mostly strongly typed array bounds being part declared as part of the type

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)

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 (-)

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

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

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.

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

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 compilers Option 2 7 compilers Option 3 3 compilers

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

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

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