PROLOG: Clauses Grammer In Prolog


Published on

PROLOG: Clauses Grammer In Prolog

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

PROLOG: Clauses Grammer In Prolog

  1. 1. Clause Grammars in Prolog<br />
  2. 2. OVERVIEW<br />Context free grammars<br />CFG recognition using append<br />CFG recognition using difference lists<br />Definite clause grammars<br />Adding Recursive Rules<br />A DCG for a simple formal language <br />
  3. 3. Context Free Grammars<br />Definite Clauses Grammars(DCGs) are a special notation for defining grammars.<br />context free grammar are a finite collection of rules which tell us that certain sentences are grammatical (that is, syntactically correct) and what their grammatical structure actually is.<br />
  4. 4. CFG<br />Consider the following examples:<br />s -> np vp<br />np -> det n<br />vp -> v np<br />vp -> v<br />det -> a<br />det -> the<br />n -> woman<br />n -> man<br />v -> shoots<br /><ul><li>here, s, np, vp, det, n, v symbols are called non-terminal symbols;
  5. 5. The symbols: a, the, woman, man, and shootsare called terminal symbols</li></ul>A context free rule consists of a single non-terminal symbol, followed by ->, followed by a finite sequence made up of terminal and/or nonterminal<br />symbols.<br />
  6. 6. CFG recognition using append<br />we can simply `turn the grammar into Prolog'.<br />Ex: the string a woman shoots a man will be represented by the list<br />[a,woman,shoots,a,man].<br />the rule s -> np vp can be thought of as saying: a list of words is an s list if it is the result of concatenating an np list with a vp list.<br />We can make use of append to turn these kinds of rules into Prolog.<br />s(Z) :- np(X), vp(Y), append(X,Y,Z).<br />np(Z) :- det(X), n(Y), append(X,Y,Z).<br />vp(Z) :- v(X), np(Y), append(X,Y,Z).<br />vp(Z) :- v(Z).<br />v([shoots]).<br />
  7. 7. det([the]).<br />det([a]).<br />n([woman]).<br />n([man]).<br />So, on posing the query s([a,woman,shoots,a,man]).<br /><ul><li>We get yes</li></ul>In fact, our little grammar generates 20 sentences. Here are the first five:<br />s(X).<br />X = [the,woman,shoots,the,woman] ;<br />X = [the,woman,shoots,the,man] ;<br />X = [the,woman,shoots,a,woman] ;<br />X = [the,woman,shoots,a,man] ;<br />X = [the,woman,shoots]<br />
  8. 8. CFG recognition using difference lists<br />A more efficient implementation can be obtained by making use of difference lists.<br />The key idea underlying difference lists is to represent the information about grammatical categories not as a single list, but as the difference between two lists. <br />For example, instead of representing a woman shoots a man as [a,woman,shoots,a,man] we might represent it as the pair of lists<br />[a,woman,shoots,a,man] [].<br />represents the sentence a woman shoots a man because it says: If I consume all the symbols on the left, and leave behind the symbols on the right, I have the sentence I am interested in.<br />That is: the sentence we are interested in is the difference between the contents of these two lists.<br />
  9. 9. Definite clause grammars<br />DCGs, quite simply is a nice notation for writing grammars that hides the underlying difference list variables.<br />Ex: The previous grammar written as a DCG:<br />s --> np,vp.<br />np --> det,n.<br />vp --> v,np.<br />vp --> v.<br />det --> [the].<br />det --> [a].<br />n --> [woman].<br />n --> [man].<br />v --> [shoots].<br />
  10. 10. Definite clause grammars<br />To find out whether a woman shoots a man is a sentence, we pose the query:<br />s([a,woman,shoots,a,man],[]).<br />That is, just as in the difference list recognizer, we ask whether we can get an s by consuming the symbols in [a,woman,shoots,a,man], leaving nothing behind.<br />Similarly, to generate all the sentences in the grammar, we pose the query:<br />s(X,[]).<br />
  11. 11. Adding Recursive Rules<br />Our original context free grammar generated only 20 sentences. However it is easy to write context free grammars that generate infinitely many sentences: <br />we need simply use recursive rules.<br />EX: Let's add the following rules to our grammar:<br />s -> s conj s<br />conj -> and<br />conj -> or<br />conj -> but<br />This rule allows us to join as many sentences together as we like using the words and, but and or.<br />
  12. 12. Adding Recursive Rules<br />Turning this grammar into DCG rules. <br />s --> s,conj,s.<br />conj --> [and].<br />conj --> [or].<br />conj --> [but].<br />First, let's add the rules at the beginning of the knowledge base before the rule s --> np,vp.<br />If now, we pose the query s([a,woman,shoots],[])? <br />Prolog gets into an infinite loop.<br />
  13. 13. So, by just reordering clauses or goals, we won't solve the problem. <br />The only possible solution is to introduce a new nonterminal symbol. We could for example use the category simple_s for sentences without embedded sentences. <br />Our grammar would then look like this:<br />s --> simple_s.<br />s --> simple_s conj s.<br />simple_s --> np,vp.<br />np --> det,n.<br />vp --> v,np.<br />vp --> v.<br />det --> [the].<br />det --> [a].<br />n --> [woman].<br />n --> [man].<br />v --> [shoots].<br />conj --> [and].<br />conj --> [or].<br />conj --> [but].<br />
  14. 14. A DCG for a simple formal language<br />we shall define a DCG for the formal language a^nb^n.<br />There are only two `words' in this language:<br />The symbol a and the symbol b.<br />The language consist of all strings made up from these two symbols that have the following form: the string must consist of an unbroken block of as of length n, followed by an unbroken block of bs of length n, and nothing else. <br />So the strings ab, aabb, aaabbb and aaaabbbb all belong to a^nb^n.<br />
  15. 15. CFG to generate this language:<br />s -> epsilon<br />s -> l s r<br />l -> a<br />r -> b<br />Turning this grammar into DCG.<br />s --> [].<br />s --> l,s,r.<br />l --> [a].<br />r --> [b].<br />And this DCG works exactly as we would hope. For example, to the query<br />s([a,a,a,b,b,b],[]).<br />we get the answer `yes',<br />
  16. 16. Visit more self help tutorials<br />Pick a tutorial of your choice and browse through it at your own pace.<br />The tutorials section is free, self-guiding and will not involve any additional support.<br />Visit us at<br />