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
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
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
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
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++}}
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
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…
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
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]
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
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
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]
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],[]).
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
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
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
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
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).
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
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