Presentation is loading. Please wait.

Presentation is loading. Please wait.

Context-Free Grammars

Similar presentations


Presentation on theme: "Context-Free Grammars"— Presentation transcript:

1 Context-Free Grammars
1. Formal Definition 2. Construction 3. Parse Tree 4. Ambiguity 5. Simplification of CFG 6. CNF & GNF

2 English Grammar sentence  noun_phrase predicate
noun_phrase  article noun predicate  verb article  a | an | the noun  boy | dog verb  runs | walks a boy runs a dog walks

3 Context-Free Grammar A grammar G=( V, T, S, P ) is said to be context-free if all productions in P have the form A   , where AV, ( V T )*

4 Palindrome Language L={ w | w{0,1}* and w = wR } recursive definition
basis , 0, 1 are palindromes. induction If w is a palindrome, so is 0w 0 and 1w1.

5 Palindrome Language L={ w | w{0,1}* and w = wR }
definition with grammars or rules 1.  is a palindrome. 2. 0 is a palindrome. 3. 1 is a palindrome. 4. If w is a palindrome, so is 0w0. 5. If w is a palindrome, so is 1w1.

6 CFG & Palindrome Language
L={ w | w{0,1}* and w = wR } 1.  is a P. 1. P   2. 0 is a P. 2. P  0 3. 1 is a P. 3. P  1 4. If w is a P, so is 0w0. 4. P  0P 0 事儿是一件事儿,只是换了另一种说法。或者采用了另一种语言。 5. If w is a P, so is 1w1. 5. P  1P 1

7 CFG of Palindrome Language
L={ w | w{0,1}* and w = wR } CFG for palindromes on {0,1} R = ({S }, {0,1}, P, S ), P is defined as follow S   , S  0, S  1, S  0S 0, S  1S 1 Compact notation S   | 0 | 1 | 0S 0 | 1S 1

8 Example 7.1 L={ 0n1n | n  0 } R = ({S }, {0,1}, P, S ), P is defined as follow S   | 0S1

9 Example 7.2 L={ 0n1m | n  m } R = ({S,A,B,C }, {0,1}, P, S ), P is defined as follow S  AC | CB, C  0C1 |  A  A0 | 0 , B  1B | 1

10 Example 7.3 L={ w{0,1}* | w contains same number of 0’s and 1’s } R = ({S }, {0,1}, P, S ), P is defined as follow S   | 0S1 | 1S0 |SS

11 Example 7.4 L={w {0,1}*| n0(w)=n1(w) and n0(v)  n1(v) where v is any prefix of w } R = ({S }, {0,1}, P, S ), P is defined as follow S   | 0S1 | SS

12 Derivations and Recursive Inferences
L={a2nbm | n  0, m  0 } R = ({S,A,B }, {a,b}, P, S ), P is defined as follow S  AB , A  |aaA , B  | Bb for w =aabb : SABaaABaaABbaaBbaaBbbaabb B Bb B  Bb S  AB A  aaA A   B  

13 Context-Free Language
Let G=( V, T, S, P ) be context-free, then L(G) = {w | w T * and S  w }

14 Left/Right Most Derivations
L={a2nbm | n  0, m  0 } S  AB , A  |aaA , B  | Bb for w =aabb : SABaaABaaABbaaBbaaBbbaabb Left most : SABaaABaaBaaBbaaBbbaabb Right most : SABABbABbbAbbaaAbbaabb

15 Parse Tree Let G = ( V,T , S, P ) be a CFG. A tree is a parse tree for G if : 1. Each interior node is labeled by a variable in V 2. Each leaf is labeled by a symbol in T{}. Any -labeled leaf is the only child of its parent. 3. If an interior node is labeled A, and its children (from left to right) labeled x1,x2, ,xk, Then A  x1,x2, ,xk  P .

16 Parse Tree Example 7.5 L={ w | w{0,1}* and w = wR }
S   | 0 | 1 | 0S 0 | 1S 1 S S 0 S 0 0 S 0 recursive inferences derivations 1 S 1 0 S 0 1 w=0110 w=00100

17 Ambiguity G = ({E, I }, {a, b, (, ), +, }, P, E )
EI |E +E |E E|(E), Ia | b Derivation for w = a a  a : EEEE+EEI+EEa+EE a+aa EEEI+Ea+Ea+EEa+aa

18 Ambiguity parse-tree for w = a a  a : E E E  E E  E E + E I I

19 Removing Ambiguity EI | E+E | EE | (E) , Ia | b
ET|E+T, TF|TF, FI|(E) , Ia|b|Ia|Ib Left most derivation for w = a a  a : EETT+TF+T I+T a+Ta+TF a+FFa+IFa+aFa+aIa+aa ETTT(E)T(E+T)T(a+a)a

20 Inherent Ambiguity What is inherent ambiguity
A CFL L is said to be inherently ambiguous if every grammar that generates it is ambiguous. Example 7.6 Let L={ w | w{0,1}* and n0(w)=n1(w) } L is not inherently ambiguous ,because there is an unambiguous CFG : S   | 0S1 | 1S0 | 0S11S0 | 1S00S1

21 Example 7.7 L={anbncmdm | n1, m1} {anbmcmdn | n1, m1} The CFG for L is : SAB | C , AaAb | ab, BcBd | cd CaCd | aDd, DbDc | bc Let w= abcd , there are two left most derivations SAB abB abcd SC aDd abcd

22 Simplification of CFG Why & what :
SA | B, A1CA | 1DE | , B1CB | 1DF, C1CC | 1DG | 0G, D1CD | 1DH | 0H, E0A, F0B, G, H1 -productions unit productions useless symbols and productions

23 Eliminating -productions
* Variable A is said to be nullable if A  . Let G=(V,T,P,S) is a CFG If A → P, then A is nullable. If A → A1 A2  AkP, and Ai → P for i=1, ,k then A is nullable. Example 7.8 G : SAB, AaAA|, BbBB| A  A is nullable. SAB  S is nullable. B  B is nullable.

24 Eliminating unit productions
Example 7.9 G : SA|B|0S1, A0A|0, B1B|1 S 0A|0|1B|1|0S1 A0A|0 B1B|1

25 Eliminating useless productions
A symbol X is useful for a grammar G=(V,T,P,S), if there is a derivation for some wT* * * S  X  w * A symbol X is generating if X  w for some wT* * A symbol X is reachable if S  X for {,}(VT)*

26 Example G : SAB|a, Ab S and A are generating , B is not. Eliminate B, that eliminate SAB, leaving Sa, Ab Now only S is reachable. So there leaves Sa. If eliminate non-reachable symbol first : SAB|a, Ab  SAB|a, Ab Then eliminate non-generating symbol : SAB|a, Ab  Sa, Ab

27 Example 7.11 G : SA | B, A1CA | 1DE | 
B1CB | 1DF, C1CC| 1DG | 0G, D1CD | 1DH | 0H, E0A, F0B, G, H1 eliminating -productions the only one : A SA | B, A1CA | 1C | 1DE, B1CB | 1DF, C1CC | 1DG | 0G, D1CD | 1DH | 0H, E0A|0, F0B, G, H1

28 SA | B, A1CA | 1C | 1DE, B1CB | 1DF,
C1CC | 1DG | 0G, D1CD | 1DH | 0H, E0A|0, F0B, G, H1 eliminating unit productions the only two : SA and SB S1CA | 1C | 1DE|1CB | 1DF, A1CA | 1C | 1DE, B1CB | 1DF, C1CC | 1DG | 0G, D1CD|1DH | 0H, E0A|0, F0B, G, H1

29 S1CA | 1C | 1DE|1CB | 1DF, A1CA | 1C | 1DE, B1CB | 1DF, C1CC | 1DG | 0G, D1CD|1DH | 0H, E0A|0, F0B, G, H1 eliminating useless symbols and productions S1DE ,A1DE , D1DH | 0H , E0A | 0, H1

30 Chomsky Normal Form(CNF)
1. A BC ; 2. A a . S1DE ,A1DE , D1DH | 0H , E0A | 0, H1 Chomsky normal form : SIE ,AIE, DIH|EH, EEA|0, IHD, H1 DIH|FH, EFA|0, F0

31 Chomsky Normal Form(CNF)
Example 7.12 Convert following grammar to CNF SABa , Aaab , BAc

32 Greibach Normal Form(GNF)
A  ax , where aT , xV Example 7.13 Convert following grammar to GNF SAB , AaA|bB|b , Bb Example 7.14 Convert following grammar to GNF S01S1|00

33 ? ? ? eliminating -productions :   L ? Greibach normal form : A  a advantage ? Chomsky normal form : A  a |BC advantage ? left recursiveness A  A shortage ?


Download ppt "Context-Free Grammars"

Similar presentations


Ads by Google