Conversion of a CFG into Greibach Normal Form
Greibach Normal Form
Definition Greibach Normal Form (GNF)
A CFG is in Greibach Normal Form if all productions are of the form
A ? a X
with aIT and XIV*.
This means, all productions start with a terminal on the PRODUCTION and have only variables following.
Theorem
For every CFG G with ??L(G) exists an equivalent grammar G in Greibach NF.
Proof and algorithm
Firstly, we convert the given grammar into Chomsky NF:
Start with a grammar G = ( V, T, P, S)
Eliminate useless variables that cannot become terminals.
Eliminate useless variables that cannot be reached.
Eliminate ?-productions.
Eliminate unit productions.
Convert grammar to Chomsky Normal Form.
Then, we convert this grammar into an equivalent grammar in Greibach NF.
Convert a CNF grammar into Greibach Normal Form:
0. Re-label all variables such that the names are A1, A2, ... , Am.
1. We want to order the productions, which are not terminal but contain variables. We use for this purpose the indexing of the variables, so that
Ai ? Aj ? with i<j for all i = 1,...,m and j = 2, ..., m
We perform the ordering process by substitution of the first variable on the PRODUCTION, if the production violates the condition above (see 2 and 3).
2. We start the ordering with A1.
A1-productions can have only a higher numbered variable as first variable on the PRODUCTION, or a single terminal.
3. We assume now that all rules are okay up to Ak-1. The next rule we encounter, with Ak on the production, is the first one, which is not okay:
Ak ? Al ? with k>l
We resolve this problem by substituting Al. Since l<k, the Al-rules have already gone through the sorting process and are in the proper format:
Al ? Aj ? with l<j
Now, we substitute the PRODUCTIONs of Al in the Ak-rule, and come up with:
Ak ? Al ? or Ak ? a for some a
If l is still less than k, we substitute again. And again and again, until we get at least Ak on the PRODUCTION - all rules up to Ak-1 are already sorted and in proper form, and the first variable on the PRODUCTION of the Ak-1-production must be therefore at least Ak.
EXAMPLE 1: TRANSFORM A CFG INTO GREIBACH NORMAL FORM
Bring the grammar G with V={S, A, B}, T={a, b} and productions P
S ? A
A ? a B a | a
B ? b A b | b
into GNF.
Solution:
1. Simplify G:
No useless variables or productions, no ?-productions.
Remove unit-production S ? A:
Replace A with PRODUCTION of A (after calculating transitive closure of unit-productions - but there is only one unit-dependency here A ? B):
S ? a B a | a new rule
2. Transform G into an equivalent grammar G in Chomsky NF:
Substitute terminals on PRODUCTION with variables:
S ? C1 B C1 | a C1 ? a
A ? C1 B C1 | a C2 ? b
B ? C2 A C2 | b
Break down the rules:
S ? C1 D1 | a D1 ? B C1 C1 ? a
A ? C1 D1 | a
B ? C2 D2 | b D2 ? A C2 C2 ? b
3. Transform G into equivalent grammar G in Greibach NF:
a. Rename the variables to V1, V2, ... in the productions P :
S=V1; A=V2; B=V3; C1=V4; C2=V5; D1=V6; D2=V7
V1 ? V4 V6 | a V6 ? V3 V4 V4 ? a
V2 ? V4 V6 | a
V3 ? V5 V7 | b V7 ? V2 V5 V5 ? b
b. Order the productions:
V1 ? V4 V6 | a
V2 ? V4 V6 | a
V3 ? V5 V7 | b
V4 ? a
V5 ? b
V6 ? V3 V4
V7 ? V2 V5
Rules for V1, V2, V3, V4, V5 are okay.
Modify V6-rule V6 ? V3 V4:
Substitute PRODUCTIONs of V3 in V6-rule:
V6 ? V5 V7 V4 | b V4
Substitute PRODUCTIONs of V5 in modified V6-rule:
V6 ? b V7 V4 | b V4 final V6-rule
Modify V7-rule V7 ? V2 V5:
Substitute PRODUCTIONs of V2 in V7-rule:
V7 ? V4 V6 V5 | a V5
Substitute PRODUCTIONs of V4 in modified V7-rule:
V7 ? a V6 V5 | a V5 final V7-rule
c. Substitute to achieve Greibach Normal Form:
Everything already done in this case. Grammar is in GNF:
V1 ? V4 V6 | a
V2 ? V4 V6 | a
V3 ? V5 V7 | b
V4 ? a
V5 ? b
V6 ? b V7 V4 | b V4
V7 ? a V6 V5 | a V
DONE
Instructor: Mohamed U. Mahdi
المادة المعروضة اعلاه هي مدخل الى المحاضرة المرفوعة بواسطة استاذ(ة) المادة . وقد تبدو لك غير متكاملة . حيث يضع استاذ المادة في بعض الاحيان فقط الجزء الاول من المحاضرة من اجل الاطلاع على ما ستقوم بتحميله لاحقا . في نظام التعليم الالكتروني نوفر هذه الخدمة لكي نبقيك على اطلاع حول محتوى الملف الذي ستقوم بتحميله .