240 likes | 322 Views
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.
E N D
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 • 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
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:
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
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,[]).
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,[]).
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]
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)
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.
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
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
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
Example: s --> a, [!]. a --> ba, [a]. a --> a, [a]. ba --> b, [a]. b --> [b]. In fact… Left Recursion and Set Enumeration
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
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
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
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
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
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
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
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].
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