Vous êtes sur la page 1sur 10

Context Free Languages: Decidability of a CFL Theorem 14.

.1 Statement: Given a CFL L and string w, there is a decision procedure that determines whether w L. Proof: By construction. Two approaches can be taken: 1. Using a grammar 2. Using a PDA

Context Free Languages: Decidability of a CFL (2) 1. Grammar-based proof Algorithm:


Boolean decideCFLUsing Grammar (CFG G, string w) { if (w == epsilon) if (nullable G.S)) return TRUE; else return FALSE; else { G = convertCGFTo CNF(G); G.V = G.V - epsilon; G.Sigma = G.Sigma - epsilon; derivations = {S}; newDerivations = NULL: for (i = 0; i < 2 * length(w) - 1; i++) { for (each d in derivations) for (each s in d) if (s in G.V - G.Sigma) { results = expand(d, s, G); newDerivations = newDerivations + results; } for (each d in newDerivations) if (w == d) return TRUE; derivations = newDerivations; newDerivations = NULL; } return FALSE; } } stringSet expand (string d, symbol s, CFG G) { result = NULL; for (each occurrence of s in d) for (each rule r in G.R) if (r.LHS == s) result = result + substitute(r.RHS, d, s); return result; }

Context Free Languages: Decidability of a CFL (3) Analysis Algorithm requires at most 2 |w| 1 steps because grammar is in CNF Time to build G independent of |w|, so is constant Let n = |w| g = search-branching factor of G : maximum number of rules with same LHS Number of derivations of length 2|w| 1 g 2n1 It takes fewer than 2n 1 steps to test each So, run time O(n2n ) 2. PDA-based proof See below Theorem 14.2 Statement: Given a CFG G = (V, , R, S), there is a PDA M such that L(M ) = L(G) which contains no transitions of the form ((p, , ), (q, )) Proof: By construction (see below). Discussion Algorithm convertCFGToPDA-TD generates PDA M that simulates derivation of a string from the given CFG M = ({p, q}, , V, , p, {q}), where contains 1. A start-up transition ((p, , ), (q, s)) whose sole job is to push S 2. Transitions of the form ((p, , X), (q, s1 s2 ...sn )) for each rule of the form X s1 s2 ...sn These replace X by s1 s2 ...sn in the derivation When n = 0, transition is ((p, , X), (q, )) 3. Transitions of the form ((p, c, c), (q, )) for each c These replace allow consumption of input Transitions of type 1 and type 2 above are -transitions

Context Free Languages: Decidability of a CFL (4) Consider a CFG in GNF 1. Rules containing S are of form S cs1 s2 ...sn , where s1 , s2 , ..., sn V No need to push S Simply read the c and push s1 s2 ...sn 2. Other rules are of form X cs1 s2 ...sn (X = S) Rather than (a) Push c, and then (b) Immediately pop c in next transition (ala sequence of rules of type 2 and 3 above) Can simply read the c and push s1 s2 ...sn Algorithm: PDA CFGToPDANoEPS (CFG G) { G = convertToGNF(G); Delta = NULL; for (each rule r in G.R) if (r of form S cs2 ...sn ) Delta = Delta + ((p, c, ), (q, s2 ...sn )); for (each rule r in G.R) if ((r of form X cs2 ...sn ) AND (X!= S)) Delta = Delta + ((p, c, X), (q, s2 ...sn )); M = ({p, q}, G.Sigma, G.V, Delta, p, {q}); return M; }

Context Free Languages: Decidability of a CFL (5) Theorem 14.3 Statement: Let M be a PDA that contains no -transitions. Consider operations of M on w where |w| = n. 1. M will halt and either accept or reject w. 2. Each computation will halt within n steps. 3. The total number of computations pursued by M is less than or equal to bn , where b is the maximum number of competing transitions from any state of M . 4. The maximum number of steps executed by any computation of m is nbn . Proof: (See p 317) Proof of Theorem 14.1, part 2: Using a PDA Algorithm Boolean decideCFLUsingPDA (CFG G, string w) { if (w == epsilon) if (nullable(G.S) return TRUE; else return FALSE; else { G = convertCFGToGNF(G); G.Sigma = G.Sigma - epsilon; G.V = G.V - epsilon; G = CFGToPDANoEPS(G); return G(w)); } }

Context Free Languages: Decidability of a CFL (6) Analysis M is constructed in constant time (only done once) Let |w| = n Time needed to analyze w is time needed to simulate all paths of M on w Total number of steps bounded by nbn Number of steps could grow exponentially, or be less than b Depends on how the simulation is performed: depth-rst search is O(bn )

Context Free Languages: Decidability of CFL Emptiness and Finiteness Theorem 14.4 Statement: Given CFL L, there is a decision procedure that determines whether 1. L = N U LL 2. L is innite Proof: By construction 1. Emptiness: L(G) is empty if S is unproductive. Boolean decideCFLEmpty (CFG G) { G = removeUnproductive(G); if (G.S not in G.V) return TRUE; else return FALSE; } 2. Non-niteness Let G = (V, , R, S) be CFG that generates L n = |V | b = branching factor of G Longest string that can be generated without repeating a NT has length bn If G generates no strings longer than this, L is nite If G generates one string longer than this, L is innite Cannot apply decideCF L(G, w) to all strings longer than bn , as there are an innite number of them Let t be the shortest string generated by G whose length is bn+1 + bn Pumping Theorem states that t = uvxyz |vy| > 0 uxz L, and |uxz| < |t| Since t is the shortest string whose length is greater than bn+1 + bn |uxz| bn+1 + bn

Context Free Languages: Decidability of CFL Emptiness and Finiteness (2) Since the Pumping Theorem states that |vxy| k (i.e., bn ), no more than bn+1 strings could be pumped out Thus, bn < |uxz| bn+1 + bn If L has any strings longer than bn , it must have at least one of length bn+1 + bn Algorithm: Boolean decideCFLInfinite (CFG G) { lexicographically enumerate all strings W L(G) where bn < |w| bn+1 + bn ; for (each w) if (return decideCFL(G, w)) return TRUE; return FALSE; }

Context Free Languages: Decidability of Equivalence Theorem 14.5 Statement: Given 2 deterministic CFLs L1 and L2 , there is a decision procedure to determine whether L1 = L2 . Proof: (See p 320)

Context Free Languages: Questions That Are Not Decidable The following are not decidable: Given CFLs L1 and L2 dened over alphabet 1. L1 = ? 2. Is L1 s complement a CFL? 3. Is L1 regular? 4. L1 = L2 ? 5. L1 L2 ? 6. l1 L2 = N U LL? 7. Is L1 inherently ambiguous? 8. Is CFG G ambiguous?

10

Vous aimerez peut-être aussi