1 / 24

LING/C SC/PSYC 438/538

LING/C SC/PSYC 438/538. Lecture 15 10/15 Sandiway Fong. Adminstrivia. Reminder Homework 4 is due tonight…. Today’s Topics. Running a regular grammar in Prolog Set membership Set enumeration The problem of left recursion Beyond regular grammars…. Regex, FSA and a Regular Grammar.

graceland
Download Presentation

LING/C SC/PSYC 438/538

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. LING/C SC/PSYC 438/538 Lecture 15 10/15 Sandiway Fong

  2. Adminstrivia • Reminder • Homework 4 is due tonight…

  3. Today’s Topics • Running a regular grammar in Prolog • Set membership • Set enumeration • The problem of left recursion • Beyond regular grammars…

  4. Regex, FSA and a Regular Grammar • Informally, we can convert RG to a FSA • by treating • non-terminals as states • and introducing (new) states for rules of the form x --> [a]. • s --> []. • s --> [b], b. • s --> [b], s. • b --> [a], c. • c --> [b]. • c --> [b], b. • c --> [b], c. a b [Powerpoint animation] in order of the RG rules b b b > s b c e b FSA RG: both encode the language from {a,b} such that each a must be surrounded by at least one b on each side

  5. Regular Grammar • We can use Prolog queries to test for membership in the language generated by our grammar • Test cases • (* denotes string not in the language): • *ab*ba[a,b] [b,a] • bab[b,a,b] • λ (empty string) [] • bb [b,b] • *baba[b,a,b,a] • babab[b,a,b,a,b] • Output:

  6. Set Enumeration using Prolog • Regular Grammar • s --> []. • s --> [b], b. • s --> [b], s. • b --> [a], c. • c --> [b]. • c --> [b], b. • c --> [b], c. • Normally, we ask the set membership question when posing a Prolog query: • e.g. ?- s([a,b],[]). no • Prolog enumeration: ?- s(List,[]). • List is a Prolog variable • asks the question for what values of List is s(List,[]) true? • ; is disjunction (look for alternative answers) writes out list in full why? Prolog matches rules in the order in which they’re written

  7. Set Enumeration using Prolog Let’s swap rules 2 and 3 • Regular Grammar • s --> []. • s --> [b], s. • s --> [b], b. • b --> [a], c. • c --> [b]. • c --> [b], b. • c --> [b], c. • Prolog enumeration: ?- s(L,[]).

  8. Set Enumeration using Prolog • Similarly, if we swap rules 6 and 7 • Regular Grammar • s --> []. • s --> [b], b. • s --> [b], s. • b --> [a], c. • c --> [b]. • c --> [b], c. • c --> [b], b. • Prolog enumeration: ?- s(L,[]).

  9. Set Enumeration using Prolog • Using three slightly different grammars obtained by permuting the rules, we obtain: Enumerating the set in ascending length of string [] [b] [b,b] [b,b,b] [b,a,b] [b,b,b,b] [b,a,b,b] [b,b,a,b] [b,b,b,b,b] [b,a,b,b,b] [b,b,a,b,b] [b,b,b,a,b] [b,a,b,a,b]

  10. Set Enumeration using Prolog • Prolog’s computation rule limits its application • match first rule each time • (explore other matching rules by backtracking) • depth-first implementation • recursively explores leftmost subtree of computation before trying next • (is memory efficient)

  11. Set Enumeration using Prolog • A breadth-first expansion of the search space • (less memory efficient) • but would be able to enumerate the strings of the language (in ascending order of length) s [] [b], [a], [b] [b], [a], [b], [a], c [b], [a], [b], [b] [b], [a], [b], [b], b [b], [a], [b], [b], c [b], [] [b], [b], [a], [b] [b], [b], [a], [b], b [b], [b], [a], [b], c [b], [b], [] [b], [b], [b], [a], c [b], [b], [b], [] [b], [b], [b], [b], b [b], [b], [b], [b], s s [] [b], b [b], s s [] [b], [a], [b] [b], [a], [b], b [b], [a], [b], c [b], [] [b], [b], [a], c [b], [b], [] [b], [b], [b], b [b], [b], [b], s s [] [b], [a], c [b], [] [b], [b], b [b], [b], s s --> []. s --> [b], b. s --> [b], s. b --> [a], c. c --> [b]. c --> [b], b. c --> [b], c.

  12. Prolog’s Computation Rule • Prolog’s computation rule limits its application • match first rule each time • (explore other matching rules by backtracking) • depth-first implementation • recursively explores leftmost subtree of computation before trying next • (is memory efficient) • Set enumeration: can be a problem • Left recursive rules: are always a problem

  13. Example: s --> a, [!]. a --> ba, [a]. a --> a, [a]. ba --> b, [a]. b --> [b]. Grammar is: a regular grammar left recursive Question What is the language of this grammar? Left Recursion and Set Enumeration • Answer: Sheeptalk! • Sentential forms: • s • a! • baa! • baa! • baa! Underscoring used here to indicate a nonterminal

  14. Example: s --> a, [!]. a --> ba, [a]. a --> a, [a]. ba --> b, [a]. b --> [b]. Prolog query: ?- s([b,a,a,!],[]). true ?- s([b,a,a,a,a,!],[]). true But it doesn’t halt when faced with a string not in the language ?- s([b,a,!],[]). ERROR: Out of local stack Left Recursion and Set Enumeration

  15. Example: s --> a, [!]. a --> ba, [a]. a --> a, [a]. ba --> b, [a]. b --> [b]. In fact… Left Recursion and Set Enumeration

  16. Example: s --> a, [!]. a --> ba, [a]. a --> a, [a]. ba --> b, [a]. b --> [b]. ?- s([b,a,!],[]). ERROR: Out of local stack Why? Left Recursion and Set Enumeration

  17. left recursive regular grammar: s --> a, [!]. a --> ba, [a]. a --> a, [a]. ba --> b, [a]. b --> [b]. Behavior halts when presented with a string that is in the language doesn’t halt when faced with a string not in the language unable to decide the language membership question Surprisingly, the query: ?- s(L,[]). enumerates the strings in the language just fine. ?- s(L,[]). L = [b, a, a, !] ; L = [b, a, a, a, !] ; L = [b, a, a, a, a, !] ; L = [b, a, a, a, a, a, !] ; L = [b, a, a, a, a, a, a, !] ; L = [b, a, a, a, a, a, a, a, !] ; L = [b, a, a, a, a, a, a, a, a|...] w L = [b, a, a, a, a, a, a, a, a, !] Left Recursion and Set Enumeration

  18. left recursive regular grammar: s --> a, [!]. a --> ba, [a]. a --> a, [a]. ba --> b, [a]. b --> [b]. Behavior halts when presented with a string that is in the language doesn’t halt when faced with a string not in the language derivation tree for ?- s(L,[]). Left Recursion and Set Enumeration [Powerpoint animation] L = [b,a,a,!] L = [b,a,a|..] L = [b,a|..] L = [b|..] L = Choice point s s a ! a ! a ba a a and so on a ba a b a b b b

  19. However, this slightly re-ordered left recursive regular grammar: s --> a, [!]. a --> a, [a]. a -->ba, [a]. ba--> b, [a]. b --> [b]. (rules 2 and 3 swapped) won’t halt when enumerating Why? Left Recursion and Set Enumeration [Powerpoint animation] s a a a a ... descends infinitely using rule 2

  20. Beyond regular languages anbn = {ab, aabb, aaabbb, aaaabbbb, ... } n≥1 is not a regular language That means no FSA, RE or RG can be built for this set Beyond Regular Languages We only have a finite number of states to play with … We’re only allowed simple free iteration (looping) Pumping Lemma proof

  21. Language anbn = {ab, aabb, aaabbb, aaaabbbb, ... } n>=1 A regular grammar extended to allow both left and right recursive rules can accept/generate it: a --> [a], b. b --> [b]. b --> a, [b]. Example: Beyond Regular Languages Set membership Set enumeration

  22. Language anbn = {ab, aabb, aaabbb, aaaabbbb, ... } n>=1 A regular grammar extended to allow both left and right recursive rules can accept/generate it: a --> [a], b. b --> [b]. b --> a, [b]. Intuition: grammar implements the stacking of partial trees balanced for a’s and b’s: A A a B a B b A b Beyond Regular Languages

  23. Language anbn = {ab, aabb, aaabbb, aaaabbbb, ... } n>=1 A regular grammar extended to allow both left and right recursive rules can accept/generate it: a --> [a], b. b --> [b]. b --> a, [b]. Beyond Regular Languages • A type-2 or context-free grammar (CFG) has no restrictions on what can go on the RHS of a grammar rule • Note: • CFGs still have a single nonterminal limit for the LHS of a rule • Example: • s --> [a], [b]. • s --> [a], s, [b].

  24. Recovering a parse tree when want Prolog to return more than just Yes/Noanswers in case of Yes, we can compute a syntax tree representation of the parse by adding an extra argument to nonterminals applies to all grammar rules (not just regular grammars) Example sheeptalk again DCG (non-regular, context-free): s --> [b], [a], a, [!]. a --> [a]. (base case) a --> [a], a. (recursive case) s b a a ! a a a Extra Argument: Parse Tree

More Related