Simplifies and normal forms - Theory of Computation

Post on 20-Jan-2017

41 views 3 download

Transcript of Simplifies and normal forms - Theory of Computation

Simplifies form and Normal form

Simplifies form

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

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)

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.

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)

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

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

Eliminating Unit Production(Unit production removal)

Eliminating Unit Production(Unit production removal)

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

Normal forms

definition

Theorem

Example of CFG Conversion

Removing Rules

Removing unit rule

More unit rules

Converting remaining rules

Presentation Outline

20May 27, 2009

• Greibach Normal Form

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.

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

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

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

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

Greibach Normal Form

26May 27, 2009

Example:

Second Step

Eliminate

Left Recursions

A1 → A2A3 | A4A4

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

A4 → A4A4A4

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

Greibach Normal Form

28May 27, 2009

Example:

A1 → A2A3 | A4A4

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

A → αX

GNF

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

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

T

hank You

01/05/23