2. CFG – Context Free Grammar
2
A Context Free Grammar is a 4 tuple (V, 𝛴, R, S), where
▸ V is a finite set called the variables
▸ Σ is a finite set, disjoint from V, called the terminals
▸ R is a finite set of rules, with each rule being a variable and a string of variables and terminals
▸ S ϵ V is the start variable
Example:
Grammar, G1 = ( { S }, { a, b }, R, S )
V = { S }
Σ = { a, b }
R is,
S → aSb | SS | ε
Example:
Grammar, G2 = ( V, Σ, R, <EXPR> )
V = { <EXPR>, <TERM>, <FACTOR> }
Σ = { a, +, x, (, ) }
R is,
<EXPR> → <EXPR> + <TERM> | <TERM>
<TERM> → <TERM> x <FACTOR> | <FACTOR>
<FACTOR> → ( <EXPR> ) | a
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
3. CNF - Chomsky Normal Form
3
▸ When working with context-free grammar, it is often convenient to have them in simplified form.
▸ One of the simplest and most useful forms is called the Chomsky Normal Form.
▸ CNF is useful in giving algorithms for working with CFGs.
Chomsky Normal Form:
A context-free grammar is in Chomsky Normal Form if every rule is of the form,
A → BC
A → a
where a is any terminal and A, B, C are any variables – except that B and C may not be the start variable.
If S is the start variable, then the rule S → 𝜀 is valid.
[ i.e. if language contains 𝜀, then we allow S -> 𝜀 where S is start symbol, and forbid S on RHS]
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
4. CFG in CNF
4
Theorem:
Any Context Free Language is generated by a Context-Free Grammar in Chomsky Normal Form.
The conversion to Chomsky Normal Form has 4 main steps:
▸ Get rid of all 𝜀 productions.
▸ Get rid of all productions where RHS is a single variable.
▸ Replace every production that is too long by shorter productions.
▸ Move all terminals to productions where RHS is one terminal.
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
5. CFG in CNF – Step 1
5
CFG to CNF conversion principle:
Rules that violate the CNF conditions are replaced with equivalent ones that are satisfactory.
Step 1:
Add a new start variable S0 and the rule S0 → S where S was the original start variable.
▸It guarantees that the start variable doesn’t occur on the right-hand side of a rule.
Example:
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
S → ASA | aB
A → B | S
B → b | ε
S0 → S
S → ASA | aB
A → B | S
B → b | 𝜀
Step 1
6. CFG in CNF – Step 2
6
Step 2:
Eliminate all 𝜀 rules of the form A → 𝜀, where A is not the start variable.
▸For each occurrence of an A on the right-hand side of a rule, add a new rule with that occurrence deleted.
– If R → uAv is a rule in which u and v are strings of variables and terminals, we add new rule,
R → uv
– Do this for each occurrence of A. If the rule is R → uAvAw then we will add rules,
R → uvAw, R → uAvw, and R → uvw
– If the rule is R → A, then the new rule becomes R → 𝜀 unless we had previously removed the rule R → 𝜀
▸Repeat this step until all 𝜀-rules are eliminated.
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
7. Example:
CFG in CNF – Step 2
7
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
Removing 𝜀-rule B → 𝜀 ,
S0 → S
S → ASA | aB | a
A → B | S | 𝜀
B → b | 𝜀
S → ASA | aB
A → B | S
B → b | ε
S0 → S
S → ASA | aB
A → B | S
B → b | 𝜀
Step 1
Step 2
Removing 𝜀-rule A → 𝜀 ,
S0 → S
S → ASA | aB | a | SA | AS | S
A → B | S | 𝜀
B → b
Step 2
8. CFG in CNF – Step 3
8
Step 3: Eliminate all the unit rules of the form A → B.
▸ Whenever a rule B → u appears, add a new rule A → u unless this was a unit rule previously removed.
▸ Repeat this step until all unit rules are eliminated.
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
9. Example:
CFG in CNF – Step 3
9
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
Removing 𝜀-rule B → 𝜀 ,
S0 → S
S → ASA | aB | a
A → B | S | 𝜀
B → b | 𝜀
S → ASA | aB
A → B | S
B → b | ε
S0 → S
S → ASA | aB
A → B | S
B → b | 𝜀
Step 1 Step 2
Removing 𝜀-rule A → 𝜀 ,
S0 → S
S → ASA | aB | a | SA | AS | S
A → B | S | 𝜀
B → b
Step 2
Removing unit rule S → S,
S0 → S
S → ASA | aB | a | SA | AS | S
A → B | S
B → b
Step 3
Removing unit rule S0 → S,
S0 → S | ASA | aB | a | SA | AS
S → ASA | aB | a | SA | AS
A → B | S
B → b
Step 3
Removing unit rule A → B,
S0 → ASA | aB | a | SA | AS
S → ASA | aB | a | SA | AS
A → B | S | b
B → b
Step 3
Removing unit rule A → S,
S0 → ASA | aB | a | SA | AS
S → ASA | aB | a | SA | AS
A → S | b | ASA | aB | a | SA | AS
B → b
Step 3
10. CFG in CNF – Step 4
10
Step 4:
Convert all the remaining rules in the proper form.
▸ Replace each rule of the form, A → u1 u2 … … uk where k>=3 and each ui is a variable or terminal symbol with these new
rules,
A → u1 A1, A1 → u2 A2, A2 → u3 A3, … … , Ak-2 → uk-1 uk
▸Replace each terminal ui in the preceding rules with the new variable Ui and add the rule,
Ui → ui
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
11. Example:
CFG in CNF – Step 4
11
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
Removing 𝜀-rule B → 𝜀 ,
S0 → S
S → ASA | aB | a
A → B | S | 𝜀
B → b | 𝜀
S → ASA | aB
A → B | S
B → b | ε
S0 → S
S → ASA | aB
A → B | S
B → b | 𝜀
Step 1 Step 2
Removing 𝜀-rule A → 𝜀 ,
S0 → S
S → ASA | aB | a | SA | AS | S
A → B | S | 𝜀
B → b
Step 2
Removing unit rule S → S,
S0 → S
S → ASA | aB | a | SA | AS | S
A → B | S
B → b
Step 3
Removing unit rule S0 → S,
S0 → S | ASA | aB | a | SA | AS
S → ASA | aB | a | SA | AS
A → B | S
B → b
Step 3
Removing unit rule A → B,
S0 → ASA | aB | a | SA | AS
S → ASA | aB | a | SA | AS
A → B | S | b
B → b
Step 3
Removing unit rule A → S,
S0 → ASA | aB | a | SA | AS
S → ASA | aB | a | SA | AS
A → S | b | ASA | aB | a | SA | AS
B → b
Step 3
Proper form conversion,
S0 → AA1 | UB | a | SA | AS
A1 → SA
U → a
S → AA1 | UB | a | SA | AS
A → b | AA1 | UB | a | SA | AS
B → b
Step 4
12. CFG in CNF – Practice Set
12
1.
S → ASB
A → aAS | a | ε
B → SbS | A | bb
2.
S → XY
X → abb | aXb | ε
Y → c | cY
3.
S → aXbY
X → aX | 𝜀
Y → bY | 𝜀
4.
S → aXbX
X → aY | bY | 𝜀
Y → X | c
5.
S → S1 | S2
S1 → S1b | Ab | 𝜀
A → aAb | ab
S2 → S2a | Ba | 𝜀
B → bBa | ba
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
6.
S → DBC | Ba
B → 0B1 | 01 | 𝜀
C → aCb | aC | Bb
D → bD | D
7.
S → aX | bY | b | ZZc
X → Yaa | abZ | 𝜀
Y → bXXb | ab | cZ
Z → a | b | XZ | 𝜀
8.
A -> BAB | B | 𝜀
B -> 00 | 𝜀
13. 13
References:
Chapter 2(section 2.1), Introduction to the Theory of Computation, 3rd Edition by Michael Sipser
THANKS!
Any questions?
You can find me at imam@cse.uiu.ac.bd