simplifications of context free grammars
Download
Skip this Video
Download Presentation
Simplifications of Context-Free Grammars

Loading in 2 Seconds...

play fullscreen
1 / 51

Simplifications of Context-Free Grammars - PowerPoint PPT Presentation


  • 83 Views
  • Uploaded on

Simplifications of Context-Free Grammars. A Substitution Rule. Equivalent grammar. Substitute. A Substitution Rule. Substitute. Equivalent grammar. In general:. Substitute. equivalent grammar. Nullable Variables. Nullable Variable:. Removing Nullable Variables. Example Grammar:.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Simplifications of Context-Free Grammars' - tyrone-santiago


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
a substitution rule
A Substitution Rule

Equivalent

grammar

Substitute

COMP 335

a substitution rule1
A Substitution Rule

Substitute

Equivalent

grammar

COMP 335

slide4

In general:

Substitute

equivalent

grammar

COMP 335

nullable variables
Nullable Variables

Nullable Variable:

COMP 335

removing nullable variables
Removing Nullable Variables

Example Grammar:

Nullable variable

COMP 335

slide7

Final Grammar

Substitute

COMP 335

unit productions
Unit-Productions

Unit Production:

(a single variable in both sides)

COMP 335

removing unit productions
Removing Unit Productions

Observation:

Is removed immediately

COMP 335

slide11

Substitute

COMP 335

slide12

Remove

COMP 335

slide13

Substitute

COMP 335

slide14

Remove repeated productions

Final grammar

COMP 335

useless productions

Useless Production

Some derivations never terminate...

Useless Productions

COMP 335

slide16

Useless Production

Another grammar:

Not reachable from S

COMP 335

slide17

contains only

terminals

In general:

if

then variable is useful

otherwise, variable is useless

COMP 335

slide18

Productions

useless

Variables

useless

useless

useless

useless

useless

useless

A production is useless

if any of its variables is useless

COMP 335

removing useless productions
Removing Useless Productions

Example Grammar:

COMP 335

slide20

First:

find all variables that can produce

strings with only terminals

Round 1:

Round 2:

COMP 335

slide21

Keep only the variables

that produce terminal symbols:

(the rest variables are useless)

Remove useless productions

COMP 335

slide22

Second:

Find all variables

reachable from

Use a Dependency Graph

not

reachable

COMP 335

slide23

Keep only the variables

reachable from S

(the rest variables are useless)

Final Grammar

Remove useless productions

COMP 335

removing all
Removing All
  • Step 1: Remove -productions
  • Step 2: Remove Unit-productions
  • Step 3: Remove Useless productions

COMP 335

chomsky normal form
Chomsky Normal Form

Each productions has form:

or

variable

variable

terminal

COMP 335

slide27

Examples:

Chomsky

Normal Form

Not Chomsky

Normal Form

COMP 335

conversion to chomsky normal form
Conversion to Chomsky Normal Form
  • Example:

Not Chomsky

Normal Form

COMP 335

slide33

In general:

From any context-free grammar

(which doesn’t produce )

not in Chomsky Normal Form

we can obtain:

An equivalent grammar

in Chomsky Normal Form

COMP 335

slide34

The Procedure

  • First remove:
      • Nullable variables
      • Unit productions

COMP 335

slide35

Then, for every symbol :

Add production

In productions: replace with

New variable:

COMP 335

slide36

Replace any production

with

New intermediate variables:

COMP 335

slide37

Theorem:

For any context-free grammar

(which doesn’t produce )

there is an equivalent grammar

in Chomsky Normal Form

COMP 335

slide38

Observations

  • Chomsky normal forms are good
  • for parsing and proving theorems
  • It is very easy to find the Chomsky normal
  • form for any context-free grammar

COMP 335

greibach normal form
Greibach Normal Form

All productions have form:

symbol

variables

COMP 335

slide40

Examples:

Greibach

Normal Form

Not Greibach

Normal Form

COMP 335

slide41

Conversion to Greibach Normal Form:

Greibach

Normal Form

COMP 335

slide42

Theorem:

For any context-free grammar

(which doesn’t produce )

there is an equivalent grammar

in Greibach Normal Form

COMP 335

slide43

Observations

  • Greibach normal forms are very good
  • for parsing
  • It is hard to find the Greibach normal
  • form of any context-free grammar

COMP 335

slide45

The CYK Membership Algorithm

Input:

  • Grammar in Chomsky Normal Form
  • String

Output:

find if

COMP 335

slide46

The Algorithm

Input example:

  • Grammar :
  • String :

COMP 335

slide51

Therefore:

Time Complexity:

Observation:

The CYK algorithm can be

easily converted to a parser

(bottom up parser)

COMP 335

ad