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 where is a finite set of non-terminals, the terminal alphabet, a finite set of productions with , and 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 | ε." is the set of all strings over that are a concatenation of any positive number of blocks
aborba, together with ε. Every generated string thus has even length and consists of alternating symbols, so — 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 has two or more distinct leftmost derivations (equivalently, two distinct parse trees). Example: ; the string
id+id*idhas 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 the parse tree is forced — the root must use exactly times and then . There is no alternative production producing ana, 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: (unambiguous, as shown above). Ambiguity definition as above. If the question wants an ambiguous grammar for the same language, with 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: | Context-sensitive | Linear-bounded automaton |
| Type 2 | Context-free: | Context-free | Pushdown automaton |
| Type 3 | Regular: or (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 | or (or ε) | left-to-right | , |
| Left-linear | or | right-to-left scan, still same language |
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.
- FA states: (start), (A,B,C are final because of productions ending in a terminal).
- Transitions: , ; , , (final); , , (final); (final), .
- 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 by left-linear derivations — 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? |
|---|---|---|---|
| → | no | ||
| – | yes (production ) | ||
| – | yes (production ) |
A state is final if the grammar has a production (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 create a non-terminal . For each transition add ; for each final state add . Start non-terminal = . Example: FA with , , final gives , .
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."
- Nullable non-terminals: (from ), (from ), (from and , both nullable). is not nullable.
- Expand each production by deleting nullable non-terminals in all combinations:
- gives ; drop .
- gives ; drop .
- gives .
- gives .
- Since ε was derivable from S, if ε is in the language keep ; 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."
- Nullable: ? only → D not nullable. → C nullable. → B nullable. → A nullable. S is not nullable (has terminal
a). - Remove ε-productions:
- (drop ε).
- (drop ε).
- expands to (with , so ).
- expands over nullable A and C to , then replace by where C appeared: .
- Remove unit productions ( 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
- 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., alone). An unreachable symbol cannot be reached from the start symbol by any derivation (e.g., 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 (two non-terminals) or (one terminal); the start symbol may additionally have .
Algorithm: (1) add new start ; (2) remove ε-productions; (3) remove unit productions; (4) replace terminals inside mixed/long RHS by new non-terminals ; (5) break RHS longer than 2 non-terminals into chained pairs.
PYQ (2024-25, 7 marks): "Convert S→aSB | ε, B→b into CNF."
- New start: ; nullable .
- Remove ε in : .
- Replace terminal
a: introduce .- (3 non-terminals) and (valid).
- Break the length-3 RHS: introduce .
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 , , 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 time.
4.2 Greibach Normal Form (GNF)
GNF: every production has the form where and (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 ; (2) for , substitute into any with appearing as the first symbol; (3) eliminate immediate left recursion using , ; (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:
- Fix order , , . Initially and already begin with later (or non-earlier) non-terminals/terminals.
- begins with earlier . Substitute : . Substitute : . The first alternative is left-recursive.
- Eliminate left recursion with and :
S -> cCSD | dc | dSC
S -> cCSD S1 | dc S1 | dSC S1
S1 -> CSD | CSD S1
- Introduce terminal-prefixed helper non-terminals (e.g., replace a tail beginning with
Cby a new symbol whose productions begin withcordafter the substitutions , are repeated to the first terminal). Writing , and helpers 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
- Verification by derivation: ✔; longer strings such as
cdccare derived by expanding 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)
- Practice: Remove unit productions from , , .
- Practice: Convert , , to CNF.
- Practice: Show two parse trees for
id+id*idusing . - Practice: Build a CFG for and derive
aaab. - Practice: Convert the FA for even number of 0s into a right-linear grammar.
6. Last-Night Survival Checklist (Unit 3 only)
- Derivations: leftmost = expand leftmost non-terminal; write each sentential form on its own line.
- Simplification order: nullable set → ε-removal → unit productions → non-generating → unreachable.
- CNF shapes: / ; add and helpers; break triples with a new symbol.
- GNF shape: ; remove left recursion first, then substitute earlier non-terminals.
- Regular grammar conversion: right-linear ⇒ FA (state is final if 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.