CS 3240 – Chapter 6. 6.1: Simplifying Grammars Substitution Removing useless variables ...

37
CS 3240 – Chapter 6

Transcript of CS 3240 – Chapter 6. 6.1: Simplifying Grammars Substitution Removing useless variables ...

Page 1: CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.

CS 3240 – Chapter 6

Page 2: CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.

6.1: Simplifying Grammars Substitution Removing useless variables Removing λ Removing unit productions

6.2: Normal Forms Chomsky Normal (CNF)

6.3: A Membership Algorithm CYK Algorithm

An example of bottom-up parsing

CS 3240 - Normal Forms for Context-Free Languages 2

Page 3: CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.

Variables in CFGs can often be eliminated If they are not recursive, you can

substitute their rules throughout See next slide

CS 3240 - Normal Forms for Context-Free Languages 3

Page 4: CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.

CS 3240 - Normal Forms for Context-Free Languages 4

A ➞ a | aaA | abBcB ➞ abbA | b

Just substitute directly for B:

A ➞ a | aaA | ababbAc | abbc

Page 5: CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.

A variable is useless if: It can’t be reached from the start state,

or

It never leads to a terminal string▪ Due to endless recursion

Both problems can be detected by a dependency graph See next slide

CS 3240 - Normal Forms for Context-Free Languages 5

Page 6: CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.

CS 3240 - Normal Forms for Context-Free Languages 6

S ➞ aSb | A | λA ➞ aA

A is useless (non-terminating):

S ➞ aSb | λ

Page 7: CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.

CS 3240 - Normal Forms for Context-Free Languages 7

S ➞ AA ➞ aA | λB ➞ bA

B is useless (non-reachable):

S ➞ AA ➞ aA | λ

Page 8: CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.

CS 3240 - Normal Forms for Context-Free Languages 8

Simplify the following:

S ➞ aS | A | CA ➞ aB ➞ aaC ➞ aCb

Page 9: CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.

CS 3240 - Normal Forms for Context-Free Languages 9

Page 10: CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.

CS 3240 - Normal Forms for Context-Free Languages 10

Simplify the following grammar:

S ➞ AB | ACA ➞ aAb | bAa | aB ➞ bbA | aaB | ABC ➞ abCa | aDbD ➞ bD | aC

Page 11: CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.

Any variable that can eventually terminate in the empty string is said to be nullable Note: a variable may be indirectly

nullable In general: if A ➞ V1V2…Vn, and all the Vi

are nullable, then A is also nullable▪ See Theorem 6.3

CS 3240 - Normal Forms for Context-Free Languages 11

Page 12: CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.

CS 3240 - Normal Forms for Context-Free Languages 12

Consider the following grammar:

S ➞ a | Xb | aYaX ➞ Y | λY ➞ b | X

Which variables are nullable?

How can we substitute the effect of λ before removing it?

Page 13: CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.

First find all nullable variablesThen substitute (A + λ) for every

nullable variable A, and expand Then remove λ everywhere from the

grammarWhat’s left is equivalent to the

original grammar except the empty string may be lost we won’t worry about that

CS 3240 - Normal Forms for Context-Free Languages 13

Page 14: CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.

CS 3240 - Normal Forms for Context-Free Languages 14

Consider the following grammar (again):

S ➞ a | Xb | aYaX ➞ Y | λY ➞ b | X

How can we substitute the effect of λ before removing it?

Page 15: CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.

S → aSbS | bSaS | λ S → aSa | bSb | XX → aYb | bYaY → aY | bY | λ

CS 3240 - Normal Forms for Context-Free Languages 15

Page 16: CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.

Unit Productions often occur in chains A ➞ B ➞ C

Must maintain the effect of B and C when substituting for A throughout

Procedure: Find all unit chains Rebuild grammar by:

▪ Keeping all non-unit productions▪ Keeping only the effect of all unit

productions/chains

CS 3240 - Normal Forms for Context-Free Languages 16

Page 17: CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.

CS 3240 - Normal Forms for Context-Free Languages 17

Page 18: CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.

1) Determine all variables reachable by unit rules for each variable

2) Keep all non-unit rules

3) Substitute non-unit rules in place of each variable reachable by unit productions

CS 3240 - Normal Forms for Context-Free Languages 18

Page 19: CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.

CS 3240 - Normal Forms for Context-Free Languages 19

S ➞ aAA ➞ BBB ➞ aBb | λ

Now remove nulls and see what happens….

(Also see the solution for #15 in 6.1)

Page 20: CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.

CS 3240 - Normal Forms for Context-Free Languages 20

S ➞ ABA ➞ BB ➞ aB | BB | λ

Page 21: CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.

Do things in the following recommended order: Remove nulls Remove unit productions Remove useless variables Simplify by substitution as desired

CS 3240 - Normal Forms for Context-Free Languages 21

Page 22: CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.

Very important for our purposesAll CNF rules are of one of the

following two forms: A ➞ c (a single terminal) A ➞ XY (exactly two variables)

Must begin the transformation after simplifying the grammar (removing λ, all unit productions, useless variables, etc.)

CS 3240 - Normal Forms for Context-Free Languages 22

Page 23: CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.

CS 3240 - Normal Forms for Context-Free Languages 23

Convert to CNF:

S ➞ bA | aBA ➞ bAA | aS | aB ➞ aBB | bS | b

(NOTE: already has no nulls or units)

Page 24: CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.

CS 3240 - Normal Forms for Context-Free Languages 24

Convert the following grammar to CNF:

S ➞ abABA ➞ bAB | λB ➞ BAa | A | λ

Page 25: CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.

CS 3240 - Normal Forms for Context-Free Languages 25

Convert the following grammar to CNF:

S ➞ aS | bS | BB ➞ bb | C | λC ➞ cC | λ

Page 26: CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.

Single terminal character followed by zero or more variables (cV*, c ∈ Σ )

V → aV → aBCD…λ allowed only in S → λSometimes need to make up new

variable names

CS 3240 - Normal Forms for Context-Free Languages 26

Page 27: CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.

CS 3240 - Normal Forms for Context-Free Languages 27

S → ABA → aA | bB | bB → b

Substitute for A in first rule (i.e., add B to each rule for A):

S → aAB | bBB | bB

The other rules are okay

Page 28: CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.

CS 3240 - Normal Forms for Context-Free Languages 28

S → abSb |aa

Add rules to generate a and b:

S → aBSB |aAA → aB → b

Page 29: CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.

The “parsing” problem How do we know if a string is generated

by a given grammar?Bottom-up parsing (CYK Algorithm)

An Example of Dynamic Programming Requires Chomsky Normal Form (CNF) Start by considering A ➞ c rules Build up the parse tree from there

CS 3240 - Normal Forms for Context-Free Languages 29

Page 30: CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.

CS 3240 - Normal Forms for Context-Free Languages 30

S ➞ XYX ➞ XA | a | bY ➞ AY | aA ➞ a

Does this grammar generate “baaa”?

Page 31: CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.

CS 3240 - Normal Forms for Context-Free Languages 31

CNF yields binary trees.(Can you find a third parse tree?)

Page 32: CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.

CS 3240 - Normal Forms for Context-Free Languages 32

Page 33: CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.

CS 3240 - Normal Forms for Context-Free Languages 33

Page 34: CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.

CS 3240 - Normal Forms for Context-Free Languages 34

Page 35: CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.

CS 3240 - Normal Forms for Context-Free Languages 35

Page 36: CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.

CS 3240 - Normal Forms for Context-Free Languages 36

Page 37: CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.

CS 3240 - Normal Forms for Context-Free Languages 37

Does the following grammar generate “abbaab”?

S ➞ SAB | λA ➞ aA | λB ➞ bB | λ