Programming Languages - In-depth
Notes
1. Syntactic and Semantic Rules of a Programming Language
Syntactic Rules:
- These define the structure and form of valid statements in a language.
- Syntax governs how tokens (keywords, operators, identifiers) are arranged.
- It is based on formal grammars like BNF (Backus-Naur Form).
- Syntax errors prevent compilation.
Example: In C, a function must be defined with a return type: int main() {...}
Semantic Rules:
- Define the meaning of syntactically correct statements.
- Ensure logic and meaning, such as type compatibility and scope.
- Example: int x = "abc"; is syntactically correct but semantically wrong.
- Semantic analysis phase in compilers ensures that operations are valid.
2. Characteristics of a Good Programming Language
A good programming language should support both the programmer and the machine. It
must:
- Simplicity: Should have minimal and simple constructs.
- Readability: Easy to read and understand, with consistent naming and indentation.
- Writability: Must facilitate ease of writing complex logic.
- Orthogonality: Few primitive constructs can be combined in a consistent way.
- Efficiency: Should use system resources effectively.
- Portability: Should run across various platforms with minimal changes.
- Modularity: Should support functions, procedures, classes for code reuse.
- Robust Error Handling: Provides clear error messages and recovery mechanisms.
- Security: Restricts access to protected data and enforces safe programming practices.
- Rich Ecosystem: Should have tools like debuggers, IDEs, and libraries.
3. Programming Language Translators
Compiler:
- Converts entire high-level source code into machine code before execution.
- Faster runtime, as the program is pre-compiled.
- Example: C, C++
Interpreter:
- Executes source code line by line.
- Useful for scripting and dynamic applications.
- Slower than compiled programs.
- Example: Python, JavaScript
Assembler:
- Converts assembly language code into machine code.
Hybrid:
- Languages like Java compile to intermediate bytecode.
- Then interpreted by the JVM (Java Virtual Machine).
- Offers portability and moderate performance.
4. Elementary Data Types
Data Objects: Abstract representations of values handled by the program.
Variables: Named references to memory locations where data is stored.
Constants: Fixed values which cannot be changed during execution.
Primitive Types:
- Integer types (int, short, long)
- Floating-point types (float, double)
- Character type (char)
- Boolean type (true, false)
- Void type (represents 'no value', typically used for functions)
5. Specification & Implementation of Elementary Data Types
Specification:
- Defines what values a data type can hold and what operations can be performed.
- Abstract Data Type (ADT): Describes the logical behavior.
Implementation:
- Describes how the data type is stored in memory (bits, bytes, alignment).
- Example: int may be implemented as 32-bit or 64-bit depending on system.
- Involves storage strategy, precision (for floats), encoding (ASCII for chars).
6. Declarations, Type Checking & Type Conversions
Declarations:
- Declare variables before use, specifying type and optionally value.
Example: int a = 5;
Type Checking:
- Ensures type safety; prevents applying incompatible operations.
- Static type checking: Done at compile-time (C, Java).
- Dynamic type checking: Done at run-time (Python, JavaScript).
Type Conversion:
- Implicit Conversion: Automatic promotion of types.
Example: int to float.
- Explicit Conversion (Type Casting):
Example: float x = (float) a / b;
7. Assignment & Initialization
Assignment:
- Assigns a value to a variable during execution.
Example: x = 10;
Initialization:
- Assigns value at declaration.
Example: int x = 10;
- Helps avoid undefined behavior due to uninitialized variables.
- Constants must be initialized at the time of declaration.
8. Numeric Data Types
Integer Types:
- Can be signed or unsigned.
- Includes: int, short, long, long long.
- Range depends on system architecture (e.g., 32-bit or 64-bit).
Floating-point Types:
- Used for real numbers with fractional parts.
- Includes: float (4 bytes), double (8 bytes), long double (10 or more bytes).
- Follow IEEE 754 standard for floating-point arithmetic.
9. Enumerations
- enum defines symbolic names for integer constants.
- Useful for readability and preventing invalid values.
Syntax:
enum Color { RED, GREEN, BLUE };
Color c = GREEN;
- Internally: RED=0, GREEN=1, etc., unless values are specified.
10. Booleans & Characters
Booleans:
- Represent truth values: true or false.
- In C: stdbool.h must be included.
- In modern languages, bool is a built-in type.
Characters:
- Represented using char type.
- Stored as numeric codes using ASCII or Unicode.
- 'A' = ASCII 65, 'a' = ASCII 97.
- Escape sequences: '\n', '\t', '\\' represent newline, tab, backslash.