Simplifies and normal forms - Theory of Computation

31
Simplifies form and Normal form

Transcript of Simplifies and normal forms - Theory of Computation

Page 1: Simplifies and normal forms - Theory of Computation

Simplifies form and Normal form

Page 2: Simplifies and normal forms - Theory of Computation

Simplifies form

Page 3: Simplifies and normal forms - Theory of Computation

Context Free Grammar (CFG)

• A context free grammar is a 4-tuple G=(V,Ʃ,S,P) where

V &Ʃ are disjoint finite setS is an element of V andP is a finite set formulas of the form A → αV → Non terminal or variableƩ → terminal symbolsS → start symbolP → production rule or grammar rules

Page 4: Simplifies and normal forms - Theory of Computation

Context Free Grammar (CFG)

Application • CFG are extensively used to specify the syntax of programming

language.• CFG is used to develop parser

Definition: Context Free Language• Language generated by CFG is called context free language• Let G= (V, Ʃ, S, P) be a CFG. The Language generated by G is

L(G):{x Ʃ*/S G* x}∈ ⟹• A language L is a context free Language(CFL) if there is a CFG G

so that L=L(G)

Page 5: Simplifies and normal forms - Theory of Computation

Simplified form & Normal forms

• In this section we discuss some slight more straight forward ways of improving a grammer without changing the resulting language.

1)eliminating certain types of productions that may be awkward to work to work.2)standardizing the productions so that they all have a certaion normal form.

Page 6: Simplifies and normal forms - Theory of Computation

Simplifies form• In this simplifies form there is three type

1) Eliminating Null able Variable(Empty Production Removal)2) Eliminating Unit Production(Unit production removal)3) Eliminating Useless Productions(Removing Useless)

Page 7: Simplifies and normal forms - Theory of Computation

Eliminating Null able Variable

(Empty Production Removal)

• The productions of context-free grammars can be coerced into a variety of forms without affecting the expressive power of the grammars.

• If the empty string does not belong to a language, then there is a way to eliminate the productions of the form A → ^ from the grammar.

• If the empty string belongs to a language, then we can eliminate ^ from all productions save for the single production S → ^ . In this case we can also eliminate any occurrences of S from the right-hand side of productions

Page 8: Simplifies and normal forms - Theory of Computation

Eliminating Null able Variable

(Empty Production Removal)

• a nullable variable in a CFG G=(V,Ʃ,S,P) is defined as follows1) Any variable A for which P contains A → ˄ is nullable2) if P contain production A → B1B2…..Bn where B1B2…Bn are nullable variable, then A is nullable.3) No other variable in V are nullable.

• Example: S → aX/YbX → S/˄Y → bY/b

Ans:S → aX/Yb/aX → SY → bY/b

Page 9: Simplifies and normal forms - Theory of Computation

Eliminating Unit Production(Unit production removal)

Page 10: Simplifies and normal forms - Theory of Computation

Eliminating Unit Production(Unit production removal)

Page 11: Simplifies and normal forms - Theory of Computation

Eliminating Unit Production(Unit production removal)

• Example:S → Aa/BA → a/bc/BB → A/bB

Ans:Unit production are S → B, A → B and B → A

S → Aa/A/bb S → Aa/a/bc/bbA → a/bc/B A → a/bc/A/bbA → a/bc/bb B → A/bbB → A/bb B → a/bc/bb

So CFG after removing unit production isS → Aa/a/bc/bbA → a/bc/bbB → a/bc/bb

Page 12: Simplifies and normal forms - Theory of Computation

Normal forms

Page 13: Simplifies and normal forms - Theory of Computation

definition

Page 14: Simplifies and normal forms - Theory of Computation

Theorem

Page 15: Simplifies and normal forms - Theory of Computation

Example of CFG Conversion

Page 16: Simplifies and normal forms - Theory of Computation

Removing Rules

Page 17: Simplifies and normal forms - Theory of Computation

Removing unit rule

Page 18: Simplifies and normal forms - Theory of Computation

More unit rules

Page 19: Simplifies and normal forms - Theory of Computation

Converting remaining rules

Page 20: Simplifies and normal forms - Theory of Computation

Presentation Outline

20May 27, 2009

• Greibach Normal Form

Page 21: Simplifies and normal forms - Theory of Computation

Greibach Normal Form

21May 27, 2009

A → αX

A context free grammar is said to be in Greibach Normal Form if all productions are in the following form:

• A is a non terminal symbols• α is a terminal symbol• X is a sequence of non terminal symbols. It may be empty.

Page 22: Simplifies and normal forms - Theory of Computation

Step1:If the start symbol S occurs on some right side, create a new start symbol S’ and a new production S’ → S.

Step 2:Remove Null productions. (Using the Null production removal algorithm discussed earlier)

Step 3:Remove unit productions. (Using the Unit production removal algorithm discussed earlier)

Step 4: Remove all direct and indirect left-recursion.

Step 5: Do proper substitutions of productions to convert it into the proper form of GNF.

Algorithm to Convert a CFG into Greibach Normal Form

Greibach Normal Form

Page 23: Simplifies and normal forms - Theory of Computation

Greibach Normal Form

23May 27, 2009

Example:

S → XA | BBB → b | SBX → bA → a

S = A1

X = A2

A = A3

B = A4

A1 → A2A3 | A4A4

A4 → b | A1A4

A2 → bA3 → a

CNF New Labels Updated CNF

Page 24: Simplifies and normal forms - Theory of Computation

Greibach Normal Form

24May 27, 2009

Example:

A1 → A2A3 | A4A4

A4 → b | A1A4

A2 → bA3 → a

First Step

Xk is a string of zero

or more variables

Ai → AjXk j > i

A4 → A1A4

Page 25: Simplifies and normal forms - Theory of Computation

Greibach Normal Form

25May 27, 2009

Example:

A1 → A2A3 | A4A4

A4 → b | A1A4

A2 → bA3 → a

A4 → A1A4

A4 → A2A3A4 | A4A4A4 | bA4 → bA3A4 | A4A4A4 | b

First Step Ai → AjXk j > i

Page 26: Simplifies and normal forms - Theory of Computation

Greibach Normal Form

26May 27, 2009

Example:

Second Step

Eliminate

Left Recursions

A1 → A2A3 | A4A4

A4 → bA3A4 | A4A4A4 | b A2 → bA3 → a

A4 → A4A4A4

Page 27: Simplifies and normal forms - Theory of Computation

Greibach Normal Form

27May 27, 209

Example:Second Step

Eliminate Left Recursions

A1 → A2A3 | A4A4

A4 → bA3A4 | A4A4A4 | b A2 → bA3 → a

A4 → bA3A4 | b | bA3A4Z | bZ

Z → A4A4 | A4A4Z

Page 28: Simplifies and normal forms - Theory of Computation

Greibach Normal Form

28May 27, 2009

Example:

A1 → A2A3 | A4A4

A4 → bA3A4 | b | bA3A4Z | bZZ → A4A4 | A4A4 ZA2 → bA3 → a

A → αX

GNF

Page 29: Simplifies and normal forms - Theory of Computation

Greibach Normal Form

29May 27, 2009

Example:A1 → A2A3 | A4A4

A4 → bA3A4 | b | bA3A4Z | bZZ → A4A4 | A4A4 ZA2 → bA3 → a

Z → bA3A4A4 | bA4 | bA3A4ZA4 | bZA4 | bA3A4A4 | bA4 | bA3A4ZA4 | bZA4

A1 → bA3 | bA3A4A4 | bA4 | bA3A4ZA4 | bZA4

Page 30: Simplifies and normal forms - Theory of Computation

Greibach Normal Form

30May 27, 2009

Example:

A1 → bA3 | bA3A4A4 | bA4 | bA3A4ZA4 | bZA4

A4 → bA3A4 | b | bA3A4Z | bZZ → bA3A4A4 | bA4 | bA3A4ZA4 | bZA4 | bA3A4A4 | bA4 | bA3A4ZA4 | bZA4 A2 → bA3 → a

Grammar in Greibach Normal Form

Page 31: Simplifies and normal forms - Theory of Computation

T

hank You

01/05/23