Simplifying CFL - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Simplifying CFL

Description:

Nullable Variables. Nullable Variables are those that can be replaced with lambda. ... nullable variables. Algorithm for removing lambda and Nullable Variables ... – PowerPoint PPT presentation

Number of Views:162
Avg rating:3.0/5.0
Slides: 25
Provided by: jonahC
Category:

less

Transcript and Presenter's Notes

Title: Simplifying CFL


1
Simplifying CFLs
  • Reading 6.1

2
Grammar Simplification
  • We want to change the production rule styles of
    CFLs to prove properties.
  • Simplification removing undesirable types of
    production rules without changing the language
    that the grammar defines.
  • Simplification does not always result in fewer
    rules!

3
Removing Lambda
  • We want lambda-free grammars, so that we dont
    deal with special empty cases.
  • We can write lambda-free grammars for any
    language that does not contain the string lambda.
  • Otherwise, consider the language L ?

4
Removing Production Rules
  • If you want to remove a rule of a CFL that has
    only one variable on the right,
  • Exchange the rule with a set of rules that
    inserts all one-step derivations of the variable
    on the right.
  • A -gt a aaA abBc
  • B -gt abbA b

A -gt a aaA ababbAc abbc
5
Nullable Variables
  • Nullable Variables are those that can be replaced
    with lambda.
  • We often want to remove lambda production rules
  • Doing so makes a grammar with no nullable
    variables

6
Algorithm for removing lambda and Nullable
Variables
  • For any production rule in the grammar of the
    form
  • A -gt ?
  • Replace all right-hand occurrences of A with an
    option that has A replaced with ?.

7
Example Removing lambda
  • S -gt aAb
  • A -gt aAb ?
  • Becomes
  • S -gt aAb ab
  • A -gt aAb ab

8
Example Removing Lambda
Iteration 1 (B) S -gt ABaC AaC A -gt BC C B -gt
b C -gt D ? D -gt d
Iteration 2 (C) S -gt ABaC AaC ABa Aa A -gt
BC C B ? B -gt b C -gt D D -gt d
  • S -gt ABaC
  • A -gt BC
  • B -gt b ?
  • C -gt D ?
  • D -gt d

Iteration 3 (A) S -gt ABaC AaC ABa Aa BaC
aC Ba a A -gt BC C B B -gt b C
-gt D D -gt d
9
Useful Variables
  • A variable in a grammar is said to be useful if
    there is at least one string w in the language
    such that
  • S xAy w
  • where x y are any sequences of variables and
    terminals.
  • A variable is useful if it is used in the
    derivation of at least one string.
  • Otherwise, it is useless.

10
Finding Useless Variables
  • A variable may be useless if there is no way to
    get a terminal string from it
  • S -gt aSb a A
  • A -gt aA
  • A is useless in this example, because it can
    never lead to terminals.

11
Finding Useless Variables
  • A variable might be useless if there is no way to
    reach it from the start symbol.
  • S -gt A
  • A -gt aA a
  • B -gt bA
  • B is useless there is no other production rule
    that uses it.

12
Dependency Graph
  • A way to tell if a variable is useless because it
    is unreachable from S.
  • Make a node for every variable.
  • Place arcs from node A to node B if B appears on
    the right of a production of A.
  • A useless variable is any that cannot be reached
    in the graph from S.

13
Dependency Graph Example
  • S -gt aaA ab
  • A -gt bC
  • B -gt Cab
  • C - gt a bb
  • B is useless because it is unreachable from S.

A
S
C
B
14
Algorithm for Removing All Useless Variables
(Part 1)
  • Make a set V1, initialize to
  • Loop
  • Look at each variable V in turn
  • Look at each of Vs production rules
  • If everything on the right of the production rule
    is either a terminal or a variable in V1, add V
    to V1
  • Continue Loop until the iteration adds no
    variables to V1.
  • Keep all production rules whose variables are all
    in V1.

15
Algorithm for Removing All Useless Variables
(Part 2)
  • Part 1 kept all variables that result in terminal
    strings.
  • Part 2 will keep all variables reachable from the
    start symbol.
  • Draw the dependency graph for the new grammar.
  • Keep only the production rules in which all
    variables can be reached in the dependency graph.

16
Example
  • Remove all useless symbols and productions from
    the following grammar
  • S -gt aS A C
  • A -gt a
  • B -gt aa
  • C -gt aCb

17
Step 1
Start with V1 Iteration 1 V1
A,B Iteration 2 V1 A,B,S Iteration 3 V1
A,B,S
  • S -gt aS A C
  • A -gt a
  • B -gt aa
  • C -gt aCb

18
Step 2
  • S -gt aS A
  • A -gt a
  • B -gt aa

A
S
B
Final Grammar S -gt aS A A -gt a
19
Unit Productions
  • Any production where one variable is replaced
    with another is a unit production.
  • Any rule of the form
  • A -gt B
  • Unit production rules are sometimes undesirable.
    (Exhaustive Search Parsing)
  • A -gt A can be removed with no effect.

20
Removing Unit Production Rules
  • Part one Draw a unit-rule dependency graph.
    That is, a dependency graph based only on unit
    production rules.
  • Find pairs A-gt B such that there is a walk in the
    graph from A to B.
  • Part two For each pair A -gt B, add an option
    which substitutes non-unit resolutions of B to
    the right hand side of A.

21
Removing Unit Productions Example
  • S -gt Aa B
  • B -gt A bb
  • A -gt a bc B

S
A
B
Pairs are S-gt BS -gt AA -gt BB -gt A
22
Removing Unit Rules
  • Original S -gt Aa B Minus Units S-gt Aa
  • B -gt A bb
    B -gt bb
  • A -gt a bc B A -gt a
    bcFor each pair, add substitutions from the
    Minus Grammar
  • (S -gt A)
  • S -gt Aa a bc
  • (S -gt B)
  • S -gt Aa a bc bb

(B -gt A)B -gt bb a bc (A -gt B)A -gt a bc
bb
23
Removing Units
  • Final Grammar is
  • S -gt Aa a bc bb
  • B -gt bb a bc
  • A -gt a bc bb

24
Complete Simplification
  1. Remove lambda productions
  2. Remove unit productions
  3. Remove useless productions
Write a Comment
User Comments (0)
About PowerShow.com