CSC262 Theory of Computation

Theory of ComputationUnit 110 min read

Formal Languages & Automata: Alphabets, Strings, Grammars & Machines

Unit 1 of Theory of Computation introduces the foundational concepts of formal languages—alphabets, strings, languages, grammars (Type-0 to Type-3), and automata (DFA, NFA, ε-NFA)—explaining how they model computation, their hierarchies, and their real-world applications in parsing, compilers, and language processing.

TAKEAWAYS:

  • Formal languages are built from alphabets, strings, and grammars (rules), and are classified into Chomsky hierarchy (Type-0 to Type-3), with regular languages being the simplest.
  • Automata (DFAs, NFAs, ε-NFAs) are abstract machines that recognize languages, where DFAs use deterministic transitions and NFAs allow nondeterminism and ε-transitions.
  • Grammars define languages via production rules, with regular grammars (right-linear/left-linear) directly convertible to automata, while context-free grammars require pushdown automata.
  • Equivalence between grammars and automata exists: every regular grammar ↔ DFA/NFA, and every context-free grammar ↔ PDA.
  • Real-world ties: Parsers in compilers (e.g., Java’s syntax), keyword search in eSewa/Khalti, and pathfinding in Pathao use these models.
  • Exam focus: Master definitions, conversions (regex → NFA/DFA, grammar → automata), and proofs (e.g., pumping lemma for non-regularity).

1. Formal Languages: The Building Blocks

012345ε (empty string)aaaaab
Strings as sequences of symbols (Σ*)

1.1 Alphabets and Strings

An alphabet (Σ) is a finite set of symbols (e.g., Σ = {0, 1}). A string is a finite sequence of symbols from Σ (e.g., 1010 ∈ Σ*).

  • Language (L): A set of strings over Σ (e.g., L = {w ∈ Σ* | w ends with 01}).
  • Operations:
    • Concatenation: .
    • Union/Intersection: , .
    • Kleene star: (includes ε, the empty string).
graph LR
    A["Σ = {0,1}"] --> B["String: 101 ∈ Σ*"]
    B --> C["Language L = {101, 0101, ...}"]
    C --> D["Operations: L1 ∪ L2, L1L2, L*"]

1.2 Grammars: Rules for Generating Languages

A grammar (G) is a 4-tuple , where:

  • : Variables (non-terminals).
  • : Terminals (alphabet).
  • : Production rules (e.g., ).
  • : Start symbol.

Chomsky Hierarchy (from most to least powerful):

Type Name Example Rule Form Automaton
0 Unrestricted Turing Machine
1 Context-Sensitive Linear-Bounded
2 Context-Free Pushdown Autom.
3 Regular or Finite Autom.

Example: Regular grammar for even 0s:

S → 0S0 | 1S1 | ε

Generates strings like 00, 110011, etc.

1.3 Real-World: Parsing in Compilers (Java, Python)

  • How it works: Compilers use context-free grammars (CFGs) to parse code. For example, Java’s syntax for if statements is defined by CFG rules like:
    Statement → IfCondition '(' Expression ')' Statement
    
  • Why it matters: Ensures programs follow grammatical rules (e.g., balanced braces {}). Tools like ANTLR or Yacc use CFGs to build parsers.
  • Nepalese tie: eSewa’s API validation uses regex (regular grammars) to check if user inputs (e.g., phone numbers) match patterns like 98XXXXXXXX.

2. Automata: Machines That Recognize Languages

2.1 Finite Automata (DFA vs. NFA)

Feature DFA NFA
Transitions Deterministic (1 per input) Nondeterministic (0 or many)
ε-transitions No Yes (ε-NFA)
Acceptance Exactly one path per string Multiple paths possible
Example Recognizes 0*1 Recognizes (0+1)*01
start0101q0q1
DFA for the language {0, 01, 011, 0111, ...}

DFA Definition: A DFA , where:

  • : States.
  • : Transition function ().
  • : Initial state.
  • : Accept states.

Example: DFA for strings ending with 01:


Trace: String 0101: q0 --0--> q1 --1--> q2 --0--> q3 --1--> q4 (accept).

2.2 NFA and ε-NFA

  • NFA: Multiple transitions possible for an input (e.g., δ(q, a) = {q1, q2}).
  • ε-NFA: Allows ε-transitions (no input consumed).
  • ε-closure(q): Set of states reachable from q via ε-transitions.

Example: NFA for (a+b)*abb:

starta, bεabbq0q1q2q3
ε-NFA for (a+b)*abb (ε-transitions shown as dashed lines)

2.3 Real-World: Pathao’s Route Planning (NFA)

  • How it works: Pathao’s algorithm for finding routes can be modeled using NFAs where:
    • States = intersections.
    • Transitions = roads (with weights for distance/time).
    • Nondeterminism = multiple paths to choose from (e.g., traffic delays).
  • Why it matters: NFAs help represent all possible paths, and algorithms like A* (used in Pathao) can simulate acceptance by exploring paths until a solution is found.

3. Equivalence and Conversions

3.1 Regular Expressions ↔ NFAs/DFAs

Every regular expression can be converted to an NFA (via Thompson’s construction) and then to a DFA (via subset construction).

Example: Convert (a+b)*ab to NFA:

starta, bεabbq0q1q2q3
NFA for (a+b)*ab (Thompson’s construction)

3.2 Regular Grammar ↔ DFA/NFA

  • Right-linear grammar → NFA (states = non-terminals, transitions = productions).
  • Left-linear grammar → DFA.

Example: Grammar , , :

3.3 Real-World: eSewa’s Phone Number Validation (Regex → NFA)

  • How it works: eSewa uses regex ^9[6-9]\d{8}$ to validate Nepali phone numbers.
    • Breaks down to NFA states:
      1. Start → check 9.
      2. Check digit 6-9.
      3. Accept 8 more digits.
  • Why it matters: Ensures only valid numbers are processed, reducing errors in transactions.

4. Key Proofs and Theorems

4.1 Pumping Lemma for Regular Languages

Statement: If is regular, there exists a pumping length such that any string with can be divided into where:

  1. ,
  2. ,
  3. For all , .

Example: Prove is not regular.

  • Assume is regular. Pick , then take .
  • Pump . For , , which is not in unless . Contradiction!

4.2 Real-World: NTC’s Traffic Light Control (DFA)

  • How it works: Traffic lights at busy intersections (e.g., Thapathali) can be modeled as a DFA where:
    • States = Green, Yellow, Red.
    • Transitions = timed (e.g., Green → Yellow after 30 sec).
  • Why it matters: Ensures deterministic, conflict-free switching, preventing accidents.

5. Exam Tip: What to Focus On

  1. Definitions: Memorize formal definitions of DFA, NFA, ε-NFA, PDA, and grammars (Type-0 to Type-3). Use tables to compare them.
  2. Conversions:
    • Regex → NFA (Thompson’s construction).
    • NFA → DFA (subset construction).
    • Regular grammar ↔ DFA/NFA.
  3. Proofs:
    • Pumping lemma applications (show non-regularity).
    • Closure properties (union, concatenation, Kleene star preserve regularity).
  4. Worked Examples:
    • Always show step-by-step traces for automata (e.g., "On input 101, the DFA transitions as: q0 → q1 → q2 → q3").
    • For grammars, derive all possible derivations (e.g., ).
  5. Real-World Links:
    • Tie regex to eSewa/Khalti validation.
    • Tie DFAs to traffic lights or NTC’s deterministic routing.
    • Tie CFGs to compiler parsers (Java/Python).

6. Common Pitfalls

  • ε-transitions: Forgetting to compute ε-closure in NFA simulations.
  • DFA minimization: Missing equivalent states during state-merging.
  • Pumping lemma: Misapplying or not ensuring .
  • Grammar conversions: Confusing left-linear vs. right-linear rules.

7. Practice Problems (Exam-Style)

  1. Convert the regex to an NFA.
  2. Design a DFA that accepts strings with an equal number of 0s and 1s.
  3. Prove is not regular using the pumping lemma.
  4. Convert the grammar , , to a DFA.
  5. Explain why is not regular (hint: use pumping lemma).

8. Summary Table: Language Classes and Automata

Language Type Grammar Type Automaton Example Language
Regular Right/Left-linear DFA/NFA (NO! Not regular)
Context-Free CFG PDA
Context-Sensitive CSG Linear-Bounded
Recursively Enumerable Type-0 Grammar Turing Machine

9. Visual Cheat Sheet

mindmap
  root((Formal Languages & Automata))
    Alphabets & Strings
      Σ*
      ε
      Operations: Union, Concatenation, *
    Grammars
      Type-0 to Type-3
      CFG Example: {a^n b^n}
    Automata
      DFA
        Deterministic
        Minimization
      NFA
        Nondeterministic
        ε-closure
      PDA
        Stack
        CFG ↔ PDA
    Real-World
      eSewa: Regex Validation
      Pathao: NFA for Routes
      Compilers: CFG Parsing
    Exam Tips
      Memorize Definitions
      Practice Conversions
      Pumping Lemma Proofs

Based on the TU BSc CSIT syllabus for Theory of Computation (CSC262), unit 1.

Discussion

Loading…