LING 438/538 Computational Linguistics Sandiway Fong Lecture 6: 9/7.

Slides:



Advertisements
Similar presentations
CSA2050: DCG I1 CSA2050 Introduction to Computational Linguistics Lecture 8 Definite Clause Grammars.
Advertisements

LING 388: Language and Computers Sandiway Fong Lecture 5: 9/5.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/8.
LING 388: Language and Computers Sandiway Fong Lecture 9: 9/27.
LING 388: Language and Computers Sandiway Fong 9/29 Lecture 11.
LING 438/538 Computational Linguistics Sandiway Fong Lecture 8: 9/29.
LING 438/538 Computational Linguistics Sandiway Fong Lecture 3: 8/29.
LING 388: Language and Computers Sandiway Fong Lecture 9: 9/22.
LING 438/538 Computational Linguistics Sandiway Fong Lecture 7: 9/12.
CS5371 Theory of Computation
LING 438/538 Computational Linguistics Sandiway Fong Lecture 9: 9/21.
LING 388: Language and Computers Sandiway Fong Lecture 12: 10/5.
LING/C SC/PSYC 438/538 Computational Linguistics Sandiway Fong Lecture 8: 9/18.
6/12/2015Prof. Hilfinger CS164 Lecture 111 Bottom-Up Parsing Lecture (From slides by G. Necula & R. Bodik)
LING/C SC/PSYC 438/538 Computational Linguistics Sandiway Fong Lecture 7: 9/11.
LING 388 Language and Computers Lecture 8 9/25/03 Sandiway FONG.
LING/C SC/PSYC 438/538 Computational Linguistics Sandiway Fong Lecture 6: 9/6.
LING 388: Language and Computers Sandiway Fong Lecture 6: 9/7.
Context-Free Grammars Lecture 7
LING 388: Language and Computers Sandiway Fong Lecture 6: 9/13.
LING 388: Language and Computers Sandiway Fong Lecture 4: 8/31.
LING 388: Language and Computers Sandiway Fong Lecture 11: 10/3.
LING 388: Language and Computers Sandiway Fong Lecture 3: 8/29.
LING 388 Language and Computers Take-Home Final Examination 12/9/03 Sandiway FONG.
LING 438/538 Computational Linguistics Sandiway Fong Lecture 12: 10/5.
LING 388: Language and Computers Sandiway Fong Lecture 17: 10/25.
LING 388 Language and Computers Lecture 11 10/7/03 Sandiway FONG.
Normal forms for Context-Free Grammars
LING 388: Language and Computers Sandiway Fong Lecture 4: 9/1.
LING 438/538 Computational Linguistics Sandiway Fong Lecture 5: 9/5.
LING 388 Language and Computers Lecture 9 9/30/03 Sandiway FONG.
Chapter 3: Formal Translation Models
Context-Free Grammars Chapter 3. 2 Context-Free Grammars and Languages n Defn A context-free grammar is a quadruple (V, , P, S), where  V is.
LING 364: Introduction to Formal Semantics Lecture 5 January 26th.
LING 388: Language and Computers Sandiway Fong Lecture 13: 10/10.
Finite State Machines Data Structures and Algorithms for Information Processing 1.
LING 388 Language and Computers Lecture 6 9/18/03 Sandiway FONG.
Context-free grammars are a subset of context-sensitive grammars
LING 388: Language and Computers Sandiway Fong Lecture 8.
LING 388: Language and Computers Sandiway Fong 10/4 Lecture 12.
Context-Free Grammars
Formal Grammars Denning, Sections 3.3 to 3.6. Formal Grammar, Defined A formal grammar G is a four-tuple G = (N,T,P,  ), where N is a finite nonempty.
Lecture 16 Oct 18 Context-Free Languages (CFL) - basic definitions Examples.
LING 388: Language and Computers Sandiway Fong Lecture 7.
1 Chapter 5 LL (1) Grammars and Parsers. 2 Naming of parsing techniques The way to parse token sequence L: Leftmost R: Righmost Top-down  LL Bottom-up.
LING 388: Language and Computers Sandiway Fong Lecture 6.
Design contex-free grammars that generate: L 1 = { u v : u ∈ {a,b}*, v ∈ {a, c}*, and |u| ≤ |v| ≤ 3 |u| }. L 2 = { a p b q c p a r b 2r : p, q, r ≥ 0 }
Classification of grammars Definition: A grammar G is said to be 1)Right-linear if each production in P is of the form A  xB or A  x where A and B are.
Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left.
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
Context-Free Grammars Chapter 11. Languages and Machines.
15/11/04 AIPP Lecture 14: Database Manipulation1 Database Manipulation Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 14 15/11/04.
LING/C SC/PSYC 438/538 Lecture 12 10/4 Sandiway Fong.
1 Section 12.3 Context-Free Parsing We know (via a theorem) that the context-free languages are exactly those languages that are accepted by PDAs. When.
LING/C SC/PSYC 438/538 Lecture 13 Sandiway Fong. Administrivia Reading Homework – Chapter 3 of JM: Words and Transducers.
Top-down Parsing lecture slides from C OMP 412 Rice University Houston, Texas, Fall 2001.
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
Re-enter Chomsky More about grammars. 2 Parse trees S  A B A  aA | a B  bB | b Consider L = { a m b n | m, n > 0 } (one/more a ’s followed by one/more.
Top-down Parsing. 2 Parsing Techniques Top-down parsers (LL(1), recursive descent) Start at the root of the parse tree and grow toward leaves Pick a production.
LING/C SC/PSYC 438/538 Lecture 15 Sandiway Fong. Did you install SWI Prolog?
LING/C SC/PSYC 438/538 Lecture 16 Sandiway Fong. SWI Prolog Grammar rules are translated when the program is loaded into Prolog rules. Solves the mystery.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
Mathematical Foundations of Computer Science Chapter 3: Regular Languages and Regular Grammars.
Chapter 4: Syntax analysis Syntax analysis is done by the parser. –Detects whether the program is written following the grammar rules and reports syntax.
CMSC 330: Organization of Programming Languages Pushdown Automata Parsing.
LING/C SC/PSYC 438/538 Lecture 17 Sandiway Fong. Last Time Talked about: – 1. Declarative (logical) reading of grammar rules – 2. Prolog query: s(String,[]).
Top-Down Parsing.
LING/C SC/PSYC 438/538 Lecture 17 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 21 Sandiway Fong.
LING/C SC/PSYC 438/538 Lecture 20 Sandiway Fong.
Presentation transcript:

LING 438/538 Computational Linguistics Sandiway Fong Lecture 6: 9/7

2 Administrivia Homework 1 –acknowledgments sent out –reviewed in class today

3 Homework 1 Review [438/538] Homework Question 1 (3pts) –(A) How many inference steps does it take to run the following query: ?- app([1,2,3,4],[5],L). –(B) How many inference steps does it take to run the following query: ?- app([1],[2,3,4,5],L). –(C) Explain why the number of steps differ despite the fact both queries return the same result. Definition –app([],L2,L2). (base case) –app([X|L1],L2,[X|L3]) :- app(L1,L2,L3). (recursive case) Key thing to notice app( [X|L1], L2, [X|L3]) :- app( L1, L2, L3). each time we recurse –the first argument is reduced to its tail, i.e. from matching [X|L1] to just L1 also, each time we recurse –the second argument is passed along unchanged, i.e. stays as L2 therefore –the number of recursive calls here depends entirely on the length of the 1st list

4 Homework 1 Review [438/538] Homework Question 1 (3pts) –(A) How many inference steps does it take to run the following query: ?- app([1,2,3,4],[5],L). –(B) How many inference steps does it take to run the following query: ?- app([1],[2,3,4,5],L). –(C) Explain why the number of steps differ despite the fact both queries return the same result. Definition app([],L2,L2). ( BC : base case) app([X|L1],L2,[X|L3]) :- app(L1,L2,L3). ( RC: recursive case) Tracing [trace] ?- app([1,2,3,4],[5],L). Call: (7) app([1, 2, 3, 4], [5], _G329) ? (RC) Call: (8) app([2, 3, 4], [5], _G395) ? (RC) Call: (9) app([3, 4], [5], _G398) ? (RC) Call: (10) app([4], [5], _G401) ? (RC) Call: (11) app([], [5], _G404) ? (BC) Exit: (11) app([], [5], [5]) ? creep Exit: (10) app([4], [5], [4, 5]) ? creep Exit: (9) app([3, 4], [5], [3, 4, 5]) ? creep Exit: (8) app([2, 3, 4], [5], [2, 3, 4, 5]) ? creep Exit: (7) app([1, 2, 3, 4], [5], [1, 2, 3, 4, 5]) ? creep L = [1, 2, 3, 4, 5]

5 Question 2 [438/538] Homework Question 2 (6 pts) –give the answer and –draw the complete computation tree (see lecture 2) for the following queries ?- app(X,Y,[1,2,3]). ?- app(_,[X],[1,2]). Computation tree ?- app(X,Y,[1,2,3]). (BC) X=[], Y=L2, [1,2,3]=L2 X=[] Y=[1,2,3] (RC) X=[X’|L1’] Y=L2’ [1,2,3]=[X’|L3’] ?- app(L1’,L2’,[2,3]). (BC) L1’=[], L2’=L2”, [2,3]=L2” X=[1] Y=[2,3] (RC) L1’=[X”|L1”] L2’=L2” [2,3]=[X”|L3”] –?- app(L1”,L2”,[3]). –(BC) L1”=[], L2”=L2”’, [3]=L2”’ –X=[1,2] Y=[3] –(RC) L1”=[X”’|L1”’] L2”=L2”’ [3]=[X”’|L3”’] ?- app(L1”’,L2”’,[]). (BC) L1”’=[], L2”’=L2””, []=L2”” X=[1,2,3] Y=[] (RC) DOESN’T MATCH Definition app([],L2,L2). ( BC : base case) app([X|L1],L2,[X|L3]) :- app(L1,L2,L3). ( RC: recursive case) BC RC

6 Question 2 [438/538] Homework Question 2 (6 pts) –give the answer and –draw the complete computation tree (see lecture 2) for the following queries ?- app(X,Y,[1,2,3]). ?- app(_,[X],[1,2]). Computation tree ?- app(_,[X],[1,2]). (BC) _=[], [X]=L2, [1,2]=L2 DOESN’T MATCH (RC) _=[X’|L1’] [X]=L2’ [1,2]=[X’|L3’] ?- app(L1’,L2’,[2]). (BC) L1’=[], L2’=L2”, [2]=L2” X=2 (RC) L1’=[X”|L1”] L2’=L2” [2]=[X”|L3”] –?- app(L1”,L2”,[]). –(BC) L1”=[], L2”=L2”’, []=L2”’ DOESN’T MATCH –(RC) DOESN’T MATCH Definition app([],L2,L2). ( BC : base case) app([X|L1],L2,[X|L3]) :- app(L1,L2,L3). ( RC: recursive case) BC RC

7 Question 2 [438/538] Homework Question 2 (2 pts) –what does the general form of the 2nd query compute for some arbitrary list L ? ?- app(_,[X],L). Key thing to notice ?- app(L1,L2,L3). concatenates L1 to L2 giving L3 but can also be used to split L3 into L1 and L2 e.g. ?- app([1,2],[3,4],[1,2,3,4]). in ?- app(_,[X],L). we are constraining the 2nd list to be of form [X], i.e. to be a list containing exactly one item X in ?- app(_,[X],[1,2,3,4]). this list can only be split as [1,2,3] and [4] in general, ?- app(_,[X],L). computes X as the last element of the list L ?- app(_,[X],L). is the same as ?- last(L,X). defined in lecture 3

8 Homework 1 Review [optional 438/mandatory 538] Homework Question 3 (4 pts) –explain what happens for the following query? ?- app(X,Y,Z). tracing shows it matches the base case and then proceeds to repeatedly match the recursive case followed by the base case each recursion lengthens lists X and Z by one (variable) element but Z always includes list X followed by list Y (invariant) answers generated are of form: X = [] Y = Z ; X = [_G394] Z = [_G394|Y] ; X = [_G394, _G400] Z = [_G394, _G400|Y] ; X = [_G394, _G400, _G406] Z = [_G394, _G400, _G406|Y] ;...

9 Last Time began a new topic –grammar N = nonterminals, T = terminals P = production rules, S = start symbol –grammar hierarchy Chomsky’s type-0 through type-3 classification –type-1 context-sensitive –type-2 context-free –type-3 regular generative power/capacity of each class determined by restrictions on what can appear in a production rule –LHS → RHS“LHS rewrites/expands to RHS”

10 Last Time Prolog’s definite clause grammar (DCG) system –rules have form: LHS --> RHS. LHS = single nonterminal RHS = conjunction (, ) of terminals, nonterminals and Prolog queries both terminals and nonterminal symbols are represented by Prolog symbols, distinguished notationally by: [a] terminal symbol a a nonterminal symbol a {a} Prolog query for predicate a/0

11 Grammar as a computer program advantage of using Prolog –grammar is a Prolog program –uses the same computation rule 1. s --> [a],b. 2. b --> [a],b. 3. b --> [b],c. 4. b --> [b]. 5. c --> [b],c. 6. c --> [b]. query –call the grammar using a regular Prolog query –?- s(List,[]). not a coincidence grammar is translated into a regular Prolog program

12 Grammar as a computer program SWI-Prolog automatically rewrites DCG rules –into regular Prolog rules when loaded in from a file so it can run the grammar like a regular Prolog program using the same computation rule –match the database facts and rules from the top for each (sub)-goal –but not from the command line –example ?- assert((s --> [a],b)). Yes ?- listing. s-->[a], b. Yes ?- s([a],[]). ERROR: Undefined procedure: s/2

13 Grammar as a computer program let’s look at the translation of s --> [a],b. translated version is s(L1, L3) :- 'C'(L1, a, L2), b(L2, L3). when using ?- listing. ?- trace. all you will see is the translated version, so it’s important we learn how to read it Notes –it adds a pair of list arguments to each nonterminal this is why we call the grammar with ?- s(List,[]). L1=List, L3=[] –it calls a builtin predicate 'C’/3 for each terminal including two list arguments around the terminal symbol

14 Grammar as a computer program piece-by-piece translation... s-->[a],b. s(L1,L3):-'C'(L1, a, L2),b(L2,L3). list pairs ( difference lists ) –L1 = the list we supply for parsing –L3 = [] (what’s left over from L1 after parsing nonterminal s ) –L2 = intermediate list (what’s left over from L1 parsing terminal a ) this in turn serves as input to nonterminal b example L1 = [a,b,b] L3 = [] L2 = [b,b] means – nonterminal s covers/spans [a,b,b] – terminal a spans the difference between [a,b,b] and [b,b] – nonterminal b spans [b,b] minus []

15 Grammar as a computer program difference list pairs... s-->[a],b. s(L1,L3):-'C'(L1, a, L2),b(L2,L3). example –L1 = [a,b] –L3 = [] –L1 = [a,b] –L2 = [b] –'C’/3 removes a from L1 leaving L2 –L2 = [b] –L3 = [] –b/2 spans L2 minus L3

16 Difference Lists difference lists –L1 - L2 –L2 is a suffix (sub-list) of L1 examples (well-formed) –[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] = [] in L1 – L2, L2 must be a suffix of L1 to be well-formed examples (ill-formed) –[a,a,b,b] – [a,a] (L2 prefix, not suffix) –[a,a,b,b] – [a,a,a,b,b,b] (not a suffix)

17 Difference Lists example query explained –?- s([a,a,b,b,b],[]). query uses the start symbol s with two arguments: –(1) sequence (as a list) to be recognized and –(2) the empty list [] –s spans [a,a,b,b,b] - [] i.e. the whole sequence equivalent query –?- s([a,a,b,b,b,d,e],[d,e]). –s is true if s covers the [a,a,b,b,b] prefix of [a,a,b,b,b,d,e] or –in showing s is true, –we consume part of [a,a,b,b,b,d,e] from left to right up to (but not including) [d,e] Reason for using difference lists? Computational efficiency –Prolog can chop the input list down an element at a time (linear time) without taking the whole list apart and calling append to extract sub-sequences

18 Extra Arguments we’ve already seen –a pair of extra arguments (shown in blue) are added through Prolog translation for the input lists –s --> [a],b. –s(L1, L3) :- 'C'(L1, a, L2), b(L2, L3). we can also manually add (still) more arguments to our nonterminals –why? so we can implement useful things like agreement and parse trees –example s(S) --> [a],b(B). s(S,L1, L3) :- 'C'(L1, a, L2), b(B,L2, L3). added arguments appear before the input lists

19 Extra Arguments: Parse Tree –want Prolog to return more than just Yes/No answers –in case of Yes, we can compute a syntax tree representation of the parse example –sheeptalk language –ba!baa!baaa!ba...a! –Prolog grammar –s --> [b], a, [‘!’]. –a --> [a]. (base case) –a --> [a], a. (right recursive case) s b‘!’‘!’ a a a a s(b,a(a,a(a)),’!’)

20 Extra Arguments: Parse Tree Prolog grammar –s --> [b], a, [‘!’]. –a --> [a]. (base case) –a --> [a], a. (right recursive case) s b‘!’‘!’ a a a a s(b,a(a,a(a)),’!’) base case –a --> [a]. –a(subtree) --> [a]. –a(a(a)) --> [a]. recursive case –a --> [a], a. –a(subtree) --> [a], a(subtree). –a(a(a,A)) --> [a], a(A).

21 Extra Arguments: Parse Tree Prolog grammar –s --> [b], a, [’!’]. –a --> [a]. (base case) –a --> [a], a. (right recursive case) s b‘!’‘!’ a a a a s(b,a(a,a(a)),’!’) base and recursive cases –a(a(a)) --> [a]. –a(a(a,A)) --> [a], a(A). start symbol case –s --> [b], a, [’!’]. –s(tree) --> [b], a(subtree), [’!’]. –s(s(b,A,’!’) ) --> [b], a(A), [’!’].

22 Extra Arguments: Parse Tree Prolog grammar –s --> [b], a, [’!’]. –a --> [a]. (base case) –a --> [a], a. (right recursive case) s b‘!’‘!’ a a a a s(b,a(a,a(a)),’!’) Prolog grammar computing a parse –a(a(a)) --> [a]. –a(a(a,A)) --> [a], a(A). –s(s(b,A,’!’)) --> [b], a(A), [’!’].

23 Next Time type-3: regular grammars