Chomsky and Greibach Normal Forms Chomsky and Greibach Normal Forms - - PowerPoint PPT Presentation

chomsky and greibach normal forms
SMART_READER_LITE
LIVE PREVIEW

Chomsky and Greibach Normal Forms Chomsky and Greibach Normal Forms - - PowerPoint PPT Presentation

Chomsky and Greibach Normal Forms Chomsky and Greibach Normal Forms p.1/24 Simplifying a CFG It is often convenient to simplify CFG Chomsky and Greibach Normal Forms p.2/24 Simplifying a CFG It is often convenient to


slide-1
SLIDE 1

Chomsky and Greibach Normal Forms

Chomsky and Greibach Normal Forms – p.1/24

slide-2
SLIDE 2

Simplifying a CFG

  • It is often convenient to simplify CFG

Chomsky and Greibach Normal Forms – p.2/24

slide-3
SLIDE 3

Simplifying a CFG

  • It is often convenient to simplify CFG
  • One of the simplest and most useful simplified forms of

CFG is called the Chomsky normal form

Chomsky and Greibach Normal Forms – p.2/24

slide-4
SLIDE 4

Simplifying a CFG

  • It is often convenient to simplify CFG
  • One of the simplest and most useful simplified forms of

CFG is called the Chomsky normal form

  • Another normal form usually used in algebraic

specifications is Greibach normal form

Chomsky and Greibach Normal Forms – p.2/24

slide-5
SLIDE 5

Simplifying a CFG

  • It is often convenient to simplify CFG
  • One of the simplest and most useful simplified forms of

CFG is called the Chomsky normal form

  • Another normal form usually used in algebraic

specifications is Greibach normal form

Note the difference between grammar cleaning and simplifi cation

Chomsky and Greibach Normal Forms – p.2/24

slide-6
SLIDE 6

Note

Normal forms are useful when more advanced topics in computation theory are approached, as we shall see further

Chomsky and Greibach Normal Forms – p.3/24

slide-7
SLIDE 7

Definition

A context-free grammar

  • is in Chomsky normal form if

every rule is of the form:

✁ ✂ ✄ ☎ ✆ ✁ ✂ ✄ ✝

where

is a terminal,

✁ ✞ ☎ ✞ ✆

are nonterminals, and

☎ ✞ ✆

may not be the start variable (the axiom)

Chomsky and Greibach Normal Forms – p.4/24

slide-8
SLIDE 8

Note

The rule

✄ ✁

, where

  • is the start variable, is not ex-

cluded from a CFG in Chomsky normal form.

Chomsky and Greibach Normal Forms – p.5/24

slide-9
SLIDE 9

Theorem 2.9

Any context-free language is generated by a context-free grammar in Chomsky normal form.

Chomsky and Greibach Normal Forms – p.6/24

slide-10
SLIDE 10

Theorem 2.9

Any context-free language is generated by a context-free grammar in Chomsky normal form. Proof idea:

Chomsky and Greibach Normal Forms – p.6/24

slide-11
SLIDE 11

Theorem 2.9

Any context-free language is generated by a context-free grammar in Chomsky normal form. Proof idea:

  • Show that any CFG
  • can be converted into a CFG

in Chomsky normal form

Chomsky and Greibach Normal Forms – p.6/24

slide-12
SLIDE 12

Theorem 2.9

Any context-free language is generated by a context-free grammar in Chomsky normal form. Proof idea:

  • Show that any CFG
  • can be converted into a CFG

in Chomsky normal form

  • Conversion procedure has several stages where the rules that

violate Chomsky normal form conditions are replaced with equivalent rules that satisfy these conditions

Chomsky and Greibach Normal Forms – p.6/24

slide-13
SLIDE 13

Theorem 2.9

Any context-free language is generated by a context-free grammar in Chomsky normal form. Proof idea:

  • Show that any CFG
  • can be converted into a CFG

in Chomsky normal form

  • Conversion procedure has several stages where the rules that

violate Chomsky normal form conditions are replaced with equivalent rules that satisfy these conditions

  • Order of transformations: (1) add a new start variable, (2)

eliminate all

  • rules, (3) eliminate unit-rules, (4) convert other rules

Chomsky and Greibach Normal Forms – p.6/24

slide-14
SLIDE 14

Theorem 2.9

Any context-free language is generated by a context-free grammar in Chomsky normal form. Proof idea:

  • Show that any CFG
  • can be converted into a CFG

in Chomsky normal form

  • Conversion procedure has several stages where the rules that

violate Chomsky normal form conditions are replaced with equivalent rules that satisfy these conditions

  • Order of transformations: (1) add a new start variable, (2)

eliminate all

  • rules, (3) eliminate unit-rules, (4) convert other rules
  • Check that the obtained CFG

defi nes the same language

Chomsky and Greibach Normal Forms – p.6/24

slide-15
SLIDE 15

Proof

Let

✂ ✞ ✄ ✞ ☎ ✞

be the original CFG.

Chomsky and Greibach Normal Forms – p.7/24

slide-16
SLIDE 16

Proof

Let

✂ ✞ ✄ ✞ ☎ ✞

be the original CFG.

Step 1: add a new start symbol

✂✁

to

, and the rule

✂✁ ☎ ✆
  • to

Chomsky and Greibach Normal Forms – p.7/24

slide-17
SLIDE 17

Proof

Let

✂ ✞ ✄ ✞ ☎ ✞

be the original CFG.

Step 1: add a new start symbol

✂✁

to

, and the rule

✂✁ ☎ ✆
  • to

Note: this change guarantees that the start symbol of

does not occur

  • n the
  • ✁✄✂
  • f any rule

Chomsky and Greibach Normal Forms – p.7/24

slide-18
SLIDE 18

Step 2: eliminate

  • rules

Repeat

  • 1. Eliminate the
  • rule
✁ ☎ ✆
  • from

where

is not the start symbol

  • 2. For each occurrence of
  • n the
  • f a rule, add a new rule to

with that occurrence of

deleted Example: replace

✂ ☎ ✆ ✄ ✁✆☎

by

✂ ☎ ✆ ✄ ✁ ☎ ✝ ✄ ☎

; replace

✂ ☎ ✆ ✄ ✁ ☎ ✁✆✞

by

✂ ☎ ✆ ✄ ✁ ☎ ✁✆✞ ✝ ✄ ☎ ✁✆✞ ✝✠✟ ✁✆☎ ✞ ✝ ✄ ☎ ✞
  • 3. Replace the rule
✂ ☎ ✆ ✁

, (if it is present) by

✂ ☎ ✆ ✁ ✝
  • unless the

rule

✂ ☎ ✆
  • has been previously eliminated

until all

rules are eliminated

Chomsky and Greibach Normal Forms – p.8/24

slide-19
SLIDE 19

Step 3: remove unit rules

Repeat

  • 1. Remove a unit rule
✁ ☎ ✆ ✂
  • 2. For each rule
✂ ☎ ✆ ✄

, add the rule

✁ ☎ ✆ ✄

to

, unless

✂ ✆ ✄

was a unit rule previously removed

until all unit rules are eliminated Note:

is a string of variables and terminals

Chomsky and Greibach Normal Forms – p.9/24

slide-20
SLIDE 20

Convert all remaining rules

Repeat

  • 1. Replace a rule
✁ ☎ ✆ ✄
✁ ✂ ✂ ✂ ✄ ✄

,

☎ ✆ ✝

, where each

✄ ✞

,

✟ ✠ ✡ ✠ ☎

, is a variable or a terminal, by:

✁ ☎ ✆ ✄
  • ,
✆ ✄ ✁ ✁ ✁

,

✂ ✂ ✂

,

✁ ✄☞☛ ✁ ☎ ✆ ✄ ✄☞☛

where

  • ,
✁ ✁

,

✂ ✂ ✂

,

✁ ✄☞☛ ✁

are new variables

  • 2. If
☎ ✆ ✌

replace any terminal

✄ ✞

with a new variable

✍ ✞

and add the rule

✍ ✞ ☎ ✆ ✄ ✞

until no rules of the form

✎ ✏ ✟ ✏ ✌✒✑ ✑ ✑ ✏ ☎

with

✓ ✔

remain

Chomsky and Greibach Normal Forms – p.10/24

slide-21
SLIDE 21

Convert all remaining rules

Repeat

  • 1. Replace a rule
✁ ☎ ✆ ✄
✁ ✂ ✂ ✂ ✄ ✄

,

☎ ✆ ✝

, where each

✄ ✞

,

✟ ✠ ✡ ✠ ☎

, is a variable or a terminal, by:

✁ ☎ ✆ ✄
  • ,
✆ ✄ ✁ ✁ ✁

,

✂ ✂ ✂

,

✁ ✄☞☛ ✁ ☎ ✆ ✄ ✄☞☛

where

  • ,
✁ ✁

,

✂ ✂ ✂

,

✁ ✄☞☛ ✁

are new variables

  • 2. If
☎ ✆ ✌

replace any terminal

✄ ✞

with a new variable

✍ ✞

and add the rule

✍ ✞ ☎ ✆ ✄ ✞

until no rules of the form

✎ ✏ ✟ ✏ ✌✒✑ ✑ ✑ ✏ ☎

with

✓ ✔

remain

Chomsky and Greibach Normal Forms – p.10/24

slide-22
SLIDE 22

Convert all remaining rules

Repeat

  • 1. Replace a rule
✁ ☎ ✆ ✄
✁ ✂ ✂ ✂ ✄ ✄

,

☎ ✆ ✝

, where each

✄ ✞

,

✟ ✠ ✡ ✠ ☎

, is a variable or a terminal, by:

✁ ☎ ✆ ✄
  • ,
✆ ✄ ✁ ✁ ✁

,

✂ ✂ ✂

,

✁ ✄☞☛ ✁ ☎ ✆ ✄ ✄☞☛

where

  • ,
✁ ✁

,

✂ ✂ ✂

,

✁ ✄☞☛ ✁

are new variables

  • 2. If
☎ ✆ ✌

replace any terminal

✄ ✞

with a new variable

✍ ✞

and add the rule

✍ ✞ ☎ ✆ ✄ ✞

until no rules of the form

✎ ✏ ✟ ✏ ✌✒✑ ✑ ✑ ✏ ☎

with

✓ ✔

remain

Chomsky and Greibach Normal Forms – p.10/24

slide-23
SLIDE 23

Example CFG conversion

Consider the grammar

whose rules are:

✆ ✁
✝ ✟ ✂ ✁ ☎ ✆ ✂ ✝
☎ ✆ ✂ ✝
  • Notation: symbols removed are green and those added are red.

After fi rst step of transformation we get:

☎ ✆
✆ ✁
✝✠✟ ✂ ✁ ☎ ✆ ✂ ✝
☎ ✆ ✂ ✝
  • Chomsky and Greibach Normal Forms – p.11/24
slide-24
SLIDE 24

Example CFG conversion

Consider the grammar

whose rules are:

✆ ✁
✝ ✟ ✂ ✁ ☎ ✆ ✂ ✝
☎ ✆ ✂ ✝
  • Notation: symbols removed are green and those added are red.

After fi rst step of transformation we get:

☎ ✆
✆ ✁
✝✠✟ ✂ ✁ ☎ ✆ ✂ ✝
☎ ✆ ✂ ✝
  • Chomsky and Greibach Normal Forms – p.11/24
slide-25
SLIDE 25

Example CFG conversion

Consider the grammar

whose rules are:

✆ ✁
✝ ✟ ✂ ✁ ☎ ✆ ✂ ✝
☎ ✆ ✂ ✝
  • Notation: symbols removed are green and those added are red.

After fi rst step of transformation we get:

☎ ✆
✆ ✁
✝✠✟ ✂ ✁ ☎ ✆ ✂ ✝
☎ ✆ ✂ ✝
  • Chomsky and Greibach Normal Forms – p.11/24
slide-26
SLIDE 26

Removing

  • rules

Removing

✂ ✆
  • :
✂✁ ✄ ☎
☎ ✆
✝✟✞ ✠ ✝ ✞ ✆ ✄ ☎ ✠ ✝
✡ ✠ ✄ ☎ ☛ ✝ ✡

Removing

✁ ✆
  • :
☞✁ ✄ ☎
☎ ✆
✝✟✞ ✠ ✝✟✞ ✝
✝ ✆
✄ ☎ ✠ ✝
✡ ✠ ✄ ☎ ☛

Chomsky and Greibach Normal Forms – p.12/24

slide-27
SLIDE 27

Removing

  • rules

Removing

✂ ✆
  • :
✂✁ ✄ ☎
☎ ✆
✝✟✞ ✠ ✝ ✞ ✆ ✄ ☎ ✠ ✝
✡ ✠ ✄ ☎ ☛ ✝ ✡

Removing

✁ ✆
  • :
☞✁ ✄ ☎
☎ ✆
✝✟✞ ✠ ✝✟✞ ✝
✝ ✆
✄ ☎ ✠ ✝
✡ ✠ ✄ ☎ ☛

Chomsky and Greibach Normal Forms – p.12/24

slide-28
SLIDE 28

Removing

  • rules

Removing

✂ ✆
  • :
✂✁ ✄ ☎
☎ ✆
✝✟✞ ✠ ✝ ✞ ✆ ✄ ☎ ✠ ✝
✡ ✠ ✄ ☎ ☛ ✝ ✡

Removing

✁ ✆
  • :
☞✁ ✄ ☎
☎ ✆
✝✟✞ ✠ ✝✟✞ ✝
✝ ✆
✄ ☎ ✠ ✝
✡ ✠ ✄ ☎ ☛

Chomsky and Greibach Normal Forms – p.12/24

slide-29
SLIDE 29

Removing unit rule

Removing

  • :
✂✁ ✄ ☎
☎ ✆
✝✟✞ ✠ ✝✟✞ ✝
✝ ✆
✄ ☎ ✠ ✝
✄ ☎ ☛

Removing

  • :
☞✁ ✄ ☎
✝ ✞ ✠ ✝ ✞ ✝
✝ ✆
☎ ✆
✝✟✞ ✠ ✝✟✞ ✝
✝ ✆
✄ ☎ ✠ ✝
✄ ☎ ☛

Chomsky and Greibach Normal Forms – p.13/24

slide-30
SLIDE 30

Removing unit rule

Removing

  • :
✂✁ ✄ ☎
☎ ✆
✝✟✞ ✠ ✝✟✞ ✝
✝ ✆
✄ ☎ ✠ ✝
✄ ☎ ☛

Removing

  • :
☞✁ ✄ ☎
✝ ✞ ✠ ✝ ✞ ✝
✝ ✆
☎ ✆
✝✟✞ ✠ ✝✟✞ ✝
✝ ✆
✄ ☎ ✠ ✝
✄ ☎ ☛

Chomsky and Greibach Normal Forms – p.13/24

slide-31
SLIDE 31

Removing unit rule

Removing

  • :
✂✁ ✄ ☎
☎ ✆
✝✟✞ ✠ ✝✟✞ ✝
✝ ✆
✄ ☎ ✠ ✝
✄ ☎ ☛

Removing

  • :
☞✁ ✄ ☎
✝ ✞ ✠ ✝ ✞ ✝
✝ ✆
☎ ✆
✝✟✞ ✠ ✝✟✞ ✝
✝ ✆
✄ ☎ ✠ ✝
✄ ☎ ☛

Chomsky and Greibach Normal Forms – p.13/24

slide-32
SLIDE 32

More unit rules

Removing

✁ ✆ ✂

:

✄ ☎ ✆
✝✟✞ ✠ ✝✟✞ ✝
✝ ✆
☎ ✆
✝✟✞ ✠ ✝✟✞ ✝
✝ ✆
✄ ☎ ✠ ✝
☛ ✠ ✄ ☎ ☛

Removing

✁ ✆
  • :
✄ ☎ ✆
✝✟✞ ✠ ✝✟✞ ✝
✝ ✆
☎ ✆
✝✟✞ ✠ ✝✟✞ ✝
✝ ✆
✄ ☎
☛ ✝ ✆
✝ ✞ ✠ ✝ ✞ ✝
✝ ✆
✄ ☎ ☛

Chomsky and Greibach Normal Forms – p.14/24

slide-33
SLIDE 33

More unit rules

Removing

✁ ✆ ✂

:

✄ ☎ ✆
✝✟✞ ✠ ✝✟✞ ✝
✝ ✆
☎ ✆
✝✟✞ ✠ ✝✟✞ ✝
✝ ✆
✄ ☎ ✠ ✝
☛ ✠ ✄ ☎ ☛

Removing

✁ ✆
  • :
✄ ☎ ✆
✝✟✞ ✠ ✝✟✞ ✝
✝ ✆
☎ ✆
✝✟✞ ✠ ✝✟✞ ✝
✝ ✆
✄ ☎
☛ ✝ ✆
✝ ✞ ✠ ✝ ✞ ✝
✝ ✆
✄ ☎ ☛

Chomsky and Greibach Normal Forms – p.14/24

slide-34
SLIDE 34

More unit rules

Removing

✁ ✆ ✂

:

✄ ☎ ✆
✝✟✞ ✠ ✝✟✞ ✝
✝ ✆
☎ ✆
✝✟✞ ✠ ✝✟✞ ✝
✝ ✆
✄ ☎ ✠ ✝
☛ ✠ ✄ ☎ ☛

Removing

✁ ✆
  • :
✄ ☎ ✆
✝✟✞ ✠ ✝✟✞ ✝
✝ ✆
☎ ✆
✝✟✞ ✠ ✝✟✞ ✝
✝ ✆
✄ ☎
☛ ✝ ✆
✝ ✞ ✠ ✝ ✞ ✝
✝ ✆
✄ ☎ ☛

Chomsky and Greibach Normal Forms – p.14/24

slide-35
SLIDE 35

Converting remaining rules

☎ ✆ ✁ ✁
✍ ✂ ✝ ✟ ✝
✝ ✁
✆ ✁ ✁
✍ ✂ ✝ ✟ ✝
✝ ✁
☎ ✆ ✂ ✝ ✁ ✁
✍ ✂ ✝✠✟ ✝
✝ ✁
✍ ☎ ✆ ✟ ✂ ☎ ✆ ✂

Chomsky and Greibach Normal Forms – p.15/24

slide-36
SLIDE 36

Converting remaining rules

☎ ✆ ✁ ✁
✍ ✂ ✝ ✟ ✝
✝ ✁
✆ ✁ ✁
✍ ✂ ✝ ✟ ✝
✝ ✁
☎ ✆ ✂ ✝ ✁ ✁
✍ ✂ ✝✠✟ ✝
✝ ✁
✍ ☎ ✆ ✟ ✂ ☎ ✆ ✂

Chomsky and Greibach Normal Forms – p.15/24

slide-37
SLIDE 37

Converting remaining rules

☎ ✆ ✁ ✁
✍ ✂ ✝ ✟ ✝
✝ ✁
✆ ✁ ✁
✍ ✂ ✝ ✟ ✝
✝ ✁
☎ ✆ ✂ ✝ ✁ ✁
✍ ✂ ✝✠✟ ✝
✝ ✁
✍ ☎ ✆ ✟ ✂ ☎ ✆ ✂

Chomsky and Greibach Normal Forms – p.15/24

slide-38
SLIDE 38

Note

  • The conversion procedure produces several

variables

along with several rules

  • .
  • Since all these represent the same rule, we

may simplify the result using a single variable and a single rule

  • Chomsky and Greibach Normal Forms – p.16/24
slide-39
SLIDE 39

Note

  • The conversion procedure produces several

variables

along with several rules

  • .
  • Since all these represent the same rule, we

may simplify the result using a single variable and a single rule

  • Chomsky and Greibach Normal Forms – p.16/24
slide-40
SLIDE 40

Note

  • The conversion procedure produces several

variables

along with several rules

  • .
  • Since all these represent the same rule, we

may simplify the result using a single variable and a single rule

  • Chomsky and Greibach Normal Forms – p.16/24
slide-41
SLIDE 41

Greibach Normal Form

A context-free grammar

✂ ✂ ✂ ✄ ☎

is in Greibach normal form if each rule

✆ ✝

has the property:

✞✟ ✠ ✁ ✆ ☎ ✝

,

✆ ✟ ✠ ✁ ✆ ☎

,

and

✡ ✝ ☛

.

Note: Greibach normal form provides a justifi ca-

tion of operator prefi x-notation usually employed in algebra.

Chomsky and Greibach Normal Forms – p.17/24

slide-42
SLIDE 42

Greibach Normal Form

A context-free grammar

✂ ✂ ✂ ✄ ☎

is in Greibach normal form if each rule

✆ ✝

has the property:

✞✟ ✠ ✁ ✆ ☎ ✝

,

✆ ✟ ✠ ✁ ✆ ☎

,

and

✡ ✝ ☛

.

Note: Greibach normal form provides a justifi ca-

tion of operator prefi x-notation usually employed in algebra.

Chomsky and Greibach Normal Forms – p.17/24

slide-43
SLIDE 43

Greibach Normal Form

A context-free grammar

✂ ✂ ✂ ✄ ☎

is in Greibach normal form if each rule

✆ ✝

has the property:

✞✟ ✠ ✁ ✆ ☎ ✝

,

✆ ✟ ✠ ✁ ✆ ☎

,

and

✡ ✝ ☛

.

Note: Greibach normal form provides a justifi ca-

tion of operator prefi x-notation usually employed in algebra.

Chomsky and Greibach Normal Forms – p.17/24

slide-44
SLIDE 44

Greibach Theorem

Every CFL

  • where
✁ ✝
  • can be generated by a

CFG in Greibach normal form.

Proof idea: Let

✄✂ ☎ ✆✞✝ ✟ ✝ ✝ ✝

be a CFG generating

. Assume that

  • is in Chomsky normal form
  • Let
✆ ✂ ☛ ✁
✁ ✁ ✝ ✂ ✂ ✂ ✝ ✁✌☞ ✍

be an ordering of nonterminals.

  • Construct the Greibach normal form from Chomsky normal form

Chomsky and Greibach Normal Forms – p.18/24

slide-45
SLIDE 45

Greibach Theorem

Every CFL

  • where
✁ ✝
  • can be generated by a

CFG in Greibach normal form.

Proof idea: Let

✄✂ ☎ ✆✞✝ ✟ ✝ ✝ ✝

be a CFG generating

. Assume that

  • is in Chomsky normal form
  • Let
✆ ✂ ☛ ✁
✁ ✁ ✝ ✂ ✂ ✂ ✝ ✁✌☞ ✍

be an ordering of nonterminals.

  • Construct the Greibach normal form from Chomsky normal form

Chomsky and Greibach Normal Forms – p.18/24

slide-46
SLIDE 46

Greibach Theorem

Every CFL

  • where
✁ ✝
  • can be generated by a

CFG in Greibach normal form.

Proof idea: Let

✄✂ ☎ ✆✞✝ ✟ ✝ ✝ ✝

be a CFG generating

. Assume that

  • is in Chomsky normal form
  • Let
✆ ✂ ☛ ✁
✁ ✁ ✝ ✂ ✂ ✂ ✝ ✁✌☞ ✍

be an ordering of nonterminals.

  • Construct the Greibach normal form from Chomsky normal form

Chomsky and Greibach Normal Forms – p.18/24

slide-47
SLIDE 47

Construction

  • 1. Modify the rules in

so that if

✁ ✞ ✆ ✁✁ ✂

then

✄ ☎ ✡
  • 2. Starting with
  • and proceeding to
✁ ☞

this is done as follows: (a) Assume that productions have been modifi ed so that for

✟ ✠ ✡ ✠ ☎

,

✁ ✞ ✆ ✁✁ ✂
  • nly if
✄ ☎ ✡

(b) If

✁ ✄ ✆ ✁✁ ✂

is a production with

✄ ✆ ☎

, generate a new set of productions substituting for the

  • the rhs of each
  • production

(c) Repeating (b) at most

☎ ☎ ✟

times we obtain rules of the form

✁ ✄ ✆ ✁✞✝ ✂

,

✟ ✆ ☎

(d) Replace rules

✁ ✄ ✆ ✁ ✄ ✂

by removing left-recursive rules

Chomsky and Greibach Normal Forms – p.19/24

slide-48
SLIDE 48

Construction

  • 1. Modify the rules in

so that if

✁ ✞ ✆ ✁✁ ✂

then

✄ ☎ ✡
  • 2. Starting with
  • and proceeding to
✁ ☞

this is done as follows: (a) Assume that productions have been modifi ed so that for

✟ ✠ ✡ ✠ ☎

,

✁ ✞ ✆ ✁✁ ✂
  • nly if
✄ ☎ ✡

(b) If

✁ ✄ ✆ ✁✁ ✂

is a production with

✄ ✆ ☎

, generate a new set of productions substituting for the

  • the rhs of each
  • production

(c) Repeating (b) at most

☎ ☎ ✟

times we obtain rules of the form

✁ ✄ ✆ ✁✞✝ ✂

,

✟ ✆ ☎

(d) Replace rules

✁ ✄ ✆ ✁ ✄ ✂

by removing left-recursive rules

Chomsky and Greibach Normal Forms – p.19/24

slide-49
SLIDE 49

Construction

  • 1. Modify the rules in

so that if

✁ ✞ ✆ ✁✁ ✂

then

✄ ☎ ✡
  • 2. Starting with
  • and proceeding to
✁ ☞

this is done as follows: (a) Assume that productions have been modifi ed so that for

✟ ✠ ✡ ✠ ☎

,

✁ ✞ ✆ ✁✁ ✂
  • nly if
✄ ☎ ✡

(b) If

✁ ✄ ✆ ✁✁ ✂

is a production with

✄ ✆ ☎

, generate a new set of productions substituting for the

  • the rhs of each
  • production

(c) Repeating (b) at most

☎ ☎ ✟

times we obtain rules of the form

✁ ✄ ✆ ✁✞✝ ✂

,

✟ ✆ ☎

(d) Replace rules

✁ ✄ ✆ ✁ ✄ ✂

by removing left-recursive rules

Chomsky and Greibach Normal Forms – p.19/24

slide-50
SLIDE 50

Removing left-recursion

Left-recursion can be eliminated by the following scheme:

  • If
✁ ✆ ✁✁
✁✁ ✁ ✂ ✂ ✂ ✝ ✁

are all

left recursive rules, and

✁ ✆ ✄
✄ ✁ ✝ ✂ ✂ ✂ ✝ ✄✆☎

are all remaining

  • rules then chose a new

nonterminal, say

  • Add the new
  • rules
✂ ✆

,

✟ ✠ ✡ ✠
  • Replace the
  • rules by
✁ ✆ ✄ ✞ ✝ ✄ ✞ ✂

,

✟ ✠ ✡ ✠ ✂

This construction preserve the language

  • .

Chomsky and Greibach Normal Forms – p.20/24

slide-51
SLIDE 51

Removing left-recursion

Left-recursion can be eliminated by the following scheme:

  • If
✁ ✆ ✁✁
✁✁ ✁ ✂ ✂ ✂ ✝ ✁

are all

left recursive rules, and

✁ ✆ ✄
✄ ✁ ✝ ✂ ✂ ✂ ✝ ✄✆☎

are all remaining

  • rules then chose a new

nonterminal, say

  • Add the new
  • rules
✂ ✆

,

✟ ✠ ✡ ✠
  • Replace the
  • rules by
✁ ✆ ✄ ✞ ✝ ✄ ✞ ✂

,

✟ ✠ ✡ ✠ ✂

This construction preserve the language

  • .

Chomsky and Greibach Normal Forms – p.20/24

slide-52
SLIDE 52

Removing left-recursion

Left-recursion can be eliminated by the following scheme:

  • If
✁ ✆ ✁✁
✁✁ ✁ ✂ ✂ ✂ ✝ ✁

are all

left recursive rules, and

✁ ✆ ✄
✄ ✁ ✝ ✂ ✂ ✂ ✝ ✄✆☎

are all remaining

  • rules then chose a new

nonterminal, say

  • Add the new
  • rules
✂ ✆

,

✟ ✠ ✡ ✠
  • Replace the
  • rules by
✁ ✆ ✄ ✞ ✝ ✄ ✞ ✂

,

✟ ✠ ✡ ✠ ✂

This construction preserve the language

  • .

Chomsky and Greibach Normal Forms – p.20/24

slide-53
SLIDE 53

21-1

slide-54
SLIDE 54

More on Greibach NF

See Introduction to Automata Theory, Languages, and Computation, J.E, Hopcroft and J.D Ullman, Addison-Wesley 1979, p. 94–96

Chomsky and Greibach Normal Forms – p.22/24

slide-55
SLIDE 55

Example

Convert the CFG

✄✂ ☎ ☛ ✁
✁ ✁ ✝ ✁✁ ✍ ✝ ☛ ✟ ✝ ✂ ✍ ✝ ✝ ✝ ✁

where

✝ ✂ ☛ ✁
✁ ✁ ✁
✁ ✁ ✆ ✁
✂ ✝ ✁
✁ ✝✠✟ ✍

into Greibach normal form.

Chomsky and Greibach Normal Forms – p.23/24

slide-56
SLIDE 56

Example

Convert the CFG

✄✂ ☎ ☛ ✁
✁ ✁ ✝ ✁✁ ✍ ✝ ☛ ✟ ✝ ✂ ✍ ✝ ✝ ✝ ✁

where

✝ ✂ ☛ ✁
✁ ✁ ✁
✁ ✁ ✆ ✁
✂ ✝ ✁
✁ ✝✠✟ ✍

into Greibach normal form.

Chomsky and Greibach Normal Forms – p.23/24

slide-57
SLIDE 57

Example

Convert the CFG

✄✂ ☎ ☛ ✁
✁ ✁ ✝ ✁✁ ✍ ✝ ☛ ✟ ✝ ✂ ✍ ✝ ✝ ✝ ✁

where

✝ ✂ ☛ ✁
✁ ✁ ✁
✁ ✁ ✆ ✁
✂ ✝ ✁
✁ ✝✠✟ ✍

into Greibach normal form.

Chomsky and Greibach Normal Forms – p.23/24

slide-58
SLIDE 58

Solution

  • 1. Step 1: ordering the rules: (Only
  • rules violate ordering

conditions, hence only

  • rules need to be changed).

Following the procedure we replace

  • rules by:
✁ ✝ ✂ ✁
✁ ✝✠✟
  • 2. Eliminating left-recursion we get:
✂ ✁
✁ ✂
  • ✝✠✟
✂ ✁✁ ✁ ✁ ✝ ✟

,

✁ ✝ ✁
✁ ✂
  • 3. All
  • rules start with a terminal. We use them to replace
✁ ✁ ✁
  • . This introduces the rules
✁ ✝ ✁
✁ ✂
  • 4. Use
  • production to make them start with a terminal

Chomsky and Greibach Normal Forms – p.24/24

slide-59
SLIDE 59

Solution

  • 1. Step 1: ordering the rules: (Only
  • rules violate ordering

conditions, hence only

  • rules need to be changed).

Following the procedure we replace

  • rules by:
✁ ✝ ✂ ✁
✁ ✝✠✟
  • 2. Eliminating left-recursion we get:
✂ ✁
✁ ✂
  • ✝✠✟
✂ ✁✁ ✁ ✁ ✝ ✟

,

✁ ✝ ✁
✁ ✂
  • 3. All
  • rules start with a terminal. We use them to replace
✁ ✁ ✁
  • . This introduces the rules
✁ ✝ ✁
✁ ✂
  • 4. Use
  • production to make them start with a terminal

Chomsky and Greibach Normal Forms – p.24/24

slide-60
SLIDE 60

Solution

  • 1. Step 1: ordering the rules: (Only
  • rules violate ordering

conditions, hence only

  • rules need to be changed).

Following the procedure we replace

  • rules by:
✁ ✝ ✂ ✁
✁ ✝✠✟
  • 2. Eliminating left-recursion we get:
✂ ✁
✁ ✂
  • ✝✠✟
✂ ✁✁ ✁ ✁ ✝ ✟

,

✁ ✝ ✁
✁ ✂
  • 3. All
  • rules start with a terminal. We use them to replace
✁ ✁ ✁
  • . This introduces the rules
✁ ✝ ✁
✁ ✂
  • 4. Use
  • production to make them start with a terminal

Chomsky and Greibach Normal Forms – p.24/24