Individual Assignment Software Construction
Name Naod Eyob ID Mdr 1026 /19
1. What is lexical structure in programming languages?
Lexical structure defines the vocabulary of a language, including the basic building blocks such as
keywords, operators, identifiers, literals, and comments.
Python
x = 10 # identifier 'x' is assigned an integer literal 10
y = x + 5 # identifier 'y' is assigned the result of an expression
using '+'
print(y) # keyword 'print' is used to display output
• Identifiers: x, y, print
• Keywords: print
• Operators: =, +
• Literals: 10, 5
2. What is syntactic structure in programming languages?
Syntactic structure defines the grammar rules for forming valid statements and expressions using
the lexical units.
Example (Python):
python
Copy code
if x > 0: # 'if' statement syntax
print("Positive") # Indented block as part of 'if' statement
else: # 'else' statement syntax
print("Non-positive")
The syntax rules ensure that constructs like if statements and blocks are correctly
formatted and nested.
3. What is contextual structure in programming languages?
Contextual structure refers to the rules that go beyond syntax, including variable
declarations and scope rules. It ensures that variables are declared before use and that
there are no name clashes.
Example (Python):
python
Copy code
def add(a, b): # function 'add' with parameters 'a' and 'b'
return a + b
result = add(5, 10) # calling 'add' with arguments
print(result)
• Function Scope: a and b are only accessible within add.
• Variable Scope: result is in the global scope.
4. What is semantic structure in programming languages?
Semantic structure defines the meaning of syntactically correct statements and
expressions. It involves type checking, data type compatibility, and other rules that ensure
the program makes logical sense.
Example (Python):
python
Copy code
x = "hello" # 'x' is a string
y = x + 10 # TypeError: cannot concatenate 'str' and 'int'
• Type Checking: Ensures x and y are used in a type-consistent manner.
• Semantic Error: Adding a string (x) and an integer (10) causes a type error.
Advantages and Disadvantages of Structures
Lexical Structure
• Advantages:
o Simplification of Parsing: Efficiently handles the code by breaking it into
tokens.
o Error Detection: Catches errors like illegal characters early.
o Portability: Helps in porting the language across different platforms.
• Disadvantages:
o Limited Scope: Can't catch context-related errors.
o Complexity in Handling Unicode: Complicates lexical analysis.
o Ambiguity: Some tokens may have different meanings in different contexts.
Syntactic Structure
• Advantages:
o Grammar Enforcement: Ensures correct syntax, enhancing consistency.
o Error Detection: Identifies structural errors.
o Structured Programming: Promotes clear and maintainable code.
• Disadvantages:
o False Positives: May flag correct code as erroneous.
o Limited Error Reporting: Syntax errors can be hard to debug.
o Rigid Rules: Can limit programming flexibility.
Contextual Structure
• Advantages:
o Context Awareness: Ensures code makes contextual sense.
o Error Detection: Detects context-specific errors.
o Enhanced Readability: Maintains clear and logical code structures.
• Disadvantages:
o Complexity: Adds to the compilation process.
o Performance Overhead: Slows down compilation.
o Steep Learning Curve: Requires understanding of context-specific rules.
Semantic Structure
• Advantages:
o Logical Consistency: Ensures the program makes logical sense.
o Error Detection: Catches semantic errors.
o Optimization Opportunities: Identifies and optimizes redundant
operations.
• Disadvantages:
o Complex Analysis: Requires deep understanding of the program's logic.
o Performance Overhead: Adds processing time during compilation.
o Error Reporting: Semantic errors can be hard to fix.
Programming Languages
1. What is imperative programming?
Imperative programming focuses on how to execute, defining the steps that change the
program's state using statements.
Example: Languages: C, Pascal, Assembly
Copy code
int main() {
int a = 5;
int b = 10;
int sum = a + b;
printf("Sum: %d", sum);
return 0;
• Advantages:
o Control: Detailed control over hardware.
o Performance: Efficient and fast.
o Familiarity: Straightforward approach.
• Disadvantages:
o Complexity: Managing state can become complex.
o Error-Prone: Careful management of states is required.
2. What is object-oriented programming?
OOP organizes code into objects containing data and methods, emphasizing classes,
objects, inheritance, encapsulation, and polymorphism.
Example: Languages: Java, C++, Python
python
Copy code
class Dog:
def __init__(self, name):
self.name = name
def bark(self):
print(f"{self.name} says woof!")
dog = Dog("Buddy")
dog.bark()
• Advantages:
o Modularity: Code is organized into manageable classes.
o Reusability: Inheritance and polymorphism promote code reuse.
o Maintainability: Encapsulation helps in updating code.
• Disadvantages:
o Complexity: Can be hard to design and understand.
o Performance Overhead: Inheritance and polymorphism can add overhead.
o Steep Learning Curve: Concepts can be difficult to master.
3. Declarative Programming
What is declarative programming? Declarative programming focuses on what to achieve
rather than how to achieve it, expressing the logic of computation without describing its
control flow.
Example: Languages: SQL, HTML, Prolog
sql
Copy code
SELECT name FROM users WHERE age > 21;
• Advantages:
o Simplicity: Easier to write and understand.
o Maintenance: Abstracts control flow, making maintenance easier.
o Conciseness: Often more concise.
• Disadvantages:
o Limited Control: Less control over task execution.
o Debugging Difficulty: Harder to debug due to abstraction.
o Performance: Can be slower due to abstraction.
4. What is functional programming?
Functional programming treats computation as the evaluation of mathematical functions,
avoiding changing state and mutable data.
Example: Languages: Haskell, Lisp, Scala
haskell
Copy code
factorial 0 = 1
factorial n = n * factorial (n - 1)
• Advantages:
o Modularity: Functions are easily testable and reusable.
o Concurrency: Easier to write concurrent programs.
• Disadvantages:
o Learning Curve: Higher-order functions and recursion can be difficult.
o Limited Libraries: Fewer libraries and tools.
Main Differences between Paradigms
• Control Flow: Imperative programming focuses on how to perform tasks with
explicit control flow, while declarative programming focuses on the outcome.
• State Management: OOP and imperative paradigms manage state through
variables and objects; functional programming emphasizes immutability.
• Abstraction Level: Declarative programming offers higher abstraction compared to
imperative and object-oriented programming.