clean way to do separate compilation. ECS140A Programming Languages 12-1

Similar documents
ECS140A Programming Languages 13-1

Today's Topics. CISC 458 Winter J.R. Cordy

Classes and Methods לאוניד ברנבוים המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Scope. CSC 4181 Compiler Construction. Static Scope. Static Scope Rules. Closest Nested Scope Rule

Classes and Methods גרא וייס המחלקה למדעי המחשב אוניברסיטת בן-גוריון

Scope. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

The basic operations defined on a symbol table include: free to remove all entries and free the storage of a symbol table

Ch. 10: Name Control

CSc 520 Principles of Programming Languages. What s a Programming Language? Example Languages. What s a Language???

Inheritance Modes. Controlling Inheritance 1

520 Principles of Programming Languages. Programming Language Design. Example Languages. 1: Introduction

CSE 307: Principles of Programming Languages

Abstract data types &

Abstract Data Types & Object-Oriented Programming

Modules. Cardelli, 1996

Classes and Methods עזאם מרעי המחלקה למדעי המחשב אוניברסיטת בן-גוריון מבוסס על השקפים של אותו קורס שניתן בשנים הקודמות

Exception Namespaces C Interoperability Templates. More C++ David Chisnall. March 17, 2011

What s a Programming Language?

History. Lecture 4: Modula-2 & InTouch. Modula-2. Aims Capitals. Comments. Niklaus Wirth, ETH, Switzerland. [RTCS Ch. 6.2; DMOC Ch.

Programming Language Concepts Scoping. Janyl Jumadinova January 31, 2017

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler so far

Lecturer: William W.Y. Hsu. Programming Languages

SE352b: Roadmap. SE352b Software Engineering Design Tools. W3: Programming Paradigms

System Software Assignment 1 Runtime Support for Procedures

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

Compiler Construction

The role of semantic analysis in a compiler

Scope. Chapter Ten Modern Programming Languages 1

Compiler Construction

Run-time Environments. Lecture 13. Prof. Alex Aiken Original Slides (Modified by Prof. Vijay Ganesh) Lecture 13

Run-time Environments

Run-time Environments

Compiler Construction

Chapter 3:: Names, Scopes, and Bindings (cont.)

Chapter 3:: Names, Scopes, and Bindings (cont.)

Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Classes, Structs and Records. Internal and External Field Access

Compiler Construction

QUIZ Friends class Y;

Wednesday, October 15, 14. Functions

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

LECTURE 18. Control Flow

Symbol Tables. ASU Textbook Chapter 7.6, 6.5 and 6.3. Tsan-sheng Hsu.

10. Abstract Data Types

CS558 Programming Languages Winter 2018 Lecture 4a. Andrew Tolmach Portland State University

Compiler Construction

Other Features of Procedures

Outline. When we last saw our heros. Language Issues. Announcements: Selecting a Language FORTRAN C MATLAB Java

An Activation Record for Simple Subprograms. Activation Record for a Language with Stack-Dynamic Local Variables

Syntax Errors; Static Semantics

CS11 Introduction to C++ Fall Lecture 1

Lecture 9: Parameter Passing, Generics and Polymorphism, Exceptions

Abstract Data Types and Encapsulation Concepts

QUIZ. What are 3 differences between C and C++ const variables?

OBJECT ORIENTED PROGRAMMING USING C++

CS 314 Principles of Programming Languages. Lecture 13

NAMES, SCOPES AND BINDING A REVIEW OF THE CONCEPTS

STRUCTURING THE PROGRAM

CST-402(T): Language Processors

CS558 Programming Languages

Programming Languages

QUIZ. What is wrong with this code that uses default arguments?

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

Final CSE 131B Spring 2004

Semantic Analysis. Compiler Architecture

Static Methods. Why use methods?

Chapter 11. Abstract Data Types and Encapsulation Concepts ISBN

Special Topics: Programming Languages

Symbol Tables. For compile-time efficiency, compilers often use a symbol table: associates lexical names (symbols) with their attributes

Chapter 11. Abstract Data Types and Encapsulation Concepts

Compiling and Interpreting Programming. Overview of Compilers and Interpreters

COP4020 Programming Languages. Compilers and Interpreters Robert van Engelen & Chris Lacher

Assembler Programming. Lecture 6

LECTURE 14. Names, Scopes, and Bindings: Scopes

Answer: Early binding generally leads to greater efficiency (compilation approach) Late binding general leads to greater flexibility

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

CSE 12 Week Eight, Lecture One

. p.1/23. Today. 1. Questions and discussion from lecture. 2. Type-checking Functions Arrays Records (maybe)

Programming Languages

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

A student was asked to point out interface elements in this code: Answer: cout. What is wrong?

Run Time Environment. Procedure Abstraction. The Procedure as a Control Abstraction. The Procedure as a Control Abstraction

D Programming Language

Implementing Subprograms

Chapter 9 :: Data Abstraction and Object Orientation

Chapter 11. Abstract Data Types and Encapsulation Concepts 抽象数据类型 与封装结构. 孟小亮 Xiaoliang MENG, 答疑 ISBN

COP4020 Programming Assignment 2 - Fall 2016

The Procedure Abstraction

15 Sharing Main Memory Segmentation and Paging

Object Oriented Programming in C#

Type Bindings. Static Type Binding

MIDTERM EXAM (Solutions)

Today s lecture. CS 314 fall 01 C++ 1, page 1

CSC 533: Organization of Programming Languages. Spring 2005

Discussion. Type 08/12/2016. Language and Type. Type Checking Subtypes Type and Polymorphism Inheritance and Polymorphism

Intermediate Programming, Spring 2017*

Symbol Table Information. Symbol Tables. Symbol table organization. Hash Tables. What kind of information might the compiler need?

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

Transcription:

Modularization separate program into different modules. motivation: separate major portions of code. improves maintainability. increases potential for reusability. e.g., scanner, parser, symbol table. abstraction mechanism: can hide details of how an abstraction is provided. separate compilation and strong type checking. (original) Pascal doesn t support separate compilation. separate compilation in Fortran and C. linker: simple: linker just resolves addresses. linker doesn t have source text around or information about type or number of parameters. modules can be checked at compile time. clean way to do separate compilation. ECS140A Programming Languages 12-1

so, a program will consist of several modules. two kinds of modules: definition, aka specification or public: defines constants, variables, types, and procedures it provides to other modules. implementation, aka body or private: provides types and procedures defined in definition module. interface between modules via import/export mechanism. providers export their services. using modules import services they need from other modules. Roles of modules: subroutine library just procedures; e.g., math library. data and operations on that data data hidden inside module e.g., can provide table abstraction. procedure enter( name: Identifier ); procedure lookup( name: Identifier ) : boolean; abstract type: type and operation define a stack type and provide operations that manipulate that type. type stack = ; procedure push( var x:stack; item: integer ) Note: that some abstractions can be modeled either way. e.g., a stack. ECS140A Programming Languages 12-2

Modula-2 History descendant of Pascal and Modula, which make sense since Wirth designed both languages. also benefits from Mesa, Xerox PARC. first published 1978 first implemented 1979 underwent revision 1984 has influenced many languages since then, and has directly led to Modula-3 and Oberon. flavor: Pascal plus modules. strongly-typed like Pascal corrected Pascal array type/size problem (open array) separate compilation uses export to make services available; uses import to bring in outside services. 3kinds of modules: program when have only one module, which does not export anything. consider two ways of defining stack asmodule astype ECS140A Programming Languages 12-3

Stack as module (shows basic mechanism, but Bad exposes stack s internal representation to user) user main program module foo1; from io import output, writef; from BadStackmod1 import push, pop, top; var i: integer; push(10); push(100); i:=pop(); writef(output, %d\n,i); end foo1. definition module BadStackmod1; export qualified push, pop, top; procedure push(item: integer); procedure pop() : integer; const MAX = 100; top: cardinal := 0; a: array [1..MAX] of integer; end BadStackmod1. implementation module BadStackmod1; procedure push(item: integer); procedure pop() : integer; Notes: end BadStackmod1. use of import/export. details of stack s internal representation are visible to user e.g., user (in module foo1 code) can access (read or write) top directly. can fix that easily: simply don t export top or, asseen on next page, move details into implementation module. ECS140A Programming Languages 12-4

Stack as module user main program module foo1; from io import output, writef; from Stackmod1 import push, pop; var i: integer; push(10); push(100); i:=pop(); writef(output, %d\n,i); end foo1. definition module Stackmod1; export qualified push, pop; procedure push(item: integer); procedure pop() : integer; end Stackmod1. implementation module Stackmod1; const MAX = 100; top: cardinal := 0; a: array [1..MAX] of integer; Notes: end Stackmod1. procedure push(item: integer); procedure pop() : integer; details of stack s internal representation no longer visible to user. e.g., user (in module foo1 code) can no longer access top directly. ECS140A Programming Languages 12-5

stack as type ( bad e.g., stack s representation is visible to user): user main program module foo2; from io import output, writef; from BadStackMod2 import stack, init, push, pop; var i: integer; s1, s2: stack; (* two instances of a stack *) init(s1); init(s2); push(s1,10); push(s2,23); push(s1,100); push(s1,40); i:=pop(s1); writef(output, %d\n,i); end foo2. definition module BadStackMod2; export qualified stack, init, push, pop; const MAX = 100; Type stack = record top: cardinal; a: array [1..MAX] of integer; end; procedure init(var s:stack); procedure push(var s:stack; item: integer); procedure pop(var s:stack) : integer; end BadStackMod2. implementation module BadStackMod2; procedure init(var s:stack); s.top := 0; end init; procedure push(var s:stack; item: integer); procedure pop(var s:stack) : integer; end BadStackMod2. Note: user can access, e.g., s1.top ECS140A Programming Languages 12-6

stack as type: user main program module foo2; from io import output, writef; from StackMod2 import stack, init, push, pop; var i: integer; s1, s2: stack; (* two instances of a stack *) init(s1); init(s2); push(s1,10); push(s2,23); push(s1,100); push(s1,40); i:=pop(s1); writef(output, %d\n,i); end foo2. definition module StackMod2; export qualified stack, init, push, pop; Type stack; (* opaque *) procedure init(var s:stack); procedure push(var s:stack; item: integer); procedure pop(var s:stack) : integer; end StackMod2. implementation module StackMod2; const MAX = 100; Type stack = pointer to stackrecord; (* opaque *) Type stackrecord = record top: cardinal; a: array [1..MAX] of integer; end; procedure init(var s:stack); new(s); sˆ.top := 0; end init; procedure push(var s:stack; item: integer); procedure pop(var s:stack) : integer; end StackMod2. ECS140A Programming Languages 12-7

Notes: opaque type what it actually is isn t visible outside module. typical implementation restriction: opaque type must be a pointer; not a big problem. must be a pointer so that compiler knows how much space to allocate for it when compiling, e.g, main, when haven t yet compiled implementation module of StackMod2. in fact more efficient, so don t need to pass around lots of data. instantiation: get pointer for each instance of stack; init allocates record. would be nice if did not need initialization. could initialize as part of declaration of type. e.g., top := 0. note declared stack as var, even though only init changes it (because it is a pointer). pop and push change only record, not the pointer such does fit in better with nature of abstraction, though. Modula-2 also supports several forms of import/export local modules, i.e., modules can be nested (additional flexibility, but complicates language) ECS140A Programming Languages 12-8