CS411-2010F-08 Context-Free Grammars 1
CS411-2010F-08 Context-Free Grammars 1
S → aS
S → Bb
B → cB
B→ǫ
Regular Expression equivalent to this CFG:
a ∗ c∗ b
08-6: CFG Example
CFG for L = {0n 1n : n > 0}
08-7: CFG Example
CFG for L = {0n 1n : n > 0}
S → 0S1 or S → 0S1|01
S → 01
08-9: Derivation
A Derivation is a listing of how a string is generated – showing what the string looks like after every replacement.
S → AB
A → aA|ǫ
B → bB|ǫ
S ⇒ AB
⇒ aAB
⇒ aAbB
⇒ abB
⇒ abbB
⇒ abb
08-10: Parse Tree
A Parse Tree is a graphical representation of a derivation.
CS411-2010F-08 Context-Free Grammars 3
A B
a A b B
S ⇒ AB ε b B
⇒ aAB
⇒ aAbB ε
⇒ abB
⇒ abbB
⇒ abb
08-11: Parse Tree
A Parse Tree is a graphical representation of a derivation.
S
A B
a A b B
A ε
S ⇒ AB a A
⇒ AbB
⇒ aAbB ε
⇒ aaAbB
⇒ aaAb
⇒ aab
08-12: Fun with CFGs
• Create a Context-Free Grammar for all strings over a,b which contain the substring “aba”
• Create a Context-Free Grammar for all strings over a,b which contain the substring “aba”
S → AabaA
A →aA
A →bA
A→ǫ
08-14: LCF G
The Context-Free Languages, LCF G , is the set of all languages that can be described by some CFG:
• LREG ⊂ LCF G ?
CS411-2010F-08 Context-Free Grammars 4
• Prove by induction that, given any regular expression r, we create a CFG G such that L[G] = L[r]
• Given any NFA M , we create a CFG G such that L[G] = L[M ]
• To Prove: Given any regular expression r, we can create a CFG G such that L[G] = L[r]
• By induction on the structure of r
• r = a, a ∈ Σ
• r = a, a ∈ Σ
S→a
08-19: LREG ⊆ LCF G
Base Cases:
• r=ǫ
• r=ǫ
S→ǫ
08-21: LREG ⊆ LCF G
Base Cases:
• r=∅
• r=∅
S → SS
08-23: LREG ⊆ LCF G
Recursive Cases:
• r = (r1 r2 )
• r = (r1 r2 )
• r = (r1 + r2 )
• r = (r1 + r2 )
• r = (r1∗ )
• r = (r1∗ )
• Create a grammar
• G = (V, Σ, R, S) such that L[G] = L[M ]
• Idea: Derivations like “backward NFA configurations”, showing past instead of future
• Example for all strings over {a, b} that contain aa, not bb
• M = (K, Σ, ∆, s, F )
• G = (V, Σ′ , R, S)
• V
• Σ′
• R
• S
• M = (K, Σ, ∆, s, F )
• G = (V, Σ′ , R, S)
S
• V =K Σ
• Σ′ = Σ
• R = {(q1 → aq2 ) : q1 , q2 ∈ K (and V ),
a ∈ Σ, ((q1 , a), q2 ) ∈ ∆}∪
{(q → ǫ) : q ∈ F }
• S=s
(Example)
08-32: CFG – Ambiguity
• A CFG is ambiguous if there exists at least one string generated by the grammar that has ¿ 1 different parse tree
S → AabaA
A →aA
• Previous CFG is ambiguous (examples)
A →bA
A→ǫ
E
E
E * E
E + E
E E
E + E N
N *
3 N N N N 5
4 5 3 4
08-35: CFG – Ambiguity
E → E + E|E − E|E ∗ E|N
N → 0|1|2|3|4|5|6|7|8|9
• If all we care about is removing ambiguity, there is a (relatively) easy way to make this unambiguous (make all
operators right-associative)
• Ambiguous:
E → E + E|E − E|E ∗ E|N
N → 0|1|2|3|4|5|6|7|8|9
• Unambiguous:
E → E + T |E − T |T
T → T ∗ N |N
N → 0|1|2|3|4|5|6|7|8|9
Can add parentheses, other operators, etc. (More in Compilers)
08-38: Fun with CFGs
• Create a CFG for all strings over {(,)} that form balanced parenthesis
• ()
CS411-2010F-08 Context-Free Grammars 8
• ()()
• (()())((()()()))
• ((((()))))
• Create a CFG for all strings over {(,)} that form balanced parenthesis
S → (S)
S → SS
S→ǫ
• Is this grammar ambiguous?
• Create a CFG for all strings over {(,)} that form balanced parenthesis
S → (S)
S → SS
S→ǫ
• Is this grammar ambiguous?
• YES! (examples)
• Create an unambiguous CFG for all strings over {(,)} that form balanced parenthesis
• Create an unambiguous CFG for all strings over {(,)} that form balanced parenthesis
S → AS
S→ǫ
A → (S)
08-43: Ambiguous Languages
• L3 is inherently ambiguous
S → S1 |S2
S1 → AB
A → aAb|ab
B → cBd|cd
S2 → aS2 d|aCd
C → bCc|bc
What happens when i = j? 08-45: (More) Fun with CFGs
• Create an CFG for all strings over {a, b} that have the same number of a’s as b’s (can be ambiguous)
• Create an CFG for all strings over {a, b} that have the same number of a’s as b’s (can be ambiguous)
S → aSb
S → bSa
S → SS
S→ǫ
08-47: (More) Fun with CFGs
S → aSa
S → bSb 08-49: (More) Fun with CFGs
S→ǫ
• Create an CFG for all palindromes over {a, b}. That is, create a CFG for:
• L = {w : w ∈ (a + b)∗ , w = wR }
• Create an CFG for all palindromes over {a, b}. That is, create a CFG for:
• L = {w : w ∈ (a + b)∗ , w = wR }
S → aSa
S → bSb
S →ǫ 08-51: (More) Fun with CFGs
S →a
S →b
HINT: We may wish to break this down into 3 different langauges ...
08-53: (More) Fun with CFGs
CS411-2010F-08 Context-Free Grammars 10
S → ABC
A → aAb
A→ǫ
B → bB
B→b
C → bCc|ǫ
08-54: (More) Fun with CFGs
• Create an CFG for all strings over {0, 1} that have the an even number of 0’s and an odd number of 1’s.
• HINT: It may be easier to come up with 4 CFGs – even 0’s, even 1’s, odd 0’s odd 1’s, even 0’s odd 1’s, odd
1’s, even 0’s – and combine them ...
• Create an CFG for all strings over {0, 1} that have the an even number of 0’s and an odd number of 1’s.
S1 → 0S3 |1S2
S2 → 0S4 |1S1
S3 → 0S1 |1S4
S4 → 0S2 |1S3