0% found this document useful (0 votes)
12 views20 pages

Role of Programming Languages: Revathy D R

The document outlines the role of programming languages, emphasizing the importance of understanding various programming concepts for improved coding efficiency and adaptability. It discusses different programming domains, language evaluation criteria, influences on language design, implementation methods, and the concepts of binding and variables. Key topics include the evolution of programming paradigms, the significance of readability and writability, and the distinctions between static and dynamic binding.

Uploaded by

tony stark
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views20 pages

Role of Programming Languages: Revathy D R

The document outlines the role of programming languages, emphasizing the importance of understanding various programming concepts for improved coding efficiency and adaptability. It discusses different programming domains, language evaluation criteria, influences on language design, implementation methods, and the concepts of binding and variables. Key topics include the evolution of programming paradigms, the significance of readability and writability, and the distinctions between static and dynamic binding.

Uploaded by

tony stark
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

REVATHY D R

MODULE 1

Role of Programming Languages


Reasons for Studying Concepts of Programming Languages:
Increased Ability to Express Ideas – Understanding different programming paradigms
and constructs allows developers to write more efficient and elegant code.
Improved Background for Choosing Appropriate Languages – Knowing multiple
languages helps in selecting the right one based on the problem requirements.
Increased Ability to Learn New Languages – Familiarity with programming concepts
makes it easier to adapt to new languages quickly.
Better Understanding of the Significance of Implementation – Knowing how
languages work internally improves debugging, optimization, and efficiency.
Better use of languages that are already known - Studying language concepts can
help you discover and use advanced features in languages you already know.
Overall Advancement of Computing – Learning programming languages contributes
to innovation and the development of better computing systems.

Programming Domains
1. Scientific Applications

The first digital computers, which appeared in the late 1940s and early 1950s, were
invented and used for scientific applications.
Typically, the scientific applications of that time used relatively simple data
structures, but required large numbers of floating-point arithmetic computations.
The most common data structures were arrays and matrices; the most common
control structures were counting loops and selections.
Fortran was the first language for scientific computing and is still used today.

2. Business Applications

The use of computers for business applications began in the 1950s.


Special computers were developed for this purpose, along with special languages.
The first successful high-level language for business was COBOL, the initial
version of which appeared in 1960. It is still the most commonly used language for
these applications.
REVATHY D R

3. Artificial Intelligence

Artificial intelligence (AI) is a broad area of computer applications characterized by


the use of symbolic rather than numeric computations.
Symbolic computation means that symbols, consisting of names rather than
numbers, are manipulated.
Also, symbolic computation is more conveniently done with linked lists of data
rather than arrays.
The first widely used programming language developed for AI applications was the
functional language LISP in 1959.
Prolog: Logic programming (1970s).
Modern AI uses C language.

4. Systems Programming

The operating system and the programming support tools of a computer system are
collectively known as its systems software.
Early languages: PL/S (IBM), BLISS (Digital), Extended ALGOL (Burroughs).
Most system software is now written in more general programming languages,
such as C and C++.
The UNIX operating system is written almost entirely in C.

5. Web Software

Eclectic mix of languages (markup, scripting, general-purpose).


Languages:
o Markup: HTML (not a programming language).
o Scripting: JavaScript, PHP (embedded in HTML for dynamic content).
o General-purpose: Java, Python, Ruby.
Modern web development often uses frameworks like React, Angular, or Django.

Language Evaluation Criteria


REVATHY D R

1. Readability

The ease with which programs can be read and understood.


The characteristics that contribute to the readability of a programming language
are:
i. Overall Simplicity
▪ A language should not have too many features; otherwise, it becomes
hard to learn and use.
▪ A second complicating characteristic of a programming language is
feature multiplicity--When a language allows multiple ways to do the
same thing, it can lead to confusion.
▪ Example in Java: There are four ways to increment a number
• count = count + 1;
• count += 1;
• count++;
• ++count;
▪ Operator Overloading -- This happens when the same symbol has
multiple meanings.
▪ Example: + is used for both integer addition and floating-point addition.
▪ A language should not be too simple (like assembly language), as it
becomes hard to write structured programs.
▪ It should have enough features to maintain clarity while avoiding
unnecessary complexity.

ii. Orthogonality
▪ Orthogonality in a programming language means that a relatively small
set of primitive constructs can be combined in a relatively small number
of ways to build the control and data structures of the language.
▪ Every possible combination of these primitives is valid and
meaningful.
▪ It makes the language simpler and easier to learn because there are
fewer exceptions to rules.
▪ In an orthogonal language, you can use the same instruction to add
two numbers, whether they are stored in memory or registers. In a
non-orthogonal language, you might need different instructions for
each case.
▪ VAX vs. IBM Assembly Language:
• VAX uses a single instruction (ADDL) to add two numbers,
regardless of whether they are in memory or registers.
• IBM requires two different instructions (A and AR) depending
on where the numbers are stored.
• VAX is more orthogonal and easier to use.
REVATHY D R
iii. Data Types
▪ The presence of adequate facilities for defining data types and data
structures is crucial for improving readability in a programming
language.
▪ For example, suppose a numeric type is used for an indicator flag
because there is no Boolean type in the language.
▪ Timeout=1
▪ The meaning of this statement is unclear, whereas in a language that
includes Boolean types, we would have the following:
▪ Timeout = true
▪ The meaning of this statement is perfectly clear.
iv. Syntax Design
▪ Special words: Languages like C use braces for statement groups,
which can be confusing. Ada and Fortran 95 improve readability
with distinct closing syntax (e.g., end if, end loop).
▪ Form and meaning: Syntax should indicate semantics. For example,
C's static keyword has different meanings based on context, adding
complexity.
2. Writability

the ease with which a language can be used to create programs.


i. Simplicity and Orthogonality
▪ A language with too many constructs may leave some features
underused or misused, as programmers may not be familiar with all of
them.
▪ A smaller set of primitive constructs combined with consistent rules
(orthogonality) is more effective than a large number of primitives.
▪ A programmer can design a solution to a complex problem after
learning only a simple set of primitive constructs.
ii. Support for Abstraction
▪ Abstraction allows programmers to define and use complex structures
or operations without dealing with all the details.
▪ There are two main types of abstraction:
• Process Abstraction: Using subprograms (e.g., a sort
algorithm) to avoid repetition in code.
• Data Abstraction: Representing complex structures like binary
trees with simple constructs. For example, binary trees in C++
or Java can be represented by a class with pointers, making
them easier to implement.
iii. Expressivity
▪ Expressivity refers to how easily and concisely a language allows
programmers to specify computations.
▪ For example, count++ in C is a more concise way of writing count =
count + 1.
REVATHY D R
3. Reliability

A program is said to be reliable if it performs to its specifications under all


conditions.
i. Type checking
▪ Type checking ensures variables and expressions are used with
compatible types.
▪ It's crucial for language reliability and is more efficient when done at
compile time.
▪ Java performs compile-time type checking, minimizing run-time
errors.
ii. Exception Handling
▪ Exception handling allows a program to intercept runtime errors,
correct them, and continue execution.
▪ Languages like Ada, C++, Java, and C# offer extensive exception
handling, enhancing reliability.
iii. Aliasing
▪ Aliasing occurs when multiple names access the same memory
location.
▪ Most languages allow aliasing, such as using two pointers to the same
variable, but this requires the programmer to be cautious, as changing
one alias also alters the value of the other.
iv. Readability and Writability
▪ Both readability and writability impact a program's reliability.
▪ Easier programs to write are more likely to be correct.
▪ Readability affects reliability during both writing and maintenance
phases.
4. Cost

Cost of Training Programmers-The cost to teach programmers how to use the


language.
Cost of Writing Programs-The cost of actually writing code in the language.
Cost of Compiling Programs-The cost of turning code into executable programs.
Cost of Executing Programs-The cost of running the program (e.g., speed, resource
usage).
Cost of Language Implementation-The cost of the tools and systems needed to use
the language (e.g., compilers, interpreters).
Cost of Poor Reliability-The cost of software failures.
Cost of Maintenance-The cost of fixing bugs and adding new features to existing
programs.
Portability: Can the language run on different systems? (e.g., Java is highly
portable.)
Generality: Can the language be used for many types of applications? (e.g., Python
is general-purpose.)
REVATHY D R
Well-definedness: Is the language clearly defined and free of ambiguities? (e.g.,
C++ is powerful but complex and sometimes ambiguous.

Influences on Language Design


1. Computer Architecture
• Von Neumann Architecture:
o Named after John von Neumann, this architecture has strongly influenced
language design.
o Key Features:
▪ Same memory is used to store both data and instructions.
▪ CPU is separate from memory.
▪ Data/instructions are piped from memory to CPU, and results are sent
back to memory.
• Effect on Language Design:
o Gave rise to imperative programming languages.
o Key language features:
▪ Variables: Represent memory cells.
▪ Assignment statements: Reflect data movement (piping).
▪ Iteration: Efficient due to stored sequential memory instructions.
• Iteration vs Recursion:
o Iteration is faster and more efficient in von Neumann systems.
o Hence, languages designed for such systems favour iteration over recursion.
• Fetch-Execute Cycle:
initialize program counter
repeat forever
fetch instruction at program counter
increment program counter
decode instruction
execute instruction
end repeat
REVATHY D R

2. Programming Design Methodologies


1. Structured Programming (Late 1960s - Early 1970s)
• Emerged due to the rising cost of software development compared to hardware.
• Focused on improving software quality and productivity.
• Introduced Top-Down Design and Stepwise Refinement.
• Problems Identified:
o Weak type checking.
o Overuse of goto statements due to poor control structures.
2. Data-Oriented Design (Late 1970s)
• Shift from procedure-oriented to data-oriented programming.
• Introduced abstract data types (ADTs) for better data management.
• SIMULA 67 was one of the first languages to support data abstraction.
3. Object-Oriented Design (1980s Onwards)
• Built upon data abstraction.
• Introduced key features:
o Encapsulation: Combine data and methods.
o Inheritance: Reuse and extend existing code.
o Dynamic Method Binding: Run-time method resolution.
REVATHY D R
• Smalltalk: First full-fledged OOP language (not widely used, but influential).
• Modern imperative languages with OOP support:
o Java, C++, C#, Ada 95

Implementation Methods
1.Compilation
Compilation is the process of translating a high-level programming language (like C, Java,
etc.) into machine language (binary code) that the computer can directly execute.

🔹 How it works:
• The compiler reads the entire source code and translates it into machine code
(usually an executable file like .exe or .out).
• After compilation, the program can be run any number of times without the source
code.
REVATHY D R
✅ Advantages:
• Fast execution: Code runs directly on the hardware.
• Optimized code: Compilers can improve performance.
• Error detection: Many errors are caught before execution.

❌ Disadvantages:
• Slow compile time: May take time to compile large programs.
• Less flexibility: Hard to modify on-the-fly.
• Platform dependency: Binary code may only work on the target machine.

2. Pure Interpretation
In pure interpretation, the program is executed line by line using an interpreter. No
separate machine code is produced.

🔹 How it works:
• Source code → Interpreter → Execution
• Each instruction is analyzed and executed at runtime.

🔸 Examples:
• Early implementations of Python, Ruby, and LISP.
• BASIC in early computing days.
REVATHY D R
✅ Advantages:
• No compilation needed: Immediate execution.
• Easier debugging: Errors are reported with reference to source lines.
• Good for scripting and rapid development.

❌ Disadvantages:
• Slow performance: Each line is re-parsed and re-executed.
• Higher memory usage: Interpreter and source must be in memory.
• Not suitable for performance-critical applications.

3.Hybrid Implementation Systems


Hybrid systems combine compilation and interpretation to get the best of both worlds.

🔹 How it works:
1. The source code is first compiled into an intermediate form (often called bytecode).
2. Then the bytecode is executed by a virtual machine (VM) or interpreter.

🔸 Examples:
• Java: Compiled to bytecode by javac, then executed by the Java Virtual Machine
(JVM).
• Python (modern): Source → Bytecode → Python Virtual Machine
REVATHY D R

✅ Advantages:
• Improved performance compared to pure interpretation.
• Platform independence: Bytecode can run on any machine with the appropriate VM.
• Good balance between speed and flexibility.

❌ Disadvantages:
• Still slower than fully compiled languages.
• Additional layer (the VM) is required.
REVATHY D R
Names & Variables

• Name is a string of characters used to identify some entity. Allow us to refer to


variables, constants, functions, types, operations etc
• A variable in an imperative language, or an object-oriented language, is a six-
tuple:

Binding, Binding time, & Referencing


• Binding is association of 2 things-an attribute with an entity.
• Binding time is the time at which a binding takes place.
• The early binding (static binding) refers to compile time binding
• late binding (dynamic binding) refers to runtime binding.
• Referencing Environment-complete set of bindings at a given point in a program

📌 Static Binding vs Dynamic Binding


REVATHY D R
🔹 Static Binding (Early Binding)
• Binding happens at compile time.
• The type of object is known at compile time.
• Faster execution as everything is decided before runtime.
• Typically used with private, final, and static methods in Java.
✅ Example: Static Binding in Java
class Dog {
void eat() {
System.out.println("Dog is eating...");
}

public static void main(String[] args) {


Dog d = new Dog(); // Type is known at compile time
d.eat(); // Compiler binds 'eat()' to Dog's method
}
}
🔍 Output:
Dog is eating...
✅ Why is this static?
• The method eat() is resolved at compile time.
• The object type (Dog) is known beforehand.

🔹 Dynamic Binding (Late Binding)


• Binding happens at run time.
• The type of object is determined during execution.
• Enables polymorphism — useful in Object-Oriented Programming.
• Usually occurs with overridden methods in inheritance.
✅ Example: Dynamic Binding in Java
class Animal {
void eat() {
System.out.println("Animal is eating...");
}
}

class Dog extends Animal {


void eat() {
System.out.println("Dog is eating...");
}

public static void main(String[] args) {


REVATHY D R
Animal a = new Dog(); // Reference is of Animal, object is of Dog
a.eat(); // At runtime, JVM decides which 'eat()' to call
}
}
🔍 Output:
Dog is eating...
✅ Why is this dynamic?
• The actual object is created at runtime (new Dog()), not just the reference.
• JVM dynamically binds the method call to the appropriate method (Dog.eat()).

📊 Comparison Table
Feature Static Binding Dynamic Binding

Binding Time Compile Time Run Time

Speed Faster Slower

Static, Private, Final Overridden methods


Method Type (Inheritance)
methods

Polymorphism ❌ Not supported ✅ Supported

Type known at compile Type known at runtime


Determination
time

🔹 What is Storage Binding?


Storage binding refers to the process of associating a variable with a physical
memory location.
The lifetime of a variable is the duration during which it is bound to that memory
location.

🧠 Types of Storage Bindings Based on Lifetime


1. Static Variables
• Definition: Bound to memory before program execution begins and remains bound
until the program ends.
• Lifetime: Entire duration of program execution.
• Use Case: Global variables or local variables in functions with the static keyword.
✅ Advantages:
• Efficient access (direct addressing).
REVATHY D R
• No runtime overhead for allocation/deallocation.
❌ Disadvantages:
• No flexibility (can't support recursion).
• Storage can’t be reused.
📌 Example (C++):
#include <iostream>
void func() {
static int count = 0; // Static variable
count++;
std::cout << count << std::endl;
}

int main() {
func(); // prints 1
func(); // prints 2 (value is retained)
return 0;
}

2. Stack-Dynamic Variables
• Definition: Storage is bound at runtime when the function is called. Deallocated
when the function exits.
• Lifetime: Duration of the function/method call.
• Use Case: Local variables inside functions without the static keyword.
✅ Advantages:
• Supports recursion.
• Memory is reused efficiently via the stack.
❌ Disadvantages:
• Slower access (indirect addressing).
• Cannot retain history across calls.
📌 Example (C++):
void demo() {
int x = 0; // Stack-dynamic variable
x++;
std::cout << x << std::endl;
}
Each call to demo() resets x to 0.

3. Explicit Heap-Dynamic Variables


• Definition: Storage is allocated and deallocated manually during runtime using
operators like new/delete or functions like malloc/free.
REVATHY D R
• Lifetime: Begins when allocated, ends when explicitly deallocated.
• Use Case: Dynamic data structures like linked lists or trees.
✅ Advantages:
• Allows dynamic memory management.
• Useful for growing/shrinking data structures.
❌ Disadvantages:
• Risk of memory leaks or dangling pointers.
• Harder to manage correctly.
📌 Example (C++):
int* ptr = new int; // allocate
*ptr = 10;
delete ptr; // deallocate

4. Implicit Heap-Dynamic Variables


• Definition: Allocation and deallocation are handled automatically at runtime by the
language, often based on assignment.
• Lifetime: Determined implicitly, usually by scope or garbage collection.
• Use Case: Scripting languages like Python, JavaScript.
✅ Advantages:
• Programmer doesn't need to manage memory directly.
• Easier to write code.
❌ Disadvantages:
• Runtime overhead due to garbage collection.
• Unpredictable performance in some cases.
📌 Example (Python):
x = "Hello" # Implicit heap-dynamic variable
x = 42 # Previous string is garbage collected

🔁 Summary Table
Bound Example
Type Lifetime Language
Time

Compile- C, C++, Java


Static Entire program (static)
time
During
Stack- C, Java, Python
Runtime function/method
Dynamic
execution
REVATHY D R

Bound Example
Type Lifetime Language
Time
Explicit C++
Runtime Until manually
Heap- (new/delete)
(manual) deallocated
Dynamic
Implicit Python,
Runtime
Heap- Until garbage collected JavaScript
(auto)
Dynamic

Let me know if you'd like diagrams or code comparisons between languages!


REVATHY D R

Understanding the Pseudocode


x: integer := 3
y : integer := 4

procedure add
x := x + y

procedure second(P : procedure)


x : integer := 5
P()

procedure first
y : integer := 6

second(add)
REVATHY D R
first()

write integer(x)
• Global Variables: x = 3, y = 4
• Procedure add modifies x by adding y.
• Procedure second(P) declares a local x = 5 and calls procedure P.
• Procedure first declares a local y = 6.

(a) Static Scoping (Lexical Scoping) Output


• Static scoping means that variables are resolved based on their definitions in the
program text.
• The add procedure modifies x using x := x + y.
• Under static scoping, x inside add refers to the global x, and y refers to the global y.
Step-by-Step Execution:
1. x = 3, y = 4 (global scope)
2. second(add) is called:
o Local x = 5 (this does not affect global x).
o add() is called inside second().
o Since add is defined in the global scope, it modifies the global x.
o add does: x = x + y → x = 3 + 4 = 7.
3. first() is called:
o Local y = 6 (does not affect global y).
4. write integer(x) prints 7 (since x was modified globally in add).
Output in Static Scoping: 7

(b) Dynamic Scoping Output


• Dynamic scoping means that variables are resolved based on the call stack at runtime.
• When add is executed inside second(), it will use the most recently declared x and y in
the current call stack.
Step-by-Step Execution:
REVATHY D R
1. x = 3, y = 4 (global scope)
2. second(add) is called:
o Local x = 5 (shadows global x).
o add() is called inside second().
o Under dynamic scoping:
▪ x refers to the most recent x, which is x = 5 (from second).
▪ y refers to the most recent y, which is y = 4 (global, since y is never
redefined in second).
o add does: x = x + y → x = 5 + 4 = 9.
3. first() is called:
o Local y = 6 (does not affect global y).
4. write integer(x) prints 3 (since the modification of x = 9 was inside second's local
scope, but the global x remains unchanged).
Output in Dynamic Scoping: 3

You might also like