Building NFA from Regular Grammars- Examples

 Example:

Grammar

  • A0aA1A_0 \to aA_1

  • A1bA1  a  bA0A_1 \to bA_1 \ \mid\ a \ \mid\ bA_0

We’ll use the standard construction:

  • One NFA state per nonterminal (A0,A1)(A_0, A_1) plus a fresh final state FF.

  • For each rule XaYX \to aY: add a transition XaYX \xrightarrow{a} Y.

  • For each rule XaX \to a (terminal only): add XaFX \xrightarrow{a} F.

  • Start state is the start nonterminal (A0A_0).

  • Accepting states: {F}\{F\} (no ε\varepsilon-rules here, so neither A0A_0 nor A1A_1 is accepting).


NFA N=(Q,Σ,δ,q0,F)N = (Q,\Sigma,\delta,q_0,F)

  • States Q={A0,A1,F}Q = \{A_0, A_1, F\}

  • Alphabet Σ={a,b}\Sigma = \{a,b\}


    Start

  • q0=A0q_0 = A_0

  • Accepting F={F}

Transitions δ\delta:

  • From A0A_0:

    • on a{A1}\{A_1\} (from A0aA1A_0 \to aA_1)

    • on b\varnothing

  • From A1A_1:

    • on a{F}\{F\}(from A1aA_1 \to a)

    • on b{A1,A0}\{A_1, A_0\} (from A1bA1A_1 \to bA_1 and A1bA0A_1 \to bA_0)

  • From FF:

    • on a\varnothing, on b\varnothing (no outgoing rules)


Transition table

Stateab
A0A_0{A1}\{A_1\}
                 ∅  \varnothing
A1A_1{F}\{F\}         {A1,A0}\{A_1, A_0\}
FF
\varnothing
             ∅\varnothing


Example

let’s build the NFA for the right-linear grammar:

  • S0S1A1S \to 0S \mid 1A \mid 1

  • A0A1A01A \to 0A \mid 1A \mid 0 \mid 1



Example:

Grammar

  • S0A    1B    0    1S \to 0A \;|\; 1B \;|\; 0 \;|\; 1

  • A0S    1B    1A \to 0S \;|\; 1B \;|\; 1

  • B0A    1SB \to 0A \;|\; 1S



Example:

S → 0A | 1A
A → 0A | 1A | +B | -B
B → 0B | 1B | 0 | 1



Comments

Popular posts from this blog

Theory Of Computation PCCST302 KTU Semester 3 BTech 2024 Scheme

Non deterministic Finite Automata NFA

Example DFAs University Questions