Top Banner Ad

BCS402 — Unit 3: Regular and Non-Regular Grammars

Table of Contents (16 sections)

BCS402 · Theory of Automata and Formal Languages · Unit 3

Regular and Non-Regular Grammars

Unit Scope & Exam Weightage: This unit covers CFG definition, derivations and languages, derivation trees and ambiguity, right-linear and left-linear regular grammars, FA→CFG and regular grammar→FA conversions, simplification of CFG (ε-removal, unit productions, useless symbols), Chomsky Normal Form, Greibach Normal Form, the Chomsky hierarchy, and programming problems on CFG properties. This unit produces many 7-mark transformation questions plus 2-mark definition/derivation questions.


Importance-Wise Roadmap (BCS402 Unit 3)

Priority Exact Syllabus Topic PYQ Evidence Marks Pattern Tag
1 Simplification of CFG (ε-removal, useless symbols) 2025-26, 2024-25 7m × 2 + 2m Step marks
2 Chomsky Normal Form conversion 2023-24, 2024-25 7m × 2 Algorithm
3 CFG design for given language 2024-25, 2025-26, 2023-24 7m × 2 + 7m Derivation check
4 Greibach Normal Form 2025-26 7m Left recursion
5 Regular grammar ↔ FA, left-linear conversion 2023-24, 2024-25 7m × 2 Construction
6 Ambiguity, derivations, language generated 2023-24, 2024-25, 2025-26 2m + predicted 7m Tree diagrams
7 Chomsky hierarchy Not directly asked Predicted 2–7m Theory

Grammar-specific examiner guidance: Show every intermediate grammar after each simplification step — examiners award marks per step even if the final grammar is slightly off. For CFG design, finish by deriving one member and showing one non-member cannot be derived.


1. CFG Basics, Derivations and Ambiguity

Context-Free Grammar (CFG): a 4-tuple G=(V,Σ,P,S)G=(V,\Sigma,P,S) where VV is a finite set of non-terminals, Σ\Sigma the terminal alphabet, PP a finite set of productions A→αA\rightarrow\alpha with A∈V,α∈(V∪Σ)∗A\in V,\alpha\in(V\cup\Sigma)^*, and S∈VS\in V the start symbol.

Derivation: replacing one non-terminal by the RHS of a production. A leftmost derivation always replaces the leftmost non-terminal; a rightmost derivation replaces the rightmost one. A parse (derivation) tree graphically records a derivation; its leaves (left to right) form the generated string.

PYQ (2023-24, 2 marks): "Let G be S→0B | 1A; A→0 | 0S | 1AA; B→1 | 1S | 0BB. Determine the leftmost derivation for 00110101."

S => 0B                 (S -> 0B)
  => 00BB               (leftmost B -> 0BB)
  => 001B               (leftmost B -> 1)
  => 0011S              (B -> 1S)
  => 00110B             (S -> 0B)
  => 001101BB           (B -> 0BB)
  => 0011010B           (leftmost B -> 0)
  => 00110101           (B -> 1)   ✔ generated

At each step only the leftmost non-terminal was expanded — the defining rule of a leftmost derivation.

PYQ (2023-24, 2 marks): "Compute the language generated by G=({S},{a,b},P,S), P: S→SS | ab | ba | ε." L(G)L(G) is the set of all strings over {a,b}\{a,b\} that are a concatenation of any positive number of blocks ab or ba, together with ε. Every generated string thus has even length and consists of alternating symbols, so L(G)={(ab)n,(ba)n and alternating concatenations∣n≥0}L(G)=\{(ab)^n,(ba)^n \text{ and alternating concatenations} \mid n\ge0\} — equivalently all ε and all alternating strings of even length, e.g., ε, ab, ba, abab, abba, baab, baba, …

PYQ (2025-26, 2 marks): "What is the ambiguity in Context-Free Grammars (CFGs)?" A CFG is ambiguous if some string in L(G)L(G) has two or more distinct leftmost derivations (equivalently, two distinct parse trees). Example: E→E+E∣E∗E∣idE\rightarrow E+E\mid E*E\mid id; the string id+id*id has two parse trees, giving different groupings.

Predicted (bank PRED, 2024-25, 7 marks): "Prove using derivation trees whether the grammar S→aSb | ε is ambiguous." This grammar is unambiguous: for anbna^nb^n the parse tree is forced — the root must use S→aSbS\rightarrow aSb exactly nn times and then S→εS\rightarrow\varepsilon. There is no alternative production producing an a, hence exactly one leftmost derivation per string.

      S
    / | \
   a  S  b
     /|\
    a S b
      |
      e          (tree for aabb)

1.1 CFG Design Worked PYQs

PYQ (2023-24, 7 marks): "Design a CFG for (i) L={0^m1^n | m≠n, m,n≥1}; (ii) L={a^p b^q c^r | p+q=r, p,q≥1}."

(i) Union of two cases: more zeros than ones, or more ones than zeros.

S  -> A | B
A  -> 0A1 | 0C          (m > n)
C  -> 0C | 01           (at least one extra 0, then >=1 one)
B  -> 0B1 | 0D          (n > m)
D  -> 11D | 11          (at least two extra 1s)

Trace 0011 (m=2,n=2) ✘; 001 (m=2,n=1): S⇒A⇒0C⇒001 ✔; 011 (m=1,n=2): S⇒B⇒0D⇒011 ✔.

(ii) Each added a must add one c; the b-block supplies equally many cs.

S -> aSc | T
T -> bTc | bc

Trace abcc (p=1,q=1,r=2): S⇒aSc⇒a(bc)c = abcc ✔. Trace abbccc: S⇒aSc⇒a(bTc)c⇒a(bbc)c c = abbccc ✔. Non-member abc cannot be derived because r would have to be 2.

Predicted (bank PRED, 2024-25/2025-26, 7 marks): "What is ambiguity in a CFG? Construct a CFG for L={a^n b^n | n≥0}." Grammar: S→aSb∣εS\rightarrow aSb\mid\varepsilon (unambiguous, as shown above). Ambiguity definition as above. If the question wants an ambiguous grammar for the same language, S→aSb∣aS′bS\rightarrow aSb\mid aS'b with S′→aS′b∣εS'\rightarrow aS'b\mid\varepsilon layered variants can create multiple trees; the standard answer pairs the unambiguous grammar with the definition and contrasts it with the ambiguous arithmetic-expression grammar.

1.2 Chomsky Hierarchy (syllabus topic)

Type Grammar restriction Language class Automaton
Type 0 No restrictions Recursively enumerable Turing machine
Type 1 Context-sensitive: αAβ→αγβ\alpha A\beta\rightarrow\alpha\gamma\beta Context-sensitive Linear-bounded automaton
Type 2 Context-free: A→γA\rightarrow\gamma Context-free Pushdown automaton
Type 3 Regular: A→aBA\rightarrow aB or A→aA\rightarrow a (right-linear) Regular Finite automaton

2. Regular Grammars and Finite Automata

2.1 Right-Linear vs. Left-Linear

Form Production shape Read direction Example
Right-linear A→aBA\rightarrow aB or A→aA\rightarrow a (or ε) left-to-right S→0A∣1BS\rightarrow0A\mid1B, A→0A∣0A\rightarrow0A\mid0
Left-linear A→BaA\rightarrow Ba or A→aA\rightarrow a right-to-left scan, still same language S→A0∣B1S\rightarrow A0\mid B1

PYQ (2023-24, 7 marks): "Write an equivalent left linear grammar from the given right linear grammar. S→0A | 1B; A→0C | 1A | 0; B→1B | 1A | 1; C→0 | 0A."

Method: construct the FA from the right-linear grammar, reverse every transition, swap start/final, then read off the left-linear grammar.

  1. FA states: SS (start), A,B,CA,B,C (A,B,C are final because of productions ending in a terminal).
  2. Transitions: S→0AS\xrightarrow0A, S→1BS\xrightarrow1B; A→0CA\xrightarrow0C, A→1AA\xrightarrow1A, A→0A\xrightarrow0(final); B→1BB\xrightarrow1B, B→1AB\xrightarrow1A, B→1B\xrightarrow1(final); C→0C\xrightarrow0(final), C→0AC\xrightarrow0A.
  3. Reverse edges and read grammar from old finals to old start:
S' -> 0S                    (old final A --0--> S reversed)
    | 1S                    (old final B --1--> S)
A'  -> 0S' | 1A' | 0        (from reversed A-targets)
B'  -> 1B' | 1A' | 1
C'  -> 0   | 0A'

Equivalently, a direct conversion replaces each right-linear chain S⇒0A⇒00C⇒0S\Rightarrow0A\Rightarrow00C\Rightarrow0 by left-linear derivations S′⇒C′0⇒A′00⇒000S'\Rightarrow C'0\Rightarrow A'00\Rightarrow000 — the same strings in reverse construction order.

PYQ (2024-25, 7 marks): "Convert the following regular grammar to a Finite Automaton: S→aA | bB; A→aS | a; B→bS | b."

State a b Final?
→ SS AA BB no
AA SS – yes (production A→aA\rightarrow a)
BB – SS yes (production B→bB\rightarrow b)

A state is final if the grammar has a production A→aA\rightarrow a (termination after that symbol). This FA accepts strings with alternating runs of a and b ending after a run: a, b, aa, bb, aab, bba, etc.

Syllabus topic: FA → CFG. For each FA state qiq_i create a non-terminal QiQ_i. For each transition δ(qi,a)=qj\delta(q_i,a)=q_j add Qi→aQjQ_i\rightarrow aQ_j; for each final state qfq_f add Qf→εQ_f\rightarrow\varepsilon. Start non-terminal = Qq0Q_{q_0}. Example: FA with q0→0q1q_0\xrightarrow0q_1, q0→1q0q_0\xrightarrow1q_0, q1q_1 final gives Q0→1Q0∣0Q1Q_0\rightarrow1Q_0\mid0Q_1, Q1→εQ_1\rightarrow\varepsilon.


3. Simplification of CFG

3.1 Removing ε-Productions

PYQ (2025-26, 7 marks): "Remove ε-productions from S→AB | BC; A→aA | ε; B→bB | ε; C→c."

  1. Nullable non-terminals: AA (from A→εA\rightarrow\varepsilon), BB (from B→εB\rightarrow\varepsilon), SS (from S→ABS\rightarrow AB and S→BCS\rightarrow BC, both nullable). CC is not nullable.
  2. Expand each production by deleting nullable non-terminals in all combinations:
    • A→aAA\rightarrow aA gives A→aA∣aA\rightarrow aA\mid a; drop A→εA\rightarrow\varepsilon.
    • B→bBB\rightarrow bB gives B→bB∣bB\rightarrow bB\mid b; drop B→εB\rightarrow\varepsilon.
    • S→ABS\rightarrow AB gives S→AB∣B∣A∣εS\rightarrow AB\mid B\mid A\mid\varepsilon.
    • S→BCS\rightarrow BC gives S→BC∣CS\rightarrow BC\mid C.
  3. Since ε was derivable from S, if ε is in the language keep S→εS\rightarrow\varepsilon; otherwise drop it. Result:
S -> AB | B | A | C | ε
A -> aA | a
B -> bB | b
C -> c

3.2 Full Simplification: Useless Symbols and Unit Productions

PYQ (2024-25, 7 marks): "Simplify the following CFG: S→AbaC; A→BC; B→b | ε; C→D | ε; D→d."

  1. Nullable: B,C,DB,C,D? D→dD\rightarrow d only → D not nullable. C→εC\rightarrow\varepsilon → C nullable. B→εB\rightarrow\varepsilon → B nullable. A→BCA\rightarrow BC → A nullable. S is not nullable (has terminal a).
  2. Remove ε-productions:
    • B→bB\rightarrow b (drop ε).
    • C→D∣dC\rightarrow D\mid d (drop ε).
    • A→BCA\rightarrow BC expands to A→BC∣B∣CA\rightarrow BC\mid B\mid C (with C→D∣dC\rightarrow D\mid d, so A→BD∣Bd∣B∣D∣dA\rightarrow BD\mid Bd\mid B\mid D\mid d).
    • S→AbaCS\rightarrow AbaC expands over nullable A and C to S→AbaC∣baC∣Aba∣baS\rightarrow AbaC\mid baC\mid Aba\mid ba, then replace CC by D∣dD\mid d where C appeared: S→AbaD∣Abad∣baD∣bad∣Aba∣baS\rightarrow AbaD\mid Abad\mid baD\mid bad\mid Aba\mid ba.
  3. Remove unit productions (A→BA\rightarrow B etc.) by substituting B's and D's RHS:
S -> AbaD | Abad | baD | bad | Aba | ba
A -> bD | bd | b | d          (D -> d, so A -> D becomes d)
B -> b
C -> D | d
D -> d
  1. Remove non-generating/unreachable symbols: all remaining non-terminals derive terminal strings and are reachable from S (C becomes unreachable after the substitution — drop C). Final simplified grammar:
S -> AbaD | Abad | baD | bad | Aba | ba
A -> bD | bd | b | d
B -> b
D -> d

PYQ (2025-26, 2 marks): "Explain the difference between non-generating and unreachable symbols." A non-generating symbol cannot derive any string of terminals (e.g., A→AbA\rightarrow Ab alone). An unreachable symbol cannot be reached from the start symbol by any derivation (e.g., X→xX\rightarrow x with no production using X). Eliminate non-generating symbols first, then unreachable ones — deleting in the opposite order can leave new useless symbols.


4. Normal Forms

4.1 Chomsky Normal Form (CNF)

CNF: every production is of the form A→BCA\rightarrow BC (two non-terminals) or A→aA\rightarrow a (one terminal); the start symbol may additionally have S→εS\rightarrow\varepsilon.

Algorithm: (1) add new start S0→SS_0\rightarrow S; (2) remove ε-productions; (3) remove unit productions; (4) replace terminals inside mixed/long RHS by new non-terminals Xa→aX_a\rightarrow a; (5) break RHS longer than 2 non-terminals into chained pairs.

PYQ (2024-25, 7 marks): "Convert S→aSB | ε, B→b into CNF."

  1. New start: S0→S∣εS_0\rightarrow S\mid\varepsilon; nullable SS.
  2. Remove ε in S→aSBS\rightarrow aSB: S→aSB∣aBS\rightarrow aSB\mid aB.
  3. Replace terminal a: introduce Xa→aX_a\rightarrow a.
    • S→XaSBS\rightarrow X_aSB (3 non-terminals) and S→XaBS\rightarrow X_aB (valid).
  4. Break the length-3 RHS: introduce A1→SBA_1\rightarrow SB.
S0 -> S | ε
S  -> X_a A1 | X_a B
A1 -> S B
B  -> X_b
X_a -> a
X_b -> b

PYQ (2023-24, 7 marks): "Reduce G=({S,A,B},{a,b},P,S) with P: S→bA | aB; A→bAA | aS | a; B→aBB | bS | b to CNF." No ε or unit productions exist. Introduce Xa→aX_a\rightarrow a, Xb→bX_b\rightarrow b, then:

S  -> X_b A | X_a B
A  -> X_b A1 | X_a S | a        with A1 -> A A
B  -> X_a B1 | X_b S | b        with B1 -> B B
X_a -> a
X_b -> b
A1 -> A A
B1 -> B B

Every RHS is now one terminal or two non-terminals — CNF achieved. A CNF parse tree is binary, which is why CYK parsing works in O(n3∣G∣)O(n^3|G|) time.

4.2 Greibach Normal Form (GNF)

GNF: every production has the form A→aαA\rightarrow a\alpha where a∈Σa\in\Sigma and α∈V∗\alpha\in V^* (a terminal followed by zero or more non-terminals). A GNF grammar has no ε-productions (except possibly for the start of the empty language) and no left recursion.

Conversion steps: (1) order non-terminals A1,…,AnA_1,\dots,A_n; (2) for i=1..ni=1..n, substitute into AiA_i any AjA_j with j<ij<i appearing as the first symbol; (3) eliminate immediate left recursion using A→Aα∣β⇒A→β∣βA′A\rightarrow A\alpha\mid\beta \Rightarrow A\rightarrow\beta\mid\beta A', A′→α∣αA′A'\rightarrow\alpha\mid\alpha A'; (4) repeat substitution until every RHS begins with a terminal; (5) introduce helper non-terminals for long tails.

PYQ (2025-26, 7 marks): "Define GNF. Convert S→CD, C→DS | d, D→SC | c into GNF."

Working:

  1. Fix order C1=CC_1=C, C2=DC_2=D, C3=SC_3=S. Initially C→DS∣dC\rightarrow DS\mid d and D→SC∣cD\rightarrow SC\mid c already begin with later (or non-earlier) non-terminals/terminals.
  2. S→CDS\rightarrow CD begins with earlier CC. Substitute CC: S→DSD∣dDS\rightarrow DSD\mid dD. Substitute DD: S→SCSD∣cCSD∣dSC∣dcS\rightarrow SCSD\mid cCSD\mid dSC\mid dc. The first alternative is left-recursive.
  3. Eliminate left recursion with β={cCSD,  dc,  dSC}\beta=\{cCSD,\;dc,\;dSC\} and α=CSD\alpha=CSD:
S  -> cCSD | dc | dSC
S  -> cCSD S1 | dc S1 | dSC S1
S1 -> CSD | CSD S1
  1. Introduce terminal-prefixed helper non-terminals (e.g., replace a tail beginning with C by a new symbol whose productions begin with c or d after the substitutions C→DS∣dC\rightarrow DS\mid d, D→SC∣cD\rightarrow SC\mid c are repeated to the first terminal). Writing Xc→cX_c\rightarrow c, Xd→dX_d\rightarrow d and helpers U,VU,V for the tails, a GNF-style terminal-first set is:
C  -> X_d | X_c S
D  -> X_c | X_c U
S  -> X_d X_c | X_c V | X_d W
S1 -> V | V S1
U  -> S C | d C U
V  -> C S D | d S D
W  -> S C | c S C
X_c -> c
X_d -> d
  1. Verification by derivation: S⇒XdXc⇒dcS\Rightarrow X_dX_c\Rightarrow dc ✔; longer strings such as cdcc are derived by expanding V,UV,U recursively. Each production now begins with a terminal, satisfying GNF.

Key distinction to state in the exam: CNF's first symbol may be a non-terminal (binary tree); GNF's first symbol must be a terminal, which makes predictive top-down parsing possible and guarantees that each derivation step consumes one input symbol.


5. Exam-Ready Question Set and Practice

Question → answer index

PYQ Where solved
Leftmost derivation of 00110101 (2023-24, 2m) §1 step-by-step
Language of `S→SS ab
Ambiguity definition (2025-26, 2m) §1
ε-removal `S→AB BC` (2025-26, 7m)
Simplify S→AbaC… (2024-25, 7m) §3.2
Non-generating vs unreachable (2025-26, 2m) §3.2
CNF of `S→aSB ε` (2024-25, 7m)
CNF of the S/A/B grammar (2023-24, 7m) §4.1
GNF of `S→CD,C→DS d,D→SC
Regular grammar → FA (2024-25, 7m) §2.1
Right-linear → left-linear (2023-24, 7m) §2.1
CFGs for m≠n and p+q=r (2023-24, 7m) §1.1

Practice questions (label: practice, not real PYQs)

  1. Practice: Remove unit productions from S→A∣bS\rightarrow A\mid b, A→B∣cA\rightarrow B\mid c, B→S∣dB\rightarrow S\mid d.
  2. Practice: Convert S→aAa∣bBbS\rightarrow aAa\mid bBb, A→aA\rightarrow a, B→bB\rightarrow b to CNF.
  3. Practice: Show two parse trees for id+id*id using E→E+E∣E∗E∣idE\rightarrow E+E\mid E*E\mid id.
  4. Practice: Build a CFG for L={aibj∣i≠j}L=\{a^ib^j\mid i\ne j\} and derive aaab.
  5. Practice: Convert the FA for even number of 0s into a right-linear grammar.

6. Last-Night Survival Checklist (Unit 3 only)

  1. Derivations: leftmost = expand leftmost non-terminal; write each sentential form on its own line.
  2. Simplification order: nullable set → ε-removal → unit productions → non-generating → unreachable.
  3. CNF shapes: A→BCA\rightarrow BC / A→aA\rightarrow a; add S0S_0 and XaX_a helpers; break triples with a new symbol.
  4. GNF shape: A→aαA\rightarrow a\alpha; remove left recursion first, then substitute earlier non-terminals.
  5. Regular grammar conversion: right-linear ⇒ FA (state is final if A→aA\rightarrow a exists); reverse automaton ⇒ left-linear.

Final grammar reminder: For ε-removal, list the nullable set explicitly before rewriting productions — that line carries marks. For CNF/GNF, state the target shape at the start of the answer so the examiner can see the transformation rule being applied correctly.