Theory of Computation · Unit 4 · 9 hrs
Context Free Grammar
Exam-focused notes for Context Free Grammar (Theory of Computation, CSC262): what the TU syllabus asks and how it has actually been tested, with 13 solved past questions from this unit.
Context Free Grammar in short
This unit moves beyond regular languages to context free grammars, the formalism behind programming language syntax, covering how a grammar generates strings, how ambiguity and normal forms are handled, where grammars sit in the Chomsky hierarchy, and how the pumping lemma bounds what a context free grammar can express.
What this unit covers
- Introduction to Context Free Grammar (CFG)
- Components of CFG
- Use of CFG
- Context Free Language (CFL)
- Types of derivations: Bottomup and Topdown approach
- Leftmost and Rightmost
- Language of a grammar
- Parse tree and its construction
- Ambiguous grammar
- Use of parse tree to show ambiguity in grammar
- Regular Grammars: Right Linear and Left Linear
- Equivalence of regular grammar and finite automata
- Simplification of CFG: Removal of Useless symbols, Nullable Symbols, and Unit Productions
- Chomsky Normal Form (CNF)
- Greibach Normal Form (GNF)
- Backus-Naur Form (BNF)
- Context Sensitive Grammar
- Chomsky Hierarchy
- Pumping Lemma for CFL
- Application of Pumping Lemma
- Closure Properties of CFL
Leftmost and Rightmost
2081 · 5 marks
Define the language of a grammar. For the grammar , show the leftmost derivation for the string 00100 with its parse tree. S→0S0∣1∣εS \rightarrow 0S0 \mid 1 \mid \varepsilonS→0S0∣1∣ε[5]
Show solved answerHide answer
Language of a Grammar and Leftmost Derivation of 00100
Given
- Grammar $G$ with productions: $$S \rightarrow 0S0 \mid 1 \mid \varepsilon$$
- Start symbol: $S$
- Terminals: ${0, 1}$
- Non-terminal: ${S}$
- Target string: $00100$
Solution
Definition: Language of a Grammar
For a grammar $G = (V, T, P, S)$ where $V$ is the set of variables (non-terminals), $T$ the set of terminals, $P$ the set of productions, and $S$ the start symbol, the language of the grammar, written $L(G)$, is the set of all terminal strings derivable from $S$:
$$L(G) = {, w \in T^* \mid S \Rightarrow^* w ,}$$
where $\Rightarrow^*$ denotes derivation in zero or more steps. Only strings consisting entirely of terminals belong to $L(G)$.
Leftmost Derivation of 00100
In a leftmost derivation, the leftmost non-terminal is expanded at each step. Since there is only one non-terminal $S$, this is straightforward.
$$S \Rightarrow 0S0 \quad (S \rightarrow 0S0)$$ $$\Rightarrow 00S00 \quad (S \rightarrow 0S0)$$ $$\Rightarrow 00100 \quad (S \rightarrow 1)$$
Complete derivation:
$$S \Rightarrow 0S0 \Rightarrow 00S00 \Rightarrow 00100$$
Therefore $00100 \in L(G)$.
Verification of leaf string: outer pair $0\ldots0$, inner pair $0\ldots0$, centre $1$ gives $0,0,1,0,0 = 00100$ ✓
Parse Tree for 00100
S
/ | \
0 S 0
/ | \
0 S 0
|
1
Reading the leaves left to right: $0,\ 0,\ 1,\ 0,\ 0 = 00100$ ✓
Summary Table
| Step | Sentential Form | Production |
|---|---|---|
| 1 | $S$ | start |
| 2 | $0S0$ | $S \rightarrow 0S0$ |
| 3 | $00S00$ | $S \rightarrow 0S0$ |
| 4 | $00100$ | $S \rightarrow 1$ |
The string $00100$ is derived in 3 steps, confirming $00100 \in L(G)$.
Equivalence of regular grammar and finite automata
2081 · 5 marks
Represent the following regular grammar to finite automata. S→aA∣aB∣εS \rightarrow aA \mid aB \mid \varepsilonS→aA∣aB∣εA→aA∣aSA \rightarrow aA \mid aSA→aA∣aSB→bB∣εB \rightarrow bB \mid \varepsilonB→bB∣ε[5]
Chomsky Normal Form
2081 · 5 marks
Convert the following grammar to CNF. S→AAB,A→aA∣ε,B→ab∣aS \rightarrow AAB, \quad A \rightarrow aA \mid \varepsilon, \quad B \rightarrow ab \mid aS→AAB,A→aA∣ε,B→ab∣a[5]
2080 · 10 marks
Define context free grammar with an example. Explain with example, how context free grammar is converted to Chomsky Normal Form.[10]
Show solved answerHide answer
Context Free Grammar and Conversion to Chomsky Normal Form
Given
This is a theory and derivation question. The only structural data needed is the grammar chosen for the worked example, which is:
$$ \begin{aligned} S &\to ASB \mid \varepsilon\ A &\to aAS \mid a\ B &\to SbS \mid A \mid bb \end{aligned} $$
No data is missing; the question is fully answerable.
Solution
Part 1: Context Free Grammar (CFG)
A Context Free Grammar is a 4-tuple $G = (V, T, P, S)$ where:
- $V$ = finite set of non-terminals (variables)
- $T$ = finite set of terminals, with $V \cap T = \varnothing$
- $P$ = finite set of productions of the form $A \to \alpha$, where $A \in V$ and $\alpha \in (V \cup T)^*$
- $S \in V$ = start symbol
Every production has exactly one non-terminal on the left, so it can be replaced regardless of surrounding context - hence "context free."
Example: $$S \to 0S1 \mid \varepsilon$$
- $V={S},\ T={0,1},\ S=S$
- Generates ${0^n1^n \mid n \ge 0} = {\varepsilon, 01, 0011, \dots}$
CFGs describe the syntax of programming languages and drive parser/compiler design.
Part 2: Chomsky Normal Form (CNF)
A CFG is in CNF if every production is of the form:
- $A \to BC$ (two non-terminals), or
- $A \to a$ (single terminal)
with the sole exception $S \to \varepsilon$ allowed if $\varepsilon$ is in the language, and $S$ must not appear on any RHS.
Conversion steps:
- Add a new start symbol.
- Eliminate $\varepsilon$-productions.
- Eliminate unit productions.
- Reduce RHS length to 2 (introduce variables).
- Replace terminals in long productions with new variables.
Worked Example
Original grammar: $$ S \to ASB \mid \varepsilon,\quad A \to aAS \mid a,\quad B \to SbS \mid A \mid bb $$
Step 1: New start symbol $S_0$
$$ S_0 \to S,\quad S \to ASB \mid \varepsilon,\quad A \to aAS \mid a,\quad B \to SbS \mid A \mid bb $$
Step 2: Eliminate $\varepsilon$-productions
Nullable variables: $S$ (since $S\to\varepsilon$). Insert versions with $S$ deleted:
- $S \to ASB \Rightarrow S \to ASB \mid AB$
- $A \to aAS \Rightarrow A \to aAS \mid aA$
- $B \to SbS \Rightarrow B \to SbS \mid bS \mid Sb \mid b$
- $S_0 \to S$ keeps $S_0 \to \varepsilon$ (since $\varepsilon$ is in the language)
$$ \begin{aligned} S_0 &\to S \mid \varepsilon\ S &\to ASB \mid AB\ A &\to aAS \mid aA \mid a\ B &\to SbS \mid bS \mid Sb \mid b \mid A \mid bb \end{aligned} $$
Step 3: Eliminate unit productions
Unit pairs: $S_0 \to S$, $B \to A$.
- $S_0 \to S$: $S_0 \to ASB \mid AB$
- $B \to A$: $B \to aAS \mid aA \mid a$
$$ \begin{aligned} S_0 &\to ASB \mid AB \mid \varepsilon\ S &\to ASB \mid AB\ A &\to aAS \mid aA \mid a\ B &\to SbS \mid bS \mid Sb \mid b \mid aAS \mid aA \mid a \mid bb \end{aligned} $$
Step 4: Replace terminals in multi-symbol productions
Introduce $X_a \to a$, $X_b \to b$. Replace terminals wherever they sit with other symbols:
$$ \begin{aligned} S_0 &\to ASB \mid AB \mid \varepsilon\ S &\to ASB \mid AB\ A &\to X_aAS \mid X_aA \mid a\ B &\to SX_bS \mid X_bS \mid SX_b \mid b \mid X_aAS \mid X_aA \mid a \mid X_bX_b\ X_a &\to a\ X_b &\to b \end{aligned} $$
Step 5: Reduce RHS length to 2
Introduce helper variables:
- $ASB$: let $C_1 \to SB$, so $\to A C_1$
- $X_aAS$: let $C_2 \to AS$, so $\to X_a C_2$
- $SX_bS$: let $C_3 \to X_bS$, so $\to S C_3$
Final CNF grammar: $$ \begin{aligned} S_0 &\to AC_1 \mid AB \mid \varepsilon\ S &\to AC_1 \mid AB\ A &\to X_aC_2 \mid X_aA \mid a\ B &\to SC_3 \mid X_bS \mid SX_b \mid b \mid X_aC_2 \mid X_aA \mid a \mid X_bX_b\ C_1 &\to SB\ C_2 &\to AS\ C_3 &\to X_bS\ X_a &\to a,\qquad X_b \to b \end{aligned} $$
Every production is now $A \to BC$ or $A \to a$ (plus the permitted $S_0 \to \varepsilon$), so the grammar is in Chomsky Normal Form.
2079 · 10 marks
Given the following expression grammar for simple arithmetic expression with operator + and *. Remove the left recursion from this grammar then simplify and convert to CNF.E→E+T∣TE \rightarrow E+T \mid TE→E+T∣TT→T+F∣FT \rightarrow T+F \mid FT→T+F∣FF→(E)∣aF \rightarrow (E) \mid aF→(E)∣a[10]
2078 · 5 marks
Construct the following grammar into Chomsky Normal Form. S→abSb∣a∣aAbS \rightarrow abSb \mid a \mid aAbS→abSb∣a∣aAbA→bS∣aAAb∣εA \rightarrow bS \mid aAAb \mid \varepsilonA→bS∣aAAb∣ε[5]
2076 · 5 marks
Define Chomsky Normal Form and Greibach Normal Form in reference to CFG. Give a suitable example of each. [5]
Introduction to Context Free Grammar
2080 · 5 marks
What is the meaning of the term 'Context Free' in context free grammar? Justify with a suitable example. What is the need of a parse tree? [5]
2080.1 · 10 marks
Define CFG. Construct a CFG that generates the language of all palindromes over {a,b} that do not contain the substring aa. Show the leftmost derivation and construct the equivalent parse tree for string babbbab.[10]
Show solved answerHide answer
Context-Free Grammar - Definition and Construction
Given
- Alphabet: $\Sigma = {a, b}$
- Language: all palindromes over ${a,b}$ that do NOT contain substring $aa$
- Target string for derivation and parse tree: babbbab
- Marks: 10
All required data is present.
Solution
1. Definition of CFG
A Context-Free Grammar is a 4-tuple $G = (V, T, P, S)$ where:
- $V$ = finite set of variables (non-terminals)
- $T$ = finite set of terminals, with $V \cap T = \varnothing$
- $P$ = finite set of productions of the form $A \to \alpha$, where $A \in V$ and $\alpha \in (V \cup T)^*$
- $S \in V$ = the start symbol
Every production has exactly one non-terminal on the left, hence "context-free". CFGs generate the class of Context-Free Languages.
2. Language Analysis
$$L = {, w \in {a,b}^* \mid w = w^R \text{ and } aa \text{ is not a substring of } w ,}$$
Constraints:
- Palindrome: $w = w^R$
- No $aa$: two $a$'s must never be adjacent
Because $w$ is a palindrome, when we place an $a$ at both ends the inner string must not begin or end with $a$ (otherwise $aa$ would form at the junction). So an $a$-wrapped layer must contain an inner block that begins and ends with $b$ (or is a single $b$).
Sample valid strings: $\varepsilon,\ a,\ b,\ aba,\ bab,\ bbb,\ babab,\ babbbab$. Invalid: $aa,\ baab,\ aabaa$.
3. Constructing the CFG
Use two variables:
| Variable | Meaning |
|---|---|
| $S$ | any palindrome without $aa$ |
| $B$ | a palindrome without $aa$ that starts and ends with $b$ |
Productions $P$:
$$ \begin{aligned} S &\to \varepsilon \mid a \mid b \ S &\to bSb \ S &\to aBa \ B &\to b \mid bSb \end{aligned} $$
Grammar: $G = ({S,B},\ {a,b},\ P,\ S)$.
Why $aa$ never occurs:
- $S \to aBa$: the inner $B$ must start and end with $b$, giving $a,b\ldots b,a$, so no adjacent $a$'s at the junction.
- $S \to bSb$ and $B \to bSb$: wrap with $b$'s, always safe.
- No production ever places two $a$'s next to each other.
The palindrome property holds because each recursive rule adds the same terminal symmetrically on both ends.
4. Leftmost Derivation for babbbab
Structural decomposition:
$$ \underbrace{b}{\text{outer}}\ \underbrace{a}{}\ \underbrace{b\ b\ b}{\text{middle}}\ \underbrace{a}{}\ \underbrace{b}_{\text{outer}} $$
- Outer $b\ldots b$ → $S \to bSb$
- Inner $abbba$: $a\ldots a$ → $S \to aBa$
- Inner $bbb$: $b\ldots b$ → $B \to bSb$
- Center $b$ → $S \to b$
Leftmost derivation (always expand leftmost non-terminal):
| Step | Sentential form | Production |
|---|---|---|
| 1 | $S$ | start |
| 2 | $bSb$ | $S \to bSb$ |
| 3 | $baBab$ | $S \to aBa$ |
| 4 | $babSbab$ | $B \to bSb$ |
| 5 | $babbbab$ | $S \to b$ |
$$S \Rightarrow bSb \Rightarrow baBab \Rightarrow babSbab \Rightarrow babbbab$$
Result: $babbbab$ ✓ (matches target)
5. Parse Tree for babbbab
S
/ | \
b S b
/ | \
a B a
/ | \
b S b
|
b
Reading the leaves left to right: $b,a,b,b,b,a,b = babbbab$ ✓
The tree is symmetric about its center, confirming both the palindrome property and the absence of substring $aa$ (each $a$ is bounded by $b$'s).
Final Answer: The CFG $G = ({S,B},{a,b},P,S)$ with productions $S \to \varepsilon \mid a \mid b \mid bSb \mid aBa,\ B \to b \mid bSb$ generates the required language. The leftmost derivation for $babbbab$ uses the sequence $S \Rightarrow bSb \Rightarrow baBab \Rightarrow babSbab \Rightarrow babbbab$, with the parse tree shown above.
Parse tree and its construction
2079 · 5 marks
Define the term: Parse Tree, left-most and right-most derivation, sentential form and ambiguity with example. [5]
Regular Grammars
2079 · 5 marks
Define regular grammar. Also explain the method of converting right linear grammar into equivalent finite automata. [5]
Chomsky Hierarchy
2078 · 5 marks
Explain about the Chomsky's Hierarchy about the language and programs. [5]
Pumping Lemma for CFL
2080.1 · 5 marks
Prove that th language is not a context free grammar. $L = {a^n b^n c^n \mid n \geq 0}$ [5]
Make Unit 4 stick
Practice CSC262 with flashcards & quizzes