Skip to main content

5.2 Type-3 Regular Grammars: Right-Linear vs Left-Linear & FA Conversions

📚Module 01Topic 1.1⏱️26 min read
🎯High-Yield For:Semester Exams • GATE CSE • Technical Interviews

💡 Core Intuition: Directional String Synthesis​

A Type-3 Regular Grammar is the generative counterpart to a Finite Automaton. While a finite state machine scans a pre-existing string symbol-by-symbol from left to right, a regular grammar synthesizes strings character-by-character along a strict, unidirectional frontier.

Directional String Synthesis Frontiers: Right-Linear vs Left-Linear

Comparing the growth directions of single-variable frontiers during language generation

Right-Linear

Right-Linear Expansion

⏩
Dominant Architecture / DomainForward Growth (Left-to-Right)
  • •Derivation steps: S ⇒ aB ⇒ abC ⇒ abc
  • •Terminals materialize from left to right.
  • •The single non-terminal variable always sits at the RIGHT frontier.
  • •Direct isomorphism with physical automaton forward scanning.
"S ⇒ aB ⇒ abC ⇒ abc (Terminals grow left-to-right)."
Left-Linear

Left-Linear Expansion

⏪
Dominant Architecture / DomainBackward Growth (Right-to-Left)
  • •Derivation steps: S ⇒ Ba ⇒ Cba ⇒ cba
  • •Terminals materialize from right to left.
  • •The single non-terminal variable always sits at the LEFT frontier.
  • •Direct isomorphism with reading reversed strings from final states.
"S ⇒ Ba ⇒ Cba ⇒ cba (Terminals grow right-to-left)."

🍳 The Everyday Analogy: Threading Beads on an Open String​

Imagine assembling a beaded necklace:

  • Right-Linear Assembly: You tie a permanent knot at the far left. You thread a bead a, leaving the open needle (variable B) at the right end. You slide on another bead b, shifting the needle further to the right. Finally, you tie off the open needle with a clasp (terminal or \epsilon). The beads grow strictly from left to right, and you only ever interact with the rightmost open tip.
  • Left-Linear Assembly: You attach the clasp at the far right. You push beads onto the string from behind, so every new bead pushes the existing strand to the right. The open needle (variable) sits continuously at the leftmost leading edge.
  • The Fatal Hybrid Trap: What happens if an artisan tries to add beads from both ends simultaneously? A rule of the form S→aSbS \to a S b adds a bead on the left and a bead on the right in the same breath. Suddenly, you must remember exactly how many beads you attached on the left so that you attach the identical number on the right! That requires an auxiliary memory counter (a stack). The system immediately bursts out of the finite-state realm and becomes a Context-Free Linear Grammar (anbna^n b^n).

💻 Bridging to Computer Science​

In finite automata, transitions are strictly forward-progressing: reading a character aa in state qiq_i moves the control unit to state qjq_j (δ(qi,a)=qj\delta(q_i, a) = q_j). This maps 1-to-1 with a Right-Linear production:

Ai→aAjA_i \to a A_j

The non-terminal variable acts as the current state, the emitted terminal acts as the consumed input symbol, and the next variable represents the target state. Because an automaton has zero stack memory, it can only track one active state at a time—which is why regular grammars can contain at most one non-terminal variable on their right-hand side.



📜 Formal Mathematical Specification of Type-3 Grammars​

A grammar G=(V,Σ,P,S)G = (V, \Sigma, P, S) is a Type-3 Regular Grammar if and only if all its production rules are either strictly Right-Linear or strictly Left-Linear.

Right-Linear Grammar (RLG)​

A grammar GG is Right-Linear if every production rule in PP adheres to one of the following canonical forms:

  1. A→aBA \to a B (where A,B∈VA, B \in V and a∈Σa \in \Sigma)
  2. A→aA \to a (where A∈VA \in V and a∈Σa \in \Sigma)
  3. A→ϵA \to \epsilon (where A∈VA \in V and ϵ\epsilon is the empty string)

Extended Right-Linear Form: Productions of the form A→wBA \to w B or A→wA \to w, where w∈Σ∗w \in \Sigma^*, are also regular. Any extended RLG can be converted into strict single-terminal RLG by introducing intermediate variables:

A→a1a2a3B  ⟺  A→a1X1,X1→a2X2,X2→a3BA \to a_1 a_2 a_3 B \iff A \to a_1 X_1, \quad X_1 \to a_2 X_2, \quad X_2 \to a_3 B

Left-Linear Grammar (LLG)​

A grammar GG is Left-Linear if every production rule in PP adheres to one of the following canonical forms:

  1. A→BaA \to B a (where A,B∈VA, B \in V and a∈Σa \in \Sigma)
  2. A→aA \to a (where A∈VA \in V and a∈Σa \in \Sigma)
  3. A→ϵA \to \epsilon (where A∈VA \in V)

Extended Left-Linear Form: Similarly, productions of the form A→BwA \to B w or A→wA \to w (where w∈Σ∗w \in \Sigma^*) are left-linear and can be decomposed into single-terminal steps.


⚠️ The Fatal Hybrid Linear Trap: Why Mixing Directions Destroys Regularity​

A common and critical error in formal language theory is confusing a Linear Grammar with a Regular Grammar.

Definition: Linear Grammar​

A grammar G=(V,Σ,P,S)G = (V, \Sigma, P, S) is a Linear Grammar if every production has at most one variable on its right-hand side:

A→uBvorA→w(u,v,w∈Σ∗,  A,B∈V)A \to u B v \quad \text{or} \quad A \to w \quad (u, v, w \in \Sigma^*, \; A, B \in V)

The Core Invariance Theorem​

CRITICAL THEOREM:
Every Regular Grammar is a Linear Grammar, but a Linear Grammar is NOT necessarily Regular!
A grammar is Type-3 Regular if and only if all its rules are uniformly right-linear, OR all its rules are uniformly left-linear. Mixing right-linear and left-linear productions destroys regularity.

Mathematical Proof by Counterexample​

Consider the following grammar GG over Σ={a,b}\Sigma = \{a, b\}:

S→aB∣ϵ,B→SbS \to a B \mid \epsilon, \quad B \to S b
  1. Analyze rule S→aBS \to a B: This is strictly right-linear (variable BB at the right end).
  2. Analyze rule B→SbB \to S b: This is strictly left-linear (variable SS at the left end).
  3. Both rules contain at most one variable on the RHS, so GG is by definition a Linear Grammar.
  4. Now, examine the language generated by deriving sentential forms: S  ⟹  aB  ⟹  a(Sb)=aSbS \implies a B \implies a (S b) = a S b Substituting recursively: S  ⟹  aSb  ⟹  a(aSb)b=a2Sb2  ⟹  ⋯  ⟹  anSbnS \implies a S b \implies a (a S b) b = a^2 S b^2 \implies \dots \implies a^n S b^n Terminating with S→ϵS \to \epsilon: anSbn  ⟹  anϵbn=anbna^n S b^n \implies a^n \epsilon b^n = a^n b^n
  5. The language generated is: L(G)={anbn∣n≥0}L(G) = \{a^n b^n \mid n \ge 0\}

Since {anbn∣n≥0}\{a^n b^n \mid n \ge 0\} is the canonical non-regular context-free language (it fails the Pumping Lemma for regular languages), grammar GG is Type-2 (Context-Free), NOT Type-3 (Regular)!


🔄 Algorithmic Conversions Between Grammars and Finite Automata​

Algorithm 1: Right-Linear Grammar (RLG) to NFA​

Given an RLG G=(V,Σ,P,S)G = (V, \Sigma, P, S), construct an equivalent NFA M=(Q,Σ,δ,q0,F)M = (Q, \Sigma, \delta, q_0, F) as follows:

Right-Linear Grammar to NFA Algorithmic Mapping Matrix

Formal 4-component transformation protocol from grammar variables to automaton states

🔘

State Set Construction (Q)

States
  • Create a distinct state q_A for every variable A ∈ V.
  • Introduce a dedicated new accepting state q_f.
  • Total state count: |Q| = |V| + 1.
🎯

Initial & Final Designations (q₀, F)

Initial / Final
  • Initial state: q₀ corresponds to start symbol S.
  • Dedicated final state: q_f is unconditionally in F.
  • Base final state: If A → ε ∈ P, then state A ∈ F as well.
⚡

Standard Transitions (A → a B)

State Transitions
  • For each rule A → a B, create transition from state A to B on symbol a.
  • Mirrors consuming symbol a and entering next state B.
🏁

Terminating Transitions (A → a)

Accepting Arc
  • For each terminal-only rule A → a, create transition from state A to q_f on a.
  • Guarantees that finishing derivation enters an accepting state.

Algorithm 2: Finite Automaton (NFA/DFA) to Right-Linear Grammar​

Given a finite automaton M=(Q,Σ,δ,q0,F)M = (Q, \Sigma, \delta, q_0, F), construct an equivalent RLG G=(V,Σ,P,S)G = (V, \Sigma, P, S) as follows:

  1. Variables (VV): Create a non-terminal variable AiA_i for every state qi∈Qq_i \in Q.
  2. Start Symbol (SS): The start symbol is the variable corresponding to the initial state: S=A0S = A_0.
  3. Productions (PP):
    • For every transition δ(qi,a)=qj\delta(q_i, a) = q_j, add production: Ai→aAjA_i \to a A_j
    • For every transition leading to an accepting state (qj∈Fq_j \in F), also add the terminating production: Ai→aA_i \to a
    • If the initial state is accepting (q0∈Fq_0 \in F), add the empty string rule: A0→ϵA_0 \to \epsilon

Algorithm 3: Left-Linear Grammar (LLG) to Finite Automata​

Left-Linear grammars generate strings from right to left, which conflicts with standard forward automaton reading. To convert an LLG to an automaton, we leverage the Language Reversal Theorem:

(LR)R=L(L^R)^R = L
Architecture Flow

Left-Linear Grammar to Finite Automaton Reversal Pipeline

Leveraging the Language Reversal Theorem ((Lᴿ)ᴿ = L) to synthesize NFAs from left-linear rules

⏪Stage 1

Left-Linear Grammar (G)

Original grammar with left-linear rules A → Ba | a | ε.

→
Invert RHS
📜Stage 2

Right-Linear Grammar (Gᴿ)

Reversed grammar A → aB | a | ε generating L(G)ᴿ.

→
Algorithm 1
⚡Stage 3

Reversed NFA (Mᴿ)

Forward automaton accepting the reversed language L(G)ᴿ.

→
Reverse Machine
🎯Stage 4

Final Target NFA (M)

Swap start/final states and invert all edge arrows to accept ((Lᴿ)ᴿ = L(G)).

Step-by-Step Reversal Procedure​

  1. Step 1: Construct Reversed Grammar GRG^R:
    • Reverse the right-hand side of every production in LLG GG: A→Ba  ⟹  A→aBA \to B a \implies A \to a B A→a  ⟹  A→aA \to a \implies A \to a A→ϵ  ⟹  A→ϵA \to \epsilon \implies A \to \epsilon
    • The resulting grammar GRG^R is strictly Right-Linear and generates L(GR)=(L(G))RL(G^R) = (L(G))^R.
  2. Step 2: Construct NFA MRM^R for GRG^R:
    • Apply Algorithm 1 to convert GRG^R into an equivalent NFA MRM^R.
  3. Step 3: Reverse the Automaton MRM^R to Obtain MM:
    • Invert the direction of all directed transition edges in MRM^R.
    • Swap the initial state and accepting states:
      • The old start state SS becomes an accepting state in MM.
      • The old accepting state qfq_f becomes the new initial state in MM.
    • Automaton MM recognizes ((L(G))R)R=L(G)((L(G))^R)^R = L(G).

Live Automaton: State-to-Variable Correspondence​

The following interactive automaton recognizes binary strings ending with 01 (L=(0+1)∗01L = (0 + 1)^* 01). Notice how each state (q0,q1,q2q_0, q_1, q_2) corresponds directly to a non-terminal variable in a Right-Linear Grammar:

🧮
Ends with '01'Suffix DFA

Accepts all binary strings ending with substring '01' (w = X01).

INPUT Σ{0, 1}
STATES|Q| = 3
Input String:
INPUT TAPE
⬇1
0
0
1
$
🗺️ State DiagramSTATE: q0
Startq0start / saw '1'q1saw '0'q2saw '01' (Accept)100101
📊 Transition Tableδ : Q × Σ → Q
State (Q)Input '0'Input '1'
▶q0
q1q0
q1
q1q2
((q2))
q1q0
⚡ STEP INTUITION:Step 0 of 4

Initial state q0. Ready to read input tape. Click 'Play' or 'Step ▶' to begin simulation.

⚡
PROCESSING TAPE: Current active state: q0 (4 symbols remaining).

⏱️ Derivation Trace vs. Automaton State Traversal​

To understand why the RLG-to-NFA equivalence holds mathematically, observe the parallel execution trace for string w=0101w = 0101 on grammar S→0A,A→1S∣1S \to 0 A, A \to 1 S \mid 1:

Step ttGrammar Derivation SequenceAutomaton Configuration (q,remaining input)(q, \text{remaining input})Applied Rule / Transition
00SS(qS,0101)(q_S, 0101)Initial configuration
110A0 A(qA,101)(q_A, 101)S→0A  ⟺  δ(qS,0)=qAS \to 0 A \iff \delta(q_S, 0) = q_A
2201S0 1 S(qS,01)(q_S, 01)A→1S  ⟺  δ(qA,1)=qSA \to 1 S \iff \delta(q_A, 1) = q_S
33010A0 1 0 A(qA,1)(q_A, 1)S→0A  ⟺  δ(qS,0)=qAS \to 0 A \iff \delta(q_S, 0) = q_A
4401010 1 0 1(qf,ϵ)(q_f, \epsilon)A→1  ⟺  δ(qA,1)=qfA \to 1 \iff \delta(q_A, 1) = q_f

Every single sentential expansion uA  ⟹  uaBu A \implies u a B directly mirrors the automaton reading symbol aa from state AA and entering state BB.


📊 Comparative Analysis & Trade-Off Matrix​

Feature / DimensionRight-Linear Grammar (RLG)Left-Linear Grammar (LLG)Mixed Linear GrammarFinite Automaton (FA)
Chomsky TypeType-3 (Regular)Type-3 (Regular)Type-2 (Context-Free)Type-3 Recognizer
Production FormA→aB∣a∣ϵA \to aB \mid a \mid \epsilonA→Ba∣a∣ϵA \to Ba \mid a \mid \epsilonA→uBv∣wA \to u B v \mid wδ(q,a)=p\delta(q, a) = p
String Synthesis FrontierExpands Left-to-RightExpands Right-to-LeftExpands both edges simultaneouslyScans Left-to-Right
Automaton EquivalenceDirect 1-to-1 mapping to NFARequires Reversal Theorem (GR→MR→MG^R \to M^R \to M)Equivalent to Pushdown Automaton (PDA)Isomorphic to RLG
Expressive PowerExactly Regular LanguagesExactly Regular LanguagesContext-Free (includes anbna^n b^n)Exactly Regular Languages
Auxiliary Memory NeededZeroZero1 Stack (LIFO)Zero

🏭 Real-World Production Context: Lexer & Scanner Generators​

In industrial software engineering tools such as Lex, Flex, and ANTLR, regular expressions and regular grammars form the backbone of the scanner pipeline:

Architecture Flow

From Lexical Rules to Runtime Scanner Execution

How compiler generators transform regular grammar specifications into high-speed table drivers

📜Input

Grammar / Regex Rules

Developer writes token definitions (e.g., ID -> [a-zA-Z][a-zA-Z0-9]*).

→
Syntax Tree
⚡AST to NFA

Thompson's Construction

Generates an equivalent non-deterministic finite automaton with epsilon transitions.

→
Subset Construction
🛡️Determinism

Powerset DFA

Eliminates non-determinism, producing a deterministic transition matrix.

→
Hopcroft's Algo
🚀Runtime

Minimal State Driver

Emits a hardcoded C/Rust 2D lookup array for O(1) per-character scanning.

Top-Down vs. Bottom-Up Parsers: The Echo of Linearity​

In industrial compiler engineering, the distinction between Right-Linear and Left-Linear grammars manifests directly in parsing algorithms:

Compiler Parsing Paradigms vs. Grammar Derivation Orientation

How Top-Down and Bottom-Up parsers leverage right-linear and left-linear recursive structures

Top-Down

Top-Down Parsers (LL(k) / Recursive Descent)

⬇️
Dominant Architecture / DomainRoot-to-Leaf Predictive Parsing
  • •Constructs derivations from start symbol S down to terminal leaves.
  • •Favors Right-Recursive / Right-Linear productions (A → aA).
  • •CRITICAL: Left-recursion (A → Aa) causes infinite loops in recursive descent!
  • •Requires left-factoring to eliminate non-deterministic branch conflicts.
"Directly mirrors forward right-linear derivation sequences."
Bottom-Up

Bottom-Up Parsers (LR(k) / LALR / Bison)

⬆️
Dominant Architecture / DomainLeaf-to-Root Shift-Reduce Parsing
  • •Constructs derivations by reducing terminal leaves back up to start symbol S.
  • •Naturally and efficiently handles Left-Recursive / Left-Linear rules (A → Aa).
  • •Conserves parser stack memory depth during large sequence parsing.
  • •Industry standard for high-performance C, C++, and Rust compiler grammars.
"Left-recursion minimizes shift-reduce stack allocation overhead."

When building high-throughput data parsers or protocol deserializers, matching your grammar's linearity to the underlying parsing engine avoids stack overflow crashes and minimizes memory footprints.


📐 Numerical Applications & Worked Examples​

Example 1: Right-Linear Grammar to NFA Construction​

Problem Statement:
Consider the Right-Linear Grammar G=(V,Σ,P,S)G = (V, \Sigma, P, S) where V={S,A}V = \{S, A\}, Σ={0,1}\Sigma = \{0, 1\}, and the production rules PP are:

S→0A∣1S∣ϵ,A→0S∣1AS \to 0 A \mid 1 S \mid \epsilon, \quad A \to 0 S \mid 1 A
  1. Construct the equivalent NFA M=(Q,Σ,δ,q0,F)M = (Q, \Sigma, \delta, q_0, F) using Algorithm 1.
  2. Formulate the state transition table.
  3. Trace the acceptance computation for input string w=010w = 010.

Step-by-Step Solution​

Step 1: Identifying States and Initial/Final Conditions​

  • Non-terminals: V={S,A}V = \{S, A\}.
  • Introduce dedicated final state qfq_f.
  • Full state set: Q={S,A,qf}Q = \{S, A, q_f\}.
  • Start state: q0=Sq_0 = S.
  • Accepting states (FF):
    • qfq_f is an accepting state by definition.
    • In addition, the grammar contains the production S→ϵS \to \epsilon. Therefore, state SS is also an accepting state: F={S,qf}F = \{S, q_f\}

Step 2: Mapping Transitions (δ)​

  1. From SS:
    • S→0A  ⟹  δ(S,0)={A}S \to 0 A \implies \delta(S, 0) = \{A\}
    • S→1S  ⟹  δ(S,1)={S}S \to 1 S \implies \delta(S, 1) = \{S\}
  2. From AA:
    • A→0S  ⟹  δ(A,0)={S}A \to 0 S \implies \delta(A, 0) = \{S\}
    • A→1A  ⟹  δ(A,1)={A}A \to 1 A \implies \delta(A, 1) = \{A\}

Notice that because neither variable has a terminating single-terminal production (like S→0S \to 0 or A→1A \to 1), transitions to qfq_f are unnecessary, and qfq_f remains unreachable. The automaton simplifies directly to a 2-state machine:

State Transition Table​

State q∈Qq \in QInput 00Input 11Is Accepting?
→∗S\to *S{A}\{A\}{S}\{S\}Yes (due to S→ϵS \to \epsilon)
AA{S}\{S\}{A}\{A\}No

Step 3: Acceptance Trace for w = 010​

  • Initial State: SS
  • Read symbol 0: δ(S,0)=A\delta(S, 0) = A
  • Read symbol 1: δ(A,1)=A\delta(A, 1) = A
  • Read symbol 0: δ(A,0)=S\delta(A, 0) = S
  • Termination: The input string is completely consumed, and the automaton rests in state SS. Since S∈FS \in F, the string w=010w = 010 is accepted.

Example 2: DFA to Right-Linear Grammar Conversion​

Problem Statement:
A Deterministic Finite Automaton MM over Σ={a,b}\Sigma = \{a, b\} accepts all strings that contain the substring "ab". Its transition table is:

StateInput aaInput bb
→q0\to q_0q1q_1q0q_0
q1q_1q1q_1q2q_2
∗q2*q_2q2q_2q2q_2

Derive the complete set of Right-Linear Grammar productions generating L(M)L(M), and trace the formal derivation of the sentence w=aabw = aab.


Step-by-Step Solution​

Step 1: Assign Non-Terminals​

Assign a non-terminal variable to each state: q0→A0(Start variable),q1→A1,q2→A2q_0 \to A_0 \quad (\text{Start variable}), \quad q_1 \to A_1, \quad q_2 \to A_2


Step 2: Formulate Production Rules​

Apply Algorithm 2:

  1. Transitions from q0q_0:

    • δ(q0,a)=q1  ⟹  A0→aA1\delta(q_0, a) = q_1 \implies A_0 \to a A_1
    • δ(q0,b)=q0  ⟹  A0→bA0\delta(q_0, b) = q_0 \implies A_0 \to b A_0
  2. Transitions from q1q_1:

    • δ(q1,a)=q1  ⟹  A1→aA1\delta(q_1, a) = q_1 \implies A_1 \to a A_1
    • δ(q1,b)=q2\delta(q_1, b) = q_2: Since q2∈Fq_2 \in F is an accepting state, add both the transition rule and the terminating rule: A1→bA2∣bA_1 \to b A_2 \mid b
  3. Transitions from q2q_2 (Accepting State):

    • Since q2q_2 is an accepting state, add: A2→ϵA_2 \to \epsilon
    • For the self-loops on q2q_2: δ(q2,a)=q2  ⟹  A2→aA2∣a\delta(q_2, a) = q_2 \implies A_2 \to a A_2 \mid a δ(q2,b)=q2  ⟹  A2→bA2∣b\delta(q_2, b) = q_2 \implies A_2 \to b A_2 \mid b

Complete Grammar G​

A0→aA1∣bA0A_0 \to a A_1 \mid b A_0 A1→aA1∣bA2∣bA_1 \to a A_1 \mid b A_2 \mid b A2→aA2∣bA2∣a∣b∣ϵA_2 \to a A_2 \mid b A_2 \mid a \mid b \mid \epsilon

Step 3: Derivation Trace of w = aab​

Begin at start symbol A0A_0:

  1. Apply A0→aA1A_0 \to a A_1: A0  ⟹  aA1A_0 \implies a A_1
  2. Apply A1→aA1A_1 \to a A_1: aA1  ⟹  aaA1a A_1 \implies a a A_1
  3. Terminate using A1→bA_1 \to b: aaA1  ⟹  aaba a A_1 \implies a a b

The string aabaab is successfully generated in 3 derivation steps.


Example 3: Left-Linear Grammar to Finite Automaton via Reversal​

Problem Statement:
Consider the Left-Linear Grammar G=(V,Σ,P,S)G = (V, \Sigma, P, S) where: V={S,A},Σ={0,1}V = \{S, A\}, \quad \Sigma = \{0, 1\} With productions: S→S0∣A1,A→A1∣0S \to S 0 \mid A 1, \quad A \to A 1 \mid 0

Convert GG into an equivalent Finite Automaton using the Language Reversal Method.


Step-by-Step Solution​

Step 1: Invert RHS to Obtain Right-Linear Grammar G^R​

Reverse the right-hand side of all production rules:

  • S→S0  ⟹  S→0SS \to S 0 \implies S \to 0 S
  • S→A1  ⟹  S→1AS \to A 1 \implies S \to 1 A
  • A→A1  ⟹  A→1AA \to A 1 \implies A \to 1 A
  • A→0  ⟹  A→0A \to 0 \implies A \to 0

The reversed grammar GRG^R is: S→0S∣1A,A→1A∣0S \to 0 S \mid 1 A, \quad A \to 1 A \mid 0


Step 2: Build NFA M^R for G^R​

Apply Algorithm 1 to GRG^R:

  • States: Q={S,A,qf}Q = \{S, A, q_f\}
  • Start state: SS
  • Accepting states: F={qf}F = \{q_f\}
  • Transitions:
    • S→0S  ⟹  δ(S,0)={S}S \to 0 S \implies \delta(S, 0) = \{S\}
    • S→1A  ⟹  δ(S,1)={A}S \to 1 A \implies \delta(S, 1) = \{A\}
    • A→1A  ⟹  δ(A,1)={A}A \to 1 A \implies \delta(A, 1) = \{A\}
    • A→0  ⟹  δ(A,0)={qf}A \to 0 \implies \delta(A, 0) = \{q_f\}

Step 3: Reverse the Automaton M^R to Obtain M​

To reverse MRM^R:

  1. Swap Initial and Final States:
    • Old final state qfq_f becomes the new start state: q0′=qfq_0' = q_f.
    • Old start state SS becomes the new accepting state: F′={S}F' = \{S\}.
  2. Reverse All Transition Edges:
    • Edge A→0qfA \xrightarrow{0} q_f in MRM^R becomes qf→0Aq_f \xrightarrow{0} A in MM.
    • Edge S→1AS \xrightarrow{1} A in MRM^R becomes A→1SA \xrightarrow{1} S in MM.
    • Self-loop S→0SS \xrightarrow{0} S remains S→0SS \xrightarrow{0} S.
    • Self-loop A→1AA \xrightarrow{1} A remains A→1AA \xrightarrow{1} A.

Final Automaton M State Table​

StateInput 00Input 11Status
→qf\to q_f{A}\{A\}∅\emptysetInitial State
AA∅\emptyset{A,S}\{A, S\}Intermediate
∗S*S{S}\{S\}∅\emptysetAccepting State

The automaton MM accepts strings derived by starting at qfq_f, reading 0 into state AA, looping on 1, transitioning to SS on 1, and looping on 0. This matches the language of GG: L(G)=01+0∗L(G) = 0 1^+ 0^*.


Example 4: Identifying Non-Regular Linear Grammars vs. Regular Grammars​

Problem Statement:
Analyze the following three grammars over Σ={a,b}\Sigma = \{a, b\} and determine whether each is a Type-3 Regular Grammar or a non-regular Linear Grammar. Explicitly state the language generated:

  1. Grammar GAG_A: S→aA,A→bS∣bS \to a A, \quad A \to b S \mid b
  2. Grammar GBG_B: S→Ab,A→Sa∣aS \to A b, \quad A \to S a \mid a
  3. Grammar GCG_C: S→aSb∣abS \to a S b \mid a b

Step-by-Step Solution​

Analyzing Grammar G_A​

  • Rules: S→aAS \to a A (right-linear), A→bSA \to b S (right-linear), A→bA \to b (terminal).
  • Every single non-terminal variable on the RHS appears at the rightmost position.
  • Classification: Type-3 Right-Linear Regular Grammar.
  • Language generated: S  ⟹  aA  ⟹  abS  ⟹  (ab)aA  ⟹  (ab)kb or (ab)kS \implies a A \implies a b S \implies (ab) a A \implies (ab)^k b \text{ or } (ab)^k L(GA)=(ab)+L(G_A) = (ab)^+ (all strings consisting of one or more repetitions of "ab").

Analyzing Grammar G_B​

  • Rules: S→AbS \to A b (left-linear), A→SaA \to S a (left-linear), A→aA \to a (terminal).
  • Every single non-terminal variable on the RHS appears at the leftmost position.
  • Classification: Type-3 Left-Linear Regular Grammar.
  • Language generated: S  ⟹  Ab  ⟹  (Sa)b  ⟹  Sab  ⟹  (Ab)ab  ⟹  abab…S \implies A b \implies (S a) b \implies S a b \implies (A b) a b \implies a b a b \dots L(GB)=(ab)+L(G_B) = (ab)^+ (identical language to GAG_A, synthesized from right to left).

Analyzing Grammar G_C​

  • Rules: S→aSbS \to a S b (variable SS is surrounded by terminals aa and bb), S→abS \to a b.
  • The variable SS is neither strictly at the right edge nor strictly at the left edge.
  • Classification: Linear Grammar (Type-2 Context-Free). It is NOT a Regular Grammar.
  • Language generated: S  ⟹  aSb  ⟹  a(aSb)b=a2Sb2  ⟹  anbnS \implies a S b \implies a (a S b) b = a^2 S b^2 \implies a^n b^n L(GC)={anbn∣n≥1}L(G_C) = \{a^n b^n \mid n \ge 1\}. This language requires counting unbounded numbers of aa's and matching them with bb's, which is provably impossible for any finite automaton.

🎯 Exam & Interview Pitfall Check​

Core Conceptual Questions
  • Why does reversing an NFA construct the correct machine for a Left-Linear Grammar?
    Because an LLG synthesizes strings from right to left (S  ⟹  Aan  ⟹  Ban−1an…S \implies A a_n \implies B a_{n-1} a_n \dots), the derivation actually generates the reversed string wRw^R when viewed as a forward process. Reversing the automaton inverts all transitions and swaps initial and final states, precisely canceling the reversal (((w)R)R=w((w)^R)^R = w) and yielding the original language.
  • Can a language be regular if its grammar is not regular?
    Yes! The grammar S→aSb∣ab∣a∣bS \to a S b \mid a b \mid a \mid b is a context-free grammar, but if a grammar generates a language that can also be recognized by some DFA, the language itself is regular. A regular language may be generated by many different grammars, some of which belong to higher Chomsky tiers.
  • What is the maximum number of states in an NFA created from an RLG with ∣V∣|V| variables?
    The NFA requires at most ∣V∣+1|V| + 1 states (one state per variable, plus one dedicated accepting state qfq_f). If the grammar already has an ϵ\epsilon-production on an existing state, no additional state is needed, keeping the state count at exactly ∣V∣|V|.
Common Interview Traps
  • Trap: Assuming Any Grammar with at Most One Variable on the RHS is Regular:
    This is the classic Linear Grammar trap. S→aSb∣ϵS \to a S b \mid \epsilon has at most one variable on the RHS, yet it generates {anbn∣n≥0}\{a^n b^n \mid n \ge 0\}, which is strictly context-free and non-regular. Regularity requires all variables to be consistently at the same edge across all productions!
  • Trap: Forgetting That Start State Can Be Final:
    When converting an RLG to an NFA, candidates often only mark the new dummy state qfq_f as accepting. If the grammar contains the rule S→ϵS \to \epsilon, the start state SS must also be designated as an accepting state; otherwise, the empty string will be incorrectly rejected.
  • Trap: Mixing Terminals and Multiple Variables:
    In a regular grammar, rules like A→aBCA \to a B C or A→BCA \to B C are strictly forbidden. There can be at most one non-terminal on the right-hand side.

💬

Discussion & Doubts