Principle Of Programming Language
Principles of Programming Languages
Section titled “Principles of Programming Languages”History & Classification of Programming Languages
Section titled “History & Classification of Programming Languages”History of Programming Languages
-
Machine Language (1st Generation)
- Consists of binary instructions (0s and 1s) executed directly by CPU.
- Hardware dependent, difficult to program and debug.
- Example:
10110000 01100001
-
Assembly Language (2nd Generation)
- Uses mnemonics instead of binary codes.
- Requires assembler to convert to machine code.
- Example:
MOV A, B - Easier than machine language but still hardware specific.
-
High-Level Languages (3rd Generation)
- Closer to human language, machine-independent.
- Needs compiler or interpreter for translation.
- Examples: FORTRAN, COBOL, BASIC, C, Pascal.
-
Very High-Level Languages (4th Generation)
- Focus on problem-solving, less coding effort.
- Often domain-specific.
- Examples: SQL, MATLAB, R, SAS.
-
Object-Oriented & Modern Languages (5th Generation)
- Emphasize objects, reusability, AI & logic-based design.
- Examples: C++, Java, Python, Prolog.
Classification of Programming Languages
Section titled “Classification of Programming Languages”-
Based on Programming Paradigm
- Imperative Languages:
Describe how to perform tasks using statements that change program state.
Examples: C, Pascal. - Declarative Languages:
Describe what the program should accomplish, not how.
Examples: SQL, Prolog.
- Imperative Languages:
-
Based on Abstraction Level
- Low-Level: Machine and Assembly languages.
- High-Level: C, Java, Python.
- Very High-Level: SQL, MATLAB.
-
Based on Execution Method
- Compiled Languages: Entire code translated before execution. (C, C++)
- Interpreted Languages: Line-by-line translation during execution. (Python, JavaScript)
-
Based on Application Domain
- System Programming Languages: C, C++.
- Scientific/Engineering: FORTRAN, MATLAB.
- Business Applications: COBOL.
- Web Development: JavaScript, PHP.
- AI/Logic Programming: Lisp, Prolog.
Evolution Trend
- Shift from hardware-dependent → machine-independent.
- From procedural → object-oriented → functional and declarative paradigms.
- Emphasis on reusability, modularity, portability, and safety.
Data Abstraction and Data Structures
Section titled “Data Abstraction and Data Structures”Data Abstraction
Section titled “Data Abstraction”- Definition: Process of hiding internal implementation details and showing only essential features.
- Purpose: Simplifies programming by focusing on what an object does rather than how it does it.
Levels of Data Abstraction
Section titled “Levels of Data Abstraction”-
Physical Level:
Describes how data is actually stored (files, memory layout). -
Logical Level:
Describes what data is stored and relationships among data (schemas, classes). -
View Level:
Describes only part of the database relevant to a user (interfaces, APIs).
Benefits
- Reduces complexity
- Increases reusability and maintainability
- Enables modularity and encapsulation
Example (C++)
class Stack {private: int arr[100], top;public: Stack() { top = -1; } void push(int x) { arr[++top] = x; } int pop() { return arr[top--]; }};Here, user interacts via push() and pop() without knowing internal implementation → abstraction.
Data Structures
Section titled “Data Structures”- Definition: Organized way to store, manage, and access data efficiently.
- Types:
- Primitive: int, float, char, bool
- Non-Primitive: Arrays, Linked Lists, Stacks, Queues, Trees, Graphs, Hash Tables
Linear Data Structures
- Array: Contiguous memory; random access.
- Linked List: Dynamic size; sequential access via pointers.
- Stack: LIFO; operations – push(), pop().
- Queue: FIFO; operations – enqueue(), dequeue().
Non-Linear Data Structures
Section titled “Non-Linear Data Structures”- Tree: Hierarchical structure with root and child nodes.
- Graph: Set of nodes (vertices) connected by edges; can represent networks.
Applications
- Arrays → Static data storage
- Linked List → Dynamic memory allocation
- Stack → Function call management, expression evaluation
- Queue → Scheduling, buffering
- Tree → Hierarchical databases, parsing
- Graph → Routing, social networks
Relationship Between Abstraction & Data Structures
- Data abstraction defines interface; data structure defines implementation.
- Example: Abstract Data Type (ADT) like Stack can be implemented using array or linked list.
Syntax and Semantics of Programming Languages
Section titled “Syntax and Semantics of Programming Languages”- Definition: Set of rules defining the structure and form of valid statements in a programming language.
- Purpose: Specifies how programs must be written.
- Described by: Grammars (often BNF – Backus-Naur Form).
Example (C++)
int sum(int a, int b) { return a + b; }Syntax specifies keywords, punctuation, and order of tokens.
Types of Syntax Rules
- Lexical Rules: Define valid tokens (identifiers, literals, operators).
- Grammatical Rules: Define how tokens combine into expressions, statements, and programs.
Syntax Errors
- Violations of grammatical structure detected by compiler’s parser.
Example: Missing semicolon, unmatched braces.
Semantics
Section titled “Semantics”- Definition: Defines the meaning of syntactically correct statements.
- Purpose: Explains what a valid statement actually does during execution.
Types of Semantics
-
Static Semantics:
Rules checked at compile time (type checking, declaration before use).
Example: Assigning string to integer variable. -
Dynamic Semantics:
Describes meaning during execution (control flow, variable binding).
Example: Runtime behavior of loops and function calls.
Example
int a = 5, b = 2;int c = a / b;- Syntax: Expression
a / bis valid. - Semantics: Division of integers results in integer (2), not 2.5.
Syntax vs Semantics
| Aspect | Syntax | Semantics |
|---|---|---|
| Meaning | Structure/grammar | Behavior/meaning |
| Checked by | Parser | Semantic analyzer/interpreter |
| Error Type | Syntax error | Logical/runtime error |
| Example Error | Missing semicolon | Wrong computation result |
Relation
- Syntax ensures correct form of code.
- Semantics ensures correct meaning of code.
- Both together define the complete specification of a programming language.
Types in Programming Languages (Features / Design / Implementation)
Section titled “Types in Programming Languages (Features / Design / Implementation)”A type defines a set of values and operations that can be performed on those values.
It provides a way to classify data and ensure correctness of operations in a program.
Features of Types
- Type Safety: Prevents operations on incompatible data (e.g., adding int + string).
- Type Checking: Ensures operations follow type rules.
- Type Conversion (Casting): Allows conversion between compatible types.
- Polymorphism: Enables one interface to handle multiple data types.
- Type Inference: Compiler deduces data type automatically (e.g.,
autoin C++). - Type Equivalence: Determines when two types are considered the same (name vs structure equivalence).
Type System Design Issues
-
Primitive Types:
- Basic built-in types (int, float, char, bool).
-
Composite (Structured) Types:
- Formed by combining primitive types.
- Examples: Arrays, Structures, Classes, Records.
-
Abstract Types:
- User-defined types that encapsulate data and operations (e.g., ADTs, classes).
-
Strong vs Weak Typing:
- Strongly Typed: Prevents implicit conversions (Java, Python).
- Weakly Typed: Allows implicit conversions (C, JavaScript).
-
Static vs Dynamic Typing:
- Static Typing: Type checked at compile time (C, C++).
- Dynamic Typing: Type checked at runtime (Python, JavaScript).
Type Implementation
-
Type Representation:
- Determines how types are stored in memory (size, alignment, encoding).
- Example:
int(4 bytes),float(4 bytes),double(8 bytes).
-
Type Checking Mechanisms:
- Compile-Time: Detects mismatches early (faster, safer).
- Run-Time: More flexible but slower.
-
Type Conversion Mechanisms:
- Implicit (Coercion): Done automatically by compiler.
- Explicit (Casting): Programmer-specified.
Example (C++)
int a = 5;double b = 2.5;double c = a + b; // implicit conversion of int → doubleint d = (int)b; // explicit conversionImportance of Type Systems
- Ensures program correctness and safety
- Enables compiler optimization
- Facilitates readability and maintainability
- Prevents runtime errors and unexpected behaviors