FAFL Online Class

50
FAFL Online Class Presented By : S R Mani Sekhar

Transcript of FAFL Online Class

FAFL Online Class

Presented By : S R Mani Sekhar

Contents

• Derivation

• Parse tree

• Ambiguous grammar

• Normal Form

context-free grammar

• A context-free grammar (CFG) consisting

• G=(V, T, P, S)• where

• V is a set of non-terminal symbols.• T is a set of terminals • P is a set of rules, P: N → (N ∪ T)*• S is the start symbol.

• Example• The grammar G= ({A}, {a, b, c}, P, A),

• P : A → aA, A → abc.

• The grammar G=({S, a, b}, {a, b}, P, S),• P: S → aSa, S → bSb, S → ε

Derivation

• leftmost derivation.

• rightmost derivation.

Derivation

• Left most derivation: The process of deriving a string by expanding the leftmost non-terminal at each step is called as leftmost derivation.

LMD

• SaA | bBA g=(v,t,p,s)

• AaB v= S,A,B

• Bb t=a,b S= S

• String : aab ; bbab S aA ; SaaB; Saab

S bBA ; S bbA ; S bbaB ; S bbab

• Right most derivation: The process of deriving a string by expanding the rightmost non-terminal at each step is called as rightmost derivation.

RMD

• SaA | bBA S aA ; S aaB; S aab

• AaB S bBA; S bBaB; S bBab; Sbbab

• Bb

• String : aab ; bbab

Parse Tree

• Parse trees are a representation of derivations

Tree

Ambiguous grammar

• an ambiguous grammar is a context-free grammar for which there exists a string that can have more than one derivation or parse tree(different structure)

• while an unambiguous grammar is a context-free grammar for which every valid string has a unique derivation or parse tree

Ambiguous grammar

i. Obtain the LMD & RMD

ii. Obtain the 2 times LMD

iii. Obtain the 2 times RMD

• generate a string ‘w’. Design a tree if two tree a different grammar is ambiguous for all the above cases

LMD, RMD and Ambiguous: Question

w = aaabbabbba

S → aB / bA G=(V,T,P,S) ; V={S,A,B; T= a,b; S=S, P }

A → aS / bAA / a S aB; S aaBB ;S aaaBB S aaabbabbba

B → bS / aBB / bB/E

This is unambiguous grammar

Normal form – CNF,GNF

• Removing є-productions• A є A aA | E

• BE 1NT E

• Removing unit productions• A B

• Removing useless symbols• Those that cannot be derived from S and those that cannot reduce to a

terminal string

• Normal forms

Epsilon Productions

• є-productions: productions of the form A є

• Nullable symbols: symbols A whereA є

• For each production that has a nullable symbol on the right hand side, add a production without that symbol; apply rule iteratively on resulting productions

• After this step, all є-productions can be removed

• Note, if the language L generated by the original grammar includes є, then the language generated by the resulting grammar will be L – {є}

S aS | aAa

A a | E

S aS | aAa |aa

A a

Epsilon ProductionsQ1S aA|Bb

A є

B b

Sol

S aA | a |Bb

Bb

Q2S ABCa | bD

A BC | b

B b | є

C c | є

D d

Soln : B, C, A

S ABCa | BCa| ACa| ABa|Ca|Aa|Ba|a |bD

A BC | C|B | b

B b

Cc

Dd

Q3 epsilon production from the grammarS aAaB | Ba B, A

A B | bBDB

B є

C c

D d | D

S aAaB |aaB | aAa |aa|Ba |a

A B | bBDB| bDB |bBD |bD

C c

Dd|D

q4S aDAaB | Ba D,A,B

A Bb | bBDB |D

B є|b

C c |cDaA

D d | D|є

S aDAaB | aAaB | aDaB |aDAa|aaB |aDa |aa |Ba|a|aAa

A Bb |b| bBDB|bDB|bBB|bBD|bB |b|D

B b

Cc | cDaA|caA|cDa|ca

D d |D

Unit Productions

• Unit productions: all productions of the form A B

• Removing unit productions• Identify unit pairs: pairs of variables (A, B) such that A * B, and the

derivation involves only unit productions

• For each unit pair (A, B), add the production A w, whenever B w and w is not a variable

• Unit productions may now be removed

1NT 1NTS A Aa

• S aA |A |aAa|B|ac|aaaa|ADA|D

Q1

S A0 | B unit: S B; BA ; AB

B A | 11

A 0 | 12 |B

• S A0 | 0 | 12 | 11

• B 0 | 12 | 11

• A 0 | 12 | 11

Q2 unit production

S AB unit: B C ; C D ; D E

A a S AB ; Aa

B C | b B d| Ab|bC |b

C D C d| Ab|bC

D E | bC D d| Ab|bC

E d| Ab E d| Ab

Useless production

• NT are generating or not

• S a

• S Aa

• Aa | D

• Reachable or not : start symbol

• S aA | B

• B b

• A a

• Dd

Removing useless productions

• Remove productions with non-generating symbols• Requires identifying generating symbols recursively: right hand side of

production contains only terminals and generating symbols

• Remove productions with non-reachable symbols• Requires identifying reachable symbols recursively: S is reachable, and so are

symbols that exist on the right hand side of productions with reachable symbols on the left hand side

Q1 useless production

S aA | bB NT: S,A,B Sbb

A aA AaaaaA

B b

Generating: NT: S, A, B

SbB ; B b

Reachable

SbB ; B b NT: S, B

soln

S bB ; Bb

Q2 useless production

S aAb | bD

A a

B b

NT: S, A,B,D

Generating: SaAb; A a; Bb

Reachable: SaAb; A a; Bb

SaAb

Aa

q3

S aA | bB

A aA | a

B bB

D ab | Ea

E aC | d

CNF- Chomsky normal form

1. Epsilon production

2. Unit production

3. Useless production

4. CNF:

1NT 2NT S ABC | a

1NT 1T

Chomsky Normal Form

• CNF: all productions are of the form• A BC (B, C are variables)

• A a (a is a terminal)

• How do we convert a grammar to an equivalent CNF grammar?

q1• S AA | BC | bB NT: S,A,B,C

• A a | Bb T:a,b,c

• Bb

• Cc

• Soln

• No eplison production, no unit, no useless

• Converting the grammar into CNF

• S XA | BC |YB

• Aa|BY

• Bb

• Cc

• Xa

• Y b

• S aA | BCB | bBb NT: S,A,B,C

• A a | Bb T:a,b,c

• Bb

• Cc

• Soln

• S XA | ZB | DB

• A a |YB

• B b

• Cc bBb- BBB -

• X a

• Yb

• Z BC

• D BB

CNF• S aAb |Bb ;A є |B; Bb; Dd

• Soln

• Removal of epsilon production (A є)

• S aAb | ab | Bb ; A B ; Bb ;Dd

• Removal of unit production (AB)

• S aAb | ab | Bb ;Ab ; Bb ; Dd

• Removal of useless production• Generating : S aAb | ab | Bb ;Ab ; Bb ; Dd• Reachable: S aAb | ab | Bb ;Ab ; Bb

• Covert the grammar in to CNF• S aAb | ab | Bb ;Ab ; Bb aAb- XAY-ZY

• CNF:

• S ZY |XY | BY ; Ab ; Bb

• X a; Yb; Z XA

GNF

• CNF

• 1NT 2NT | 1T

• GNF

• 1NT 1T {any no.of NT}

• S a | aA | aAB | bASASJJLJL