Context-Free Grammars: Simplification and Normal Forms, Study notes of Design and Analysis of Algorithms

How to simplify context-free grammars by applying substitution rules, removing useless productions, and eliminating ε-productions and unit-productions. It also introduces chomsky normal form and greibach normal form, which are important normal forms for context-free grammars.

Typology: Study notes

2010/2011

Uploaded on 09/01/2011

visir66
visir66 🇮🇳

4.4

(74)

97 documents

1 / 37

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Simplification of Context-Free
Grammars
Some useful substitution rules.
Removing useless productions.
Removing -productions.
Removing unit-productions.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25

Partial preview of the text

Download Context-Free Grammars: Simplification and Normal Forms and more Study notes Design and Analysis of Algorithms in PDF only on Docsity!

Simplification of Context-Free

Grammars

  • (^) Some useful substitution rules.
  • (^) Removing useless productions.
  • (^) Removing -productions.
  • (^) Removing unit-productions.

Some Useful Substitution Rules

G = (V, T, S, P)

A  x 1 Bx 2  P B  y 1 | y 2 | ... | yn  P L(G) = L(G^) G^ = (V, T, S, P^) A  x 1 y 1 x 2 | x 1 y 2 x 2 | ... | x 1 ynx 2  P^

Some Useful Substitution Rules

G = (V, T, S, P)

A  Ax 1 | Ax 2 | ... | Axn  P A  y 1 | y 2 | ... | ym  P L(G) = L(G^) G^ = (V{Z}, T, S, P^) A  yi | yiZ (i =1, m)  P^ Z  xi | xiZ (i =1, n)  P^

Example

G = ({A, B}, {a, b}, A, P) A  Aa | aBc |  B  Bb | ba A  aBc | aBcZ | Z |  A  aBc | aBcZ | Z |  Z  a | aZ Z  a | aZ B  Bb | ba B  ba | baY Y  b | bY

Removing Useless Productions

G = (V, T, S, P)

A  V is useful iff there is w  L(G) such that: S * xAy * w A production is useless it it involves any useless variable.

Example

G = ({S, A, B}, {a, b}, S, P) S  A A  aA |  B  bA

Example

G = ({S, A, B, C}, {a, b}, S, P) S  aS | A | C S  aS | A S  aS | A A  a A  a A  a B  aa B  aa C  aCb dependency graph S A B

Theorem

Let G = (V, T, S, P) be a context-free grammar. Then there exists an equivalent grammar G^ = (V^, T^, S, P^) that does not contain any useless variables or productions.

Theorem

Proof:

  • Construct (V 1 , T, S, P 1 ) such that V 1 contains only variables A for which A * w  T. 1. Set V 1 to . 2. Repeat until no more variables are added to V 1 : For every A  T for which P has a production of the form A  x 1 x 2 ... xn (xi  TV 1 ) add A to V 1. 3. Take P 1 as all the productions in P with symbols in (V 1  T)*.

Theorem

Proof:

  • Draw the variable dependency graph for G 1 and find all variables that cannot be reached from S.
  • (^) Remove those variables and the productions involving them.
  • (^) Eliminate any terminal that does not occur in a useful production.  G^ = (V^, T^, S, P^)

Example

S  aS 1 b S  aS 1 b | ab S 1  aS 1 b |  S 1  aS 1 b | ab

Theorem

Let G = (V, T, S, P) be a CFG such that   L(G). Then there exists an equivalent grammar G^ having no -productions.

Theorem

Proof:

  • (^) For each production in P of the form: A  x 1 x 2 ... xm (m  1, xi  VT) put into P^ that production as well as all those generated by replacing null variables with  in all possible combination. Exception: if all xi are nullable, then A   is not put into P^.

Example

S  ABaC S  ABaC | BaC | AaC | ABa | aC | Aa |Ba | a A  BC A  B | C | BC B  b |  B  b C  D |  C  D D  d D  d VN = {A, B, C}