Presentation is loading. Please wait.

Presentation is loading. Please wait.

LING 388 Language and Computers Lecture 8 9/25/03 Sandiway FONG.

Similar presentations


Presentation on theme: "LING 388 Language and Computers Lecture 8 9/25/03 Sandiway FONG."— Presentation transcript:

1 LING 388 Language and Computers Lecture 8 9/25/03 Sandiway FONG

2 Administrivia Last Lecture Last Lecture  Homework 2  Exercise 1: 2 pts, Exercise 2: 3pts, Exercise 3: 1pt This Lecture This Lecture  Homework 2 contd…  Exercise 4: 1pt, Exercise 5: 3 pts Next Lecture Next Lecture  Back in classroom  Franklin 220

3 Administrivia Revised TA Office Hours Revised TA Office Hours  Mondays 11am - 12pm  Haury 317  Other times by appointment  clin@email.arizona.edu clin@email.arizona.edu

4 Homework 1 Exercise 2 Review Homework Question Homework Question  Use both built-ins atom_chars/2 and append/3 to write a general rule  addNT/2 “add n’t” defined as follows:  addNT(X,Y) converts between a modal or auxiliary verb X and its contracted negative counterpart Y  Examples: could couldn’t, is isn’t  Make sure it (A) rejects may mayn’t(A) rejects may mayn’t (B) handles irregular forms can can`t, shall shan`t, will won`t(B) handles irregular forms can can`t, shall shan`t, will won`t

5 Introduction Last time… Last time…  We saw two ways of encoding FSA using Prolog rules This lecture… This lecture…  We look at how Prolog’s Definite Clause Grammar (DCG) notation can be used to encode regular grammars

6 Let’s revisit L = {a + b + } sx y a a b b

7 Regular Grammars From Lecture 6… From Lecture 6…  Right-recursive regular grammar G ab  S -> aB  B -> aB  B -> bC  B -> b  C -> bC  C -> b  L(G ab ) = {a + b + }

8 Regular Grammars Equivalent DCG for G ab : Equivalent DCG for G ab :  s --> [a], b.  b --> [a], b.  b --> [b], c.  b --> [b].  c --> [b], c.  c --> [b]. Notes: Notes:  --> = “rewrites”  Terminal symbols are enclosed in square brackets  Commas separate symbols on the right  Every rule ends with a period

9 Regular Grammars DCG rules and Prolog clauses DCG rules and Prolog clauses  Each DCG rule defines a clause with name = LHS of rule  Example: DCG rule s --> [a], b.s --> [a], b. is “syntactic sugar” in SWI-Prolog for a clause for predicate s/2: s(L1,L3) :- ‘C’(L1,a,L2), b(L2,L3).s(L1,L3) :- ‘C’(L1,a,L2), b(L2,L3). where ‘C’(L1,a,L2) calls a SWI-Prolog built-in that holds if the constant a is the difference between L1 and L2  DCG rule is automatically converted into the underlying Prolog clause  Use query ?- listing. to inspect…

10 Regular Grammars DCG rules and Prolog clauses contd. DCG rules and Prolog clauses contd.  The corresponding Prolog clause has arity 2  Example: s --> [a], b.s --> [a], b. s(L1,L3) :- ‘C’(L1,a,L2), b(L2,L3).s(L1,L3) :- ‘C’(L1,a,L2), b(L2,L3).  Each non-terminal takes two lists as arguments  The difference between the two lists represents the string covered by the non-terminal or terminal in the case of ‘C’  Example: s(L1,L3)L1 – L3s(L1,L3)L1 – L3 ‘C’(L1,a,L2)L1 – L2 = a‘C’(L1,a,L2)L1 – L2 = a b(L2,L3)L2 – L3b(L2,L3)L2 – L3

11 Difference Lists Examples of well-formed difference lists: Examples of well-formed difference lists:  [a,a,b,b] – [] = [a,a,b,b]  [a,a,b,b] – [b] = [a,a,b]  [a,a,b,b] – [a,b,b] = [a]  [a,a,b,b] – [a,a,b,b] = [] Notes: Notes:  In L1 – L2, L2 must be a suffix of L1, otherwise L1 – L2 is not a well-formed difference list  Examples of ill-formed difference lists:  [a,a,b,b] – [a,a]  [a,a,b,b] – [a,a,a,b,b,b]

12 Difference Lists We can visualize difference lists as follows: We can visualize difference lists as follows:  s --> [a], b.  s(L1,L3) :- ‘C’(L1,a,L2), b(L2,L3). Alternatively, given a non-terminal x and difference list L-L’ Alternatively, given a non-terminal x and difference list L-L’  L represents the input list to be parsed  L’ represents the remainder of the input list after rule x has been parsed L1 L2 L3

13 Exercise 4: Regular Grammars Consult the DCG for G ab Consult the DCG for G ab Use ?- listing. to see the underlying DCG rule conversion Use ?- listing. to see the underlying DCG rule conversion Run essentially the same Prolog queries used for the FSA in Lecture 7 Run essentially the same Prolog queries used for the FSA in Lecture 7  ?- s([a,a,b,b],[]).  ?- s([a,b,a],[]).  ?- s([a,X],[]). Confirm for yourselves that the DCG returns the same results as the FSA

14 Exercise 4: Regular Grammars Example of Derivation: Example of Derivation:  ?- s([a,a,b,b],[]).S -> [a], b 'C'([a, a, b, b], a, L) L = [a, b, b] 'C'([a, a, b, b], a, L) L = [a, b, b] b([a, b, b], [])b -> [a], b b([a, b, b], [])b -> [a], b 'C'([a, b, b], a,L)L = [b, b] 'C'([a, b, b], a,L)L = [b, b] b([b, b], [])b -> [a], b b([b, b], [])b -> [a], b  'C'([b, b], a,L)FAIL  b([b, b], []) b -> [b], c 'C'([b, b], b,L)L = [b] 'C'([b, b], b,L)L = [b] c([b], [])c -> [b], c c([b], [])c -> [b], c 'C'([b], b, L) L = [] 'C'([b], b, L) L = [] c([], [])c -> [b], c c([], [])c -> [b], c  'C'([], b, L)FAIL  c([b], [])c -> [b]

15 Exercise 4: Regular Grammars Note:  We can access any non-terminal, not just the start symbol s Run Prolog queries for non-terminal c Run Prolog queries for non-terminal c  ?- c([b,b,b],[]).  ?- c([a,b],[]).

16 Exercise 4: Regular Grammars Homework Question: Homework Question:  What language does the sub-grammar starting with non-terminal b accept?  Give the answer in the form of a regular expression

17 Beyond Regular Grammars Recall from Lecture 6… Recall from Lecture 6…  For regular grammars, can’t have both left and right recursive rules in the same grammar A -> aBA -> aB A -> BaA -> Ba  Would change the expressive power of the grammar formalism and lose the correspondence with FSA and regular expressions

18 Beyond Regular Grammars DCG rules have no such limitations DCG rules have no such limitations  For example:  We can have both left and right recursive rules  We can have rules with more than one non- terminal and terminal symbol on the right side  We can even have complex non-terminals that contain variables and values…later

19 Left and Right Recursive Rules Recall from Lecture 6… Recall from Lecture 6…  The following grammar G n with starting non- terminal A generates the non-regular language L = {a n b n | n>=0 } A -> A ->  A -> aBA -> aB B -> AbB -> Ab B -> bB -> b  Can’t build a FSA to handle G n

20 Left and Right Recursive Rules Equivalent DCG for G n : Equivalent DCG for G n :  a --> []. a(L,L).  a --> [a], b. a(L1,L3) :- ‘C’(L1,a,L2), b(L2,L3).  b --> a, [b]. b(L1,L3) :- a(L1,L2), ‘C’(L2,b,L3).  b --> [b]. b([b|L],L).

21 Exercise 5: Left and Right Recursive Rules Consult DCG for G n Consult DCG for G n Check DCG conversion using ?- listing. Check DCG conversion using ?- listing. Run Prolog queries: Run Prolog queries:  ?- a([],[]).  ?- a([a,b],[]).  ?- a([a,a,a,b,b,b],[]).  Check also that…  ?- a([a,a,a,b,b],[]).#a > #b  ?- a([a,a,b,b,b],[]).#a < #b  ?- a([a],[]).No b all fail

22 Exercise 5: Left and Right Recursive Rules Homework Question (A) Homework Question (A)  What does the query ?- a(X,[]). return?  Note: Use  ?- set_prolog_flag(toplevel_print_options,[max_depth(0)]).  to see the entire list Homework Question (B) Homework Question (B)  What happens to the query ?- a(X,[]). if we switch the order of the clauses for non-terminal a?  Explain why


Download ppt "LING 388 Language and Computers Lecture 8 9/25/03 Sandiway FONG."

Similar presentations


Ads by Google