Simplifications of context free grammars
Download
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
Simplifications of context free grammars

Simplifications of Context-Free Grammars

COMP 335


A substitution rule
A Substitution Rule

Equivalent

grammar

Substitute

COMP 335


A substitution rule1
A Substitution Rule

Substitute

Equivalent

grammar

COMP 335


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


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



Substitute

COMP 335


Remove

COMP 335


Substitute

COMP 335


Remove repeated productions

Final grammar

COMP 335


Useless productions

Useless Production

Some derivations never terminate...

Useless Productions

COMP 335


Useless Production

Another grammar:

Not reachable from S

COMP 335


contains only

terminals

In general:

if

then variable is useful

otherwise, variable is useless

COMP 335


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


First:

find all variables that can produce

strings with only terminals

Round 1:

Round 2:

COMP 335


Keep only the variables

that produce terminal symbols:

(the rest variables are useless)

Remove useless productions

COMP 335


Second:

Find all variables

reachable from

Use a Dependency Graph

not

reachable

COMP 335


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


Normal forms for context free grammars

Normal FormsforContext-free Grammars

COMP 335


Chomsky normal form
Chomsky Normal Form

Each productions has form:

or

variable

variable

terminal

COMP 335


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





Final grammar in Chomsky Normal Form:

Initial grammar

COMP 335


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


The Procedure

  • First remove:

    • Nullable variables

    • Unit productions

COMP 335


Then, for every symbol :

Add production

In productions: replace with

New variable:

COMP 335


Replace any production

with

New intermediate variables:

COMP 335


Theorem:

For any context-free grammar

(which doesn’t produce )

there is an equivalent grammar

in Chomsky Normal Form

COMP 335


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


Examples:

Greibach

Normal Form

Not Greibach

Normal Form

COMP 335


Conversion to Greibach Normal Form:

Greibach

Normal Form

COMP 335


Theorem:

For any context-free grammar

(which doesn’t produce )

there is an equivalent grammar

in Greibach Normal Form

COMP 335


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


The cyk parser

The CYK Parser

COMP 335


The CYK Membership Algorithm

Input:

  • Grammar in Chomsky Normal Form

  • String

Output:

find if

COMP 335


The Algorithm

Input example:

  • Grammar :

  • String :

COMP 335






Therefore:

Time Complexity:

Observation:

The CYK algorithm can be

easily converted to a parser

(bottom up parser)

COMP 335


ad