<<

Context-free Problems with Regular Context-free Grammar and Is English a ? Bad question! We do not even know what English is! Two eggs and bacon make(s) a big breakfast Backtrack Can you slide me the salt? He didn't ought to do that But—No! Martin Kay I put the wine you brought in the fridge I put the wine you brought for Sandy in the fridge Should we bring the wine you put in the fridge out Stanford University now? and University of the Saarland You said you thought nobody had the right to claim that they were above the law

Martin Kay Context-free Grammar 1 Martin Kay Context-free Grammar 2

Problems with Regular Problems with Regular Language Language

You said you thought nobody had the right to claim [You said you thought [nobody had the right [to claim that they were above the law that [they were above the law]]]]

Martin Kay Context-free Grammar 3 Martin Kay Context-free Grammar 4 Problems with Regular Context-free Grammar Language Nonterminal symbols ~ grammatical categories Is English mophology a regular language? Bad question! We do not even know what English Terminal Symbols ~ words morphology is! They sell collectables of all sorts Productions ~ (unordered) (rewriting) rules This concerns unredecontaminatability Distinguished Symbol This really is an untiable knot. But—Probably! (Not sure about Swahili, though) Not all that important • Terminals and nonterminals are disjoint • Distinguished symbol

Martin Kay Context-free Grammar 5 Martin Kay Context-free Grammar 6

Context-free Grammar Context-free Grammar

Nonterminal symbols ~ grammatical categories Nonterminal symbols ~ grammatical categories S, NP, VP, Det, N, V S, NP, VP, Det, N, V Terminal Symbols ~ words Terminal Symbols ~ words the, dog, cat, chased the, dog, cat, chased Productions ~ (unordered) (rewriting) rules Productions ~ (unordered) (rewriting) rules S → NP VP Det → the S → NP VP Det → the NP → Det N N → dog NP → Det N N → dog VP → V NP N → cat VP → V NP N → cat V → chased V → chased Distinguished Symbol Distinguished Symbol S S

Martin Kay Context-free Grammar 7 Martin Kay Context-free Grammar 8 Context-free Grammar Context-free Grammar

Nonterminal symbols ~ grammatical categories Nonterminal symbols ~ grammatical categories S, NP, VP, Det, N, V S, NP, VP, Det, N, V Terminal Symbols ~ words Terminal Symbols ~ words the, dog, cat, chased the, dog, cat, chased Productions ~ (unordered) (rewriting) rules Productions ~ (unordered) (rewriting) rules S → NP VP Det → the S → NP VP Det → the S S NP → Det N N → dog NP → Det N N → dog NP VP NP VP VP → V NP N → cat VP → V NP N → cat V → chased V → chased Det N Distinguished Symbol Distinguished Symbol S S

Martin Kay Context-free Grammar 9 Martin Kay Context-free Grammar 10

Context-free Grammar Context-free Grammar

Nonterminal symbols ~ grammatical categories Nonterminal symbols ~ grammatical categories S, NP, VP, Det, N, V S, NP, VP, Det, N, V Terminal Symbols ~ words Terminal Symbols ~ words the, dog, cat, chased the, dog, cat, chased Productions ~ (unordered) (rewriting) rules Productions ~ (unordered) (rewriting) rules S → NP VP Det → the S → NP VP Det → the S S NP → Det N N → dog NP → Det N N → dog NP VP NP VP VP → V NP N → cat VP → V NP N → cat V → chased Det N V NP V → chased Det N V NP Distinguished Symbol Distinguished Symbol Det N S S

Martin Kay Context-free Grammar 11 Martin Kay Context-free Grammar 12 Context-free Grammar Context-free Grammar

Nonterminal symbols ~ grammatical categories Nonterminal symbols ~ grammatical categories S, NP, VP, Det, N, V S, NP, VP, Det, N, V Terminal Symbols ~ words Terminal Symbols ~ words the, dog, cat, chased the, dog, cat, chased Productions ~ (unordered) (rewriting) rules Productions ~ (unordered) (rewriting) rules S → NP VP Det → the S → NP VP Det → the S S NP → Det N N → dog NP → Det N N → dog NP VP NP VP VP → V NP N → cat VP → V NP N → cat V → chased Det N V NP V → chased Det N V NP Distinguished Symbol the Det N Distinguished Symbol the dog chasedDet N S S the cat

Martin Kay Context-free Grammar 13 Martin Kay Context-free Grammar 14

Context-free Grammar Context-free Grammar

Nonterminal symbols ~ grammatical categories S, NP, VP, Det, N, V Terminal Symbols ~ words the, dog, cat, chased • Defines a language (set of strings) and Productions ~ (unordered) (rewriting) rules S → NP VP Det → the • Defines a corresponding set of structures S NP → Det N N → dog NP VP VP → V NP N → cat V → chased Det N V NP Distinguished Symbol the dog chasedDet N S the cat

Martin Kay Context-free Grammar 15 Martin Kay Context-free Grammar 16 Structure and Aithmetic Expressions

sum ! prod add_op sum sum sum ! prod 14 sum prod ! base mul_op prod prod ad_op sum prod prod ! base 12 base + prod ad_op base base ! a | b | ... ( sum ) base - y num plus num times num base ! ( sum ) 2 3 4 ad_op ! + prod ad_op sum x ad_op ! - 2 + 3 ! 4 base + prod mul_op ! * a base mul_op ! / b

Martin Kay Context-free Grammar 17 Martin Kay Context-free Grammar 18

Aithmetic Expressions Embedding sum sum ! prod add_op sum Arbitrarily embedded parentheses are not prod sum ! prod possible in a regular language because the number of open parentheses that still prod ! base mul_op prod base requiring matching close parentheses can be prod ! base ( sum ) greater than the number of states in the base ! a | b | c ... automaton, whatever that number is. prod base ! ( sum ) Hence, by the pumping lemma, an automaton ad_op ! + base with only a finite number of states cannot characterize the language. ad_op ! - ( sum ) mul_op ! * . . mul_op ! /

Martin Kay Context-free Grammar 19 Martin Kay Context-free Grammar 20 Lots of Problems!

More than one structure can correspond to a That it rained slept That it rained were single string soundly. certainly true. S Mary told the boy Mary His arguments convinces me. John told that it rained NP VP me Me told Mary it. Det N Vi

That can burst For the moment, just forget the problems Pro aux Vi

NP VP S Martin Kay Context-free Grammar 21 Martin Kay Context-free Grammar 22

Why "Context-free"? Convenience You can define something once, and then use it in many places in the grammar S S S NP VP NP VP Det N V NP NP

the dog chased Det N Vd NP NP S the cat S NP VP VP NP VP What can be in this subtree Vi Vt NP is determined only by this symbol Vi and not by any of this "context" John told me that it rained That it rained surprised John Martin Kay Context-free Grammar 23 Martin Kay Context-free Grammar 24 How many (binary) trees How many (binary) trees with n terminals? with n terminals?

Any binary number Any binary number represents a set of represents a set of binary branching trees binary branching trees

2n-2 2n-2

Exponential Exponential

⬈ ⬈ ⬉ ⬉ ⬈ ⬈ ⬉ ⬈ ⬉ ⬉ ⬈ ⬈ ⬉ ⬈ ⬈ ⬉ ⬉ ⬈ ⬈ ⬉ ⬈ ⬉ ⬉ ⬈ ⬈ ⬉ 1 1 1 0 0 1 1 0 1 0 0 1 1 0 1 1 1 0 0 1 1 0 1 0 0 1 1 0

Martin Kay Context-free Grammar 25 Martin Kay Context-free Grammar 26

Catalan Numbers Catalan Numbers 1 terminal 2 terminals Terminals Trees Terminals Trees 1 1 1 1 2 1

Why? Martin Kay Context-free Grammar 27 Martin Kay Context-free Grammar 28 Catalan Numbers Catalan Numbers 3 terminals 3 terminals Terminals Trees Terminals Trees left ! 1 1 1 1 ! right 2 1 ! right left ! 2 1

Martin Kay Context-free Grammar 29 Martin Kay Context-free Grammar 30

Catalan Numbers Catalan Numbers 4 terminals 4 terminals Terminals Trees Terminals Trees left ! 1 1 1 1 2 1 left ! 2 1 ! right 3 2 ! right 3 2

1 ! 2 = 2 1 ! 2 = 2 1 ! 1 = 1

Martin Kay Context-free Grammar 31 Martin Kay Context-free Grammar 32 Catalan Numbers Catalan Numbers 4 terminals 4 terminals Terminals Trees Terminals Trees 1 1 ! right 1 1 ! right 2 1 2 1 left ! 3 2 left ! 3 2 4 5

1 ! 2 = 2 1 ! 2 = 2 1 ! 1 = 1 1 ! 1 = 1 2 ! 1 = 2 2 ! 1 = 2 5

Martin Kay Context-free Grammar 33 Martin Kay Context-free Grammar 34

Catalan Numbers Catalan Numbers 5 terminals 4 terminals Terminals Trees Terminals Trees left ! 1 1 1 1 2 1 left ! 2 1 3 2 3 2 ! right 4 5 ! right 4 5

1 ! 5 = 5 1 ! 5 = 5 1 ! 2 = 2

Martin Kay Context-free Grammar 35 Martin Kay Context-free Grammar 36 Catalan Numbers Catalan Numbers 4 terminals 4 terminals Terminals Trees Terminals Trees 1 1 1 1 ! right 2 1 ! right 2 1 left ! 3 2 3 2 4 5 left ! 4 5

1 ! 5 = 5 1 ! 5 = 5 1 ! 2 = 2 1 ! 2 = 2 2 ! 1 = 2 2 ! 1 = 2 5 ! 1 = 5 14

Martin Kay Context-free Grammar 37 Martin Kay Context-free Grammar 38

Catalan Numbers Catalan Numbers

The number of binary trees with n terminals Terminals Trees 1 1 The number of arbitrarily branching trees 2 1 with n nodes. 3 2 (= 1*1 + 1*1) 4 5 (= 1*2 + 1*1 + 2*1) 5 14 (= 1*5 + 1*2 + 2*1 + 5*1) 6 42 (= 1*14 + 1*5 + 2*2 + 5*1 + 14*1) ... Any can be encoded in a 2n - 1 binary tree with the same Cat(n) = n number of terminals as the 2n - 1 original tree has nodes.

Martin Kay Context-free Grammar 39 Martin Kay Context-free Grammar 40 Catalan Numbers Analyzing Sentences There are n nonterminals in the binary tree for the n nodes in the original tree, except for the root. • Recognition: determining if a given string is There are n+1 terminals in a binary tree with n a member of the language defined by the nonterminals. ! grammar Let there be: a arcs, • Parsing: Determining what structure(s) a n nonterminals, and t terminals grammar assigns to a string There are two arcs downward from each nonterminal, so a = 2n • Note: Parsing encompasses recognition. There is one arc upward from each terminal and nonterminal except the root, so a = n + t -1 Ergo n = t - 1

Martin Kay Context-free Grammar 41 Martin Kay Context-free Grammar 42

Top-down More procedurally

" the # dog $ chased % the & cat ' " the # dog $ chased % the & cat '

S → NP VP Det → the S → NP VP Det → the NP → Det N N → dog NP → Det N N → dog VP → V NP N → cat VP → V NP N → cat V → chased V → chased

If there is a between ! and ", then there If there is a sentence between ! and ", look for an NP beginning at ! must be an NP between ! and some x and a VP between x and ". and and let x be where it ends.

x " If there is an NP between ! and x , then there Look for a VP beginning at and ending at . must be a Det between ! and some y and a N between y and x .

Martin Kay Context-free Grammar 43 Martin Kay Context-free Grammar 44 Definite-clause grammar Top-down s(A, C) :- np(A, B), vp(B, C). | ?- s(X, Y). These do not have to be numbers. They could be ... anything! Just so vp(A, C) :- v(A, B), np(B, C). X = 1, they are different from one another. np(A, C) :- det(A, B), n(B, C). Y = 6 ? det(A, B) :- the(A, B). | ?- np(X, Y). n(A, B) :- dog(A, B). X = 1, " the # dog $ chased % the & cat ' n(A, B) :- cat(A, B). Y = 3 ? ; v(A, B) :- chased(A, B). X = 4, S → NP VP Det → the Y = 6 ? ; NP → Det N N → dog the(1, 2). no VP → V NP N → cat dog(2, 3). | ?- V → chased chased(3, 4). the(4, 5). cat(5, 6). Martin Kay Context-free Grammar 45 Martin Kay Context-free Grammar 46

Definite-clause grammar Top-down s(A, C) :- np(A, B), vp(B, C). | ?- s(X, Y). These do not have to be numbers. They could be ... anything! Just so vp(A, C) :- v(A, B), np(B, C). X = start, they are different from one another. np(A, C) :- det(A, B), n(B, C). Y = end ? ; det(A, B) :- the(A, B). no n(A, B) :- dog(A, B). | ?- np(X, Y). " the # dog $ chased % the & cat ' n(A, B) :- cat(A, B). X = start, v(A, B) :- chased(A, B). Y = dog ? ; S → NP VP Det → the X = chased, NP → Det N N → dog the(start, the1). Y = end ? ; VP → V NP N → cat dog(the1, dog). no V → chased chased(dog, chased). The sentence has to be part of the the(chased, the2). So let's use tails of the string itself! cat(the2, end). program! Martin Kay Context-free Grammar 47 Martin Kay Context-free Grammar 48 [the, dog, chased, the, cat] Top-down Renamed String Positions [dog, chased, the, cat] s(A, C) :- np(A, B), vp(B, C). [chased, the, cat] vp(A, C) :- v(A, B), np(B, C). [the, cat] np(A, C) :- det(A, B), n(B, C). det([the | X], X). [cat] n([dog | X], X). [] n([cat | X], X). There is a noun between positions A and v([chased | X], X). B if A is a string beginning with the the dog chased the cat word "dog" and B is the remainder of that string.

If there is a sentence between [the, dog, chased, the, cat] and [], then there must be an NP between [the, dog, chased, the, cat] and some X and a VP between that X and [].

Martin Kay Context-free Grammar 49 Martin Kay Context-free Grammar 50

Renamed String Positions But its only a recognizer!

s(A, C) :- np(A, B), vp(B, C). s(A, C, s(NP, VP)) :- np(A, B, NP), vp(B, C, VP). vp(A, C) :- v(A, B), np(B, C). vp(A, C, vp(V, NP)) :- v(A, B, V), np(B, C, NP). np(A, C) :- det(A, B), n(B, C). np(A, C, np(Det, N)) :- det(A, B, Det), n(B, C, N). det([the | X], X). det([the | X], X, det(the)). n([dog | X], X). n([dog | X], X, n(dog)). n([cat | X], X). n([cat | X], X, n(cat)). v([chased | X], X). v([chased | X], X, v(chased)).

| ?- s([the,dog,chased,the,cat], []). yes | ?- s([the,dog,chased,the,cat], [], S). S = s(np(det(the),n(dog)),vp(v(chased),np(det(the),n (cat)))) ?

Martin Kay Context-free Grammar 51 Martin Kay Context-free Grammar 52 Caveat Parsor Left s(A, C, s(NP, VP)) :- np(A, B, NP), vp(B, C, VP). NP --> NP PP vp(A, C, vp(V, NP)) :- v(A, B, V), np(B, C, NP). np(A, C, np(Det, N)) :- det(A, B, Det), n(B, C, N). det([the | X], X, det(the)). To find an NP, first find an NP !!! pp(A, C, pp(P, NP)) :- p(A, B, P), np(B, C, NP). np(A, C, np(NP, PP)) :- np(A, B, NP), pp(B, C, PP). NP --> Det N vp(A, C, vp(VP, PP)) :- vp(A, B, VP), pp(B, C, PP). n([dog | X], X, n(dog)). Det --> NP apostrophe-s n([cat | X], X, n(cat)). | ?- s([the,dog,chased,the,cat], [], S). n([houseS = s(np(det(the),n(dog)),vp(v(chased),np(det(the),n(cat)))) | X], X, n(house)). ? ; v([chased! Resource |error: X], insufficientX, v(chased)). memory p([round | X], X, p(round)).

Martin Kay Context-free Grammar 53 Martin Kay Context-free Grammar 54

Left Recursion

For the moment, we will just look for a different parsing that does not have A grammar that contains instances of left the problem. recursion canCOMING be replaced SOON! by one with no which • describes the same language • assigns the same structures to the sentences. Also, for a while, we will concentrate on recognizers because we know how to turn them into parsers.

Martin Kay Context-free Grammar 55 Martin Kay Context-free Grammar 56 Stack-String Models Stack-String Models

Stack String Stack String [s/[np, vp]] [the, dog, chased, ...] [s/[np, vp]] [the, dog, chased, ...] ⤴ ⤵ Access only from the middle

[s/[np,vp],np/[det,n]] [the, dog, chased, ...]

[s/[np,vp],np/[det,n]],det/[the] [the, dog, chased, ...]

Pattern 1: Push

Martin Kay Context-free Grammar 57 Martin Kay Context-free Grammar 58

0. [s/[np,vp]] [the,dog,chased,the,cat] 0 1. [s/[np,vp],np/[det,n]] [the,dog,chased,the,cat] 1 Stack-String Models 2. [s/[np,vp],np/[det,n],det/[the]] [the,dog,chased,the,cat] 1 3. [s/[np,vp],np/[det,n]] [det,dog,chased,the,cat] 4 4. [s/[np,vp],np/[n]] [dog,chased,the,cat] 3 Stack String 5. [s/[np,vp],np/[n],n/[dog]] [dog,chased,the,cat] 1 6. [s/[np,vp],np/[n]] [n,chased,the,cat] 4 7. [s/[np,vp]] [np,chased,the,cat] 4 [s/[np,vp],np/[det,n]],det/[the] [the, dog, chased, ...] 8. [s/[vp]] [chased,the,cat] 3 9. [s/[vp],vp/[v,np]] [chased,the,cat] 1 10. [s/[vp],vp/[v,np],v/[chased]] [chased,the,cat] 1 Pattern 4: Complete rule 11. [s/[vp],vp/[v,np]] [v,the,cat] 4 12. [s/[vp],vp/[np]] [the,cat] 3 [s/[np,vp],np/[det,n]] [det, dog, chased, ...] 13. [s/[vp],vp/[np],np/[det,n]] [the,cat] 1 14. [s/[vp],vp/[np],np/[det,n],det/[the]] [the,cat] 1 15. [s/[vp],vp/[np],np/[det,n]] [det,cat] 4 16. [s/[vp],vp/[np],np/[n]] [cat] 3 Pattern 3: Pop 17. [s/[vp],vp/[np],np/[n],n/[cat]] [cat] 1 18. [s/[vp],vp/[np],np/[n]] [n] Top down 4 [s/[np,vp],np/[n]] [dog, chased, the, cat] 19. [s/[vp],vp/[np]] [np] 4 20. [s/[vp]] [vp] recognizer 4 21. [] [s] 4

Martin Kay Context-free Grammar 59 Martin Kay Context-free Grammar 60 1: Push For convenience, write 2: Reduce with rule Looking for Q A → B C ... Looking for Q

Put it on the stack as A/[B, C ...] move([P/[Q | R | Stack], String, " move([P/[Q | R | Stack], String, " [P/[Q | R, Q/S] | Stack], String) [Q/S, P/[Q | R | Stack], String) This would satisfy the rule

:- rule(Q/S). A rule that could find Q :- rule(Q/S). So replace it move([P/[Q | R | Stack], [S | String], # move([P/[Q | R | Stack], [S | String], # [P/[Q | R | Stack], [Q | String]) [P/[Q | R | Stack], [Q | String])

:- rule(Q/[S]). :- rule(Q/[S]). A rule that could find Q move([P/[Q , R | S | Stack], [Q | String], $ move([P/[Q , R | S | Stack], [Q | String], $ [P/[R | S | Stack], String). [P/[R | S | Stack], String). move([P/[Q] | Stack, [Q | String], % move([P/[Q] | Stack], [Q | String], % Stack, [P | String]). Stack, [P | String]).

Martin Kay Context-free Grammar 61 Martin Kay Context-free Grammar 62

3: Pop 4: Complete Rule Looking for Q Looking for Q move([P/[Q | R] | Stack], String, " move([P/[Q | R] | Stack], String, " [Q/S, P/[Q | R] | Stack], String) [Q/S, P/[Q | R] | Stack], String) :- rule(Q/S). :- rule(Q/S). move([P/[Q | R] | Stack], [S | String], # move([P/[Q | R] | Stack], [S | String], # [P/[Q | R] | Stack], [Q | String]) [P/[Q | R] | Stack], [Q | String]) :- rule(Q/[S]). :- rule(Q/[S]). move([P/[Q , R | S] | Stack], [Q | String], $ move([P/[Q , R | S] | Stack], [Q | String], $ [P/[R | S] | Stack], String). [P/[R | S] | Stack], String). move([P/[Q] | Stack], [Q | String], % move([P/[Q] | Stack], [Q | String], % Here it is Stack, [P | String]). Stack, [P | String]). This will satisfy the So delete both of them So delete both of them whole rule Martin Kay Context-free Grammar 63 Martin Kay Context-free Grammar 64 The Three Main Paradigms parse(Stack0, String0) :- ! move(Stack0, String0, Stack, String), ! parse(Stack, String). parse([], [_]). • Top-down • Bottom-up —Shift-reduce —Left-corner

Martin Kay Context-free Grammar 65 Martin Kay Context-free Grammar 66

Top-down (again)

parse([], String, String). parse([Goal | Goals], [Goal | String], Rest) :- parse(Goals, String, Rest). parse([Goal | Goals], String0, Rest) :- rule(Goal, Rhs), parse(Rhs, String0, String1), We can manage with less than four cases parse(Goals, String1, Rest).

The empty set of goals is satisfied by the empty string, leaving everything else as the remainder

Martin Kay Context-free Grammar 67 Martin Kay Left-corner Parsing Top-down Top-down parse([], String, String). parse([], String, String). parse([Goal | Goals], [Goal | String], Rest) :- parse([Goal | Goals], [Goal | String], Rest) :- parse(Goals, String, Rest). parse(Goals, String, Rest). parse([Goal | Goals], String0, Rest) :- parse([Goal | Goals], String0, Rest) :- rule(Goal, Rhs), rule(Goal, Rhs), parse(RHS, String0, String1), parse(RHS, String0, String1), parse(Goals, String1, Rest). parse(Goals, String1, Rest).

If the Goal is found at the beginning of the string, try If there is a rule that would replace the next to show that the remainder of the string satisfies the remainder of the goals. goal by some more specific goals (RHS), then

Martin Kay Left-corner Parsing Martin Kay Left-corner Parsing

Top-down Top-down parse([], String, String). parse([], String, String). parse([Goal | Goals], [Goal | String], Rest) :- parse([Goal | Goals], [Goal | String], Rest) :- parse(Goals, String, Rest). parse(Goals, String, Rest). parse([Goal | Goals], String0, Rest) :- parse([Goal | Goals], String0, Rest) :- rule(Goal, Rhs), rule(Goal, Rhs), parse(RHS, String0, String1), parse(RHS, String0, String1), parse(Goals, String1, Rest). parse(Goals, String1, Rest).

If there is a rule that would replace the next If there is a rule that would replace the next goal by some more specific goals (RHS), then goal by some more specific goals (RHS), then try to satisfy these more specific goals, and try to satisfy these more specific goals, and then then try to satisfy the rest of the original set of goals

Martin Kay Left-corner Parsing Martin Kay Left-corner Parsing Bottom-up

0. [s/[np,vp]] [the,dog,chased,the,cat] 0 1. [np/[det,n],s/[np,vp]] [the,dog,chased,the,cat] 1 [] [the, dog, chased, ...] 2. [det/[the],np/[det,n],s/[np,vp]] [the,dog,chased,the,cat] 1 3. [np/[det,n],s/[np,vp]] [det,dog,chased,the,cat] 4 4. [det/[the],np/[det,n],s/[np,vp]] [det,dog,chased,the,cat] 1 det/[the] 4. [np/[n],s/[np,vp]] [dog,chased,the,cat] 3 5. [n/[cat],np/[n],s/[np,vp]] [dog,chased,the,cat] 1 5. [n/[dog],np/[n],s/[np,vp]] [dog,chased,the,cat] 1 6. [np/[n],s/[np,vp]] [n,chased,the,cat] 4 [det/[]] [dog, chased, the, cat] 7. [n/[cat],np/[n],s/[np,vp]] [n,chased,the,cat] 1 7. [n/[dog],np/[n],s/[np,vp]] [n,chased,the,cat] 1 7. [n/[house],np/[n],s/[np,vp]] [n,chased,the,cat] 1 7. [s/[np,vp]] [np,chased,the,cat] 4 8. [np/[det,n],s/[np,vp]] [np,chased,the,cat] 1 ... [] [det, dog, chased, ...]

Martin Kay Context-free Grammar 73 Martin Kay Context-free Grammar 74

0. [] [the,dog,chased,the,cat] 0 Bottom-up 1. [det/[]] [dog,chased,the,cat] 2 2. [] [det,dog,chased,the,cat] 1 3. [np/[n]] [dog,chased,the,cat] 2 [] [det, dog, chased, ...] 4. [np/[n],n/[]] [chased,the,cat] 2 5. [np/[n]] [n,chased,the,cat] 1 6. [np/[]] [chased,the,cat] 3 7. [] [np,chased,the,cat] 1 np/[det, n] 8. [s/[vp]] [chased,the,cat] 2 9. [s/[vp],v/[]] [the,cat] 2 10. [s/[vp]] [v,the,cat] 1 11. [s/[vp],vp/[np]] [the,cat] 2 [np/[n]] [dog, chased, the, cat] 12. [s/[vp],vp/[np],det/[]] [cat] 2 13. [s/[vp],vp/[np]] [det,cat] 1 14. [s/[vp],vp/[np],np/[n]] [cat] 2 15. [s/[vp],vp/[np],np/[n],n/[]] [] 2 16. [s/[vp],vp/[np],np/[n]] [n] 1 n/[dog] 17. [s/[vp],vp/[np],np/[]] [] 3 18. [s/[vp],vp/[np]] [np] 1 19. [s/[vp],vp/[np],s/[vp]] [] 2 19. [s/[vp],vp/[]] [] 3 [np/[n],n/[]] [chased, the, cat] 20. [s/[vp]] [vp] 1 21. [s/[]] [] 3 22. [] [s] 1 Martin Kay Context-free Grammar 75 Martin Kay Context-free Grammar 76 Left Corner Left Corner This rule is satisfied

So put the result in the string Need to consume a Q Here is a rule that could do it move([P/[] | Stack], String, move([P/[] | Stack], String, Stack, [P |String]) :- !. Stack, [P |String]) :- !. move(Stack, [Q | String], move(Stack, [Q | String], [P/R | Stack], String) :- rule(P/[Q | R]). [P/R | Stack], String) :- rule(P/[Q | R]). move([P/[Q | R] | Stack], [Q | String], move([P/[Q | R] | Stack], [Q | String], [P/R | Stack], String). [P/R | Stack], String).

No more Q

Try to satisfy rest of the rule

Martin Kay Context-free Grammar 77 Martin Kay Context-free Grammar 78

Left Corner Left-corner

Need to consume a Q Same as for top-down.

move([P/[] | Stack], String, Here is one Stack, [P |String]) :- !. move(Stack, [Q | String], [P/R | Stack], String) :- rule(P/[Q | R]). parse([], String, String). parse([Goal | Goals], [Goal | String], Rest) :- move([P/[Q | R] | Stack], [Q | String], parse(Goals, String, Rest). [P/R | Stack], String). parse(Goals, [Item | String0], Rest) :- rule(P, [Item | Rhs]), parse(RHS, String0, String1), parse(Goals, [P | String1], Rest). No more Q

Martin Kay Context-free Grammar 79 Martin Kay Left-corner Parsing Left-corner Left-corner Find a rule that could form a phrase beginning Find a rule that could form a phrase beginning with the leftmost item in the string (the left with the leftmost item in the string (the left corner). corner). With the right-hand side of the rule as a sequence of goals, try to find such a phrase.

parse([], String, String). parse([], String, String). parse([Goal | Goals], [Goal | String], Rest) :- parse([Goal | Goals], [Goal | String], Rest) :- parse(Goals, String, Rest). parse(Goals, String, Rest). parse(Goals, [Item | String0], Rest) :- parse(Goals, [Item | String0], Rest) :- rule(P, [Item | Rhs]), rule(P, [Item | Rhs]), parse(RHS, String0, String1), parse(RHS, String0, String1), parse(Goals, [P | String1], Rest). parse(Goals, [P | String1], Rest).

Martin Kay Left-corner Parsing Martin Kay Left-corner Parsing

Left-corner Top-down vs. Left-corner

Find a rule that could form a phrase beginning parse([], String, String). with the leftmost item in the string (the left parse([Goal | Goals], [Goal | String], Rest) :- corner). parse(Goals, String, Rest). With the right-hand side of the rule as a parse([Goal | Goals], String0, Rest) :- sequence of goals, try to find such a phrase. rule(Goal, RHS), Replace the phrase with its category, and parse(RHS, String0, String1), continue trying to satisfy the original goals. parse(Goals, String1, Rest).

parse([], String, String). parse([Goal | Goals], [Goal | String], Rest) :- parse([], String, String). parse(Goals, String, Rest). parse([Goal | Goals], [Goal | String], Rest) :- parse(Goals, [Item | String0], Rest) :- parse(Goals, String, Rest). rule(P, [Item | RHS]), parse(Goals, [Item | String0], Rest) :- parse(RHS, String0, String1), parse(Goals, [P | String1], Rest). rule(P, [Item | RHS]), parse(RHS, String0, String1), parse(Goals, [P | String1], Rest).

Martin Kay Left-corner Parsing Martin Kay Left-corner Parsing Shift reduce Finite-state guide

the dog chased the cat NP ! Det • N det dog chased the cat Grammar det n chased the cat S ! NP VP 2:NP np chased the cat N VP ! V NP np v the cat NP ! Det N Det Det np v det cat NP NP np v det n V 2:VP np v np VP np vp 2:S s S ! • NP VP VP ! V • NP S ! NP • VP NP ! • Det N NP ! • Det N VP ! • V NP

Martin Kay Context-free Grammar 85 Martin Kay Left-corner Parsing

Grammar Grammar S ! NP VP S ! NP VP VP ! V NP VP ! V NP NP ! Det N NP ! Det N

S ! • NP VP S ! • NP VP NP ! • Det N

Martin Kay Left-corner Parsing Martin Kay Left-corner Parsing NP ! Det • N NP ! Det • N Grammar Grammar S ! NP VP S ! NP VP VP ! V NP VP ! V NP NP ! Det N NP ! Det N Det Det

NP NP

S ! • NP VP S ! • NP VP S ! NP • VP S ! NP • VP NP ! • Det N NP ! • Det N VP ! • V NP

Martin Kay Left-corner Parsing Martin Kay Left-corner Parsing

NP ! Det • N NP ! Det • N Grammar Grammar S ! NP VP S ! NP VP 2:NP 2:NP N VP ! V NP N VP ! V NP NP ! Det N NP ! Det N Det Det Det NP NP NP V 2:VP VP 2:S S ! • NP VP S ! • NP VP VP ! V • NP S ! NP • VP S ! NP • VP NP ! • Det N NP ! • Det N NP ! • Det N VP ! • V NP VP ! • V NP

Martin Kay Left-corner Parsing Martin Kay Left-corner Parsing Reachability—Top down Reachability—Top down parse([], String, String). parse([], String, String). parse([Goal | Goals], [Goal | String], Rest) :- parse([Goal | Goals], [Goal | String], Rest) :- parse(Goals, String, Rest). parse(Goals, String, Rest). parse([Goal | Goals], String0, Rest) :- rule(Goal, Rhs), parse([Goal | Goals], [Item1 | String0], Rest) :- parse(RHS, String0, String1), rule(Goal, [Item2 | Rhs]), parse(Goals, String1, Rest). reachable(Item2, Item1), parse(RHS, [Item1 | String0], String1), parse(Goals, String1, Rest).

Martin Kay Left-corner Parsing Martin Kay Left-corner Parsing

Reachability—Bottom-up Reachability–Bottom-up

parse([], String, String). parse([], String, String). parse([Goal | Goals], [Goal | String], Rest) :- parse([Goal | Goals], [Goal | String], Rest) :- parse(Goals, String, Rest). parse(Goals, String, Rest). parse([Goal | Goals], [Item | String0], Rest) :- parse(Goals, [Item | String0], Rest) :- rule(P, [Item | Rhs]), rule(P, [Item | Rhs]), reachable(Goal, P), parse(RHS, String0, String1), parse(RHS, String0, String1), parse(Goals, [P | String1], Rest). parse([Goal | Goals], [P | String1], Rest).

Martin Kay Left-corner Parsing Martin Kay Left-corner Parsing Remember this? A

B A grammar that contains instances of left B ... C, D are reachable recursion canCOMING be replaced SOON! by one with no left from A. recursion which ∴ B ... C, D are (proper) left • C describes the same language corners of A. • assigns the same structures to the sentences. Transitive closure of the D left-daughter relation.

Martin Kay Context-free Grammar 97 Martin Kay Context-free Grammar 98

Rules s --> np, vp. Eliminating Left The Left-corner np --> det, n. n --> adj, n. Recursion (step 1) Transformation np --> np, pp. det --> np, ap_s. Reachability s --> np, s/np. S pp --> prep, np. s: np s --> det, s/det. vp --> iv. VP P/Q: The part of a P detnp --> det, np/det. vp --> vt, np. np --> np, np/np. that follows a Q vp --> vp, pp. np: det det --> np, det/np. NP NP det: det det --> det, det/det. det --> [the]. n: n pp --> prep, pp/prep. Det N V Det N n --> [dog]. vp --> iv, vp/iv. n --> [cat] vp: iv tv vp --> vt, vp/vt. The dog chased the cat n --> [bone]. vp --> vp, vp/vp n --> [field]. pp: prep adj --> [big]. Det prep --> [with]. S/Det prep --> [in]. vi --> [slept]. vt --> [chased]. S

Martin Kay Context-free Grammar 99 Martin Kay Left-corner Parsing Rules Rules s --> np, vp. s --> np, s/np. s --> np, vp. s --> np, s/np. np --> det, n. Step 2 s --> det, s/det. np --> det, n. Step 2 s --> det, s/det. n --> adj, n. np --> det, np/det. n --> adj, n. np --> det, np/det. np --> np, pp. np --> np, np/np. np --> np, pp. np --> np, np/np. det --> np, ap_s. det --> np, det/np. det --> np, ap_s. det --> np, det/np. pp --> prep, np. det --> det, det/det. pp --> prep, np. det --> det, det/det. vp --> iv. pp --> prep, pp/prep. vp --> iv. pp --> prep, pp/prep. vp --> vt, np. vp --> iv, vp/iv. vp --> vt, np. vp --> iv, vp/iv. vp --> vp, pp. vp --> vt, vp/vt. vp --> vp, pp. vp --> vt, vp/vt. vp --> vp, vp/vp vp --> vp, vp/vp det --> [the]. det --> [the]. n --> [dog]. s/np --> vp, s/s. n --> [dog]. s/np --> vp, s/s. n --> [cat] s/det --> n, s/np. n --> [cat] s/det --> n, s/np. n --> [bone]. np/det --> n, np/np. n --> [bone]. np/det --> n, np/np. n --> [field]. np/np --> []. n --> [field]. np/np --> []. adj --> [big]. np/np --> ap/s, np/det. adj --> [big]. np/np --> ap_s, np/det. prep --> [with]. det/np --> ap_s, det/det. prep --> [with]. det/np --> ap_s, det/det. prep --> [in]. pp/prep --> np, pp/pp prep --> [in]. pp/prep --> np, pp/pp vi --> [slept]. ... vi --> [slept]. ... vt --> [chased]. vt --> [chased].

Martin Kay Left-corner Parsing Martin Kay Left-corner Parsing

Caching Caching top-down

parse([], String, String). foo(Input1, Input2, Output1, Output2) parser(Goal,parse([Goal String) :- | Goals], [Goal | String], Rest) :- try(parse(Goals, String, Rest)). Inputs Outputs retractall(done(_)), parse([Goal | Goals], String0, Rest) :- retractall(result(_)), rule(Goal, RHS), try(parse([Goal], try(parse(RHS, String, [])). String0, String1)), try(foo(A, B, C, F)) :- try(parse(Goals, String1, Rest)). \+ done(foo(A, B)), try(parse(Goals, String0, String)) :- assert(done(foo(A, B))), \+ done(parse(Goals, String0)), foo(A, B, C, D), assert(done(parse(Goals, String0))), assert(result(foo(A, B, C, D))), Find all solutions on the parse(Goals, String0, String), fail. first call! assert(result(parse(Goals, String0, String))), try(foo(A, B, C, D)) :- fail. result(foo(A, B, C, D)). try(parse(Goals, String0, String)) :- result(parse(Goals, String0, String)).

Martin Kay Left-corner Parsing Martin Kay Left-corner Parsing apply_rule as a memo- A repackaged left-corner function parser apply_rule(String0, String) :- ! table(String0, Strings), parse([], String, String). !, parse([Goal | Goals], [Goal | String0], String) :- ! member(String, Strings). ! parse(Goals, String0, String). apply_rule(String0, String) :- parse(Goals, String0, String) :- ! setof(S, apply_rule0(String0, S), Strings), ! apply_rule(String0, String1), ! assert(table(String0, Strings)), ! parse(Goals, String1, String). ! member(String, Strings). apply_rule([Item | String0], [Phrase | String]) :- apply_rule0([Item | String0], [Phrase | ! rule(Phrase, [Item | Rhs]), String]) :- ! parse(Rhs, String0, String), ! rule(Phrase, [Item | Rhs]), ! parse(Rhs, String0, String).

Martin Kay Left-corner Parsing Martin Kay Left-corner Parsing

The Cache table([the,dog,chased,the,cat], • [det,dog,chased,the,cat]). Bottom-up parsers also use a stack, but in table([dog,chased,the,cat], [n,chased,the,cat]). this case, the stack represents a summary of the input already seen, rather than a table([det,dog,chased,the,cat], [np,chased,the,cat]). prediction about input yet to be seen. table([chased,the,cat], [v,the,cat]). table([the,cat], [det,cat]). table([cat], [n]). table([det,cat], [np]). table([v,the,cat], [vp]). table([np,chased,the,cat], [s]).

Martin Kay Left-corner Parsing Martin Kay Left-corner Parsing Backtracking Complexity

• Exponential—why?

Martin Kay Left-corner Parsing