Recall warm-up
- Arden: solution of R = Q + RP when ε ∉ L(P)?
Stuck? Peek at 2.2.2 FA → regex: state elimination and Arden’s theorem
- Prep: new start S (ε to old start), new accept E (ε from old accepting). Merge parallel arrows with +.
- Eliminate k: R(p,q) ← R₄ + R₁R₂*R₃ (no loop ⇒ R₂* = ε).
- Arden: R = Q + RP, ε ∉ P ⇒ R = QP*. Mirror: R = Q + PR ⇒ R = P*Q.
- Incoming equations: q = Σ p·a (+ ε for start); answer = sum over accepting states.
- Is (a+b)* equal to a*(ba*)*?
Stuck? Peek at 2.1.2 Writing regexes, identities and equivalence
- ∅ + r = r; ∅r = ∅; εr = r; ∅* = ε* = ε; (r*)* = r*; r*r* = r*; ε + rr* = r*; (rs)*r = r(sr)*.
- (r+s)* = (r*s*)* = (r*+s*)* = r*(sr*)* = (r*s)*r*.
- ≠: (r+s)* vs r*+s* vs r*s*; (rs)* vs r*s*.
- No “aa”: (b+ab)*(ε+a). At most one a: b*(ε+a)b*. Even #0: 1*(01*01*)*.
- Equality: compare shortest strings; counterexample kills; identities or minimal DFAs prove.
- How many strings of length n are there over a 2-letter alphabet?
Stuck? Peek at 0.3.2 Σᵏ, Σ*, Σ⁺ and languages
- |Σᵏ| = |Σ|ᵏ; Σ⁰ = {ε}; Σ* = ⋃ Σᵏ (k ≥ 0); Σ⁺ = Σ* − {ε}.
- #strings of length ≤ n over k symbols = (kⁿ⁺¹ − 1)/(k − 1); binary: 2ⁿ⁺¹ − 1.
- Language = any subset of Σ*. ∅ (no strings) ≠ {ε} (one string). Exactly r of one symbol in length n (binary): C(n, r).
- Shortlex order lists every string at a finite position.
The Hook
English sentences are built by rules like “a sentence is a noun phrase followed by a verb phrase”. Programming languages are defined the same way in their manuals: “an expression is a term, or an expression plus a term”. Machines check strings; regexes describe them; a grammar generates them by rewriting symbols step by step. Grammars become the main tool of Phase 3, so we meet them here first.
Predict First
Rules: S → aS and S → b. Starting from S and applying rules until no S is left, which strings can you produce?
Core Concept
Prediction check
S ⇒ b; S ⇒ aS ⇒ ab; S ⇒ aS ⇒ aaS ⇒ aab; … The strings are b, ab, aab, … = a*b.
Formal definition
- V — a finite set of variables (non-terminals), written in capitals: S, A, B.
- T — a finite set of terminals (the alphabet of the generated strings), lowercase: a, b.
- P — a finite set of productions (rules) α → β: “replace α by β”.
- S ∈ V — the start symbol.
Shorthand: A → x | y means two productions A → x and A → y. A → ε means A can vanish.
Derivations
A derivation step uA v ⇒ uβv applies a rule A → β somewhere in a string. ⇒* means “zero or more steps”. A string of variables and terminals reachable from S is a sentential form; one with only terminals is a sentence.
Grammars can be stronger than finite automata
Consider S → aSb | ε. Derivations: S ⇒ ε; S ⇒ aSb ⇒ ab; S ⇒ aSb ⇒ aaSbb ⇒ aabb. L(G) = { aⁿbⁿ | n ≥ 0 } — a language we proved is not regular (1.3.3). So grammars in general can describe more than DFAs. Which grammars stay within the regular languages? That is the next lesson’s question.
Reading a grammar
Ask: what does each variable generate on its own? Build up from the variables with only terminal rules. Example: A → aA | ε generates a*; B → bB | b generates b⁺; S → AB generates a*b⁺.
Putting it together
- A grammar (V, T, P, S) generates strings by rewriting variables until only terminals remain.
- L(G) = all terminal strings derivable from S.
- General grammars can generate non-regular languages such as aⁿbⁿ.
Visual
G: S → aSb | ε derivation of aabb
S
S ⇒ aSb ├─ a
⇒ aaSbb ├─ S ─┬─ a
⇒ aabb (S → ε) │ ├─ S ── ε
│ └─ b
└─ b (a tree of rule applications)
G2: S → aS | bS | a L(G2) = strings over {a,b} ending in a = (a+b)*aWorked Examples
G: S → 0S | 1. Give L(G) as a regex.
Show the full solution
0*1.
G: S → AB, A → aA | ε, B → bB | ε. L(G)?
Show the full solution
A gives a*, B gives b*, so L(G) = a*b*. Derive “ab”: S ⇒ AB ⇒ aAB ⇒ aB ⇒ abB ⇒ ab.
G: S → aSa | bSb | a | b | ε. Describe L(G). Is “abba” in it? “aba”? “ab”?
Show the full solution
Each step wraps the same symbol on both ends; the middle ends as a, b or ε. These are exactly the palindromes over {a,b}. abba: S ⇒ aSa ⇒ abSba ⇒ abba ✓. aba: S ⇒ aSa ⇒ aba ✓. ab: not a palindrome ✗.
GATE Traps
- A derivation must end with no variables left; sentential forms with variables are not in L(G).
- A → ε lets a variable disappear — it can make ε ∈ L(G).
- Grammars in general are more powerful than finite automata; only special forms stay regular.
Memory Hook
A grammar is a recipe book: start with “S”, keep replacing ingredients by sub-recipes until only raw food (terminals) is left.
Connect the Dots
Now: which shapes of rules generate exactly the regular languages? Answer: rules that grow the string from one end only — right-linear or left-linear grammars.
Self-Test
- MCQ
In G = (V, T, P, S), the set T contains:
- variables
- terminals
- productions
- start symbols
- MSQ
G: S → aS | bS | ε. Which strings are in L(G)?
- ε
- ab
- bba
- none of length 2
- MCQ
L(S → aSbb | ε) is:
- { aⁿbⁿ }
- { aⁿb²ⁿ : n ≥ 0 }
- a*b*
- { a²ⁿbⁿ }
- NAT
G: S → aSa | bSb | a | b | ε. How many strings of length 4 are in L(G)?
Write your answer before opening the key.
Rule Card
- G = (V, T, P, S); A → x | y; sentential form; ⇒, ⇒*; L(G) = {w ∈ T* : S ⇒* w}.
- S → aSb | ε gives aⁿbⁿ (not regular) — general grammars exceed DFAs.
- Palindromes: S → aSa | bSb | a | b | ε.
Concept Ledger
- grammar G = (V, T, P, S)
- variable (non-terminal), terminal
- production A → β
- start symbol
- derivation ⇒, ⇒*
- sentential form, sentence
- language of a grammar L(G)
Answer Vault
Reveal answers when you’re ready
Warm-up answers
- R = QP*.
- Yes.
- 2ⁿ.
Self-test answers
- B — Terminals form the output alphabet.
- A: That is V.
- C: That is P.
- D: S is one variable.
- ABC — L(G) = (a+b)*.
- D: All four length-2 strings are in it.
- B — Each a brings two b’s.
- A: Two b’s per a.
- C: Counts are tied.
- D: Reversed ratio.
- 4 — Palindromes of length 4: choose the first 2 symbols: 2² = 4.
Flashcards
- Four parts of a grammar?
Reveal answer
Variables V, terminals T, productions P, start symbol S. - L(S → aSb | ε)?
Reveal answer
{ aⁿbⁿ : n ≥ 0 } - Grammar for palindromes over {a,b}?
Reveal answer
S → aSa | bSb | a | b | ε