Theory of Computation
CSE 2233
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
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
CFG – Application
3
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
CFG Design >> DFA to CFG
4
Steps:
▸ For each state qi in the DFA, create a variable Ri for your CFG
▸ For each transition rule δ(qi, a) = qk in your DFA, add the rule Ri → aRk to your CFG
▸ For each accept state qa in your DFA, add the rule Ra → 𝜀
▸ If q0 is the start state in your DFA, then R0 is the starting variable in your CFG.
CFG rules:
R1 → 0 R3 | 1 R2
R2 → 0 R1 | 1 R3 | ε
R3 → 0 R3 | 1 R3
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
CFG >> DFA to CFG – Ex. 1
5
L(M) = { w | w ends with 01 }
CFG rules:
Q0 → 0 Q1 | 1 Q0
Q1 → 0 Q1 | 1 Q2
Q2 → 0 Q1 | 1 Q0 | ϵ
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
CFG >> DFA to CFG – Practice 1
6
L(M1) = { an bm cl | n, m, l >= 0 }
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
CFG >> DFA to CFG – Practice 2
7
L(M2) = { All binary strings with both an even number of zeros and an even number of ones }
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
CFG Design >> RE to CFG
8
Steps:
▸ If the RE is epsilon or a character in the alphabet, add to G the production
<RE> → RE
▸ If the RE is R1. R2, add to G the production
<RE> → <R1> <R2>
then create productions for regular expressions R1 and R2
▸ If the RE is R1 | R2, add to G the production
<RE> → <R1> | <R2>
and create productions for regular expression R1 and R2
▸ Assume the RE is R1* , add to G the production
<RE> → <R1> <RE> | epsilon
and create productions for regular expression R1
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
CFG >> RE to CFG – Ex 1
9
Build a CFG G for the RE, (0 | 1)* 111
Soln: For (0 | 1) ,
<0 | 1> → <0> | <1>
<0> → 0
<1> → 1
For (0 | 1)* ,
< (0 | 1)* > → < 0 | 1 > < (0 | 1)* > | ε
For (0 | 1)* 111,
<RE> → < (0 | 1)* > <1> <1> <1>
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
Final CFG rules:
<RE> → < (0 | 1)* > <1> <1> <1>
< (0 | 1)* > → < 0 | 1 > < (0 | 1)* > | ε
<0 | 1> → <0> | <1>
<0> → 0
<1> → 1
CFG >> RE to CFG – Practices
10
Language, L(R) Regular Expression
{ w | w contains a single 1 } 0* 1 0*
{ w | w consists of exactly three 1’s } 0* 1 0* 1 0* 1 0*
{ w | w has at least a single 1 } (0 | 1)* 1 (0 | 1)*
{ w | w contains at least three 1’s } (0 | 1)* 1 (0 | 1)* 1 (0 | 1)* 1 (0 | 1)*
{ w | w has at most one 1 } 0* | 0* 1 0*
{ w | w contains an even number of 1’s } (0* 1 0* 1 0*)* 0*
{ w | the number of 1’s withing w can be evenly divided by 3 } (0* 1 0* 1 0* 1 0* )* 0*
{ w | w is a string of even length } (ΣΣ)*, here Σ = (0 | 1)
{ w | the length of w is a multiple of 3 } (ΣΣΣ)*, here Σ = (0 | 1)
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
CFG >> RE to CFG – Practices
11
Language, L(R) Regular Expression
{ w | w starts with 101 } 101 (0 | 1)*
{ w | w contains the string 001 as a substring } (0 | 1)* 001 (0 | 1)*
{ w | w ends with 3 consecutive 1’s } (0 | 1)* 111
{ w | w doesn’t end with 11 } 𝜀 | 0 | 1 | (0 | 1)* (00 | 01 | 10)
{ w | w ends with an even nonzero number of 0’s } ( (0 | 1)* 1 | 𝜀 ) 00 (00)*
{ w | w starts and ends with the same symbol } 0 | 1 | 0Σ*0 | 1Σ*1, here Σ = (0 | 1)
{ w | w has at least 3 characters and the 3rd character is 0 } (0 | 1) (0 | 1) 0 (0 | 1)*
{ w | every zero in w is followed by at least one 1 } 1* (01+)*
{ w | w consists of alternating zeros and ones } (1 | 𝜀) (01)* (0 | 𝜀)
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
CFG >> RE to CFG – Practices
12
Language, L(R) Regular Expression
{ 01, 10 } 01 | 10
01* | 1* (0 | 𝜀) 1*
{ 𝜀, 0, 1, 01 } (0 | 𝜀) (1 | 𝜀)
𝜙 R𝜙
{ 𝜀 } 𝜙*
{ w | w starts with 0 and has odd length or, starts with 1 and has
even length }
0 ( (0 | 1) (0 | 1) )* | 1 (0 | 1) ( (0 | 1) (0 | 1) )*
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
CFG Design >> Linked Terminals
13
Terminals may be linked to one another in that they have the same number of occurrences (or a related number)
Example 1:
{ 0n 1n | n >= 0 }
CFG rules,
S → 0 S 1 | ε
Example 2:
{ 0n 12n | n > 0 }
CFG rules,
S → 0 S 11 | 011
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
CFG >> Linked Terminals – Steps to follow
14
▸ { w | w is a palindrome }
S → ε I 0 | 1 | 0S0 | 1S1
Base Case: 𝜀 , 0, 1 are palindromes.
Recursive Step: if w is a palindrome then 0w0 and 1w1 are also palindromes.
▸ { w | w is a string of balanced parentheses } over w = { ( , ) }
S → ( S ) S | ε
or,
S → SS | (S) | ε
Base Case: the empty string is balanced
Recursive Step: Find out the closing parenthesis that matches the first opening parenthesis. Removing the first
parenthesis and the matching parenthesis forms two new strings of balanced parentheses.
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
CFG >> Linked Terminals – Practice set 1
15
▸ L = any string
S → 0S | 1S | ε
▸ L = any string with only even no of 1’s and no 0’s
S → 1S1 | ε
▸ L = { w | w contains 100 as substring }
S → P100P
P → 0P | 1P | ε
▸ L = all strings that start and end with the same symbol
S → 0P0 | 1P1 | 0 | 1
P → 0P | 1P | ε
▸ L = { w | the length of w is even }
S → 0S0 | 0S1 | 1S0 | 1S1 | ε
▸ L = { w | the length of w is odd and its middle symbol is a 0 }
S → 0 | 0S0 | 1S0 | 0S1 | 1S1
▸ L = { w | w is a palindrome }
S → ε | 0 | 1 | 0S0 | 1S1
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
CFG >> Linked Terminals – Practice set 1
16
▸ L = { 0* }
S → 0S | ε
▸ L = { 0*1 }
S → 0 S | 1
▸ L = { 0n 1n | n >= 0 }
S → 0S1 | ε
▸ L = { 0n 1n | n>=1 }
S → 0S1 | 01
▸ L = { 02n 13n | n>=0 }
S → 00S111 | ε
▸ L = { 1n 0* 1n | n >=0 }
S → 1 S 1 | P
P → 0P | ε
▸ L = { 0n1m | n = m and n, m>=0 } = { 0n 1n | n>=0 }
S → 0S1 | ε
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
CFG >> Linked Terminals – Practice set 1
17
▸ { 0n 1m | n>m and n, m>=0 } ; let n=m+k then { 0k0m1m | n, m>=0 and k>0 }
S → 0S | 0P
P → 0P1 | ε
▸ { 0n 1m | n=2m and n, m>=0 } = { 02m 1m | m>=0 }
S → 00S1 | ε
▸ { 0x1y 0z | z=x+y and x, y, z >= 0 } = { 0x 1y 0x+y | x, y >= 0 } = { 0x 1y 0y 0x | x, y >= 0 }
S → 0S0 | P
P → 1P0 | ε
▸ All strings of the form 0a 1b 0c where a+c=b [ in short: 0a 1a 1c 0c ]
S → TU
T → 0T1 | ε
U → 1U0 | ε
▸ { 0x1y 0z | z=x-y and x, y, z >= 0 } = { 0z+y 1y 0z | y, z >= 0 } = { 0z 0y 1y 0z | y, z >=0 }
S → 0S0 | P
P → 0P1 | ε
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
CFG >> Linked Terminals – Practice set 1
18
▸ L = { w | w has the same number of 0’s and 1’s }
S → 0S1S | 1S0S | ε
or,
S → SS | 0S1 | 1S0 | ε
▸ L = all strings with more 1’s than 0’s
S → T1S | T1T | S1T
T → TT | 0T1 | 1T0 | ε
▸ L = all strings with distinct number of 0’s and 1’s
S → U | V
U → T0U | T0T | U0T
V → T1V | T1T | V1T
T → TT | 0T1 | 1T0 | ε
▸ L = all strings with an odd number of a’s and an odd number of b’s
S → EaEbE | EbEaE
E → EaEaE | EbEbE | ε | SS
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
CFG Design >> Union of CFG’s
19
▸ Many Context Free Languages are the union of simpler CFLs.
▸ First break the CFL into simpler pieces and construct individual grammars for each piece.
▸ These individual grammars can be easily merged into a grammar for the original language by combining their rules and
then adding the new rule,
S → S1 | S2 | … … … | Sk
where the variables Si , 1 <= i <= k are the start variables for the individual grammars.
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
CFG >> Union of CFG’s – Ex. 1
20
CFL = CFL1 U CFL2 = { 0n 1n | n>=0 } U { 1n 0n | n>=0 }
CFG1 for CFL1, S1 → 0 S1 1 | ε
CFG2 for CFL2, S2 → 1 S2 0 | ε
CFG for CFL,
S → S1 | S2
S1 → 0 S1 1 | ε
S2 → 1 S2 0 | ε
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
CFG >> Union of CFG’s – Ex. 2
21
CFL = CFL1 U CFL2 = { ai bj ck | i, j, k >=0 and i=j or j=k }
= { ai bj ck | i, j, k >=0 and i=j } U { ai bj ck | i, j, k >=0 and j=k }
CFG1 for CFL1 , S2 → S2 c | S1
S1 → a S1 b | ε
CFG2 for CFl2, S4 → a S4 | S3
S3 → b S3 c | ε
CFG for CFL, S → S2 | S4
S2 → S2 c | S1
S1 → a S1 b | ε
S4 → a S4 | S3
S3 → b S3 c | ε
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
CFG >> Union of CFG’s – Ex. 3
22
L = { 0n 1m | n != m } = { 0n 1m | n > m } U { 0n 1m | n < m }
Soln:
CFG for L1 = { 0n 1m | n > m } here n = m + k
S1 → 0S1 | 0S2
S2 → 0S21 | ε
CFG for L2 = { 0n 1m | n < m } here m = n + k
S3 → S31 | S41
S4 → 0S21 | ε
Union of these CFG’s,
S → S1 | S3
S1 → 0S1 | 0S2
S2 → 0S21 | ε
S3 → S31 | S21
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
CFG >> Linked Terminals – Practice set 2
23
▸ L = { 0n 1m 0m 1n | n, m >=0 }
S → 0S1 | C
C → 1C0 | ε
▸ L = { an bm ck | n, m, k >=0 and n=m+k }
S → aSc | B
B → aBb | ε
▸ L = { an bm ck | n, m, k >=0 and n = 2m + 3k }
CFG = ?
▸ L = { an bm | 0 <= n <= m <= 2n }
S → aSbb | aSb | ε
▸ L = { an bm | 2n <= m <=3n }
S → aSbbb | aSbb | ε
▸ L = { an bn | n is not a multiple of 3 }
S → ab | aabb | aaaSbbb
▸ L = { an bm ck | k = |n-m| }, Hint: if n >= m then k = n-m otherwise k = m - n
CFG =?
Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
24
THANKS!
Any questions?
You can find me at imam@cse.uiu.ac.bd
References:
Chapter 2(section 2.1), Introduction to the Theory of Computation, 3rd Edition by Michael Sipser

TOC 6 | CFG Design

  • 1.
    Theory of Computation CSE2233 Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 2.
    CFG – ContextFree 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.
    CFG – Application 3 MohammadImam Hossain | Lecturer, Dept. of CSE | UIU
  • 4.
    CFG Design >>DFA to CFG 4 Steps: ▸ For each state qi in the DFA, create a variable Ri for your CFG ▸ For each transition rule δ(qi, a) = qk in your DFA, add the rule Ri → aRk to your CFG ▸ For each accept state qa in your DFA, add the rule Ra → 𝜀 ▸ If q0 is the start state in your DFA, then R0 is the starting variable in your CFG. CFG rules: R1 → 0 R3 | 1 R2 R2 → 0 R1 | 1 R3 | ε R3 → 0 R3 | 1 R3 Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 5.
    CFG >> DFAto CFG – Ex. 1 5 L(M) = { w | w ends with 01 } CFG rules: Q0 → 0 Q1 | 1 Q0 Q1 → 0 Q1 | 1 Q2 Q2 → 0 Q1 | 1 Q0 | ϵ Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 6.
    CFG >> DFAto CFG – Practice 1 6 L(M1) = { an bm cl | n, m, l >= 0 } Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 7.
    CFG >> DFAto CFG – Practice 2 7 L(M2) = { All binary strings with both an even number of zeros and an even number of ones } Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 8.
    CFG Design >>RE to CFG 8 Steps: ▸ If the RE is epsilon or a character in the alphabet, add to G the production <RE> → RE ▸ If the RE is R1. R2, add to G the production <RE> → <R1> <R2> then create productions for regular expressions R1 and R2 ▸ If the RE is R1 | R2, add to G the production <RE> → <R1> | <R2> and create productions for regular expression R1 and R2 ▸ Assume the RE is R1* , add to G the production <RE> → <R1> <RE> | epsilon and create productions for regular expression R1 Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 9.
    CFG >> REto CFG – Ex 1 9 Build a CFG G for the RE, (0 | 1)* 111 Soln: For (0 | 1) , <0 | 1> → <0> | <1> <0> → 0 <1> → 1 For (0 | 1)* , < (0 | 1)* > → < 0 | 1 > < (0 | 1)* > | ε For (0 | 1)* 111, <RE> → < (0 | 1)* > <1> <1> <1> Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU Final CFG rules: <RE> → < (0 | 1)* > <1> <1> <1> < (0 | 1)* > → < 0 | 1 > < (0 | 1)* > | ε <0 | 1> → <0> | <1> <0> → 0 <1> → 1
  • 10.
    CFG >> REto CFG – Practices 10 Language, L(R) Regular Expression { w | w contains a single 1 } 0* 1 0* { w | w consists of exactly three 1’s } 0* 1 0* 1 0* 1 0* { w | w has at least a single 1 } (0 | 1)* 1 (0 | 1)* { w | w contains at least three 1’s } (0 | 1)* 1 (0 | 1)* 1 (0 | 1)* 1 (0 | 1)* { w | w has at most one 1 } 0* | 0* 1 0* { w | w contains an even number of 1’s } (0* 1 0* 1 0*)* 0* { w | the number of 1’s withing w can be evenly divided by 3 } (0* 1 0* 1 0* 1 0* )* 0* { w | w is a string of even length } (ΣΣ)*, here Σ = (0 | 1) { w | the length of w is a multiple of 3 } (ΣΣΣ)*, here Σ = (0 | 1) Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 11.
    CFG >> REto CFG – Practices 11 Language, L(R) Regular Expression { w | w starts with 101 } 101 (0 | 1)* { w | w contains the string 001 as a substring } (0 | 1)* 001 (0 | 1)* { w | w ends with 3 consecutive 1’s } (0 | 1)* 111 { w | w doesn’t end with 11 } 𝜀 | 0 | 1 | (0 | 1)* (00 | 01 | 10) { w | w ends with an even nonzero number of 0’s } ( (0 | 1)* 1 | 𝜀 ) 00 (00)* { w | w starts and ends with the same symbol } 0 | 1 | 0Σ*0 | 1Σ*1, here Σ = (0 | 1) { w | w has at least 3 characters and the 3rd character is 0 } (0 | 1) (0 | 1) 0 (0 | 1)* { w | every zero in w is followed by at least one 1 } 1* (01+)* { w | w consists of alternating zeros and ones } (1 | 𝜀) (01)* (0 | 𝜀) Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 12.
    CFG >> REto CFG – Practices 12 Language, L(R) Regular Expression { 01, 10 } 01 | 10 01* | 1* (0 | 𝜀) 1* { 𝜀, 0, 1, 01 } (0 | 𝜀) (1 | 𝜀) 𝜙 R𝜙 { 𝜀 } 𝜙* { w | w starts with 0 and has odd length or, starts with 1 and has even length } 0 ( (0 | 1) (0 | 1) )* | 1 (0 | 1) ( (0 | 1) (0 | 1) )* Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 13.
    CFG Design >>Linked Terminals 13 Terminals may be linked to one another in that they have the same number of occurrences (or a related number) Example 1: { 0n 1n | n >= 0 } CFG rules, S → 0 S 1 | ε Example 2: { 0n 12n | n > 0 } CFG rules, S → 0 S 11 | 011 Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 14.
    CFG >> LinkedTerminals – Steps to follow 14 ▸ { w | w is a palindrome } S → ε I 0 | 1 | 0S0 | 1S1 Base Case: 𝜀 , 0, 1 are palindromes. Recursive Step: if w is a palindrome then 0w0 and 1w1 are also palindromes. ▸ { w | w is a string of balanced parentheses } over w = { ( , ) } S → ( S ) S | ε or, S → SS | (S) | ε Base Case: the empty string is balanced Recursive Step: Find out the closing parenthesis that matches the first opening parenthesis. Removing the first parenthesis and the matching parenthesis forms two new strings of balanced parentheses. Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 15.
    CFG >> LinkedTerminals – Practice set 1 15 ▸ L = any string S → 0S | 1S | ε ▸ L = any string with only even no of 1’s and no 0’s S → 1S1 | ε ▸ L = { w | w contains 100 as substring } S → P100P P → 0P | 1P | ε ▸ L = all strings that start and end with the same symbol S → 0P0 | 1P1 | 0 | 1 P → 0P | 1P | ε ▸ L = { w | the length of w is even } S → 0S0 | 0S1 | 1S0 | 1S1 | ε ▸ L = { w | the length of w is odd and its middle symbol is a 0 } S → 0 | 0S0 | 1S0 | 0S1 | 1S1 ▸ L = { w | w is a palindrome } S → ε | 0 | 1 | 0S0 | 1S1 Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 16.
    CFG >> LinkedTerminals – Practice set 1 16 ▸ L = { 0* } S → 0S | ε ▸ L = { 0*1 } S → 0 S | 1 ▸ L = { 0n 1n | n >= 0 } S → 0S1 | ε ▸ L = { 0n 1n | n>=1 } S → 0S1 | 01 ▸ L = { 02n 13n | n>=0 } S → 00S111 | ε ▸ L = { 1n 0* 1n | n >=0 } S → 1 S 1 | P P → 0P | ε ▸ L = { 0n1m | n = m and n, m>=0 } = { 0n 1n | n>=0 } S → 0S1 | ε Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 17.
    CFG >> LinkedTerminals – Practice set 1 17 ▸ { 0n 1m | n>m and n, m>=0 } ; let n=m+k then { 0k0m1m | n, m>=0 and k>0 } S → 0S | 0P P → 0P1 | ε ▸ { 0n 1m | n=2m and n, m>=0 } = { 02m 1m | m>=0 } S → 00S1 | ε ▸ { 0x1y 0z | z=x+y and x, y, z >= 0 } = { 0x 1y 0x+y | x, y >= 0 } = { 0x 1y 0y 0x | x, y >= 0 } S → 0S0 | P P → 1P0 | ε ▸ All strings of the form 0a 1b 0c where a+c=b [ in short: 0a 1a 1c 0c ] S → TU T → 0T1 | ε U → 1U0 | ε ▸ { 0x1y 0z | z=x-y and x, y, z >= 0 } = { 0z+y 1y 0z | y, z >= 0 } = { 0z 0y 1y 0z | y, z >=0 } S → 0S0 | P P → 0P1 | ε Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 18.
    CFG >> LinkedTerminals – Practice set 1 18 ▸ L = { w | w has the same number of 0’s and 1’s } S → 0S1S | 1S0S | ε or, S → SS | 0S1 | 1S0 | ε ▸ L = all strings with more 1’s than 0’s S → T1S | T1T | S1T T → TT | 0T1 | 1T0 | ε ▸ L = all strings with distinct number of 0’s and 1’s S → U | V U → T0U | T0T | U0T V → T1V | T1T | V1T T → TT | 0T1 | 1T0 | ε ▸ L = all strings with an odd number of a’s and an odd number of b’s S → EaEbE | EbEaE E → EaEaE | EbEbE | ε | SS Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 19.
    CFG Design >>Union of CFG’s 19 ▸ Many Context Free Languages are the union of simpler CFLs. ▸ First break the CFL into simpler pieces and construct individual grammars for each piece. ▸ These individual grammars can be easily merged into a grammar for the original language by combining their rules and then adding the new rule, S → S1 | S2 | … … … | Sk where the variables Si , 1 <= i <= k are the start variables for the individual grammars. Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 20.
    CFG >> Unionof CFG’s – Ex. 1 20 CFL = CFL1 U CFL2 = { 0n 1n | n>=0 } U { 1n 0n | n>=0 } CFG1 for CFL1, S1 → 0 S1 1 | ε CFG2 for CFL2, S2 → 1 S2 0 | ε CFG for CFL, S → S1 | S2 S1 → 0 S1 1 | ε S2 → 1 S2 0 | ε Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 21.
    CFG >> Unionof CFG’s – Ex. 2 21 CFL = CFL1 U CFL2 = { ai bj ck | i, j, k >=0 and i=j or j=k } = { ai bj ck | i, j, k >=0 and i=j } U { ai bj ck | i, j, k >=0 and j=k } CFG1 for CFL1 , S2 → S2 c | S1 S1 → a S1 b | ε CFG2 for CFl2, S4 → a S4 | S3 S3 → b S3 c | ε CFG for CFL, S → S2 | S4 S2 → S2 c | S1 S1 → a S1 b | ε S4 → a S4 | S3 S3 → b S3 c | ε Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 22.
    CFG >> Unionof CFG’s – Ex. 3 22 L = { 0n 1m | n != m } = { 0n 1m | n > m } U { 0n 1m | n < m } Soln: CFG for L1 = { 0n 1m | n > m } here n = m + k S1 → 0S1 | 0S2 S2 → 0S21 | ε CFG for L2 = { 0n 1m | n < m } here m = n + k S3 → S31 | S41 S4 → 0S21 | ε Union of these CFG’s, S → S1 | S3 S1 → 0S1 | 0S2 S2 → 0S21 | ε S3 → S31 | S21 Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 23.
    CFG >> LinkedTerminals – Practice set 2 23 ▸ L = { 0n 1m 0m 1n | n, m >=0 } S → 0S1 | C C → 1C0 | ε ▸ L = { an bm ck | n, m, k >=0 and n=m+k } S → aSc | B B → aBb | ε ▸ L = { an bm ck | n, m, k >=0 and n = 2m + 3k } CFG = ? ▸ L = { an bm | 0 <= n <= m <= 2n } S → aSbb | aSb | ε ▸ L = { an bm | 2n <= m <=3n } S → aSbbb | aSbb | ε ▸ L = { an bn | n is not a multiple of 3 } S → ab | aabb | aaaSbbb ▸ L = { an bm ck | k = |n-m| }, Hint: if n >= m then k = n-m otherwise k = m - n CFG =? Mohammad Imam Hossain | Lecturer, Dept. of CSE | UIU
  • 24.
    24 THANKS! Any questions? You canfind me at imam@cse.uiu.ac.bd References: Chapter 2(section 2.1), Introduction to the Theory of Computation, 3rd Edition by Michael Sipser