Survey of Programming Languages
Survey of Programming Languages
Lesson 2
Lesson Note: Programming Paradigms
What Are Programming Paradigms?
A programming paradigm is a style or way of programming that provides a structure for writing
and organizing code. Different paradigms support different approaches to solving problems and
structuring programs.
a. Imperative Paradigm
• Focus: How to achieve a result.
• Programs are written as a sequence of commands for the computer to perform.
• Key Concepts: Statements, variables, loops, and conditionals.
• Examples: C, Python, Java (partly).
b. Declarative Paradigm
• Focus: What result is desired, not how to achieve it.
• Tells the computer what the program should accomplish.
• Key Concepts: Facts and rules (logic programming), queries.
• Examples: SQL, Prolog, HTML (declarative structure).
c. Procedural Paradigm
• Subset of imperative programming.
• Organizes code into reusable procedures or functions.
• Key Concepts: Function calls, modularity, scope.
• Examples: C, Pascal.
e. Functional Paradigm
• Based on mathematical functions and immutability.
• Avoids side effects; supports higher-order functions.
• Key Concepts: Pure functions, recursion, first-class functions.
• Examples: Haskell, Lisp, Scala, Elixir.
f. Scripting Paradigm
• Used for automating repetitive tasks or enhancing web applications.
• Typically interpreted and lightweight.
• Key Concepts: Script files, shell commands, rapid development.
• Examples: JavaScript, Bash, Python, Perl.
Comparison of Paradigms
OOP Objects & data Large software, UI apps Java, C++, Python
Lesson 3
Syntax and Semantics of Programming Languages
Introduction
In the study of programming languages, two fundamental concepts are syntax and semantics:
• Syntax: The structure or form of expressions, statements, and program units.
• Semantics: The meaning of those expressions, statements, and program units.
Syntax refers to the rules that define the correct combinations of symbols in a programming language.
Types of Syntax
1. Concrete Syntax
o How programs are written by humans.
o Includes symbols, keywords, operators, punctuation (e.g., {}, ;, if, while).
2. Abstract Syntax
o A simplified, structural version that removes unnecessary symbols and focuses on the
program's logical structure (e.g., abstract syntax trees).
Grammar
• Programming languages are usually defined by formal grammar (e.g., Backus-Naur Form -
BNF or Extended BNF).
• Grammar rules define how valid statements and expressions are formed.
Syntax Errors
• Occur when code does not follow the defined syntax.
• Detected at compile-time or during interpretation.
Semantics of Programming Languages
Definition
Semantics defines the meaning behind syntactically correct statements.
Types of Semantics
Static Semantics
o Concerns rules that can be checked without executing the program (e.g., type
checking, scope rules).
o Violations usually result in compile-time errors.
Dynamic Semantics
o Describes what the program does when executed (runtime behavior).
o Includes evaluation of expressions, control flow, and memory manipulation.
Approaches to Defining Semantics
Operational Semantics
o Describes how a program executes step-by-step on an abstract machine.
o Used in interpreters.
Denotational Semantics
o Maps language constructs to mathematical objects.
o Used for formal reasoning and proving correctness.
Axiomatic Semantics
o Uses logical assertions to reason about program behavior.
o Foundation for program verification (e.g., Hoare Logic).
Examples
Example 1: Syntax Error (python)
print("Hello World" # Missing closing parenthesis
Example 2: Semantic Error (python)
x = 10 / 0 # Division by zero
Data Types
Primitive Data Types
These are the most basic data types provided by a programming language:
Type Systems
• Static Typing: Types are declared at compile time (e.g., Java, C++)
• Dynamic Typing: Types are determined at runtime (e.g., Python, JavaScript)
• Strong Typing: Prevents implicit type conversion
• Weak Typing: Allows more flexibility in type conversion
Data Structures
Data structures are used to organize and manage data efficiently for various operations such as
searching, sorting, insertion, and deletion.
Linear Data Structures
Array
o Fixed-size collection of elements.
o Example: int arr[5] = {1, 2, 3, 4, 5};
Linked List
o Each element (node) contains data and a reference to the next node.
o Types: Singly, Doubly, Circular
Stack
o Follows Last-In-First-Out (LIFO) principle.
o Operations: push(), pop()
Queue
o Follows First-In-First-Out (FIFO) principle.
o Types: Simple, Circular, Priority Queue
Non-Linear Data Structures
Tree
o Hierarchical structure with nodes and edges.
o Special types: Binary Tree, Binary Search Tree (BST)
Graph
o Set of nodes (vertices) connected by edges.
o Types: Directed, Undirected, Weighted
Hash Table
• Stores data in key-value pairs.
• Allows fast access, insertion, and deletion.
• Used in dictionaries/maps.
Variables
• A variable is a symbolic name that refers to a memory location where data is stored.
• It allows the programmer to store, retrieve, and manipulate data during program execution.
Example (Python):
python
x = 10
name = "Alice"
Scope
• Scope defines the visibility or accessibility of a variable within different parts of a program.
• There are typically three main types of scope:
a. Local Scope
• Variables declared within a function or block.
• Only accessible within that function or block.
python
def greet():
message = "Hello"
print(message) # 'message' is local to greet()
b. Global Scope
• Declared outside all functions.
• Accessible from anywhere in the program (unless shadowed).
python
name = "Alice"
def greet():
print(name) # Can access global variable 'name'
c. Nonlocal (Enclosed) Scope
• Applies to nested functions.
• Variables declared in the outer function are accessible to the inner function.
python
def outer():
count = 5
def inner():
print(count) # Accesses 'count' from the enclosing scope
inner()
Lifetime
• Lifetime refers to the duration a variable exists in memory during program execution.
a. Local Variable Lifetime
• Exists only during the execution of the function/block.
• Memory is released once the function exits.
b. Global Variable Lifetime
• Exists throughout the program's execution.
• Created when the program starts and destroyed when it ends.
In summary
Lifetime Duration a variable exists in memory Temporary (local) or full program (global)
Importance in Programming
• Prevents naming conflicts and unintended side effects.
• Helps in memory management.
• Encourages modular and bug-free code.
d. Jump/Branching Statements
Used to alter the normal flow of control.
• break: Exits the current loop.
• continue: Skips the rest of the current iteration and starts the next.
• return: Exits from a function and optionally returns a value.
python
for i in range(10):
if i == 5:
break
print(i)