Algorithms and Decision Procedures For Context-Free Languages
Algorithms and Decision Procedures For Context-Free Languages
Procedures for
Context-Free Languages
Chapter 14
Decision Procedures for CFLs
Membership: Given a language L and a string w, is w in L?
Two approaches:
● If L is context-free, then there exists some context-free
grammar G that generates it. Try derivations in G and see
whether any of them generates w.
Problem:
Normal Forms for Grammars
Chomsky Normal Form, in which all rules are of one of the
following two forms:
● X a, where a , or
● X BC, where B and C are elements of V - .
Advantages:
● Parsers can use binary trees.
● Exact length of derivations is known:
A B
A A B B
a a b B B
b b
Conversion to Chomsky Normal Form
For any context-free grammar G, there exists a context-free
grammar G’ in Chomsky normal form such that L(G) = L(G) – {}.
Algorithm:
removeEps(G: cfg) =
1. Let G = G.
2. Find the set N of nullable variables in G.
3. For each modifiable rule P R of G do
Add the rule P .
4. Delete from G all rules of the form X .
5. Return G.
removeUnits(G)
1. Let G = G.
2. Until no unit productions remain in G do:
2.1 Choose some unit production X Y.
2.2 Remove it from G.
2.3 Consider only rules that still remain.
For every rule Y , where V*, do:
Add to G the rule X unless it is a rule
that has already been removed once.
3. Return G.
Example
S aACa | aAa | aCa | aa
AB|a
BC|c
C cC | c
Remove A B. Add A C | c.
Remove B C. Add B cC (B c, already there).
Remove A C. Add A cC (A c, already there).
So removeUnits returns:
S aACa | aAa | aCa | aa
A a | c | cC
B c | cC
C cC | c
Mixed Rules
removeMixed(G) =
1. Let G = G.
2. Create a new nonterminal Ta for each terminal a in .
3. Modify each rule whose right-hand side has length greater
than 1 and that contains a terminal symbol by substituting
Ta for each occurrence of the terminal a.
4. Add to G, for each Ta, the rule Ta a.
5. Return G.
Example
S aACa | aAa | aCa | aa
A a | c | cC
B c | cC
C cC | c
removeMixed returns:
removeLong returns:
S TaS1 S TaS3 S TaS4 S TaTa
S1 AS2 S3 ATa S4 CTa
S2 CTa
A a | c | TcC
B c | TcC
C TcC | c
Using a Grammar
decideCFLusingGrammar(L: CFL, w: string) =
3. If w then:
3.1 Construct G in Chomsky normal form such that
L(G) = L(G) – {}.
1. Let G = removeunproductive(G).
● Is L regular?
● Is L1 = L2?
● Is L1 L2?
● Is L1 L2 = ?
● Is L inherently ambiguous?
● Is G ambiguous?