LING/C SC/PSYC 438/538 Lecture 21 Sndiwy Fong
Tody's Topics Homework 8 Review Optionl Homework 9 (mke up on Homework 7)
Homework 8 Review Question1: write Prolog regulr grmmr for the following lnguge: Σ = {0, 1} L = strings from Σ* with n odd number of 0's nd n even number of 1's ssume 0 is n even number Hint: strt with FSA How mny grmmr rules do you hve? submit your progrm nd smple runs Exmples: *[] 0 000 00000 *1 *10 *01 *11 011 101 110 (empty string)
Homework 8 Review # 0's cn be odd or even only # 1's cn be odd or even only Four possible sttes (bbrevited): e0e1: even 0 even 1 o0e1: odd 0 even 1 e0o1: even 0 odd 1 o0o1: odd 0 odd 1 Strt stte: e0e1 empty string: zero 0's nd 1's Accept stte: o0e1 1. e0e1 --> [0], o0e1. 2. e0e1 --> [1], e0o1. 3. o0e1 --> []. 4. o0e1 --> [0], e0e1. 5. o0e1 --> [1], o0o1. 6. o0o1 --> [0], e0o1. 7. o0o1 --> [1], o0e1. 8. e0o1 --> [0], o0o1. 9. e0o1 --> [1], e0e1.
Homework 8 Review?- [hw8]. true ; true.?- e0e1([0,0,0,0],[]). flse.?- e0e1([1,1,1],[]). flse. flse.?- e0e1([0],[]).?- e0e1([1,0],[]). true ; flse.?- e0e1([],[]). flse.?- e0e1([0,1,1],[]). true ; flse. flse.?- e0e1([0,1],[]).?- e0e1([0,0,0],[]). true ; flse.?- e0e1([1],[]). flse.?- e0e1([1,0,1],[]). true ; flse. flse.?- e0e1([0,0],[]). flse.?- e0e1([1,1],[]). flse.?- e0e1([1,1,0],[]).
Homework 8 Review Recll Σ * Prolog code?
Homework 8 Review 1. e0e1 --> [0], o0e1. 2. e0e1 --> [1], e0o1. 3. o0e1 --> []. 4. o0e1 --> [0], e0e1. 5. o0e1 --> [1], o0o1. 6. o0o1 --> [0], e0o1. 7. o0o1 --> [1], o0e1. 8. e0o1 --> [0], o0o1. 9. e0o1 --> [1], e0e1. 1. e0e1 --> [0]. 2. e0e1 --> [0], o0e1. 3. e0e1 --> [1], e0o1. 4. o0e1 --> [0], e0e1. 5. o0e1 --> [1], o0o1. 6. o0o1 --> [0], e0o1. 7. o0o1 --> [1]. 8. o0o1 --> [1], o0e1. 9. e0o1 --> [0], o0o1. 10.e0o1 --> [1], e0e1. right recursive cnonicl formt for regulr grmmrs
Homework 8 Review (D)FSA: Optionl Homework 9 (mke-up opportunity for Homework 7) Give regex for this DFSA Implement it in Perl or Python nd show it works correctly Due next Mondy
Homework 8 Review Wht hppens when you run the following Prolog query on your grmmr? s(string,[]).
Homework 8 Review?- sigms(l), e0e1(l,[]). L = [0] ; L = [0, 0, 0] ; L = [1, 1, 0] ; L = [1, 0, 1] ; L = [0, 1, 1] ; L = [0, 0, 0, 0, 0] ; L = [1, 1, 0, 0, 0] ; L = [1, 0, 1, 0, 0] ; L = [0, 1, 1, 0, 0] ; L = [1, 0, 0, 1, 0] ; L = [0, 1, 0, 1, 0] ; L = [0, 0, 1, 1, 0] ; L = [1, 1, 1, 1, 0] ; L = [1, 0, 0, 0, 1] ; L = [0, 1, 0, 0, 1] ; L = [0, 0, 1, 0, 1] ; L = [1, 1, 1, 0, 1] ; L = [0, 0, 0, 1, 1] ; L = [1, 1, 0, 1, 1] ; L = [1, 0, 1, 1, 1] ; L = [0, 1, 1, 1, 1] ; L = [0, 0, 0, 0, 0, 0, 0] ; L = [1, 1, 0, 0, 0, 0, 0] ; L = [1, 0, 1, 0, 0, 0, 0]
Homework 8 Review Use itertive deepening (mteril beyond this introductory course):?- [id_met, hw8]. true.?- id(e0e1(string,[])). String = [0] ; String = [0, 0, 0] ; String = [0, 1, 1] ; String = [1, 0, 1] ; String = [1, 1, 0] ; String = [0, 0, 0, 0, 0] ; String = [0, 0, 0, 1, 1] ; String = [0, 0, 1, 0, 1] ; String = [0, 0, 1, 1, 0] ; String = [0, 1, 0, 0, 1] ; String = [0, 1, 0, 1, 0] ; String = [0, 1, 1, 0, 0] ; String = [0, 1, 1, 1, 1] ; String = [1, 0, 0, 0, 1] ; String = [1, 0, 0, 1, 0] ; String = [1, 0, 1, 0, 0] ; String = [1, 0, 1, 1, 1] ; String = [1, 1, 0, 0, 0] ; String = [1, 1, 0, 1, 1] ; String = [1, 1, 1, 0, 1] ; String = [1, 1, 1, 1, 0] ; String = [0, 0, 0, 0, 0, 0, 0] ; String = [0, 0, 0, 0, 0, 1, 1] ; String = [0, 0, 0, 0, 1, 0, 1] ; String = [0, 0, 0, 0, 1, 1, 0] ; String = [0, 0, 0, 1, 0, 0, 1] ; String = [0, 0, 0, 1, 0, 1, 0] ; String = [0, 0, 0, 1, 1, 0, 0]
Left Recursion nd Set Enumertion Exmple: 1. s -->, [!]. 2. --> b, []. 3. -->, []. 4. b --> b, []. 5. b --> [b]. Grmmr is: regulr grmmr left recursive (nonterminl on left) Question Wht is the lnguge of this grmmr? Answer: Sheeptlk! b..! (# 's > 1) Sententil forms: s! b! b! b! Underscoring used here to indicte nonterminl 12
Left Recursion nd Set Enumertion Exmple: 1. s -->, [!]. 2. --> b, []. 3. -->, []. 4. b --> b, []. 5. b --> [b]. Prolog query:?- s([b,,,!],[]). true?- s([b,,,,,!],[]). true But it doesn t hlt when fced with string not in the lnguge?- s([b,,!],[]). ERROR: Out of locl stck 13
Left Recursion nd Set Enumertion Exmple: s -->, [!]. --> b, []. -->, []. b --> b, []. b --> [b]. In fct 14
Left Recursion nd Set Enumertion Exmple: 1. s -->, [!]. 2. --> b, []. 3. -->, []. 4. b --> b, []. 5. b --> [b]. Why??- s([b,,!],[]). ERROR: Out of locl stck 15
Left Recursion nd Set Enumertion left recursive regulr grmmr: 1. s -->, [!]. 2. --> b, []. 3. -->, []. 4. b --> b, []. 5. b --> [b]. Behvior hlts when presented with string tht is in the lnguge doesn t hlt when fced with string not in the lnguge unble to decide the lnguge membership question Surprisingly, the query:?- s(l,[]). enumertes the strings in the lnguge just fine.?- s(l,[]). L = [b,,,!] ; L = [b,,,,!] ; L = [b,,,,,!] ; L = [b,,,,,,!] ; L = [b,,,,,,,!] ; L = [b,,,,,,,,!] ; L = [b,,,,,,,,...] w L = [b,,,,,,,,,!]
Left Recursion nd Set Enumertion left recursive regulr grmmr: 1. s -->, [!]. 2. --> b, []. 3. -->, []. 4. b --> b, []. 5. b --> [b]. Choice point derivtion tree for?- s(l,[]). L = [b..] [b,..] [b,,..] [b,,,!] s! [Powerpoint nimtion] s! Behvior hlts when presented with string tht is in the lnguge doesn t hlt when fced with string not in the lnguge b b b b b nd so on b
Left Recursion nd Set Enumertion However, this slightly re-ordered left recursive regulr grmmr: 1. s -->, [!]. 2. -->, []. 3. --> b, []. 4. b --> b, []. 5. b --> [b]. Why? s [Powerpoint nimtion] (rules 2 nd 3 swpped) won t hlt when enumerting... descends infinitely using rule #2
So fr We've tlked bout: 1. Declrtive (logicl) reding of grmmr rules 2. Prolog query: s(string,[]). Cse 1. String is known: Is String L(G)? Cse 2. String is unknown: enumerte L(G) 3. Different serch strtegies Prolog's (left-to-right) depth-first serch Itertive deepening
Beyond Regulr Lnguges Beyond regulr lnguges n b n = {b, bb, bbb, bbbb,... } n 1 is not regulr lnguge Tht mens no FSA, RE or RG cn be built for this lnguge 1. We only hve finite number of sttes to ply with 2. We re only llowed simple free itertion (looping) 3. Pumping Lemm proof Perl regex:
Beyond Regulr Lnguges Lnguge n b n = {b, bb, bbb, bbbb,... } n>=1 Exmple: Set membership A regulr grmmr extended to llow both left nd right recursive rules cn ccept/generte it: nbn.pl 1. --> [], b. 2. b --> [b]. 3. b -->, [b]. Set enumertion
Beyond Regulr Lnguges Lnguge n b n = {b, bb, bbb, bbbb,... } n>=1 A regulr grmmr extended to llow both left nd right recursive rules cn ccept/generte it: 1. --> [], b. 2. b --> [b]. 3. b -->, [b]. Intuition: grmmr implements the stcking of prtil trees blnced for s nd b s: A B b A A B b
Beyond Regulr Lnguges Lnguge n b n = {b, bb, bbb, bbbb,... } n>=1 A regulr grmmr extended to llow both left nd right recursive rules cn ccept/generte it: 1. --> [], b. 2. b --> [b]. 3. b -->, [b]. A type-2 or context-free grmmr (CFG) hs no restrictions on wht cn go on the RHS of grmmr rule Note: CFGs still hve single nonterminl limit for the LHS of rule Exmple: 1. s --> [], [b]. 2. s --> [], s, [b].
Extr Argument: Prse Tree Recovering prse tree when wnt Prolog to return more thn just true/flse nswers in cse of true, we cn compute syntx tree representtion of the prse by dding n extr rgument to nonterminls pplies to ll grmmr rules (not just regulr grmmrs) Exmple s sheeptlk gin DCG (non-regulr, context-free): b! s --> [b], [],, [!]. --> []. (bse cse) --> [],. (recursive cse)
Extr Argument: Prse Tree Tree: b s! Prolog term dt structure: hierrchicl llows sequencing of rguments functor(rg 1,..,rg n ) ech rg i could be nother term or simple tom s(b,,(,()),!)
Extr Arguments: Prse Tree DCG s --> [b],[],, [!]. --> []. --> [],. bse cse --> []. (subtree) --> []. (()) --> []. (bse cse) (right recursive cse) recursive cse --> [],. (subtree) --> [], (subtree). ((,A)) --> [], (A). b s! s(b,,(,()),!) Ide: for ech nonterminl, dd n rgument to store its subtree
Extr Arguments: Prse Tree Prolog grmmr s --> [b], [],, [!]. --> []. (bse cse) --> [],. (right recursive cse) bse nd recursive cses (()) --> []. ((,A)) --> [], (A). b s! strt symbol cse s --> [b], [],, [!]. s(tree) --> [b], [], (subtree), [!]. s(s(b,,a,!) ) --> [b], [], (A), [!]. s(b,,(,()),!)
Extr Arguments: Prse Tree Prolog grmmr s --> [b], [],, [!]. --> []. --> [],. (bse cse) (right recursive cse) Equivlent Prolog grmmr computing prse s(s(b,,a,!)) --> [b], [], (A), [!]. (()) --> []. ((,A)) --> [], (A).
Quick Homework 10 Modify this grmmr to build prse trees for strings in the lnguge n b n, n 1. nbn.pl 1. --> [], b. 2. b --> [b]. 3. b -->, [b]. Show your code nd give exmple runs. Due next Mondy night