Top Banner
LING 388 Language and Computers Lecture Lecture 8 8 9/ 9/ 25 25 /03 /03 Sandiway FONG Sandiway FONG
22

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

Dec 19, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: LING 388 Language and Computers Lecture 8 9/25/03 Sandiway FONG.

LING 388Language and Computers

Lecture Lecture 88

9/9/2525/03/03

Sandiway FONGSandiway FONG

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

Administrivia

Last LectureLast Lecture Homework 2 Homework 2

Exercise 1: 2 pts, Exercise 2: 3pts, Exercise 3: 1ptExercise 1: 2 pts, Exercise 2: 3pts, Exercise 3: 1pt This LectureThis Lecture

Homework 2 contd…Homework 2 contd… Exercise 4: 1pt, Exercise 5: 3 ptsExercise 4: 1pt, Exercise 5: 3 pts

Next LectureNext Lecture Back in classroom Back in classroom

Franklin 220 Franklin 220

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

Administrivia

Revised TA Office HoursRevised TA Office Hours Mondays 11am - 12pmMondays 11am - 12pm Haury 317Haury 317 Other times by appointmentOther times by appointment [email protected]@email.arizona.edu

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

Homework 1 Exercise 2 Review Homework QuestionHomework Question

Use both built-ins atom_chars/2 and append/3 to write a Use both built-ins atom_chars/2 and append/3 to write a general rulegeneral rule

addNT/2 “add n’t” addNT/2 “add n’t” defined as follows:defined as follows:

addNT(X,Y) converts between a modal or auxiliary verb addNT(X,Y) converts between a modal or auxiliary verb X and its contracted negative counterpart YX and its contracted negative counterpart Y

ExamplesExamples: could <-> couldn’t, is <-> isn’t: could <-> couldn’t, is <-> isn’t Make sure itMake sure it

• (A) rejects may <-> mayn’t(A) rejects may <-> mayn’t• (B) handles irregular forms can <-> can`t, shall <-> (B) handles irregular forms can <-> can`t, shall <->

shan`t, will <-> won`tshan`t, will <-> won`t

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

Introduction

Last time…Last time… We saw two ways of encoding FSA using We saw two ways of encoding FSA using

Prolog rulesProlog rules This lecture…This lecture…

We look at how Prolog’s Definite Clause We look at how Prolog’s Definite Clause Grammar (DCG) notation can be used to Grammar (DCG) notation can be used to encode regular grammarsencode regular grammars

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

Let’s revisit Let’s revisit L = {aL = {a++bb++}}

s x

y

aa

b

b

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

Regular Grammars

From Lecture 6…From Lecture 6… Right-recursive regular grammar GRight-recursive regular grammar Gabab

S -S -> > aBaB BB -> -> aBaB BB -> -> bCbC B B -> -> bb CC -> -> bCbC CC -> -> bb

L(GL(Gabab) = {a) = {a++bb++}}

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

Regular Grammars Equivalent DCG for GEquivalent DCG for Gabab::

s --> [a], b.s --> [a], b. b --> [a], b.b --> [a], b. b --> [b], c.b --> [b], c. b --> [b].b --> [b]. c --> [b], c.c --> [b], c. c --> [b].c --> [b].

NotesNotes:: --> = “rewrites”--> = “rewrites” Terminal symbols are enclosed in square bracketsTerminal symbols are enclosed in square brackets Commas separate symbols on the rightCommas separate symbols on the right Every rule ends with a periodEvery rule ends with a period

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

Regular Grammars DCG rules and Prolog clausesDCG rules and Prolog clauses

Each DCG rule defines a clause with name = LHS of ruleEach DCG rule defines a clause with name = LHS of rule ExampleExample::

DCG ruleDCG rule• s --> [a], b.s --> [a], b.is “syntactic sugar” in SWI-Prolog for a clause for predicate s/2: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 where ‘C’(L1,a,L2) calls a SWI-Prolog built-in that holds if the constant constant aa is the difference between is the difference between L1L1 and and L2L2

DCG rule is automatically converted into the underlying Prolog DCG rule is automatically converted into the underlying Prolog clauseclause

Use query ?- listing. to inspect… Use query ?- listing. to inspect…

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

Regular Grammars DCG rules and Prolog clauses contd.DCG rules and Prolog clauses contd.

The corresponding Prolog clause has arity 2The corresponding Prolog clause has arity 2 ExampleExample::

• 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 argumentsEach non-terminal takes two lists as arguments The difference between the two lists represents the string The difference between the two lists represents the string

covered by the non-terminal or terminal in the case of ‘C’covered by the non-terminal or terminal in the case of ‘C’ Example:Example:

• s(L1,L3)s(L1,L3) L1 – L3L1 – L3• ‘‘C’(L1,a,L2)C’(L1,a,L2) L1 – L2 = aL1 – L2 = a• b(L2,L3)b(L2,L3) L2 – L3L2 – L3

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

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] – [] = [a,a,b,b] [a,a,b,b] – [b] = [a,a,b][a,a,b,b] – [b] = [a,a,b] [a,a,b,b] – [a,b,b] = [a][a,a,b,b] – [a,b,b] = [a] [a,a,b,b] – [a,a,b,b] = [][a,a,b,b] – [a,a,b,b] = []

NotesNotes:: In L1 – L2, L2 must be a suffix of L1, otherwise L1 – L2 is not a In L1 – L2, L2 must be a suffix of L1, otherwise L1 – L2 is not a

well-formed difference listwell-formed difference list Examples of ill-formed difference lists:Examples of ill-formed difference lists:

[a,a,b,b] – [a,a] [a,a,b,b] – [a,a] [a,a,b,b] – [a,a,a,b,b,b][a,a,b,b] – [a,a,a,b,b,b]

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

Difference Lists

We can visualize difference lists as follows:We can visualize difference lists as follows: 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).

Alternatively, given a non-terminal Alternatively, given a non-terminal xx and difference list and difference list L-L’L-L’ LL represents the input list to be parsed represents the input list to be parsed L’L’ represents the remainder of the input list after rule represents the remainder of the input list after rule x x has been parsed has been parsed

L1

L2

L3

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

Exercise 4: Regular Grammars

Consult the DCG for GConsult the DCG for Gabab Use ?- listing. to see the underlying DCG rule Use ?- listing. to see the underlying DCG rule

conversionconversion Run essentially the same Prolog queries used for Run essentially the same Prolog queries used for

the FSA in Lecture 7the FSA in Lecture 7?- s?- s([a,a,b([a,a,b,b,b]],[],[]).).?- s([a,b,a],[]).?- s([a,b,a],[]).?- s([a,X],[]).?- s([a,X],[]).

Confirm for yourselves that the DCG returns the Confirm for yourselves that the DCG returns the same results as the FSAsame results as the FSA

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

Exercise 4: Regular Grammars Example of Derivation:Example of Derivation:

?- s?- s([a,a,b([a,a,b,b,b]],[],[]).). S -> [a], bS -> [a], b 'C'([a, a, b, b], a, L) 'C'([a, a, b, b], a, L) L = [a, b, b]L = [a, b, b] b([a, b, b], [])b([a, b, b], []) b -> [a], bb -> [a], b 'C'([a, b, b], a,L)'C'([a, b, b], a,L) L = [b, b]L = [b, b]

b([b, b], [])b([b, b], []) b -> [a], bb -> [a], b 'C'([b, b], a,L)'C'([b, b], a,L) FAILFAIL

b([b, b], []) b([b, b], []) b -> [b], cb -> [b], c 'C'([b, b], b,L)'C'([b, b], b,L) L = [b]L = [b] c([b], [])c([b], []) c -> [b], cc -> [b], c 'C'([b], b, L) 'C'([b], b, L) L = []L = [] c([], [])c([], []) c -> [b], cc -> [b], c

'C'([], b, L)'C'([], b, L) FAILFAIL c([b], [])c([b], []) c -> [b]c -> [b]

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

Exercise 4: Regular Grammars

NoteNote:: We can access any non-terminal, not just We can access any non-terminal, not just

the start symbol the start symbol ss Run Prolog queries for non-terminal Run Prolog queries for non-terminal cc

?- c([b,b,b],[]).?- c([b,b,b],[]). ?- c([a,b],[]).?- c([a,b],[]).

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

Exercise 4: Regular Grammars

Homework Question:Homework Question: What language does the sub-grammar What language does the sub-grammar

starting with non-terminal starting with non-terminal bb accept? accept? Give the answer in the form of a regular Give the answer in the form of a regular

expressionexpression

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

Beyond Regular Grammars

Recall from Lecture 6…Recall from Lecture 6… For regular grammars, For regular grammars, can’tcan’t have both left and have both left and

right recursive rules in the same grammarright recursive rules in the same grammar• A -> aBA -> aB• A -> BaA -> Ba

Would change the expressive power of the Would change the expressive power of the grammar formalism and lose the grammar formalism and lose the correspondence with FSA and regular correspondence with FSA and regular expressionsexpressions

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

Beyond Regular Grammars

DCG rules have no such limitationsDCG rules have no such limitations For exampleFor example::

We can have both left and right recursive We can have both left and right recursive rulesrules

We can have rules with more than one non-We can have rules with more than one non-terminal and terminal symbol on the right terminal and terminal symbol on the right sideside

We can even have complex non-terminals We can even have complex non-terminals that contain variables and values…that contain variables and values…laterlater

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

Left and Right Recursive Rules

Recall from Lecture 6…Recall from Lecture 6… The following grammar GThe following grammar Gnn with starting non- with starting non-

terminal A generates the terminal A generates the non-regularnon-regular language language L = {aL = {annbbnn | n>=0 } | n>=0 }

• A ->A ->• A -> aBA -> aB• B -> AbB -> Ab• B -> bB -> b

Can’t build a FSA to handle GCan’t build a FSA to handle Gnn

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

Left and Right Recursive Rules

Equivalent DCG for GEquivalent DCG for Gnn:: a --> [].a --> []. a(L,L).a(L,L).

a --> [a], b.a --> [a], b. a(L1,L3) :- ‘C’(L1,a,L2), b(L2,L3).a(L1,L3) :- ‘C’(L1,a,L2), b(L2,L3).

b --> a, [b].b --> a, [b]. b(L1,L3) :- a(L1,L2), ‘C’(L2,b,L3).b(L1,L3) :- a(L1,L2), ‘C’(L2,b,L3).

b --> [b].b --> [b]. b([b|L],L).b([b|L],L).

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

Exercise 5: Left and Right Recursive Rules

Consult DCG for GConsult DCG for Gnn Check DCG conversion using ?- listing.Check DCG conversion using ?- listing. Run Prolog queries:Run Prolog queries:

?- a([],[]).?- a([],[]). ?- a([a,b],[]).?- a([a,b],[]). ?- a([a,a,a,b,b,b],[]).?- a([a,a,a,b,b,b],[]). Check also that…Check also that…

?- a([a,a,a,b,b],[]).?- a([a,a,a,b,b],[]). #a > #b #a > #b ?- a([a,a,b,b,b],[]).?- a([a,a,b,b,b],[]). #a < #b#a < #b ?- a([a],[]).?- a([a],[]). No bNo b

all failall fail

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

Exercise 5: Left and Right Recursive Rules

Homework Question (A)Homework Question (A) What does the query ?- a(X,[]). return?What does the query ?- a(X,[]). return? NoteNote: Use: Use

?- set_prolog_flag(toplevel_print_options,[max_depth(0)]).?- set_prolog_flag(toplevel_print_options,[max_depth(0)]). to see the entire listto see the entire list

Homework Question (B)Homework Question (B) What happens to the query ?- a(X,[]). if we What happens to the query ?- a(X,[]). if we

switch the order of the clauses for non-terminal switch the order of the clauses for non-terminal aa??

Explain whyExplain why