0% found this document useful (0 votes)
579 views

Programming Language Design Issues

The document discusses several key topics related to programming language design, including: 1) Influences on language design such as computer architecture and programming methodologies. Most languages are designed around the von Neumann architecture which uses variables, assignment statements, and iterative control structures. 2) Major programming paradigms including imperative, functional, rule-based, and object-oriented. Imperative languages like FORTRAN use statements to manipulate memory locations. Functional languages like LISP define computations as mathematical functions. 3) Issues in language design elements like names, with considerations for maximum length, allowed characters, case sensitivity, and reserved keywords.

Uploaded by

selemon
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
579 views

Programming Language Design Issues

The document discusses several key topics related to programming language design, including: 1) Influences on language design such as computer architecture and programming methodologies. Most languages are designed around the von Neumann architecture which uses variables, assignment statements, and iterative control structures. 2) Major programming paradigms including imperative, functional, rule-based, and object-oriented. Imperative languages like FORTRAN use statements to manipulate memory locations. Functional languages like LISP define computations as mathematical functions. 3) Issues in language design elements like names, with considerations for maximum length, allowed characters, case sensitivity, and reserved keywords.

Uploaded by

selemon
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 47

Chapter 2

LANGUAGE DESIGN ISSUES


Topics Covered

Influences on Language Design


Programming environments
Virtual Computers
Binding Times
Language Paradigms

2
Influences on Language Design
• Several factors influence the basic design of
programming languages.
• Most important of these are Computer Architecture
and Programming Design Methodologies.
• Computer Architecture
• Most Programming languages have been designed
around the prevalent computer architecture, called
the von Neumann architecture.
• These languages are called imperative languages.

3
Influences on Language Design… cont’d
• Computer Architecture … cont’d
• von Neumann architecture.
• Both Data and programs are stored in memory
• CPU, which executes instructions, is separate from the
memory
• Therefore, instructions and data must be transmitted, or
pied, from the memory to the CPU.
• Results of operations in the CPU must be moved back
to memory.

4
Influences on Language Design… cont’d
• Computer Architecture … cont’d
• von Neumann architecture.

5
Influences on Language Design… cont’d
• Computer Architecture … cont’d
• von Neumann architecture… cont’d
• Because of the Von Neumann architecture, the
central features of imperative languages are
• Variables, which model the memory cells;
• Assignment statements, which are based on the
pipping operation;
• Iterative form of iterations, which is the most efficient
way to implement iteration on this architecture.
• Why is iteration operation fast on Von Neumann
computers?
6
Influences on Language Design… cont’d
• Computer Architecture … cont’d
• von Neumann architecture… cont’d
• The execution of a machine code program on a von Neumann
architecture computer occurs in a process called the fetch-execute
cycle.
• Each instruction to be executed must be moved from memory to the
processor.
• The address of the next instruction to be executed is maintained in a
register called the program counter.
• Initialize the program counter(pc).
RepeatAlgorithms
forever for fetch-execute cycle
Fetch the instruction
1. Repeat forever pointed to by the pc
1.1 Fetch
Increment the the
pc instruction
to point at pointed to by
the next the pc
instruction
1.2 Increment the pc to point at the next instruction
Decode the instruction
1.3 Decode the instruction
1.4 the
Execute Execute the instruction
instruction
2. End repeat
End repeat
7
Influences on Language Design… cont’d
• Computer Architecture … cont’d
• von Neumann architecture… cont’d
• Algorithms for fetch-execute cycle
Algorithms for fetch-execute cycle
1. Repeat forever
1.1 Fetch the instruction pointed to by the pc
1.2 Increment the pc to point at the next instruction
1.3 Decode the instruction
1.4 Execute the instruction
2. End repeat

• What does mean by line 1.3?


• The instruction is examined to determine what action it
specifies.
• When is program execution terminated?
• Read @ page 20 of reference 7( see your course outline).
8
Influences on Language Design… cont’d
• Programming Design Methodologies
• 1950s and early 1960s: simple applications; worry about
machine efficiency.
• Late 1960s: people efficiency became important; readability,
better control structure.
• Structured programming
• Top-down design and step-wise refinement
• Late 1970s: process-oriented to data-oriented
• Data abstraction
• Middle 1980s: object-oriented programming
• Data abstraction + Inheritance + Polymorphism
9
Language Paradigms
• In programming language’s definition, paradigm is a
set of basic principles, concepts, and methods for how
a computation or algorithms is expressed.
• Major paradigms
• Imperative(procedural)
• Functional (applicative)
• Rule-based (declarative)
• Object-oriented

10
Language Paradigms
• Major paradigms… cont’d
• Imperative(procedural) programming paradigm
• Expresses computation by fully-specified and fully-controlled
manipulation of a named data in a stepwise fashion.
• A program consists of a sequence of statements, and the execution
of each statement causes the computer to change the value of one
or more locations in its memory, that is, to enter a new state.
• Data or values are initially stored in variables (memory locations),
taken out of ( read from) memory, manipulated in ALU, and then
stored back in the same or different variables (memory locations).
• The syntax of such languages generally has the form
statemetn1;
statement 2;
…….
11
Language Paradigms
• Major paradigms… cont’d
• Imperative(procedural) … cont’d
• Abstracts the von Neumann machine.
• FORTRAN, COBOL, C, Pascal, PL/I are all imperative
languages.
• Because of the Von Neumann architecture, the
central features of imperative languages are
• Variables, which model the memory cells;
• Assignment statements, which are based on the
pipping operation;
• Iterative form of iterations, which is the most efficient
way to implement iteration on this architecture.
12
Language Paradigms
• Major paradigms… cont’d
• Functional(Applicative) programming paradigm
• Expresses computation in terms of mathematical functions.
• It differs from imperative and object-oriented
• No memory location
• Each function will take a number of values as input (parameters) and produce
a single return value ( output of the function)
• The returned value can not be stored for later use.
• It has to be used either as the final output or immediately as parameter value of
another function.
• Functional programming is about defining functions and organizing the return
values of one or more functions as parameters of another function.
• Syntax: function(function-1(…(function2(function1(data)))…))
• ML, Scheme and LISP are examples of functional languages.

13
Language Paradigms
• Major paradigms… cont’d
• Rule-based(declarative) programming paradigm
 Expresses computation in terms of logic predicates.
 A logic program is a set of facts, rules and questions.
 Rule-based languages execute by checking for the presence of a
certain enabling condition and, when present, executing an
appropriate action.
 The enabling conditions are usually written in terms of predicate
calculus and take the form:
Enabling condition1  action1
Enabling condition2  action2
. . . .
Enabling conditionn  actionn

 Prolog is the best know example of a declarative language.


14
Language Paradigms
• Major paradigms… cont’d
• Object-oriented programming paradigm
• In object-oriented languages, data structures and algorithms support
the abstraction of data and endeavor to allow the programmer to use
data in a fashion that closely represents its real world use.
• Data abstraction is implemented by use of
• Encapsulation – data and procedures belonging to a class can
only be accessed by that classes (with noteworthy exceptions).
• Polymorphism – the same functions and operators can mean
different things depending on the parameters or operands,
• Inheritance – New classes may be defined in terms of other,
simpler classes.
• Typical OOP languages : Smalltalk, C++, Java, and C#.
15
Names
16
A name (identifier) is a string of characters used to identify some entity in a program.
The following are the primary design issues for names:
 Maximum length?
 Are connector characters allowed?
 Are names case sensitive?
 Are the special words of the language reserved words or keyword?
Variables
17
Aprogram variable is an abstraction of a computer
memory cell or collection of cells.
A variable can be characterized as a sextuple of
attributes:
Name

 Address
Value
Type
 Lifetime
Scope
Variables( continued)
18
 Address:-
the address of a variable is the machine
memory address with which it is associated.
 A variable may have different addresses at different times during execution
 A variable may have different addresses at different places in a program
 It is possible to have multiple variables that have the same address.
 If two variable names can be used to access the same memory location, they are
called aliases
 Aliases are created via pointers, reference variables, C and C++ unions
 Aliasing is a hindrance to readability because it allows a variable to have its value changed by
an assignment to a different variable.
 Aliasing also makes program verification more difficult.
 Two pointer variables are aliases when they point to the same memory location. The same
is true for reference variables. This kind of aliasing is simply a side effect of the nature of
pointers and references.
Variables( continued)
 Type:-The type of a variable determines the range of values19the
variable can store and the set of operations that are defined for
values of the type; in the case of floating point, type also
determines precision.
 Value:-The value of a variable is the contents of the memory cell
or cells associated with the variable.
 The l-value of a variable is its address
 because the address is what is required when the name of a variable
appears in the left side of an assignment.
 The r-value of a variable is its value
 because it is what is required when the name of the variable
appears in the right side of an assignment statement.
 It is convenient to think of computer memory in terms of abstract
cells, rather than physical cells.
Binding Times - Terminology
• A binding is an association between an attribute and an entity, such as
between a variable and its type or value, or between an operation and a
symbol.
• Binding time is the time at which a binding takes place.
• Possible binding times:
– At language design time --e.g., bind operator symbols to operations.
– At language implementation time --e.g., a data type, such as int in C, is
bound to a range of possible values at language implementation time.
– At compile time –e.g., a variable in a Java or C program is bound to a
particular data type.
– At load time –e.g., (a variable in Fortran, and a static variable in C) may
be bound to a storage cell when the program is loaded into memory.
That same binding does not happen until run time in some cases, as
with variables declared in Java methods.
– At Run Time –e.g., bind a nonstatic variable to a memory cell.
– At link time –e.g., a call to a library subprogram is bound to the
subprogram code at link time.

20
Binding Times - Terminology
• Def: A binding is static if it first occurs before run
time and remains unchanged throughout
program execution.
• Def: A binding is dynamic if it first occurs during
execution or can change during execution of
the program.
• Tradeoff: In general,
– Early binding -> greater efficiency, less flexibility
(unchanged).
– Late binding -> more flexibility, less efficiency.
(Discuss flexibility and efficiency)
21
Binding: Type Binding
 Before a variable can be referenced in a program, it must be
bound to a data type.
 The two important aspects of this binding are:
 how the type is specified and
 when the binding takes place.
 Static type binding
 Declaration is a static type binding
 Explicit declaration
 A statement in a program listing variable names and
specifies that they are a particular type.
 Example: int x,y; float radious;
 Implicit declaration
 A means of associating variables with types through
default conventions
 done by the language processor, either a compiler or
an interpreter.
22
Binding: Type Binding
 Implicit declaration
 Several different bases for implicit variable type bindings.
1. Naming Conventions:
 the compiler or interpreter binds a variable to a type based on
the syntactic form of the variable’s name.
 For example, in Fortran:
 If the identifier begins with one of the letters I, J, K, L, M, or N, or their
lowercase versions, it is implicitly declared to be Integer type;
otherwise, it is implicitly declared to be Real type.
 Thus, with implicit typing, the variable COUNT is REAL whilst the variable
KOUNT is an INTEGER.
2. Type inference (Context)
 In context, the type of the value assigned to the variable in a
declaration statement will be the type of the variable.
 Example: in C# a var declaration of a variable must include an
initial value, whose type is made the type of the variable.

The type of sum, total and


name are int, float and string
23 respectively
Binding: Type Binding
 Dynamic type binding
 the type of a variable is not specified by a declaration
statement, nor can it be determined by the spelling of its
name
 Instead, the variable is bound to a type when it is
assigned a value in an assignment statement.
 When the assignment statement is executed, the
variable being assigned is bound to the type of the
value of the expression on the right side of the
assignment
 Example:

 The primary advantage of dynamic binding of variables to


types is that it provides more programming flexibility.
 For example, a program to process numeric data in a
language that uses dynamic type binding can be written
as a generic program, meaning that it is capable of
24 dealing with data of any numeric type.
Storage Bindings and Lifetime
Stack-Dynamic variables
 Example: C++ and Java—allow variable
declarations to occur anywhere a statement can
appear, and the declaration can be at the
beginning or just anywhere in the middle

 Even if variables are


declared at different
positions in the code,
storage binding is
done (elaboration) for
all variables when the
function is called

25
Storage Bindings and Lifetime
Storage/Memory layout

Higher address Heap growing direction


Heap

Free

Stack growth direction


Stack

Data Segment (Fixed size)

Code Segment (Fixed size)


Lower address
Storage Bindings and Lifetime
 Memory/Storage Layout
 Code Segment
 Aka text segment, contains the machine instructions of the program.
 Data Segment
 Contains the static data of the program, i.e. the variables that exist throughout
program execution.
 Global variables are static, as are variables declared as static in C, C++ or Java.
 Stack Segment
 Contains the system stack, which is used as temporary storage.
 The stack is a simple data structure with a LIFO access policy.
 Operations: Push and Pop
 Heap Segment
 A pool of memory used for dynamically allocated memory, such as with malloc()
in C or new in C++ or Java
 The size of the text and data segments are known as soon as compilation or
assembly is completed.
 The stack and heap segments grow and shrink during program execution.
 They tend to grow toward each other.
27
Storage Bindings and Lifetime
 The memory cell to which a variable is bound must be
taken from a pool of available memory  Allocation
 Deallocation is the process of placing a memory cell that
has been unbound from a variable back into the pool of
available memory
 The lifetime of a variable is the time during which the
variable is bound to a specific memory location.
 So, the lifetime of a variable begins when it is bound to a specific
cell and ends when it is unbound from that cell.
 Four category of scalar variables according to their
lifetime:
 Static variables
 Stack-Dynamic Variables
 Explicit Heap-Dynamic Variables
28  Implicit Heap-Dynamic Variables
Storage Bindings and Lifetime
 Static variables
 bound to memory cells before program execution begins
and remain bound to those same memory cells until
program execution terminates
 Applications in programming
 Global variables
 Local static variables for history sensitive
subprograms
 Advantage
 Efficiency: - Direct memory access
- No run-time overhead
 Disadvantage
 Reduced flexibility (static binding)
 Storage cannot be shared among variables
 Example: Consider two subprograms, both of which
require large arrays and suppose that the two
subprograms are never active at the same time. If
29 the arrays are static, they cannot share the same
storage for their arrays.
Storage Bindings and Lifetime
Stack-Dynamic variables
 Storage bindings are created for variables when their
declaration statements are elaborated (type binding is static)
 Elaboration refers to allocation and binding of storage which
takes place when execution reaches code to which the
declaration is attached
 (Occurs during run-time)
 Example: the variable declarations that appear at the
beginning of a Java method are elaborated when the
method is called and the variables defined by those
declarations are deallocated when the method
completes its execution.
 Stack-dynamic variables are allocated from the run-time
stack.
30
Storage Bindings and Lifetime
Stack-Dynamic variables
 Advantage: recursive subprograms require some
form of dynamic local storage so that each active
copy of the recursive subprogram has its own
version of the local variables

31
Storage Bindings and Lifetime
Stack-Dynamic variables
 Advantage: recursive subprograms require some
form of dynamic local storage so that each active
copy of the recursive subprogram has its own
version of the local variables

32
Storage Bindings and Lifetime
Stack-Dynamic variables
 Advantage: recursive subprograms require some
form of dynamic local storage so that each active
copy of the recursive subprogram has its own
version of the local variables

33
Storage Bindings and Lifetime
Stack-Dynamic variables
 Advantage: recursive subprograms require some
form of dynamic local storage so that each active
copy of the recursive subprogram has its own
version of the local variables

34
Storage Bindings and Lifetime
Stack-Dynamic variables
 Advantage: recursive subprograms require some
form of dynamic local storage so that each active
copy of the recursive subprogram has its own
version of the local variables

35
Storage Bindings and Lifetime
Stack-Dynamic variables
 Advantage: recursive subprograms require some
form of dynamic local storage so that each active
copy of the recursive subprogram has its own
version of the local variables

36
Storage Bindings and Lifetime
Stack-Dynamic variables
 Advantage: recursive subprograms require some
form of dynamic local storage so that each active
copy of the recursive subprogram has its own
version of the local variables

37
Storage Bindings and Lifetime
Stack-Dynamic variables
 Advantage: recursive subprograms require some
form of dynamic local storage so that each active
copy of the recursive subprogram has its own
version of the local variables

38
Storage Bindings and Lifetime
Stack-Dynamic variables
 Advantage: recursive subprograms require some
form of dynamic local storage so that each active
copy of the recursive subprogram has its own
version of the local variables

39
Storage Bindings and Lifetime
Stack-Dynamic variables
 Advantage: recursive subprograms require some
form of dynamic local storage so that each active
copy of the recursive subprogram has its own
version of the local variables

40
Storage Bindings and Lifetime
Stack-Dynamic variables
 Advantage: recursive subprograms require some
form of dynamic local storage so that each active
copy of the recursive subprogram has its own
version of the local variables

41
Storage Bindings and Lifetime
Stack-Dynamic variables
 Advantage: Subprograms share the same memory
space for their locals (Compare with Static
variables)
 Disadvantage:
 Run-time allocation/deallocation overhead
 Slower accesses because indirect addressing is
required
 Subprograms cannot be history sensitive (no
static variables)

42
Storage Bindings and Lifetime
Explicit Heap-Dynamic Variables
 Explicit heap-dynamic variables are memory cells that are
allocated and deallocated by explicit run-time instructions
written by the programmer
 Allocation commands such as “new” and deallocation commands
such as “delete” in C++
 Memory cells are allocated from and deallocated to a collection
of memory cells often referred to as the heap
 These variables can only be referenced through pointers or
reference variables.
 In Java, all variables are reference variables and are heap-
dynamic
 Explicit heap-dynamic variables are bound to types at compile
time, but to memory at the time they are created/ie run-time
 Used to create dynamic structures, such as linked lists and trees,
43 that need to grow and/or shrink during execution
Storage Bindings and Lifetime
Explicit Heap-Dynamic Variables
 Deallocating or destroying Explicit Heap-Dynamic
Variables
 Explicit: by using operators (Example C++ uses
delete operator)

 Implicit: the programmer is not required to


explicitly include a code to destroy the
variables.
 Example: C# and Java included a
44
Garbage Collection facility.
Storage Bindings and Lifetime

Explicit Heap-Dynamic Variables


 Disadvantages
 Difficulty of using pointer and reference variables correctly
 Reliability
 Cost of references to variables
 Complexity of storage management implementation

45
Storage Bindings and Lifetime
Implicit Heap-Dynamic Variables
 Bound to heap storage only when they are assigned
values
 All their attributes are bound every time they are
assigned (Type, Memory cell, Value, array subscripts)
 Consider the following JavaScript assignment
statement:

 Regardless of whether the variable named highs was


previously used in the program or what it was used for,
it is now an array of five numeric values.
 Advantage of such variables is that they have the
highest degree of flexibility, allowing highly generic
code to be written
46  Disadvantage of such variables is the run-time
overhead of maintaining all the dynamic attributes,
Scope
 The range of statements in which the variable
is visible
 The binding scope is the textual region ot the
program in which a binding is active.
 A variable is visible in a statement if it can be
referenced in that statement.
 A variable is local in a program unit or block if
it is declared there.
 The nonlocal variables of a program unit or
block are those that are visible within the Global
program unit or block but are not declared Non-local
there.
local
 Global variables are a special category of
nonlocal variables
47

You might also like