Download presentation
Presentation is loading. Please wait.
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 AV, ( 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 : SABaaABaaABbaaBbaaBbbaabb 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 : SABaaABaaABbaaBbaaBbbaabb Left most : SABaaABaaBaaBbaaBbbaabb Right most : SABABbABbbAbbaaAbbaabb
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 )
EI |E +E |E E|(E), Ia | b Derivation for w = a a a : EEEE+EEI+EEa+EE a+aa EEEI+Ea+Ea+EEa+aa
18
Ambiguity parse-tree for w = a a a : E E E E E E E + E I I
19
Removing Ambiguity EI | E+E | EE | (E) , Ia | b
ET|E+T, TF|TF, FI|(E) , Ia|b|Ia|Ib Left most derivation for w = a a a : EETT+TF+T I+T a+Ta+TF a+FFa+IFa+aFa+aIa+aa ETTT(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 | n1, m1} {anbmcmdn | n1, m1} The CFG for L is : SAB | C , AaAb | ab, BcBd | cd CaCd | aDd, DbDc | bc Let w= abcd , there are two left most derivations SAB abB abcd SC aDd abcd
22
Simplification of CFG Why & what :
SA | B, A1CA | 1DE | , B1CB | 1DF, C1CC | 1DG | 0G, D1CD | 1DH | 0H, E0A, F0B, G, H1 -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 AkP, and Ai → P for i=1, ,k then A is nullable. Example 7.8 G : SAB, AaAA|, BbBB| A A is nullable. SAB S is nullable. B B is nullable.
24
Eliminating unit productions
Example 7.9 G : SA|B|0S1, A0A|0, B1B|1 S 0A|0|1B|1|0S1 A0A|0 B1B|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 wT* * * S X w * A symbol X is generating if X w for some wT* * A symbol X is reachable if S X for {,}(VT)*
26
Example G : SAB|a, Ab S and A are generating , B is not. Eliminate B, that eliminate SAB, leaving Sa, Ab Now only S is reachable. So there leaves Sa. If eliminate non-reachable symbol first : SAB|a, Ab SAB|a, Ab Then eliminate non-generating symbol : SAB|a, Ab Sa, Ab
27
Example 7.11 G : SA | B, A1CA | 1DE |
B1CB | 1DF, C1CC| 1DG | 0G, D1CD | 1DH | 0H, E0A, F0B, G, H1 eliminating -productions the only one : A SA | B, A1CA | 1C | 1DE, B1CB | 1DF, C1CC | 1DG | 0G, D1CD | 1DH | 0H, E0A|0, F0B, G, H1
28
SA | B, A1CA | 1C | 1DE, B1CB | 1DF,
C1CC | 1DG | 0G, D1CD | 1DH | 0H, E0A|0, F0B, G, H1 eliminating unit productions the only two : SA and SB S1CA | 1C | 1DE|1CB | 1DF, A1CA | 1C | 1DE, B1CB | 1DF, C1CC | 1DG | 0G, D1CD|1DH | 0H, E0A|0, F0B, G, H1
29
S1CA | 1C | 1DE|1CB | 1DF, A1CA | 1C | 1DE, B1CB | 1DF, C1CC | 1DG | 0G, D1CD|1DH | 0H, E0A|0, F0B, G, H1 eliminating useless symbols and productions S1DE ,A1DE , D1DH | 0H , E0A | 0, H1
30
Chomsky Normal Form(CNF)
1. A BC ; 2. A a . S1DE ,A1DE , D1DH | 0H , E0A | 0, H1 Chomsky normal form : SIE ,AIE, DIH|EH, EEA|0, IHD, H1 DIH|FH, EFA|0, F0
31
Chomsky Normal Form(CNF)
Example 7.12 Convert following grammar to CNF SABa , Aaab , BAc
32
Greibach Normal Form(GNF)
A ax , where aT , xV Example 7.13 Convert following grammar to GNF SAB , AaA|bB|b , Bb Example 7.14 Convert following grammar to GNF S01S1|00
33
? ? ? eliminating -productions : L ? Greibach normal form : A a advantage ? Chomsky normal form : A a |BC advantage ? left recursiveness A A shortage ?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.