An Introduction to Definite Clause Grammars Grammars and

  • Slides: 14
Download presentation
An Introduction to Definite Clause Grammars • Grammars and Algorithms • Prolog Recogniser •

An Introduction to Definite Clause Grammars • Grammars and Algorithms • Prolog Recogniser • DCGs Feb 2008 - MR Definite Clause Grammar 1

CF Recognition in Pure Prolog • Use lists to represent strings. the monkey grinned

CF Recognition in Pure Prolog • Use lists to represent strings. the monkey grinned [the, monkey, grinned] • Reinterpret CF rules as Prolog clauses s ® np vp s : - np, vp. • Use append to concatenate the strings s(Z) : - np(X), vp(Y), append(X, Y, Z) • Reinterpret lexical rules as simple assertions n ® [monkey] becomes n([monkey]). Feb 2008 - MR Definite Clause Grammar 2

Complete Recogniser Program s(Z) : - np(X), vp(Y), append(X, Y, Z). np(Z): - d(X),

Complete Recogniser Program s(Z) : - np(X), vp(Y), append(X, Y, Z). np(Z): - d(X), n(Y), append(X, Y, Z). vp(Z): - v(Z). d([the]). n([monkey]). v([grinned]). v([danced]). Feb 2008 - MR Definite Clause Grammar 3

Running the Recogniser ? - s([the, monkey, grinned]). yes. ? - s([the, monkey, spat]).

Running the Recogniser ? - s([the, monkey, grinned]). yes. ? - s([the, monkey, spat]). no. ? - s(X). X=[the, monkey, grinned]; X=[the, monkey, danced] Feb 2008 - MR Definite Clause Grammar 4

Difference Lists • Using append is very inefficient. • We can avoid append by

Difference Lists • Using append is very inefficient. • We can avoid append by using difference lists. • Key idea is to represent a string as the difference between two lists. • For this purpose we employ two pointers. Feb 2008 - MR Definite Clause Grammar 5

Difference Lists • Here are different ways of representing the string abc [a, b,

Difference Lists • Here are different ways of representing the string abc [a, b, c] [ ] [a, b, c, x] [a, b, c, x, y] [x, y] • a • b • c • x • y • P 1 Feb 2008 - MR P 2 Definite Clause Grammar 6

Recogniser Using Difference Lists s(X, Z) : np(X, Z): vp(X, Y): - np(X, Y),

Recogniser Using Difference Lists s(X, Z) : np(X, Z): vp(X, Y): - np(X, Y), vp(Y, Z). d(X, Y), n(Y, Z). v(X, Y). d([the|Rest], Rest). n([monkey|Rest], Rest). v([grinned|Rest], Rest). v([danced|Rest], Rest). Feb 2008 - MR Definite Clause Grammar 7

Running the Recogniser ? - s([the, monkey, grinned], []). yes. ? - s([the, monkey,

Running the Recogniser ? - s([the, monkey, grinned], []). yes. ? - s([the, monkey, spat], []). no. ? - s(X, []). X=[the, monkey, grinned]; X=[the, monkey, danced] Feb 2008 - MR Definite Clause Grammar 8

Difference Lists Advantages • append not used • efficiency Disadvantages • More difficult to

Difference Lists Advantages • append not used • efficiency Disadvantages • More difficult to understand the interpreter Can we retain advantages and eliminate disadvantages? Feb 2008 - MR Definite Clause Grammar 9

Definite Clause Grammars (DCGs) s vp vp d v np np --> --> Feb

Definite Clause Grammars (DCGs) s vp vp d v np np --> --> Feb 2008 - MR np, vp. v. v, np. [walks]. [hits]. [suzie]. [fido]. Definite Clause Grammar 10

How DCGs are compiled • We run the DCG recogniser in exactly the same

How DCGs are compiled • We run the DCG recogniser in exactly the same way as we run the difference-list recogniser. • The reason is that DCG notation is just syntactic sugar. The DCG rule x --> y, z. is actually compiled into an ordinary clause x(V 1, V 2) : - y(V 1, V 3), z(V 3, V 2). • whilst the preterminal rule x --> [word]. compiles to the clause x([word|W], W]). Feb 2008 - MR Definite Clause Grammar 11

Use of Curly Brackets • Suppose we have s(Z) -> np(X), vp(Y). and there

Use of Curly Brackets • Suppose we have s(Z) -> np(X), vp(Y). and there is an ordinary predicate p(X, Y, Z) which operates on the arguments. • We enclose the ordinary predicate in curly brackets to avoid the automatic addition of two argument places. s(Z) -> np(X), vp(Y), {p(X, Y, Z)} Feb 2008 - MR Definite Clause Grammar 12

Adding Features • Suppose we wish to impose number agreement between np and vp

Adding Features • Suppose we wish to impose number agreement between np and vp • To solve the problem of multiplying categories, we need to add feature constraints to our grammar specifications, in order to express the following sort of fact s ® np vp constraint: number(np) = number(vp) • Fortunately we can do just that because the constituents of rules can be full Prolog terms, which may include variables arguments. Feb 2008 - MR Definite Clause Grammar 13

Fixing the Grammar OLD GRAMMAR NEW GRAMMAR s np np vp s np(N) vp(N)

Fixing the Grammar OLD GRAMMAR NEW GRAMMAR s np np vp s np(N) vp(N) --> --> Feb 2008 - MR np, vp. n. d, n. v np. Definite Clause Grammar --> --> np(N), vp(N). n(N). d(N), n(N). v(N), np(_). 14