Download presentation
Presentation is loading. Please wait.
Published byViktoria Sternberg Modified over 6 years ago
1
LING/C SC/PSYC 438/538 Lecture 21 Sandiway Fong
2
Today's Topics Homework 8 Review
Optional Homework 9 (make up on Homework 7)
3
Homework 8 Review Question1: write a Prolog regular grammar for the following language: Σ = {0, 1} L = strings from Σ* with an odd number of 0's and an even number of 1's assume 0 is an even number Hint: start with a FSA How many grammar rules do you have? submit your program and sample runs Examples: *[] (empty string) *1 *10 *01 *11
4
Homework 8 Review # 0's can be odd or even only
Four possible states (abbreviated): e0e1: even 0 even 1 o0e1: odd 0 even 1 e0o1: even 0 odd 1 o0o1: odd 0 odd 1 Start state: e0e1 empty string: zero 0's and 1's Accept state: o0e1 e0e1 --> [0], o0e1. e0e1 --> [1], e0o1. o0e1 --> []. o0e1 --> [0], e0e1. o0e1 --> [1], o0o1. o0o1 --> [0], e0o1. o0o1 --> [1], o0e1. e0o1 --> [0], o0o1. e0o1 --> [1], e0e1.
5
Homework 8 Review ?- [hw8]. true. ?- e0e1([0],[]). true ; false. ?- e0e1([0,0,0],[]). ?- e0e1([0,0],[]). ?- e0e1([0,0,0,0],[]). ?- e0e1([],[]). ?- e0e1([1],[]). ?- e0e1([1,1],[]). ?- e0e1([1,1,1],[]). ?- e0e1([0,1,1],[]). ?- e0e1([1,0,1],[]). ?- e0e1([1,1,0],[]). ?- e0e1([1,0],[]). ?- e0e1([0,1],[]).
6
Homework 8 Review Recall Σ* Prolog code?
7
Homework 8 Review e0e1 --> [0]. e0e1 --> [0], o0e1.
o0e1 --> [0], e0e1. o0e1 --> [1], o0o1. o0o1 --> [0], e0o1. o0o1 --> [1]. o0o1 --> [1], o0e1. e0o1 --> [0], o0o1. e0o1 --> [1], e0e1. e0e1 --> [0], o0e1. e0e1 --> [1], e0o1. o0e1 --> []. o0e1 --> [0], e0e1. o0e1 --> [1], o0o1. o0o1 --> [0], e0o1. o0o1 --> [1], o0e1. e0o1 --> [0], o0o1. e0o1 --> [1], e0e1. right recursive canonical format for regular grammars
8
Homework 8 Review (D)FSA:
Optional Homework 9 (make-up opportunity for Homework 7) Give a regex for this DFSA Implement it in Perl or Python and show it works correctly Due next Monday
9
Homework 8 Review What happens when you run the following Prolog query on your grammar? s(String,[]).
10
Homework 8 Review ?- sigmas(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]
11
Homework 8 Review Use iterative deepening (material beyond this introductory course): ?- [id_meta, hw8]. String = [0, 0, 1, 1, 0] ; String = [1, 1, 1, 1, 0] ; true. String = [0, 1, 0, 0, 1] ; String = [0, 0, 0, 0, 0, 0, 0] ; String = [0, 1, 0, 1, 0] ; String = [0, 0, 0, 0, 0, 1, 1] ; ?- id(e0e1(String,[])). String = [0, 1, 1, 0, 0] ; String = [0, 0, 0, 0, 1, 0, 1] ; String = [0] ; String = [0, 1, 1, 1, 1] ; String = [0, 0, 0, 0, 1, 1, 0] ; String = [0, 0, 0] ; String = [1, 0, 0, 0, 1] ; String = [0, 0, 0, 1, 0, 0, 1] ; String = [0, 1, 1] ; String = [1, 0, 0, 1, 0] ; String = [0, 0, 0, 1, 0, 1, 0] ; String = [1, 0, 1] ; String = [1, 0, 1, 0, 0] ; String = [0, 0, 0, 1, 1, 0, 0] String = [1, 1, 0] ; String = [1, 0, 1, 1, 1] ; String = [0, 0, 0, 0, 0] ; String = [1, 1, 0, 0, 0] ; String = [0, 0, 0, 1, 1] ; String = [1, 1, 0, 1, 1] ; String = [0, 0, 1, 0, 1] ; String = [1, 1, 1, 0, 1] ;
12
Left Recursion and Set Enumeration
Question What is the language of this grammar? Example: s --> a, [!]. a --> ba, [a]. a --> a, [a]. ba --> b, [a]. b --> [b]. Grammar is: a regular grammar left recursive (nonterminal on left) Answer: Sheeptalk! ba..a! (# a's > 1) Sentential forms: s a! baa! Underscoring used here to indicate a nonterminal
13
Left Recursion and Set Enumeration
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,!],[]). But it doesn’t halt when faced with a string not in the language ?- s([b,a,!],[]). ERROR: Out of local stack
14
Left Recursion and Set Enumeration
In fact… Example: s --> a, [!]. a --> ba, [a]. a --> a, [a]. ba --> b, [a]. b --> [b].
15
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?
16
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. 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, !]
17
Left Recursion and Set Enumeration
derivation tree for ?- s(L,[]). [Powerpoint animation] 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 L = [b,a,a,!] L = [b,a,a|..] L = [b,a|..] L = L = [b|..] Choice point s s a ! a ! ba a a a and so on b a ba a b a b b
18
Left Recursion and Set Enumeration
[Powerpoint animation] 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? s a a a a ... descends infinitely using rule #2
19
So far … We've talked about:
1. Declarative (logical) reading of grammar rules 2. Prolog query: s(String,[]). Case 1. String is known: Is String ∈ L(G)? Case 2. String is unknown: enumerate L(G) 3. Different search strategies Prolog's (left-to-right) depth-first search Iterative deepening
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 language We only have a finite number of states to play with … We’re only allowed simple free iteration (looping) Pumping Lemma proof Perl regex:
21
Beyond Regular Languages
Example: Language anbn = {ab, aabb, aaabbb, aaaabbbb, ... } n>=1 A regular grammar extended to allow both left and right recursive rules can accept/generate it: anbn.pl a --> [a], b. b --> [b]. b --> a, [b]. Set membership Set enumeration
22
Beyond Regular Languages
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: B A b a
23
Beyond Regular Languages
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]. 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
Extra Argument: Parse Tree
Recovering a parse tree when want Prolog to return more than just true/false answers in case of true, 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 a ! b
25
Extra Argument: Parse Tree
! b Prolog term data structure: hierarchical allows sequencing of arguments functor(arg1,..,argn) each argi could be another term or simple atom Tree: s(b,a,a(a,a(a)),!)
26
Extra Arguments: Parse Tree
DCG s --> [b],[a], a, [!]. a --> [a]. (base case) a --> [a], a. (right recursive case) s a ! b base case a --> [a]. a(subtree) --> [a]. a(a(a)) --> [a]. s(b,a,a(a,a(a)),!) recursive case a --> [a], a. a(subtree) --> [a], a(subtree). a(a(a,A)) --> [a], a(A). Idea: for each nonterminal, add an argument to store its subtree
27
Extra Arguments: Parse Tree
Prolog grammar s --> [b], [a], a, [!]. a --> [a]. (base case) a --> [a], a. (right recursive case) s a ! b base and recursive cases a(a(a)) --> [a]. a(a(a,A)) --> [a], a(A). start symbol case s --> [b], [a], a, [!]. s(tree) --> [b], [a], a(subtree), [!]. s(s(b,a,A,!) ) --> [b], [a], a(A), [!]. s(b,a,a(a,a(a)),!)
28
Extra Arguments: Parse Tree
Prolog grammar s --> [b], [a], a, [!]. a --> [a]. (base case) a --> [a], a. (right recursive case) Equivalent Prolog grammar computing a parse s(s(b,a,A,!)) --> [b], [a], a(A), [!]. a(a(a)) --> [a]. a(a(a,A)) --> [a], a(A).
29
Quick Homework 10 Modify this grammar to build parse trees for strings in the language anbn, n≥1. anbn.pl a --> [a], b. b --> [b]. b --> a, [b]. Show your code and give example runs. Due next Monday night
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.