The regular grammar for the language L = {w|na(w) and nb(w) are both even, w ∈…

2014

The regular grammar for the language L = {w|na(w) and nb(w) are both even, w ∈ {a, b}*} is given by : (Assume, p, q, r and s are states)

  1. A.

    p → aq | br | λ, q → bs | ap

    r → as | bp, s → ar | bq, p and s are initial and final states.

  2. B.

    p → aq | br, q → bs | ap

    r → as | bp, s → ar | bq, p and s are initial and final states.

  3. C.

    p → aq | br | λ, q → bs | ap

    r → as | bp, s → ar | bq p is both initial and final states.

  4. D.

    p → aq | br, q → bs | ap

    r → as | bp, s → ar | bq p is both initial and final states.

Attempted by 66 students.

Show answer & explanation

Correct answer: C

Solution overview: Construct a grammar that tracks the parity (even/odd) of the number of a's and b's. Use four states to represent the four parity combinations.

State mapping:

  • p: even number of a's and even number of b's (start and accepting state)

  • q: odd number of a's and even number of b's

  • r: even number of a's and odd number of b's

  • s: odd number of a's and odd number of b's

Productions (regular grammar):

  • p → a q | b r | λ

  • q → b s | a p

  • r → a s | b p

  • s → a r | b q

Why this works:

  • Reading an 'a' moves between states that differ in the a-parity (p↔q or r↔s), and reading a 'b' moves between states that differ in the b-parity (p↔r or q↔s). Each production flips exactly the affected parity.

  • The λ production at p ensures the empty string (zero a's and zero b's) is accepted, as required.

  • Only p is accepting because only p represents even counts for both letters. Accepting any other state would allow strings with odd counts to be accepted.

Common mistakes to avoid:

  • Omitting λ from the start state will reject the empty string, which should be accepted.

  • Marking an odd-parity state as accepting will incorrectly accept strings where one or both letters occur an odd number of times.

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Mppsc Assistant Professor