Download presentation
Presentation is loading. Please wait.
Published byArron Gaines Modified over 9 years ago
1
Normal Forms for Context-Free Grammars Definition: A symbol X in V T is useless in a CFG G=(V, T, P, S) if there does not exist a derivation of the form S wXy * wxy where w, x, y are in T *. Definition: A symbol X in V T is inaccessible in a CFG G = (V, T, P, S) if X does not appear in any sentential form. (inaccessible implies not reachable) Definition: A symbol X is generating if X w for some w in T *.
2
Algorithm (NF1) (Is L(G) empty?) Input: A CFG G = (V, T, P, S). Output: “YES” if L(G) = , “NO” otherwise. Method: Construct sets N 0, N 1, …recursively as follows: 1.N 0 = ; i = 1; 2.N i = {A | A in P and in (N i-1 T) * } N i-1 ; 3. If N i N i-1 then begin i = i +1; go to 2; end; 4.N e = N i ; 5.If S is in N e then output “NO” else output “YES”.
3
Example: S AA | AB A SA | AB B BB | b
4
Algorithm (NF2): (Removal of inaccessible symbols) Input: A CFG G = (V, T, P, S). Output: CFG G = (V, T, P, S) such that (1) L(G) = L(G), (2) No X in V T is unreachable. Method: Construct sets N 0, N 1, …recursively as follows: 1.N 0 = {S}; i = 1; 2.N i = {X | some A X is in P and A in N i-1 } N i-1 ; 3.If N i N i-1 then begin i=i+1; go to 2; end; 4.V = N i V; T = N i T; P = those productions of P with symbols from N i ; S = S;
5
Algorithm (NF3): (Useless Symbol Removal) Input: A CFG G = (V, T, P, S). Output: CFG G = (V, T, P, S) such that (1) L(G) = L(G), (2) No X in V T is useless. Method: 1.Apply algorithm NF1 to G to get N e. 2.Let G 1 = (V N e, T, P 1, S) where P 1 contains those productions of P involving only symbols from N e T. 3.Apply algorithm NF2 to G 1 to obtain G = (V, T, P, S)
6
Example: S a | A A AB B b
7
Definitions: 1.A is called -production 2.A is called A-production 3.A B where A and B are in V is called unit production (or single production). 4.A variable A is nullable if A . 5.A CFG G = (V, T, P, S) is -free if either (i) P has no -productions, or (ii) there is exactly one -production S and S does not appear on the right side of any production in P.
8
Algorithm (NF4): Conversion to -free grammar Input: A CFG G = (V, T, P, S). Output: Equivalent -free CFG G = (V, T, P, S) Method: 1)Construct N e = {A | A in V and A + } 2)If A 0 B 1 1 B 2 2 … B k k is in P, k 0 and for 1 i k each B i in N e but no symbol in any j is in N e, 0 j k, add to P all productions of the form A 0 X 1 1 X 2 2 … X k k where X i is either B i or , without adding A to P 3)If S is in N e add to P the productions S | S where S is a new symbol and set V = V {S}. Otherwise set V = V and S =S. 4)G =(V, T, P, S)
9
Example: S aSbS | bSaS |
10
Algorithm (NF5): Removal of unit productions Input: An -free CFG G = (V, T, P, S). Output: Equivalent -free CFG G = (V, T, P, S) Method 1.Construct for each A in V the sets N A = {B | A * B} as follows: (a) N 0 = {A}; i = 1; (b) N i = {C | B C is in P and B in N i-1 } N i-1 (c)If N i N i-1 then i=i+1; go to (b); else N A = N i 2. Construct P as follows: If B is in P and not a unit production, place A in P for A such that B is in N A. 3. G = (V, T, P, S)
11
Example: E E + T | T T T * F | F E ( E ) | a
12
Definition: A CFG G = (V, T, P, S) is said to be cycle-free if there is no derivation of the form A * A for any A in V. G is said to be proper it it is cycle-free, -free, and has no useless symbols. -free and no unit productions imply cycle-free.
13
Chomsky Normal Form A CFG G = (V, T, P, S) is said to be in Chomsky Normal Form (CNF) if each production in P is one of the forms (1)A BC with A, B, C in V, or (2)A a with a in T, or (3)If is in L(G), then S is a production and S does not appear on the right side of any production
14
Algorithm (NF6) Conversion to CNF Input: A proper CFG G=(V, T, P, S) with no single production Output: A CFG G 1 =(V 1, T, P 1, S) such that L(G) = L(G 1 ) Method: From G, construct G 1 as follows: 1)Add each production of the form A → a in P to P 1 2)Add each production of the form A → BC in P to P 1 (A, B, C are non terminals)
15
3) If S → is in P, add S → to P 1 4) For each production of the form A → X 1 …X k in P, k > 2, add to P 1 the following productions: A → Y 1 → Y 2 … → Y k-1 Y k where Y j stands for X i if X i is a nonterminal otherwise it is a new nonterminal added to V 1
16
5) For each production of the form A → X 1 X 2 where either X 1 or X 2 or both are terminals add to P 1 the production A → Y 1 Y 2 6) For each terminal a replaced by a Y, add the production Y → a
17
Example: S bA | aB A bAA | aS | a B aBB | bB | b
18
Greibach Normal Form Recursive, left-recursive, right-recursive: A nonterminal A in a CFG G = (V,T,P,S) is said to be recursive if A + A for some and . If = , then A is left-recursive, if = , then A is right-recursive. A grammar with at least one left-(right-) recursive nonterminal is said to be left- (right-) recursive
19
Lemma: Let G = (V,T,P,S) be a CFG in which A → A 1 | …|A m | 1 | … | n are all the A-productions in P and no begins with A. Let G 1 = (V {B}, T, P 1, S) where P 1 is P with the above productions are replaced by A → 1 | …| n | 1 B| … | n B B → 1 | … | m | 1 B | …| m B Then L(G) = L(G 1 )
20
Algorithm – elimination of left recursion. Input: A proper CFG G=(V,T,P,S) Output: A CFG G 1 with no left-recursion Method: Let V = {A 1,..., A n }. Transform G so that if A i → is a production, then begins with either a terminal of a symbol A j such that j > i. this can be accomplished by substitution for lower numbered productions and eliminating immediate left-recursion.
21
Example: A → BC | a B → CA | Ab C → AB | CC | a
22
Definition: A CFG G = (V,T,P,S) is said to be in Greibauch normal form (GNF) if G is - free and each non -production is of the form A → a where a is a terminal and is a string in V*.
23
Lemma: Let G=(V,T,P,S) be a non-left recursive grammar. Then there is linear order < on V such that A → B is in P, the A < B. Proof: Let R be a relation A R B if and only if A * B for some . R is a partial order Extend R to a linear order
24
Algorithm: Conversion to GNF Input: G=(V,T,P,S), G non-left-recursive, proper CFG Output: A CFG G 1 in GNF Method: 1) Construct a linear order on V 2) Set i= n-1 3) If i=0 go to step 5. Otherwise replace each production of the form A i → A j , j>i, by A i → 1 | …| m where A j → 1 | …| m are all the A j -productions ( i ’s begin with terminal)
25
4) Set i=i-; go to step 2 5) For each production A → aX 1 …X k, if X j is a terminal replace it by a nonterminal Y j and add a production Y j → X j E → T | TE 1 E 1 → +T | +TE 1 T → F | FT 1 T 1 → *F | *FT 1 F → (E) | a
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.