slides - Computer Science - University of San Francisco
slides - Computer Science - University of San Francisco slides - Computer Science - University of San Francisco
Automata TheoryCS411-2013F-08Context-Free GrammarsGuest Lecturer: Sophie EngleDepartment of Computer ScienceUniversity of San Francisco
- Page 2 and 3: 08-0: Context-Free GrammarsSet of T
- Page 4: 08-2: CFG ExampleS → aSS → BbB
- Page 7 and 8: 08-5: CFG ExampleS → aSS → BbB
- Page 9 and 10: 08-7: CFG ExampleCFG for L = {0 n 1
- Page 11 and 12: 08-9: CFG Formal DefinitionExample:
- Page 13 and 14: 08-11: Parse TreeA Parse Tree is a
- Page 16 and 17: 08-14: Fun with CFGsCreate a Contex
- Page 18: 08-16: Fun with CFGsCreate a Contex
- Page 22 and 23: 08-20: L REG ⊆ L CFGBase Cases:r
- Page 24 and 25: 08-22: L REG ⊆ L CFGBase Cases:r
- Page 26 and 27: 08-24: L REG ⊆ L CFGBase Cases:r
- Page 28 and 29: 08-26: L REG ⊆ L CFGRecursive Cas
- Page 30 and 31: 08-28: L REG ⊆ L CFGRecursive Cas
- Page 32 and 33: 08-30: L REG ⊆ L CFGRecursive Cas
- Page 37 and 38: 08-35: CFG - AmbiguityA CFG is ambi
- Page 39 and 40: 08-37: CFG - AmbiguityE → E +E|E
- Page 41 and 42: 08-39: CFG - AmbiguityE → E +E|E
- Page 43 and 44: 08-41: Fun with CFGsCreate a CFG fo
- Page 45 and 46: 08-43: Fun with CFGsCreate a CFG fo
- Page 47 and 48: 08-45: Fun with CFGsCreate an unamb
- Page 49 and 50: 08-47: Ambiguous LanguagesL 1 = {a
- Page 51 and 52: 08-49: (More) Fun with CFGsCreate a
Automata TheoryCS411-2013F-08Context-Free GrammarsGuest Lecturer: Sophie EngleDepartment <strong>of</strong> <strong>Computer</strong> <strong>Science</strong><strong>University</strong> <strong>of</strong> <strong>San</strong> <strong>Francisco</strong>
08-0: Context-Free GrammarsSet <strong>of</strong> Terminals (Σ)Set <strong>of</strong> Non-TerminalsSet <strong>of</strong> Rules, each <strong>of</strong> the form: → Special Non-Terminal – Initial Symbol
08-1: Generating Strings with CFGsStart with the initial symbolRepeat:Pick any non-terminal in the stringReplace that non-terminal with the right-handside <strong>of</strong> some rule that has that non-terminal asa left-hand sideUntil all elements in the string are terminals
08-2: CFG ExampleS → aSS → BbB → cBB → ǫGenerating a string:S replace S with aSaS replace S wtih BbaBb replace B wtih cBacBb replace B wtih ǫacb Final String
08-4: CFG ExampleS → aSS → BbB → cBB → ǫRegular Expression equivalent to this CFG:
08-5: CFG ExampleS → aSS → BbB → cBB → ǫRegular Expression equivalent to this CFG:a ∗ c ∗ b
08-6: CFG ExampleCFG for L = {0 n 1 n : n > 0}
08-7: CFG ExampleCFG for L = {0 n 1 n : n > 0}S → 0S1 or S → 0S1|01S → 01(note – can write:A → αA → βasA → α|β )(examples: 01, 0011, 000111)
08-8: CFG Formal DefinitionG = (V,Σ,R,S)V = Set <strong>of</strong> symbols, both terminals & non-terminalsΣ ⊂ V set <strong>of</strong> terminals (alphabet for the languagebeing described)R ⊂ ((V −Σ)×V ∗ ) Finite set <strong>of</strong> rulesS ∈ (V −Σ) Start symbol
08-9: CFG Formal DefinitionExample:S → 0S1S → 01Set theory Definition:G = (V,Σ,R,S)V = {S,0,1}Σ ⊂ V = {0,1}R ⊂ ((V −Σ)×V ∗ ) = {(S,0S0),(S,01)}S ∈ (V −Σ) = S
08-10: DerivationA Derivation is a listing <strong>of</strong> how a string is generated –showing what the string looks like after everyreplacement.S → ABA → aA|ǫB → bB|ǫS ⇒ AB⇒ aAB⇒ aAbB⇒ abB⇒ abbB⇒ abb
08-11: Parse TreeA Parse Tree is a graphical representation <strong>of</strong> aderivation.SABS ⇒ AB⇒ aAB⇒ aAbB⇒ abB⇒ abbB⇒ abba A b BεbBε
08-12: Parse TreeA Parse Tree is a graphical representation <strong>of</strong> aderivation.SABa A b BS ⇒ AB⇒ AbB⇒ aAbB⇒ aaAbB⇒ aaAb⇒ aabaAAεε
08-14: Fun with CFGsCreate a Context-Free Grammar for all strings over{a,b} which contain the substring “aba”S → AabaAA →aAA →bAA → ǫGive a parse tree for the string: bbabaa
08-15: Fun with CFGsCreate a Context-Free Grammar for all strings over{a,b} that begin or end with the substring bba(inclusive or)
08-16: Fun with CFGsCreate a Context-Free Grammar for all strings over{a,b} that begin or end with the substring bba(inclusive or)S → bbaAS → AbbaA →bAA →aAA → ǫ
08-18: L REG ⊆ L CFGWe will prove L REG ⊆ L CFG in two different ways:Prove by induction that, given any regularexpression r, we create a CFG G such thatL[G] = L[r]Given any NFA M, we create a CFG G such thatL[G] = L[M]
08-20: L REG ⊆ L CFGBase Cases:r = a, a ∈ Σ
08-21: L REG ⊆ L CFGBase Cases:S → ar = a, a ∈ Σ
08-22: L REG ⊆ L CFGBase Cases:r = ǫ
08-23: L REG ⊆ L CFGBase Cases:S → ǫr = ǫ
08-24: L REG ⊆ L CFGBase Cases:r = ∅
08-25: L REG ⊆ L CFGBase Cases:r = ∅S → SS
08-26: L REG ⊆ L CFGRecursive Cases:r = (r 1 r 2 )L[G 1 ] = L[r 1 ], Start symbol <strong>of</strong> G 1 = S 1L[G 2 ] = L[r 2 ], Start symbol <strong>of</strong> G 2 = S 2
08-27: L REG ⊆ L CFGRecursive Cases:r = (r 1 r 2 )L[G 1 ] = L[r 1 ], Start symbol <strong>of</strong> G 1 = S 1L[G 2 ] = L[r 2 ], Start symbol <strong>of</strong> G 2 = S 2G = all rules from G 1 and G 2 , plus plus newnon-terminal S, and new rule:S → S 1 S 2New start symbol S
08-28: L REG ⊆ L CFGRecursive Cases:r = (r 1 +r 2 )L[G 1 ] = L[r 1 ], Start symbol <strong>of</strong> G 1 = S 1L[G 2 ] = L[r 2 ], Start symbol <strong>of</strong> G 2 = S 2
08-29: L REG ⊆ L CFGRecursive Cases:r = (r 1 +r 2 )L[G 1 ] = L[r 1 ], Start symbol <strong>of</strong> G 1 = S 1L[G 2 ] = L[r 2 ], Start symbol <strong>of</strong> G 2 = S 2G = all rules from G 1 and G 2 , plus new non-terminal S,and new rules:S → S 1S → S 2Start symbol = S
08-30: L REG ⊆ L CFGRecursive Cases:r = (r ∗ 1)L[G 1 ] = L[r 1 ], Start symbol <strong>of</strong> G 1 = S 1
08-31: L REG ⊆ L CFGRecursive Cases:r = (r ∗ 1)L[G 1 ] = L[r 1 ], Start symbol <strong>of</strong> G 1 = S 1G = all rules from G 1 , plus new non-terminal S, andnew rules:S → S 1 SS → ǫStart symbol = S(Example)
08-35: CFG – AmbiguityA CFG is ambiguous if there exists at least onestring generated by the grammar that has > 1different parse treePrevious CFG is ambiguous (examples)S → AabaAA →aAA →bAA → ǫ
08-36: CFG – AmbiguityConsider the following CFG:E → E +E|E −E|E ∗E|NN → 0|1|2|3|4|5|6|7|8|9Is this CFG ambiguous?Why is this a problem?
08-37: CFG – AmbiguityE → E +E|E −E|E ∗E|NN → 0|1|2|3|4|5|6|7|8|9EEE + EE*EN E * E3 N NE + E NN N 54534
08-38: CFG – AmbiguityE → E +E|E −E|E ∗E|NN → 0|1|2|3|4|5|6|7|8|9If all we care about is removing ambiguity, there isa (relatively) easy way to make this unambiguous(make all operators right-associative)
08-39: CFG – AmbiguityE → E +E|E −E|E ∗E|NN → 0|1|2|3|4|5|6|7|8|9Non-ambiguous:E → N|N +E|N −E|N ∗EN → 0|1|2|3|4|5|6|7|8|9If we were writing a compiler, would this be a goodCFG?How can we get correct associativity
08-40: CFG – AmbiguityAmbiguous:E → E +E|E −E|E ∗E|NN → 0|1|2|3|4|5|6|7|8|9Unambiguous:E → E +T|E −T|TT → T ∗N|NN → 0|1|2|3|4|5|6|7|8|9Can add parentheses, other operators, etc. (More inCompilers)
08-41: Fun with CFGsCreate a CFG for all strings over {(,)} that formbalanced parenthesis()()()(()())((()()()))((((()))))
08-42: Fun with CFGsCreate a CFG for all strings over {(,)} that formbalanced parenthesisS → (S)S → SSS → ǫIs this grammar ambiguous?
08-43: Fun with CFGsCreate a CFG for all strings over {(,)} that formbalanced parenthesisS → (S)S → SSS → ǫIs this grammar ambiguous?YES! (examples)
08-44: Fun with CFGsCreate an unambiguous CFG for all strings over{(,)} that form balanced parenthesis
08-45: Fun with CFGsCreate an unambiguous CFG for all strings over{(,)} that form balanced parenthesisS → ASS → ǫA → (S)
08-46: Ambiguous LanguagesA language L is ambiguous if all CFGs G thatgenerate it are ambiguousExample:L 1 = {a i b i c j d j |i,j > 0}L 2 = {a i b j c j d i |i,j > 0}L 3 = L 1 ∪L 2L 3 is inherently ambiguous(Create a CFG for L 3 )
08-47: Ambiguous LanguagesL 1 = {a i b i c j d j |i,j > 0}L 2 = {a i b j c j d i |i,j > 0}L 3 = L 1 ∪L 2S → S 1 |S 2S 1 → ABA → aAb|abB → cBd|cdS 2 → aS 2 d|aCdC → bCc|bcWhat happens when i = j?
08-48: (More) Fun with CFGsCreate an CFG for all strings over {a, b} that havethe same number <strong>of</strong> a’s as b’s (can be ambiguous)
08-49: (More) Fun with CFGsCreate an CFG for all strings over {a, b} that havethe same number <strong>of</strong> a’s as b’s (can be ambiguous)S → aSbS → bSaS → SSS → ǫ
08-50: (More) Fun with CFGsCreate an CFG for L = {ww R : w ∈ (a+b) ∗ }
08-51: (More) Fun with CFGsCreate an CFG for L = {ww R : w ∈ (a+b) ∗ }S → aSaS → bSbS → ǫ
08-52: (More) Fun with CFGsCreate an CFG for all palindromes over {a,b}.That is, create a CFG for:L = {w : w ∈ (a+b) ∗ ,w = w R }
08-53: (More) Fun with CFGsCreate an CFG for all palindromes over {a,b}.That is, create a CFG for:L = {w : w ∈ (a+b) ∗ ,w = w R }S → aSaS → bSbS → ǫS → aS → b
08-54: (More) Fun with CFGsCreate an CFG for L = {a i b j c k : j > i+k}
08-55: (More) Fun with CFGsCreate an CFG for L = {a i b j c k : j > i+k}HINT: We may wish to break this down into 3 differentlangauges ...
08-56: (More) Fun with CFGsCreate an CFG for L = {a i b j c k : j > i+k}S → ABCA → aAbA → ǫB → bBB → bC → bCc|ǫ
08-57: (More) Fun with CFGsCreate an CFG for all strings over {0, 1} that havethe an even number <strong>of</strong> 0’s and an odd number <strong>of</strong>1’s.HINT: It may be easier to come up with 4CFGs – even 0’s, even 1’s, odd 0’s odd 1’s,even 0’s odd 1’s, odd 1’s, even 0’s – andcombine them ...
08-58: (More) Fun with CFGsCreate an CFG for all strings over {0, 1} that havethe an even number <strong>of</strong> 0’s and an odd number <strong>of</strong>1’s.S 1 = Even 0’s Even 1’sS 2 = Even 0’s Odd 1’sS 3 = Odd 0’s Even 1’sS 4 = Odd 0’s Odd 1’sS 1 → 0S 3 |1S 2S 2 → 0S 4 |1S 1S 3 → 0S 1 |1S 4S 4 → 0S 2 |1S 3