Docsity

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

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}