Unit 1: Syntax, Semantics and Pragmatics
Unit 1: Syntax, Semantics and Pragmatics
Unit 1
Syntax :
Semantics :
Pragmatics
practical aspects of how constructs and features of a language may be used to achieve
various objectives.
aims to explain how and why people use languages in the way they do
what we think of when we talk about programmers’ competence or “best practices” or
patterns and anti-patterns
Example :
Variables :
a storage address paired with an associated symbolic name, which contains some known
or unknown quantity of information referred to as a value
way to reference the stored value, in addition to referring to the variable itself, depending
on the context
separation of name and content allows the name to be used independently of the exact
information it represents
variable’s storage address may be referenced by several different identifiers (aliasing)
Expressions :
combination of one or more constants, variables, operators, and functions that the
programming language interprets and computes to produce another value
the resulting value is usually one of various primitive types, such as numerical, string,
boolean, complex data type or many others
Statements :
Binding Time
source file has many names whose properties need to be determined
meaning of these properties might be determined at different phases of the life cycle of a
program
examples of such properties include:
set of values associated with a type
type of a variable
memory location of the compiled function
value stored in a variable
binding is the act of associating properties with names.
binding time - the time at which a binding takes place. It can take place at :
language design time
set of possible types for a variable
set of possible meanings for an operator symbol
language implementation time
A data type, such as int in C, is bound to a range of possible values
compile time
variable is bound to a particular data type.
link time
call to a library subprogram is bound to the subprogram code.
load time
static variable may be bound to a memory cell when the program is loaded into
memory.
run time
bind a nonstatic local variable to a memory cell
A binding is static if it first occurs before run time and remains unchanged throughout program
execution.
A binding is dynamic if it first occurs during execution or can change during execution of the
program
sb happens at the compile-time and db happens at the runtime. Hence, called early and late
binding
in sb function definition and call are linked during the compile-time, whereas in db the
function calls are not resolved until runtime
sb happens when all information needed to call a function is available at the compile-time,
db happens when all information needed for a function call cannot be determined at
compile-time.
sb can be achieved using the normal function calls, function overloading and operator
overloading while db is achieved using the virtual functions.
sb results in faster execution of a program
db is flexible since a single function can handle different type of objects at runtime
(reduces the size of the codebase, makes source code more readable)
Assignment
an assignment statement sets and/or re-sets the value stored in the storage location(s)
denoted by a variable name; in other words, it copies a value into the variable
Some languages use the idea of l-values and r-values, deriving from the typical mode of
evaluation on the left and right hand side of an assignment statement.
An l-value refers to an object that persists beyond a single expression. Has requirement
that the operand on the left side of the assignment operator is modifiable, usually a
variable.
An r-value is a temporary value that does not persist beyond the expression that uses it.
Pulls or fetches the value stored in a variable or constant.
(Old) Fortran is static: all data, including activation records, are statically allocated. Each
function has only one activation record — no recursion!
Functional languages (Scheme, ML) and some OO languages (Smalltalk) are heap-oriented:
almost all data, including AR, allocated dynamically.
Most languages are in between: data can go anywhere ARs go on the stack.
Activation Record
Constants
a value that cannot be altered by the program during normal execution, i.e., the value is
constant.
when associated with an identifier, a constant is said to be “named,” although the terms
“constant” and “named constant” are often used interchangeably
contrasted with a variable, which is an identifier with a value that can be changed during
normal execution, i.e., the value is variable
useful for both programmers and compilers
for programmers they are a form of self-documenting code and allow reasoning about
correctness
for compilers they allow compile-time and run-time checks that verify that constancy
assumptions are not violated, and allow or simplify some compiler optimizations.
Initialization
Statements that provide these kinds of capabilities are called control statements. A control
structure is a control statement and the collection of statements whose execution it controls.
Control Statements:
Selection statements
two-way (if-else)
n-way or multiple selection (if-elif-else / switch)
Loop statements
counter controlled loops (for)
logically controlled loops (while / do-while)
user located control mechanisms (break / continue)
based on data structures (for)
unconditional branching (goto)
Unit 2
a basic type is a data type provided by a programming language as a basic building block.
Most languages allow more complicated composite types to be recursively constructed
starting from basic types.
a built-in type is a data type for which the programming language provides built-in support.
Pointers
int a = 5;
int *ptr = NULL;
ptr = &a;
Structured type
compound data type which falls under user-defined category
used for grouping simple data types or other compound data types
contains a sequence of member variable names along with their type/attributes
members inside a structure are placed sequentially next to next in the memory layout
minimum size of a structure is the sum total of all sizes of members, considering padding
Coercion
coercion : implicit conversion of a value into another of a different data type (it is
automatically done)
casting : explicit conversion performed by code instructions
treats a variable of one data type as if it belongs to a different data type
languages that support implicit conversion define the rules that will be automatically
applied when primitive compatible values are involved
double x, y;
x = 3; // implicitly coercion (coercion)
y = (double) 5; // explicitly coercion (casting)
int main() {
double a = 5 + 6.3; // polymorphic operator
std::cout << a << std::endl;
f(5);
f((double) 6);
}
Type Equivalence
Type is a property of program constructs such as expressions
defines a set of values (range of variables) and a set of operations on those values
classes are one instantiation of the modern notion of the type
Type Systems
a collection of rules that assign types to program constructs (more constraints added to
checking the validity of the programs, violation of such constraints indicate errors)
languages type system specifies which operations are valid for which types
type systems provide a concise formalization of the semantic checking rules
type rules are defined on the structure of expressions
type rules are language specific
Type Checking
Statically typed languages: all or almost all type checking occurs at compilation time. (C,
Java)
Dynamically typed languages: almost all checking of types is done as part of program
execution (Scheme)
Untyped languages: no type checking (assembly, machine code)
Tradeoffs
languages that support OOP use inheritance for code reuse and extensibility in the form of
classes
classes – the definitions for the data format and available procedures for a given type or
class of object; may also contain data and procedures
objects – instances of classes
leads to the following terms:
class variables – belong to the class as a whole; there is only one copy of each one
attributes – data that belongs to individual objects; every object has its own copy
member variables – refers to both the class and instance variables that are defined by
a particular class
class methods – belong to the class as a whole and have access only to class
variables and inputs from the procedure call
instance methods – belong to individual objects, and have access to instance
variables for the specific object they are called on, inputs, and class variables
objects are accessed like variables with complex internal structure
in many languages are effectively pointers, serving as actual references to a single
instance of said object in memory within a heap or stack
provide a layer of abstraction which can be used to separate internal from external code
Information Hiding
the principle of segregation of the design decisions in a computer program that are most
likely to change
protecting other parts of the program from extensive modification if the design decision is
changed
providing a stable interface which protects the remainder of the program from the
implementation (the details that are most likely to change)
think of information hiding as being the principle and encapsulation being the technique
Encapsulation
the bundling of data with the methods that operate on that data
restricting of direct access to some of an object’s components
to hide the values or state of a structured data object inside a class, preventing
unauthorized parties’ direct access to them
using “getters” and “setters” to access the values, and other client classes call these
methods to retrieve and modify the values within the object.
Abstraction
the process of removing details or attributes in the study of objects or systems to focus
attention on details of greater importance
the creation of abstract concept-objects by mirroring common features or attributes of
various non-abstract objects
Abstract Data type (ADT) is a type (or class) for objects whose
behaviour is defined by a set of value and a set of operations
only mentions what operations are to be performed but not how these operations will
be implemented
does not specify how data will be organized in memory and what algorithms will be
used for implementing the operations
called “abstract” because it gives an implementation-independent view.
Inheritance
Type Parameterization
Polymorphism
the provision of a single interface to entities of different types or the use of a single symbol
to represent multiple different types
classes of polymorphism are:
Ad hoc polymorphism: defines a common interface for an arbitrary set of individually
specified types (includes function and operator overloading).
Parametric polymorphism: when one or more types are not specified by name but by
abstract symbols that can represent any type.
Subtyping: when a name denotes instances of many different classes related by some
common superclass.
inheritance: overriding of functions occurs when one class is inherited from another class.
overloading can occur without inheritance.
signature: overloaded functions must differ in function signature ie either number of
parameters or type of parameters should differ. In overriding, function signatures must be
same.
scope of functions: overridden functions are in different scopes; whereas overloaded
functions are in same scope.
Behavior of functions:
overriding is needed when derived class function has to do some added or different job
than the base class function
overloading is used to have same name functions which behave differently depending
upon parameters passed to them.
A module is a single file (or files) that are imported under one import and used. e.g.
import my_module