Skip to main content

NFA and DFA from grammar σ→bσ|aC

Short answer

NFA: states σ, C, F; start σ, final F. Transitions: σ –a→ C, σ –b→ σ; C –b→ C, C –b→ F. Subset construction gives DFA: {σ} –a→ {C}, –b→ {σ}; {C} –b→ {C,F}; {C,F} –b→ {C,F}. Final state {C,F}. Language b*ab⁺.

Consider the regular grammar G = (N, T, P, σ) where N = set of non-terminal symbols = {σ, C}, T = set of terminal symbols = {a, b}, P is the set of production rules = {σ→bσ, σ→aC, C→bC, C→b} and σ being the starting symbol. Construct a non-deterministic finite state automaton equivalent to the given regular grammar. Use this non-deterministic finite state automaton to generate equivalent deterministic finite state automaton.

Grammar: σ → bσ | aC, C → bC | b. Start symbol σ.

The language is L = b*ab⁺ (any number of b's, then one a, then one or more b's).

Part 1: NFA from the grammar

Rules for converting a right-linear grammar:

  • Each non-terminal becomes a state; the start symbol is the start state.
  • A rule X → aY gives a transition X –a→ Y.
  • A rule X → a (terminal only) gives a transition X –a→ F, where F is a new final state.

Here: σ → bσ gives σ –b→ σ; σ → aC gives σ –a→ C; C → bC gives C –b→ C; C → b gives C –b→ F.

NFA M = ({σ, C, F}, {a, b}, δ, σ, {F}):

Stateab
→ σ{C}{σ}
C∅{C, F}
*F∅∅

(→ marks the start state, * marks a final state. C on b goes to both C and F, so this is non-deterministic.)

        b           b
       ┌─┐         ┌─┐
       ↓ │         ↓ │
 ──→ ( σ ) ──a──→ ( C ) ──b──→ (( F ))

(σ loops on b; C loops on b and also goes to F on b.)

Part 2: DFA by subset construction

Start state = {σ}. Compute the move for each new subset until no new subset appears.

DFA stateab
→ {σ}{C}{σ}
{C}∅{C, F}
*{C, F}∅{C, F}
∅ (dead)∅∅
  • {σ} on a: δ(σ, a) = {C}; on b: {σ}.
  • {C} on a: ∅; on b: δ(C, b) = {C, F}.
  • {C, F} on a: δ(C, a) ∪ δ(F, a) = ∅; on b: δ(C, b) ∪ δ(F, b) = {C, F}.
  • Final states: those containing F, so {C, F}.

Rename A = {σ}, B = {C}, D = {C, F}, and E = dead state:

         b                 b
        ┌─┐               ┌─┐
        ↓ │               ↓ │
 ──→ ( A ) ──a──→ ( B ) ──b──→ (( D ))
                    │            │
                    a            a
                    ↓            ↓
                   ( E ) ←───────┘   (E loops on a, b)

The DFA M' = ({A, B, D, E}, {a, b}, δ', A, {D}) accepts exactly b*ab⁺. I checked this with a program on all strings up to length 8.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Discrete Structure chapter 2 questions →