0% found this document useful (0 votes)
7 views40 pages

Lecture 21

The document discusses decidability in context-free grammars (CFGs), highlighting key undecidable questions and presenting algorithms for decidable questions such as emptiness, finiteness, and membership. It details methods to determine if a CFG generates any strings, whether it is finite or infinite, and how to use the CYK algorithm to check if a specific string can be generated by a CFG. The lecture emphasizes that some questions about context-free languages are undecidable and cannot be resolved by algorithms.

Uploaded by

Asma Ayub
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views40 pages

Lecture 21

The document discusses decidability in context-free grammars (CFGs), highlighting key undecidable questions and presenting algorithms for decidable questions such as emptiness, finiteness, and membership. It details methods to determine if a CFG generates any strings, whether it is finite or infinite, and how to use the CYK algorithm to check if a specific string can be generated by a CFG. The lecture emphasizes that some questions about context-free languages are undecidable and cannot be resolved by algorithms.

Uploaded by

Asma Ayub
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Lecture No 21

CFG: Decidability

Dr. Nazir Ahmad Zafar


Open Questions
Question No 1
• How can we tell whether or not two different context-free
grammars define the same language?
Question No 2
• Given a particular context-free grammar, how can we tell
whether or not it is ambiguous?
Question No 3
• How can we tell whether the complement of a context-free
language is context-free?
Question No 4
• How can we tell whether the intersection of two context-
free languages is context-free?
Dr. Nazir Ahmad Zafar
Open Questions
• There are many questions about context-free
languages that are undecidable
• That is there are no algorithms and there will
never be any algorithms to answer such questions
• It has been proven that algorithms to answer
these questions do not and will not ever exist
• These are topics for advanced computation
theory and will not be discussed in this
introductory course

Dr. Nazir Ahmad Zafar


Decidable Questions
1 Emptiness:
Given a context-free grammar G, does G generate
any strings?
2 Finiteness
Given a context-free grammar G, is the language
generated by G finite or infinite?
3 Membership
Given a context-free grammar G, and a string w, does G
generate w?
We shall consider these questions one at a time
Dr. Nazir Ahmad Zafar
Emptiness and Uselessness

Theorem (1. Emptiness)


Given any context-free grammar, there is an algorithm to
determine whether or not it can generate any words at all.

PROOF:
The proof will be by constructive example.

Dr. Nazir Ahmad Zafar


Proof: Emptiness and Uselessness
• If CFG contains S → Λ, then, CFL is not empty.
• If CFG contains S → t, t is a string of terminals CFL not empty.
• If CFG contains no such production then
– For each non-terminal N with N → t, pick one production for
N (if there are more than one) and replace N by t in the right
side of each production wherever it lies. Remove all such
productions from the CFG. The resultant CFG will be
changed, it will generate at least one word of the old CFL.
– Repeat the above process until either it eliminates S or no new
non-terminal is eliminated.
– If S is eliminated then CFG generates some words otherwise it
generates empty language.

Dr. Nazir Ahmad Zafar


Example 1:
Consider the following CFG which is in CNF
S→XY
X→AX
X→AA
A→a
Y→BY
Y→BB
B→b

Step 1: Replace all A’s by a and all B’s by b. This gives


Dr. Nazir Ahmad Zafar
S→ XY
X→ aX
X→ aa
Y→ bY
Y→ bb

Step 1: Replace all X’s by aa and all Y’s by bb.


S→ aabb
Step 1: Replace all S’s by aabb.
Step 2: Terminate step 1 and discover that S has been eliminated.
Note: Therefore CFG produces at least one word.

Dr. Nazir Ahmad Zafar


Example 2:
Consider this CFG
S→XY
X→AX
A→a
Y→BY
Y→BB
B→b

Step 1: Replace all A’s by a and all B’s by b. This gives

Dr. Nazir Ahmad Zafar


S→ XY
X→ aX
Y→ bY
Y→ bb
Step 1: Replace all Y’s by bb. This gives
S→ Xbb
X → aX
Step 2: Terminate step 1 and discover S is still there.
Note:
This CFG generates no word.

Dr. Nazir Ahmad Zafar


Theorem (Usefulness)
There is an algorithm to decide whether a particular non-
terminal X in a given CFG is ever used in generation of
words
Procedure
• Paint all X’s blue.
• If any non-terminal is in the left side of a production with
any blue nonterminal in right side, paint that nonterminal
blue at all occurrences of it throughout the grammar.
• Repeat step 4 until no new nonterminal is painted.
• If S is blue then X is useful member of CFG, otherwise no
Dr. Nazir Ahmad Zafar
Example: Consider the grammar
S→ ABa
S→ b
A→Xb
B→bAA
X→ aaa
To determine whether X is ever used to generate some words?

Procedure:
• X is blue, so A is blue. Thus B and S are also blue.
• Since S is blue so X can be used to generate certain word from
the given CFG.

Dr. Nazir Ahmad Zafar


2. Finiteness:
Theorem: There is an algorithm to decide whether a context-free
grammar G generates an infinite or a finite language
Procedure: To test whether X is self-embedded
• Change all X’s on the left side of the productions into a Greek
letter Ψ and keep all X’s on the right side as such.
• Paint all X’s blue.
• If Y is any nonterminal on the left side of the production with
X/blue in the right side, then paint Y blue.
• Repeat step until no new nonterminal is painted.
• If Ψ is painted, then the X is self-embedded, otherwise not.
If any nonterminal is self-embedded, language is infinite
Dr. Nazir Ahmad Zafar
2. Finiteness:
Example: Does X is self embedded in the following CFG?
S→ ABa
S→ b
A→ Xb
B→ bAA
X→ bA| aaa
Introduce the Ψ
S→ ABa
S→ b
A→ Xb
B→ bAA
Ψ→ bA| aaa
Dr. Nazir Ahmad Zafar
S→ ABa
S→ b
A→ Xb
B→ bAA
Ψ→ bA| aaa
Now the paint is
X is blue
A→ Xb, so A is blue
Ψ→ bA, so Ψ is blue
B→bAA, so B is blue
S→ ABa, so S is blue
Conclusion:
Ψ is blue, so the language generated by this CFG is infinite.

Dr. Nazir Ahmad Zafar


3. Membership -CYK Algorithm for Deciding CFLs

• CYK (Cocke, Younger, Kasami) Algorithm which tests a


grammar G to see if it can generate string w.
• We will construct the total language tree for G far enough that all
strings of the length of w are generated and search that tree for w.

Theorem: Given a CFG and a sting X in the same alphabet,


we can decide whether or not X can be generated by the CFG.
Example: S→XY
X →XA |a |b
Y → AY| a
A→a

Dr. Nazir Ahmad Zafar


• Total language tree of the above grammar

• In order to decide whether the grammar generates the given


string of length 4, we need to go down the tree far enough that
we have generated all possible strings of length 4.
• Then search the tree for the given string. If we will not find it,
we can answer the membership question "no“ otherwise yes
Dr. Nazir Ahmad Zafar
CYK Algorithm for Deciding CFLs
Example: Consider CFG in CNF
S→ AA
A →AA | a
• The language is of form a* without Λ and a
• Let us use CYK algorithm to test whether X = aaa is generated
Solution:
• The list is as
Substring All Producing Nonterminal
x1 = a A
x2 = a A
x3 = a A
• Both substrings of length two are aa and are factorable into AA.

Dr. Nazir Ahmad Zafar


• This is the right side of two productions whose left sides are S and
A. Therefore the list is as
Substring All Producing Nonterminal
x1x2 S, A
x2x3 S, A
• This is of length 3 substring.
• Therefore the list is as
Substring All Producing Nonterminal
x = x1x2x3 S, A
• From this we see that X can be derived from start symbol S and so
it is in the language.
• It can be seen that any string of a’s length more than 1 can also be
produces by this CFG.

Dr. Nazir Ahmad Zafar


CYK Algorithm for Deciding CFLs

Consider the grammar G given by


S  ε | AB | XB
T  AB | XB
X  AT
Aa
Bb
1. Is x = aaabb in L(G )
2. Is x = aaabbb in L(G )

Dr. Nazir Ahmad Zafar


CYK Algorithm for Deciding CFLs

The algorithm is “bottom-up” in that we start


with bottom of derivation tree.

S  ε | AB | XB
a a a b b
T  AB | XB
X  AT
Aa
Bb

Dr. Nazir Ahmad Zafar


CYK Algorithm for Deciding CFLs

1) Write variables for all length 1 substrings

S  ε | AB | XB a a a b b
T  AB | XB A A A B B
X  AT
Aa
Bb

Dr. Nazir Ahmad Zafar


CYK Algorithm for Deciding CFLs

2) Write variables for all length 2 substrings


a a a b b
S  ε | AB | XB
A A A B B
T  AB | XB
X  AT S,T
T
Aa
Bb

Dr. Nazir Ahmad Zafar


CYK Algorithm for Deciding CFLs

3) Write variables for all length 3 substrings


a a a b b
S  ε | AB | XB
T  AB | XB A A A B B
X  AT S,T
T
Aa
X
Bb

Dr. Nazir Ahmad Zafar


CYK Algorithm for Deciding CFLs

4) Write variables for all length 4 substrings


a a a b b
S  ε | AB | XB
A A A B B
T  AB | XB
X  AT S,T
T
Aa X
Bb
S,T

Dr. Nazir Ahmad Zafar


CYK Algorithm for Deciding CFLs
5) Write variables for all length 5 substrings.

S  ε | AB | XB a a a b b
T  AB | XB
A A A B B
X  AT
Aa S,T
T
Bb X
S,T
REJECT!
X
Dr. Nazir Ahmad Zafar
CYK Algorithm for Deciding CFLs
Now look at aaabbb :

S  ε | AB | XB a a a b b b
T  AB | XB
X  AT
Aa
Bb

Dr. Nazir Ahmad Zafar


CYK Algorithm for Deciding CFLs
1) Write variables for all length 1 substrings.

S  ε | AB | XB a a a b b b
T  AB | XB
A A A B B B
X  AT
Aa
Bb

Dr. Nazir Ahmad Zafar


CYK Algorithm for Deciding CFLs
2) Write variables for all length 2 substrings.

S  ε | AB | XB a a a b b b
T  AB | XB A A A B B B
X  AT S,T
Aa
Bb

Dr. Nazir Ahmad Zafar


CYK Algorithm for Deciding CFLs
3) Write variables for all length 3 substrings.

S  ε | AB | XB a a a b b b
T  AB | XB
A A A B B B
X  AT
Aa S,T
T
Bb X

Dr. Nazir Ahmad Zafar


CYK Algorithm for Deciding CFLs
4) Write variables for all length 4 substrings.

S  ε | AB | XB a a a b b b
T  AB | XB A A A B B B
X  AT S,T
T
Aa
X
Bb
S,T

Dr. Nazir Ahmad Zafar


CYK Algorithm for Deciding CFLs
5) Write variables for all length 5 substrings.

S  ε | AB | XB a a a b b b
T  AB | XB
A A A B B B
X  AT
Aa S,T
T
Bb X
S,T
X
Dr. Nazir Ahmad Zafar
CYK Algorithm for Deciding CFLs
6) Write variables for all length 6 substrings.

S  ε | AB | XB a a a b b b
T  AB | XB A A A B B B
X  AT
Aa
S,T
T
Bb X
S,T
S is included so X
aaabbb accepted!
S,T
Dr. Nazir Ahmad Zafar
CYK Algorithm for Deciding CFLs
Can also use a table for same purpose.
end at 1: 2: 3: 4: 5: 6:
aaabbb aaabbb aaabbb aaabbb aaabbb aaabbb
start at
0:aaabbb
1:aaabbb
2:aaabbb
3:aaabbb
4:aaabbb
5:aaabbb
Dr. Nazir Ahmad Zafar
CYK Algorithm for Deciding CFLs
1. Variables for length 1 substrings.
end at 1: 2: 3: 4: 5: 6:
aaabbb aaabbb aaabbb aaabbb aaabbb aaabbb
start at
0:aaabbb A
1:aaabbb A
2:aaabbb A
3:aaabbb B
4:aaabbb B
5:aaabbb B
Dr. Nazir Ahmad Zafar
CYK Algorithm for Deciding CFLs

2. Variables for length 2 substrings.


end at 1: 2: 3: 4: 5: 6:
aaabbb aaabbb aaabbb aaabbb aaabbb aaabbb
start at
0:aaabbb A -
1:aaabbb A -
2:aaabbb A S,T
3:aaabbb B -
4:aaabbb B -
5:aaabbb B
Dr. Nazir Ahmad Zafar
CYK Algorithm for Deciding CFLs

3. Variables for length 3 substrings.


end at 1: 2: 3: 4: 5: 6:
aaabbb aaabbb aaabbb aaabbb aaabbb aaabbb
start at
0:aaabbb A - -
1:aaabbb A - X
2:aaabbb A S,T -
3:aaabbb B - -
4:aaabbb B -
5:aaabbb B
Dr. Nazir Ahmad Zafar
CYK Algorithm for Deciding CFLs
4. Variables for length 4 substrings.
end at 1: 2: 3: 4: 5: 6:
aaabbb aaabbb aaabbb aaabbb aaabbb aaabbb
start at
0:aaabbb A - - -
1:aaabbb A - X S,T
2:aaabbb A S,T - -
3:aaabbb B - -
4:aaabbb B -
5:aaabbb B
Dr. Nazir Ahmad Zafar
CYK Algorithm for Deciding CFLs
5. Variables for length 5 substrings.
end at 1: 2: 3: 4: 5: 6:
aaabbb aaabbb aaabbb aaabbb aaabbb aaabbb
start at
0:aaabbb A - - - X
1:aaabbb A - X S,T -
2:aaabbb A S,T - -
3:aaabbb B - -
4:aaabbb B -
5:aaabbb B
Dr. Nazir Ahmad Zafar
CYK Algorithm for Deciding CFLs
6. Variables for aaabbb. ACCEPTED!
end at 1: 2: 3: 4: 5: 6:
aaabbb aaabbb aaabbb aaabbb aaabbb aaabbb
start at
0:aaabbb A - - - X S,T
1:aaabbb A - X S,T -
2:aaabbb A S,T - -
3:aaabbb B - -
4:aaabbb B -
5:aaabbb B
Dr. Nazir Ahmad Zafar

You might also like