UNIT-1
Introduction: Syntax, Semantics, and Pragmatics
1. Syntax
- Defines the grammar rules for constructing valid program structures.
- Includes elements like keywords, operators, punctuation, and syntax rules.
- Determines how code is written and organized.
2. Semantics
- Specifies the meaning of valid syntactic structures.
- Deals with the behavior of programs when executed.
- Covers aspects such as data manipulation, control flow, and error handling.
3. Pragmatics
- Focuses on the effective use of language in specific contexts.
- Considers factors like code readability, maintainability, and performance.
- Addresses conventions, idioms, and best practices within a programming
community.
Formal Translation Models, Variables, Expressions & Statements
1. Formal Translation Models
- Provide a systematic approach to converting high-level programming constructs
into machine-executable code.
- Often involve intermediate representations like abstract syntax trees or
intermediate code.
2. Variables
- Can hold different values during program execution.
- Have a name, type, and scope.
- Allow programmers to manipulate data dynamically.
3. Expressions
- Combinations of constants, variables, operators, and function calls that produce
a value.
- Can be evaluated to yield a result.
- Can be simple or complex depending on the operators and operands involved.
4. Statements
- Imperative constructs that perform actions or control the flow of execution.
- Include assignment statements, control structures, and function calls.
- Form the building blocks of algorithms and programs.
Binding Time Spectrum
1. Static Binding
- Occurs during compile time.
- Helps in resolving names and types before program execution.
- Examples include variable binding in statically-typed languages.
2. Dynamic Binding
- Occurs during runtime.
- Involves resolving names or types when the program is executing.
- Examples include dynamic memory allocation and dynamic dispatch in
object-oriented programming.
Key points on the binding time spectrum include:
● Compile Time: This is when most syntactic and semantic analysis occurs.
Variables may be allocated storage and types determined.
● Load Time: Some characteristics, such as memory addresses or library
dependencies, may be resolved during program loading.
● Link Time: In languages with separate compilation units, linking occurs to
combine different modules into a single executable.
● Run Time: Characteristics such as dynamic memory allocation, variable
values, and function calls are determined during program execution.
Variables and Expressions; Assignment
1. Variables and Expressions
- Variables store data that can be manipulated using expressions.
- Expressions are evaluated to produce a value.
- Variables can be used within expressions to represent changing data.
2. Assignment
- Assigns a value to a variable.
- Uses the assignment operator (=) to store the result of an expression in a
variable.
- Can also involve compound assignment operators like +=, -=, etc.
I-values and R-values, Environments and Stores
1. I-values and R-values
- I-values: Identifiers or memory locations where a value can be stored.
- R-values: Values that can be assigned or computed.
I-values and R-values:
● An l-value (I-value) refers to an expression that can appear on the left side of
an assignment operator.
● An r-value refers to an expression that can appear on the right side of an
assignment operator.
● In x = y + 3, x is the l-value, and y + 3 is the r-value.
2. Environments and Stores
- Environments: Maps identifiers to their associated values or memory locations.
- Stores: Physical memory locations where data is stored during program
execution.
- During execution, the environment maps variable names to memory locations,
and the store maps memory locations to their current values.
Storage Allocation; Constants and Initialization; Statement-level Control
Structure
1. Storage Allocation
- Involves allocating memory for variables and data structures.
- Can be static, stack-based, or dynamic depending on the programming language
and execution environment.
- Memory management techniques like garbage collection may also be involved.
● Storage allocation refers to the process of assigning memory locations to
variables and constants.
● It can be done statically, dynamically, or automatically.
● Static allocation allocates memory at compile time, and the allocation
remains fixed throughout the program's execution.
● Dynamic allocation allocates memory at run time, typically using functions
like malloc in languages like C.
● Automatic allocation allocates memory when a variable is declared and
deallocates it when the variable goes out of scope.
2. Constants and Initialization
- Constants are values that remain unchanged throughout program execution.
- Initialization assigns an initial value to a variable or data structure.
- Variables may be initialized during declaration or later in the program.
● Constants are values that do not change during the execution of a program.
● They are typically allocated statically in memory.
● Initialization refers to the process of assigning an initial value to a variable or
constant.
● Constants can be initialized at compile time or run time, depending on the
programming language and context.
3. Statement-level Control Structure
- Determines the order of execution of statements within a program.
- Includes conditional statements (if-else), looping constructs (while, for), and
branching statements (switch-case).
- Provides mechanisms for altering the flow of control based on runtime
conditions.
UNIT-2
Primitive Types: Pointers; Structured Types; Coercion; Notion of Type
Equivalence; Polymorphism: Overloading, Inheritance, Type Parameterization
1. Pointers
- Variables that store memory addresses.
- Allow direct access to memory locations.
- Commonly used in languages like C and C++ for dynamic memory allocation and
manipulation.
2. Structured Types
- Composite types composed of multiple elements of potentially different types.
- Examples include arrays, records, structs, and classes.
- Enable grouping related data together for easier management and manipulation.
3. Coercion
- Automatic conversion of one data type to another.
- Can be implicit or explicit.
- Helps facilitate operations between different data types.
4. Notion of Type Equivalence
- Determines when two types are considered equivalent.
- Can be based on structural equivalence (same structure) or nominal equivalence
(same name).
- Important for type checking and compatibility.
5. Polymorphism
● Overloading:
● Overloading allows multiple functions or methods to have the same
name but different parameter lists or return types.
● It enables creating more intuitive and expressive interfaces by
providing multiple ways to use the same operation.
● Overloading can be static (compile-time) or dynamic (run-time),
depending on the language's type system.
● Inheritance:
● Inheritance is a mechanism that allows a class (subclass) to inherit
properties and behavior from another class (superclass).
● It promotes code reuse, extensibility, and hierarchical organization of
classes.
● Inheritance supports the "is-a" relationship, where a subclass is a
specialized version of its superclass.
● Type Parameterization:
● Type parameterization, also known as generics or templates, allows
classes or functions to operate on multiple data types without
sacrificing type safety.
● It enables writing reusable and type-safe code by parameterizing types
with placeholders.
● Type parameterization facilitates writing generic algorithms and data
structures that can work with any data type.
Abstract Data Types; Information Hiding and Abstraction; Visibility,
Procedures, Modules, Classes, Packages
1. Abstract Data Types (ADTs)
- Encapsulate data and operations into a single unit.
- Hide implementation details from users.
- Provide an interface for interacting with the data structure.
Abstract Data Types:
● Abstract Data Types (ADTs) are mathematical models that define data
structures and the operations that can be performed on them, without
specifying how these operations are implemented.
● They encapsulate data and operations into a single entity, providing a clear
interface for interacting with the data while hiding its internal representation.
● Examples of ADTs include stacks, queues, lists, trees, and graphs.
2. Information Hiding and Abstraction
- Information hiding conceals the implementation details of a module or data
structure.
- Abstraction focuses on the essential characteristics while ignoring irrelevant
details.
- Both concepts enhance modularity, maintainability, and security.
Information Hiding:
● Information hiding is a principle of software design that aims to restrict
access to certain components or details of a system, exposing only those
necessary for interaction.
● It helps manage complexity, improve maintainability, and prevent unintended
dependencies between different parts of the system.
● Information hiding is typically achieved through encapsulation, where data
and implementation details are hidden within a module or class.
Abstraction:
● Abstraction is the process of simplifying complex systems by focusing on
essential properties and ignoring irrelevant details.
● It allows developers to work with high-level concepts without needing to
understand the underlying complexities.
● Abstraction is often achieved through the use of abstract data types,
interfaces, and design patterns.
3. Visibility
- Specifies the accessibility of members (variables, methods, etc.) within a class or
module.
- Common visibility modifiers include public, private, and protected.
- Controls which parts of a program can access certain data or functionalities.
4. Procedures, Modules, Classes, Packages
- Procedures: Named blocks of code that perform a specific task.
Procedures:
● Procedures, also known as functions or methods, are named blocks of code
that perform a specific task.
● They encapsulate a sequence of instructions and can accept input
parameters and return output values.
● Procedures promote code reuse, modularity, and maintainability by breaking
down complex tasks into smaller, manageable units.
- Modules: Logical units of code organization that encapsulate related
functionality.
Modules:
● Modules are units of software organization that encapsulate related
components, such as functions, data structures, and constants.
● They promote modular programming, allowing developers to partition their
codebase into manageable and reusable units.
● Modules help reduce complexity, improve code organization, and facilitate
collaboration among team members.
- Classes: Blueprint for creating objects with data and behavior.
● Classes are blueprints for creating objects, combining data (attributes) and
behavior (methods) into a single entity.
● They enable object-oriented programming (OOP) paradigms, such as
encapsulation, inheritance, and polymorphism.
● Classes provide a mechanism for modeling real-world entities and defining
their interactions in a structured manner.
- Packages: Collection of related modules or classes bundled together for reuse
and distribution.
● Packages, also known as namespaces or modules in some languages, are
containers for organizing related classes, functions, and other resources.
● They provide a hierarchical structure for organizing code and prevent naming
conflicts between different components.
● Packages facilitate code reuse, distribution, and dependency management in
large-scale software projects.
Objects and Object-Oriented Programming
1. Objects
- Instances of classes that encapsulate data and behavior.
- Have attributes (data members) and methods (functions) associated with them.
- Enable modeling real-world entities and interactions in code.
Objects:
● Definition: An object is a fundamental unit of a software system that
represents a real-world entity, concept, or abstraction.
● Attributes: Objects have attributes or properties that describe their state or
characteristics. These attributes are represented by variables known as
instance variables or fields.
● Behavior: Objects exhibit behavior through methods or functions, which define
the actions they can perform or the operations they support.
● Encapsulation: Encapsulation is the bundling of data (attributes) and methods
(behavior) into a single unit (object). It hides the internal state of an object
and only exposes a public interface for interacting with it.
● Example: In a banking application, a "Customer" object might have attributes
like name, address, and account balance, along with methods like deposit,
withdraw, and transfer.
2. Object-Oriented Programming (OOP)
- Programming paradigm centered around objects and classes.
- Emphasizes concepts like encapsulation, inheritance, and polymorphism.
- Facilitates modular and reusable code design.
Object-Oriented Programming (OOP):
● Paradigm: Object-Oriented Programming (OOP) is a programming
paradigm that revolves around the concept of objects. It emphasizes
modular design, code reusability, and maintainability.
● Four Pillars:
● Encapsulation: Encapsulation ensures that the internal state of
an object is hidden from the outside world and accessed only
through well-defined interfaces.
● Inheritance: Inheritance allows a class (subclass) to inherit
properties and behavior from another class (superclass). It
promotes code reuse and supports the "is-a" relationship.
● Polymorphism: Polymorphism allows objects of different types
to be treated as instances of a common superclass. It enables
dynamic method dispatch and supports method overriding and
method overloading.
● Abstraction: Abstraction simplifies complex systems by
focusing on essential properties and hiding irrelevant details. It
allows developers to work with high-level concepts without
needing to understand the underlying complexities.
● Classes: Classes are blueprints for creating objects. They define the
structure and behavior of objects by specifying attributes and methods.
Instances of a class are called objects.
● Modularity: OOP promotes modularity by organizing code into reusable,
self-contained modules (classes and objects). This makes code easier
to understand, maintain, and extend.
● Example: In a car rental system, classes like Car, Customer, and
RentalTransaction might be defined, each encapsulating data and
behavior relevant to its respective entity.
Object-Oriented Programming has become ubiquitous in software development due
to its many benefits, including code reuse, modularity, extensibility, and scalability. It
provides a powerful framework for designing and building complex systems by
modeling real-world entities in a natural and intuitive way.