Programming Language Handout
Programming Language Handout
Statements end with ';' (C,C++, Pascal), with'.' (Prolog), or do not have an ending
symbol (FORTRAN)
Variables must start with any letter (C, C++, Java), or only with a capital letter
(Prolog).
The symbol for assignment statement is '=', or ':=' , or something else.
Readability – a program is considered readable if the algorithm and data are apparent
by inspection.
Writeability – ease of writing the program.
Verifiability – ability to prove program correctness (very difficult issue)
Translatability – ease of translating the program into executable form.
Lack of ambiguity – the syntax should provide for ease of avoiding ambiguous
structures.
Character set – the alphabet of the language. Several different character sets are used:
ASCII, EBCIDIC, Unicode.
Identifiers – strings of letters of digits usually beginning with a letter
Operator Symbols – +-*/
Keywords or Reserved Words – used as a fixed part of the syntax of a statement.
Noise words – optional words inserted into statements to improve readability.
Comments – used to improve readability and for documentation purposes.
Comments are usually enclosed by special markers.
Blanks – rules vary from language to language. Usually only significant in literal
strings.
Delimiters – used to denote the beginning and the end of syntactic constructs.
Expressions – functions that access data objects in a program and return a value
Statements – these are the sentences of the language, describe a task to be performed.
Overall Program-Subprogram Structure
E.G. C/C++ use header files as specification components. Packages contain either the
specification of the interface definitions or the source program implementation.
Data descriptions separated from executable statements. A centralized data division
contains all data declarations. E.G. COBOL. Separate programs for data divisions (data
declaration, global), procedure divisions (executable statements, sub units) and
environment (declarations concerning to external operating environments) Advantage
- logical data format independent on algorithms.
Unseparated subprogram definitions: No syntactic distinction between main
program statements and subprogram statements. Subprograms begin (function call) and
end (return) are not differentiated syntactically. Allows for run-time translation and
execution.
3.2 Stages in Translation
Translation is process of converting source program into executable object program, ensuring
efficiency. Translation is divided into two parts,
Lexical analysis (scanning) – identifying the tokens of the programming language: the
keywords, identifiers, constants and other symbols appearing in the language.
In the program
Void main()
{
Printf ("Hello World\n");
}
Example:
int variable1;
The meaning is that the program needs 4 bytes in the memory to serve
as a location for variable1. Further on, a specific set of operations only
can be used with variable1, namely integer operations.
Symbol–table maintenance
Insertion of implicit information
Error detection
Macro processing and compile-time operations
Macro, binding during translation; Subprogram, binding at
runtime
Compile time operations- sequence of statements to be compiled
#define PC……
Ifdef PC….windows..#else...unix….#endif
The final result is the executable code of the program. It is obtained in three
main steps:
2.3. Bootstrapping
The compiler for a given language can be written in the same language.
The process is based on the notion of a virtual machine. A virtual machine is
characterized by the set of operations, assumed to be executable by the
machine.
Assume we have:
A real machine (at the lowest level with machine code operations
implemented in hardware)
A firmware machine (next level - its set is the assembler language
operations and the program that translates them into machine operations
is stored in a special read-only memory)
A virtual machine for some internal representation (this is the third level,
and there is a program that translates each operation into assembler
code)
A compiler for the language L (some language) written in L (the same
language)
The translation of the compiler into the internal representation is done manually
- the programmer manually re-writes the compiler into the internal
representation. This is done once and though tedious, it is not difficult - the
programmer uses the algorithm that is encoded into the compiler. From there on
the internal representation is translated into assembler and then into machine
language.
3.3 Formal Translation Models
The corresponding rule here says that the article must precede
the noun.
Here you see the words "article" and "noun". These words
correspond to certain categories of words in the language.
For example, the words boy, book, room, class, are all nouns,
while read, speak, write, are verbs.
Rule Meaning
<exp> ::= <exp> "+" <exp> | <exp> "*" <exp> | "(" <exp> ")" | "a" | "b" | "c"
Rule1: S SS
Rule2: S (S)
Rule3: S ()
S (S) by Rule2
(SS) by Rule1
( ( ) S ) by Rule3
( ( ) ( ) ) by Rule3
The strings obtained at each step are called sentential forms. They may
contain both terminal and non-terminal symbols. The last
string obtained in the derivation contains only terminal symbols. It is
called a sentence in the language.
Ambiguity
Compilers and interpreters use grammars to build the data-structures
that they will use to process programs. Therefore, ideally a given
program should be described by only one derivation tree. However,
depending on how the grammar was designed, ambiguities are possible.
A grammar is ambiguous if some phrase in the language generated by
the grammar has two distinct derivation trees. For instance, the grammar
below, which we have been using as our running example, is ambiguous.
A a
A aB
String1 String2
A 0A | 1A | 0
The first two alternatives are used to generate any binary string, and third
alternative is used to end the generation with a final 0.
Regular expressions
Formal definition:
Example:
Generated String
0 U 1, 0,1
Exam-like questions
1. 1. Data object:
Attributes: determine how the location may be used. Most important attribute
- the data type.
Type: determines the set of data values that the object may take and
the applicable operations.
Name: the binding of a name to a data object.
Component: the binding of a data object to one or more data objects.
Location: the storage location in memory assigned by the system.
Value: the assignment of a bit pattern to a name.
Constants: a data object with a name that is permanently bound to a value for
its lifetime.
1. 3. Persistence
Data objects are created and exist during the execution of the program. Some data
objects exist only while the program is running. They are called transient data
objects. Other data objects continue to exist after the program terminates, e.g. data
files. They are called persistent data objects. In certain applications, e.g. transaction-
based systems the data and the programs coexist practically indefinitely, and they
need a mechanism to indicate that an object is persistent. Languages that provide such
mechanisms are called persistent languages.
2. Data types
A data type is a class of data objects with a set of operations for creating and
manipulating them.
3. Operations that define the possible manipulations of data objects of that type.
The domain and the range are specified by the operation signature
the number, order, and data types of the arguments in the domain,
the number, order, and data type of the resulting range
The operations available to the larger class are available to the subtype.
This can be implemented using inheritance.
4. Storage representation
Declarations provide information about the name and type of data objects
needed during program execution.
e.g. in FORTRAN - the first letter in the name of the variable determines the
type
Perl - the variable is declared by assigning a value
Examples:
declaration: float Sub(int, float)
signature: Sub: int x float --> float
Purpose of declaration
Depending on the data types operations having same name may have
different meaning, e.g. integer addition and float addition
Advantages: Flexibility
Disadvantages:
Difficult to debug
Type information must be kept during execution
Software implementation required as most hardware does not provide
support
Type inference: implicit data types, used if the interpretation is unambiguous. Used
in ML
Explicit type conversion : routines to change from one data type to another.
Assignment - the basic operation for changing the binding of a value to a data object.
The assignment operation can be defined using the concepts L-value and R-value
Consider executing: A = A + B;
For each named object, its position on the right-hand-side of the assignment
operator (=) is a content-of access, and its position on the left-hand-side of the
assignment operator is an address-of access.
o address-of is an L-value
o contents-of is an R-value
o Value, by itself, generally means R-value
Initialization
Uninitialized data object - a data object has been created, but no value is
assigned, i.e. only allocation of a block storage has been performed.
4.2: Elementary Data Types :
Scalar Data types, Composite Data Types
Exam-like questions
Scalar data types represent a single object, i.e. only one value can be derived.
In general, scalar objects follow the hardware architecture of a computer.
Integers
Specification
Arithmetic
Relational
Assignment
Bit operations
Subranges
Specification
Mantissa: 105
Exponent: 2
Booleans
Use a particular bit for the value, e.g. the last bit; 1 - true, 0 -
false.
Use the entire storage; a zero value would then be false,
otherwise - true.
Characters
Relational
Assignment
Testing the type of the character - e.g. digit, letter, special
symbol.
An upper bound for length is set and any string over that length
is truncated
c. Unbounded length - storage allocation at run time. Strings can be of any length.
Operations
Implementation
Specification:
Operations:
Creation operation:
Implementation
Methods:
Implementation problems:
Characteristics:
File operations:
Open
Read
Write
End-of-file
Close
Storage representation
Includes:
Storage management
What is to be checked:
Multidimensional arrays
Associative arrays
Instead of using an integer index, elements are selected by a key value, that is
a part of the element. Usually the elements are sorted by the key and binary
search is performed to find an element in the array.
5. Records
Number of components
Data type of each component
Selector used to name each component.
Implementation:
For some data types storage must begin on specific memory boundaries
(required by the hardware organization). For example, integers must be
allocated at word boundaries (e.g. addresses that are multiples of 4). When the
structure of a record is designed, this fact has to be taken into consideration.
Otherwise the actual memory needed might be more than the sum of the
length of each component in the record. Here is an example:
struct employee
{ char Division;
int IdNumber; };
The first variable occupies one byte only. The next three bytes will remain
unused and then the second variable will be allocated to a word boundary.
Careless design may result in doubling the memory requirements.
In most languages, programs and data objects are separate structures (Ada, C,
C++).
Other languages however do not distinguish between programs and data - e.g.
PROLOG. Data structures are considered to be a special type of program
statements and all are treated in the same way.
Exam-like questions
Exam-like questions
Information hiding
Information hiding is the term used for the central principal in the design of
programmer-defined abstract data types.
a. By providing a virtual computer that is simpler to use and more powerful than
the actual underlying hardware computer.
b. The language provides facilities that aid the programmer to construct
abstractions.
1. does not need to know the hidden information in order to use the abstraction,
2. is not permitted to directly use or manipulate the hidden information
even if desiring to do so.
Subprograms
Type definitions
B. Encapsulation by subprograms
a. Specification
b. implementation
Implementation of a subprogram:
A better approach:
The activation record contains only the parameters, results and local
data. This is the dynamic part. It has same structure, but different values
for the variables.
3. Basics
Type definitions are used to define new data types. Note, that they do not define a
complete abstract data type, because the definitions of the operations are not
included.
Examples:
struct rational_number
{int numerator, denominator;};
Issues
In general, the compiler has no way to know how to compare data values of
user-defined type. It is the task of the programmer that has defined that
particular data type to define also the operations with the objects of that
type.
Parameters allow the user to prescribe the size of data types needed – array
sizes.
1. What is an abstract data type? How does it differ from a structured data type?
2. Explain briefly the concept "encapsulation" and how it can be achieved by means
of subprograms.
3. Explain and compare the concepts "subprogram definition" and "subprogram
activation record".
4. Discuss the two aspects of type equivalence: name equivalence and structural
equivalence
5. Discuss briefly data object equality
Chapter : Encapsulation – Abstract data types, Subprograms and Type
Definitions
Information hiding
Information hiding is the term used for the central principal in the design of
programmer-defined abstract data types.
a. By providing a virtual computer that is simpler to use and more powerful than
the actual underlying hardware computer.
b. The language provides facilities that aid the programmer to construct
abstractions.
1. does not need to know the hidden information in order to use the abstraction,
2. is not permitted to directly use or manipulate the hidden information
even if desiring to do so.
Subprograms
Type definitions
B. Encapsulation by subprograms
a. Specification
b. implementation
Implementation of a subprogram:
A better approach:
The activation record contains only the parameters, results and local
data. This is the dynamic part. It has same structure, but different values
for the variables.
3. Basics
Type definitions are used to define new data types. Note, that they do not define a
complete abstract data type, because the definitions of the operations are not
included.
Examples:
struct rational_number
{int numerator, denominator;};
Issues
In general, the compiler has no way to know how to compare data values
of user-defined type. It is the task of the programmer that has defined that
particular data type to define also the operations with the objects of that
type.
Parameters allow the user to prescribe the size of data types needed – array
sizes.
1. What is an abstract data type? How does it differ from a structured data type?
2. Explain briefly the concept "encapsulation" and how it can be achieved by means
of subprograms.
3. Explain and compare the concepts "subprogram definition" and "subprogram
activation record".
4. Discuss the two aspects of type equivalence: name equivalence and structural
equivalence
5. Discuss briefly data object equality
Chapter . Inheritance
Exam-like questions
Basic idea: The data components and the programs that implement the operations are
hidden from the external world. The object is encapsulated.
E.G. private section: accessible only to the class functions (class functions are called
also methods)
public section: contains the methods - to be used by other programs
This is the case when the data components may be of different type,
however the operations stay the same, e.g. a list of integers, a list of
characters.
2. Derived classes
Generalization and specialization: Down the hierarchy the objects become more
specialized, up the hierarchy - more generalized.
3. Multiple inheritance
4. Inheritance of methods
class Figure
{
public:
Figure();
virtual void draw();
virtual void erase();
void center();
void set_color(T Color);
void position_center();
};
void Figure:: center()
{
erase();
position_center();
draw();
}
Box a_box;
a_box.draw(); // overrides base class
a_box.set_color(C); // inherits the method
a_box.center(); // makes use of virtual
// functions
5. Polymorphism
Example:
When printing a person's name, we may want to print the full name, or to print only
the first and the last name. We may use two functions that have the same name but
different number of arguments:
Exam-like questions
Sequence control
Levels of sequence control
Sequencing with expressions
Statement level sequence control
Prime programs
Exam-like questions
1. Sequence control
Sequence control : the control of the order of execution of the operations both
primitive and user defined.
o Constants
o Data objects
o Other operations
Example 1: 3 * (var1 + 5)
Example 2: 3* var1 +5
Example 3: 3 + var1 +5
o Operator's precedence
o Operator's associativity
Precedence concerns the order of applying operations, associativity deals with the
order of operations of same precedence.
Precedence and associativity are defined when the language is defined - within the
semantic rules for expressions.
o Prefix notation
o Postfix notation
o Infix notation
There are algorithms to evaluate prefix and postfix expressions and algorithms to
convert an infix expression into prefix/postfix notation, according to the operators'
precedence and associativity.
3. 2. Other expressions
Languages may have some specific operations, e.g. for processing arrays and vectors,
built-in or user defined. Precedence and associativity still need to be defined -
explicitly in the language definition or implicitly in the language implementation.
Problems:
goto X
if Y goto X – transfer control to the statement labeled X if Y is
true.
break
i. Compound statements
Typical syntax:
begin
statement1;
statement2;
...
end;
j. Conditional statements
nested if statements
case statements
Example :
case Tag is
statement1
end;
statement2
end;
statement3
end;
end case
Examples:
Solutions vary with languages, e.g. in C++ - break statement, assert for exceptions.
5. Prime programs
o 1 entry arc
o 1 exit arc
o There is a path from entry arc to any node to exit arc
All primes can be enumerated. Fig. 8.9 gives the primes with up to 4 nodes.
Question: Can any prime program be built out of structure control statements?
The answer is given by the structure theorem:
Any flowchart can be represented using only if statements, while statements and
sequence control statements
Exam-like questions
Exam-like questions
Subprogram control: interaction among subprograms and how subprograms manage to pass
data among themselves in a structured and efficient manner.
Terminology:
Copy rule view of subprograms: the effect of a call statement is the same as if
the subprogram were copied and inserted into the main program.
Execution of subprograms
Outline of the method:
On call instruction:
Hardware support - CIP is the program counter, CEP is not used, simple
jump executed on return.
7. Recursive subprograms
Specification
Syntactically - no difference
Semantically - multiple activations of the same subprogram exist
simultaneously at some point in the execution.
Implementation
Data control features: determine the accessibility of data at different points during
program execution.
Example: x = y + 2*z;
The result of multiplication is transmitted directly as an operand of
the addition operation.
1. Variables
2. Formal parameters
3. Subprograms
4. Defined types
5. Defined constants
6. Labels
7. Exception names
8. Primitive operations
9. Literal constants
Visibility of associations
The static scope of a declaration is that part of the program text where a
use of the identifier is a reference to that particular declaration of the
identifier.
2. Block structure
Parameter transmission
o Actual and formal parameters
o Methods for transmitting parameters
o Transmission semantics
o Implementation of parameter transmission
Explicit common environment
Exam-like questions
A. Parameter transmission
Obtained through
o parameters
o non-local references
Returned through
o parameters
o assignments to non-local variables
o explicit function values
6. Actual and Formal Parameters
Establishing a Correspondence
Call by value – the value of the actual parameter is copied in the location of
the formal parameter.
Examples:
begin EL = 0
k:=2; el := 0 RETURN
end; END
i := 1; I=1
i := 2;
A[i] := 0;
On exit we have
i = 2, A[1] = 1, A[2] = 0.
Pass by reference:
8. Transmission semantics
Types of parameters:
input information
output information (the result)
both input and output
Return results:
Using parameters
Using functions with a return value
on entry:
copying the entire contents of the actual
parameter in the formal parameter, or copying
the pointer to the actual parameter
on exit:
copying result values into actual parameters
or copying function values into registers
Thus the compiler has two main tasks in the implementation of parameter
transmission
3.
It must generate the correct executable code for transmission of
parameters, return of results, and each reference to a formal-
parameter name.
4. It must perform the necessary static type checking to ensure that
the type of each actual-parameter data object matches that declared
for the corresponding formal parameter
B. Explicit common environment
Exam-like questions
/* main program */
....
integer i = 1,j = 2;
subprog(i,j);
print(i,j);
.....
k = k + 1;
m = m + i;
print (i,j,k,m);
end;
What values would be printed in the three modes of parameter transmission? Fill in
the table below:
i j k m i j
Pass by
reference
Pass by value
Pass by value -
result
Solution
To solve the problem we have to determine the scope of each variable name and the reference
environments in the three cases of parameter transmisson.
A. Pass by reference:
i Loc1
j Loc2
subprog …..
Name Location
k Loc1
m Loc2
i Loc1
j Loc2
Loc 1 Loc 2
i in main j in main
i in subprog j in subprog
k in subprog m in subprog
M1 1 2
M2
S1 2
S3 4
S3 Printed: 2, 4, 2, 4
M3 Printed: 2, 4
B. Pass by value:
Name Location
i Loc1
j Loc2
subprog …..
Reference environment of subprog:
Name Location
k Loc3
m Loc4
i Loc1
j Loc2
M1 1 2
M2 1 2
S1 2
S2 3
S3 Print: 1, 2, 2, 3
M3 Print: 1, 2
C. Pass by value-result:
Name Location
i Loc1
j Loc2
subprog …..
Name Location
k Loc3
m Loc4
i Loc1
j Loc2
M1 1 2
M2 1 2
S1 2
S2 3
S3 Print: 1, 2, 2, 3
Exit subprog 2 3
M3 Print: 2, 3
i j k m i j
Pass by 2 4 2 4 2 4
reference
Pass by value 1 2 2 3 1 2
Pass by value - 1 2 2 3 2 3
result
Concurrency means that an application is making progress on more than one task at the same time
(concurrently). Well, if the computer only has one CPU the application may not make progress on more than
one task at exactly the same time, but more than one task is being processed at a time inside the application. It
does not completely finish one task before it begins the next.
Parallelism
Parallelism means that an application splits its tasks up into smaller subtasks which can be processed in parallel,
for instance on multiple CPUs at the exact same time.
As you can see, concurrency is related to how an application handles multiple tasks it works on. An application
may process one task at at time (sequentially) or work on multiple tasks at the same time (concurrently).
Parallelism on the other hand, is related to how an application handles each individual task. An application may
process the task serially from start to end, or split the task up into subtasks which can be completed in parallel.
As you can see, an application can be concurrent, but not parallel. This means that it processes more than one
task at the same time, but the tasks are not broken down into subtasks.
An application can also be parallel but not concurrent. This means that the application only works on one task at
a time, and this task is broken down into subtasks which can be processed in parallel.
Additionally, an application can be neither concurrent nor parallel. This means that it works on only one task at
a time, and the task is never broken down into subtasks for parallel execution.
Finally, an application can also be both concurrent and parallel, in that it both works on multiple tasks at the
same time, and also breaks each task down into subtasks for parallel execution. However, some of the benefits
of concurrency and parallelism may be lost in this scenario, as the CPUs in the computer are already kept
reasonably busy with either concurrency or parallelism alone. Combining it may lead to only a small
performance gain or even performance loss. Make sure you analyze and measure before you adopt a concurrent
parallel model blindly.