Screenshot 2024-07-22 at 8.41.47 AM
Screenshot 2024-07-22 at 8.41.47 AM
Context-Free Grammars
Part 1
Context-Free Grammars and Languages
Languages Hierarchy
Recursively Enumerable Languages
In set notation
{ambn | m, n ≥ 0}
Finite No dependency in the
Regular
automata
In regular expression languages
a*b*
(a + b)*
{anbn | n ≥ 0}
{ambn | m = n and m, n ≥ 0} There is dependency
Pushdown (not more than two
Context-free {ambncm+n | m, n > 0}
automata parts of dependencies in
{ambmcndn | m ³ 0, n > 0} the language)
{am+1b*c2m | m >= 0}
Context- There is dependency
{anbncn | n ≥ 0}
sensitive / Turing (more than two parts of
Recursively machine
{am+1bmcn | m = n and m, n >= 0} dependencies in the
enumerable {ww | w ∊ {a, b}*} language)
3
What does dependency means?
• {anbn | n ≥ 0}
• {ambnc2m | m, n > 0}
• {ambmcndn | m ³ 0, n > 0}
• {ambncpdpenfm | m, n, p ³ 0}
• {anbncn | n ≥ 0}
• {anbncndn | n ≥ 0}
• {ambmcndnen | m ³ 0, n > 0}
Theory of Computer Science 4
How to recognize the languages
• Given a set of languages. How to recognize either
the language is a Regular or non-Regular language?
S ® aA S ® aSbb | A
A ® aA | bA | b A ® cA | c
Theory of Computer Science 5
1. a*b*
2. {anbn | n ≥ 0}
3. {am+1bmc2n | m, n >= 0}
4. {amban |m, n ³ 1}
5. {am+1b*cm | m >= 0}
6. {am+1bmcm | m >= 0}
7. {ambn | m = n and m, n ≥ 0}
8. {am+1bnc* | m = n and m, n > 0}
9. {anbmcn+m | n, m > 0}
10. {ambncp | m, n, p ³ 0}
11. {am+1bmcn | m = n and m, n >= 0}
12. {ambncpdq | m = n + p + q; m, n, p, q ³ 0}
13. {am+nbncm | n, m ³ 0}
14. {anb2ncm | m = nTheory
andofn,Computer
m > 0} Science 7
Languages Hierarchy
Recursively Enumerable Languages
10
Formal Definition of CFG
• We can give a formal description to a
particular CFG by specifying each of its
four components, for example,
G = ({S, A}, {0, 1}, P, S) where P
consists of three rules:
S → 0S1 Can write as:
S → 0S1
S→A S → A OR
A→l S → 0S1 | A
S ® AB
A ® aA | l
B ® bB | l
• The terminal symbols are {a, b, l}
• The variable symbols are S, A and
B
Theory of Computer Science 12
Context-Free Grammars
• Grammars are used to generate
strings of a language.
• An A rule can be applied to the
variable A whenever and wherever
it occurs.
• No limitation on applicability of a
rule – it is context free
13
Context-Free Grammars
• CFG have no restrictions on the right-hand side of
production rules. All of the following are valid CFG
production rules:
S ® aSbS S®c
S®λ S ® xyz
S ® ABcDEF S ® MZK
• Notice that there can be a λ, one or more terminals,
one or more non-terminals and any combination of
terminals and non-terminals on the right-hand side
of a production rule.
Theory of Computer Science 14
Generating Strings with CFG
• Generate a string by applying rules
– Start with the initial symbol
– Repeat:
• Pick any non-terminal in the string
• Replace that non-terminal with the right-hand side of
some rule that has that non-terminal as a left-hand
side
• Repeat until all elements in the string are terminals
E.g. : P: S ® AB
Note:
A ® aA | l
This → used in rule
B ® bB | l This ⇒ used in derivation
We can derived string ab as:
S ⇒ AB ⇒ aAB ⇒ alB ⇒ albB ⇒ albl ⇒ ab
15
Generating Strings with CFG
P: S ® aS S ® Bb
B ® cB B®l
• Generating a string:
S replace S with aS
aS replace S with Bb
aBb replace B with cB
acBb replace B with l
acb Final String
Theory of Computer Science 16
Generating Strings with CFG
P: S ® aS S ® Bb
B ® cB B®l
• Generating a string:
S replace S with aS
aS replace S with aS
aaS replace S with Bb
aaBb replace B with cB
aacBb replace B with cB
aaccBb replace B with l
aaccb Final String
Theory of Computer Science 17
Generating Strings with CFG
P: S ® aS S ® Bb
B ® cB B®l
• Regular expression equivalent to
this CFG: a∗c∗b
• Shortest string: S ⇒ Bb ⇒ lb.
Theory of Computer Science 18
How to convert RE to CFG
• General rules:
• a+b
• ab
• a*
Theory of Computer Science 19
How to convert RE to CFG
• General rules:
• a+b S®a|b
• ab S ® aA
A®b
• a* S ® aS | λ
Theory of Computer Science 20
Checkpoint 1 Question
• What is the language of this grammar ?
• S ® aA a(a + b)*b
A ® aA | bA | b
• S ® aA aa*bb*
A ® aA | bB
B ® bB | l
• S ® aS | bA a*bba*
A ® bB
B ® aB | l
21
Checkpoint 2 Question
• What is the grammar of this language ?
• b(a + b)*
• (a + b)*a
• (a + b)+
22
Derivation
• A derivation is a listing of how a string is generated –
showing what the string looks like after every
replacement.
S ® AB S ⇒ AB
A ® aA | l ⇒ aAB
B ® bB | l ⇒ aAbB
⇒ abB
⇒ abbB
⇒ abb
30
CFG – Generating strings
• E.g. : G = ({S, A}, {a, b}, P, S)
P: S ® AA
A ® AAA | bA | Ab | a
• The following strings can be derived:
S ⇒ AA
S ⇒ aA rule [A ® a] Shortest
S ⇒ aAAA rule [A ® AAA] string ?
S ⇒ abAAA rule [A ® bA]
S ⇒ abaAA rule [A ® a]
S ⇒ abaAbA rule [A ® Ab]
S ⇒ abaabA rule [A ® a]
S ⇒ abaaba rule [A ® a]
31
CFG – Generating strings
G = (V, S, P, S), V = {S, A}, S = {a, b},
P: S ® AA
A ® AAA | bA | Ab | a
• Four distinct derivations of ababaa in G:
(a) S ⇒ AA (b) S ⇒ AA (c) S ⇒ AA (d) S ⇒ AA
⇒ aA ⇒ AAAA ⇒ Aa ⇒ aA
⇒ aAAA ⇒ aAAA ⇒ AAAa ⇒ aAAA
⇒ abAAA ⇒ abAAA ⇒ AAbAa ⇒ aAAa
⇒ abaAA ⇒ abaAA ⇒ AAbaa ⇒ abAAa
⇒ ababAA ⇒ ababAA ⇒ AbAbaa ⇒ abAbAa
⇒ ababaA ⇒ ababaA ⇒ Ababaa ⇒ ababAa
⇒ ababaa ⇒ ababaa ⇒ ababaa ⇒ ababaa
(a) & (b) leftmost, (c) rightmost, (d) arbitrary
32
Context-Free Grammars
S ⇒ aS ⇒ aSa ⇒ aba
• Sentencial forms are the strings derivable
from start symbol of the grammar.
• Sentences are forms that contain only
terminal symbols.
• A set of strings over S is context-free
language if there is a context-free grammar
that generates it.
33
Derivation Tree, DT
G = ({S, A}, {a, b}, P, S)
P: S ® AA
A ® AAA | bA | Ab | a
36
End of Part 1
Chapter 4
Context-Free Grammars
Chapter 4
Context-Free Grammars
Part 2
Structures of the Language
Regular Grammars
Structures of the Language
Three structures of languages
1. Recursive Structure
2. Two-parts Structure
3. Nested Structure
47
Example
• A string w is a palindrome if w = wR
• The set of palindrome over {a, b}
can be derived using rules:
S®a|b|l
S ® aSa | bSb
48
Example of CFG
• RE ab*a RG S ® aA
A ® bA
A®a
Theory of Computer Science 58
RE ⇒ RG
59
How to construct RG from an FA
• Use a new non-terminal for every new
character
• Each loop state turns into a recursive
definition on a non-terminal
• Example: F.A. =
Regular Grammar = S -> aA
A -> bA
A -> a
Sept2011 Theory of Computer Science 60
How to convert FA to RG
A ® aB | l
B ® aB | bC
C ® aB | bC | l.
Chapter 4
Context-Free Grammars