SlideShare a Scribd company logo
Lecture 7: Definite Clause Grammars Theory Introduce  context free grammars  and some related concepts Introduce  definite clause grammars , the Prolog way of working with context free grammars  (and other grammars too) Exercises Exercises of LPN: 7.1, 7.2, 7.3  Practical work
Context free grammars Prolog offers a special notation for defining  grammars , namely DCGs or definite clause grammars So what is a grammar? We will answer this question by discussing  context free grammars CFGs are a very powerful mechanism, and can handle most syntactic aspects of  natural languages   (such as English or Italian)
Example of a CFG s    np vp np    det n vp    v np vp    v det     the det     a n     man n     woman v     shoots
Ingredients of a grammar  The    symbol is used to define the  rules The symbols  s ,  np ,  vp ,  det ,  n ,  v  are called the  non-terminal  symbols The symbols in italics are the  terminal  symbols:    the, a, man,    woman, shoots   s    np vp np    det n vp    v np vp    v det     the det     a n     man n     woman v     shoots
A little bit of linguistics The non-terminal symbols in this grammar have a traditional meaning in linguistics: np :   noun phrase vp :   verb phrase det :  determiner n :  noun v :  verb s :  sentence
More linguistics In a linguistic grammar, the non-terminal symbols usually correspond to  grammatical categories In a linguistic grammar, the terminal symbols are called the  lexical items , or simply words  (a computer scientist might call them the  alphabet )
Context free rules The grammar contains  nine context free rules A context free rule consists of:  A single non-terminal symbol followed by   followed by a finite sequence of terminal or non-terminal symbols s    np vp np    det n vp    v np vp    v det     the det     a n     man n     woman v     shoots
Grammar coverage Consider the following string: the woman shoots a man Is this string grammatical according to our grammar? And if it is, what syntactic structure does it have?
Syntactic structure s  vp np  np  det  n  v  det  n the  woman  shoots  a  man s    np vp np    det n vp    v np vp    v det     the det     a n     man n     woman v     shoots
Parse trees Trees representing the syntactic structure of a string are often called  parse trees Parse trees are important: They give us information about the string They gives us information about structure
Grammatical strings If we are given a string of words, and a grammar, and it turns out we can build a parse tree, then we say that  the string is  grammatical   (with respect to the given grammar) E.g.,  the man shoots  is grammatical If we cannot build a parse tree, the given string is  ungrammatical  (with respect to the given grammar) E.g.,  a shoots woman  is ungrammatical
Generated language The  language generated by a grammar  consists of all the strings that the grammar classifies as grammatical For instance  a woman shoots a man a man shoots   belong to the language generated by our little grammar
Recogniser A context free  recogniser  is a program which correctly tells us whether or not a string belongs to the language generated by a context free grammar To put it another way, a  recogniser  is a program that correctly classifies strings as grammatical or ungrammatical
Information about structure But both in linguistics and computer science, we are not merely interested in whether a string is grammatical or not We also want to know  why  it is grammatical: we want to know what its structure is  The parse tree gives us this structure
Parser  A context free  parser  correctly decides whether a string belongs to the language generated by a context free grammar And it also tells us what its structure is To sum up:  A recogniser just says  yes  or  no   A parser also gives us a parse tree
Context free language We know what a context free grammar is, but what is a context free language? Simply: a context free language is a language that can be generated by a context free grammar Some human languages are context free, some others are not English and Italian are probably context free Dutch and Swiss-German are not context free
Theory vs. Practice So far the theory, but how do we work with context free grammars in Prolog? Suppose we are given a context free grammar.  How can we write a recogniser for it? How can we write a parser for it? In this lecture we will look at how to define a recogniser
CFG recognition in Prolog We shall use lists to represent strings   [a,woman,shoots,a,man] The rule  s    np vp   can be  thought as concatenating an  np -list with a  vp -list resulting in an  s -list We know how to concatenate lists in Prolog: using append/3 So let`s turn this idea into Prolog
CFG recognition using append/3 s (C):- np(A), vp(B), append(A,B,C). np (C):- det(A), n(B), append(A,B,C). vp (C):- v(A), np(B), append(A,B,C). vp (C):- v(C). det([the]).  det([a]).  n([man]).  n([woman]).  v([shoots]).
CFG recognition using append/3 ?- s ([the,woman,shoots,a,man]). yes ?- s (C):- np(A), vp(B), append(A,B,C). np (C):- det(A), n(B), append(A,B,C). vp (C):- v(A), np(B), append(A,B,C). vp (C):- v(C). det([the]).  det([a]).  n([man]).  n([woman]).  v([shoots]).
CFG recognition using append/3 ?- s (S). S = [the,man,shoots,the,man]; S = [the,man,shoots,the,woman]; S = [the,woman,shoots,a,man] … s (C):- np(A), vp(B), append(A,B,C). np (C):- det(A), n(B), append(A,B,C). vp (C):- v(A), np(B), append(A,B,C). vp (C):- v(C). det([the]).  det([a]).  n([man]).  n([woman]).  v([shoots]).
CFG recognition using append/3 ?- np ([the,woman]). yes ?-  np (X). X = [the,man]; X = [the,woman] s (C):- np(A), vp(B), append(A,B,C). np (C):- det(A), n(B), append(A,B,C). vp (C):- v(A), np(B), append(A,B,C). vp (C):- v(C). det([the]).  det([a]).  n([man]).  n([woman]).  v([shoots]).
Problems with this recogniser It doesn`t use the input string to guide the search Goals such as np (A) and vp(B) are called with uninstantiated variables Moving the append/3 goals to the front is still not very appealing --- this will only shift the problem  --- there will be a lot of calls to append/3 with uninstantiated variables
Difference lists A more efficient implementation can be obtained by using  difference lists This is a sophisticated Prolog technique for representing and working with lists Examples: [a,b,c]-[ ]  is the list [a,b,c] [a,b,c,d]-[d]  is the list [a,b,c] [a,b,c|T]-T  is the list [a,b,c] X-X  is the empty list [ ]
CFG recognition using difference lists s (A-C):- np(A-B), vp(B-C). np (A-C):- det(A-B), n(B-C).  vp (A-C):- v(A-B), np(B-C).  vp (A-C):- v(A-C). det([the|W]-W).  det([a|W]-W).  n([man|W]-W).  n([woman|W]-W).  v([shoots|W]-W).
CFG recognition using difference lists ?- s ([the,man,shoots,a,man]-[ ]). yes ?- s (A-C):- np(A-B), vp(B-C). np (A-C):- det(A-B), n(B-C).  vp (A-C):- v(A-B), np(B-C).  vp (A-C):- v(A-C). det([the|W]-W).  det([a|W]-W).  n([man|W]-W).  n([woman|W]-W).  v([shoots|W]-W).
CFG recognition using difference lists ?-  s (X-[ ]). S = [the,man,shoots,the,man]; S = [the,man,shoots,a,man]; … . s (A-C):- np(A-B), vp(B-C). np (A-C):- det(A-B), n(B-C).  vp (A-C):- v(A-B), np(B-C).  vp (A-C):- v(A-C). det([the|W]-W).  det([a|W]-W).  n([man|W]-W).  n([woman|W]-W).  v([shoots|W]-W).
Summary so far The recogniser using difference lists is a lot more efficient than the one using append/3 However, it is not that easy to understand and it is a pain having to keep track of all those difference list variables It would be nice to have a recogniser as simple as the first and as efficient as the second This is possible: using DCGs
Definite Clause Grammars What are DCGs? Quite simply, a nice notation for writing grammars that hides the underlying difference list variables Let us look at three examples
DCGs: first example s  --> np, vp. np  --> det, n. vp  --> v, np. vp  --> v. det  --> [the].  det  --> [a]. n   --> [man].  n   --> [woman].  v   --> [shoots].
DCGs: first example ?- s ([a,man,shoots,a,woman],[ ]). yes ?- s  --> np, vp. np  --> det, n. vp  --> v, np. vp  --> v. det  --> [the].  det  --> [a]. n   --> [man].  n   --> [woman].  v   --> [shoots].
DCGs: first example ?-  s (X,[ ]). S = [the,man,shoots,the,man]; S = [the,man,shoots,a,man]; … . s  --> np, vp. np  --> det, n. vp  --> v, np. vp  --> v. det  --> [the].  det  --> [a]. n   --> [man].  n   --> [woman].  v   --> [shoots].
DCGs: second example We added some recursive rules to the grammar… What and how many sentences does this grammar generate? What does Prolog do with this DCG? s --> s, conj, s.  s  --> np, vp. np  --> det, n.  vp  --> v, np.  vp  --> v. det  --> [the].  det  --> [a]. n   --> [man].  n   --> [woman].  v   --> [shoots]. conj --> [and].  conj --> [or].  conj --> [but].
DCG without left-recursive rules s --> simple _ s, conj, s.  s --> simple _ s. simple _ s  --> np, vp. np  --> det, n.  vp  --> v, np.  vp  --> v. det  --> [the].  det  --> [a]. n   --> [man].  n   --> [woman].  v   --> [shoots]. conj --> [and].  conj --> [or].  conj --> [but].
DCGs are not magic! The moral: DCGs are a nice notation, but you cannot write arbitrary context-free grammars as a DCG and have it run without problems DCGs are ordinary Prolog rules in disguise So keep an eye out for left-recursion!
DCGs: third example We will define a DCG for a formal language A formal language is simple a set of strings Formal languages are objects that computer scientist and mathematicians define and study Natural languages are languages that human beings normally use to communicate We will define the language  a n b n
DCGs: third example s  --> []. s  --> l,s,r. l  --> [a]. r  --> [b]. ?-  s ([a,a,a,b,b,b],[ ]). yes ?-  s ([a,a,a,a,b,b,b],[ ]). no We will define the formal   language  a n b n
DCGs: third example s  --> []. s  --> l,s,r. l  --> [a]. r  --> [b]. ?- s(X,[ ]). X = [ ]; X = [a,b]; X = [a,a,b,b]; X = [a,a,a,b,b,b] … . We will define the formal   language  a n b n
Exercises LPN 7.1 LPN 7.2 LPN 7.3
Summary of this lecture  We explained the idea of grammars and context free grammars are We introduced the Prolog technique of using difference lists We showed that difference lists can be used to describe grammars Definite Clause Grammars is just a nice Prolog notation for programming with difference lists
Next lecture More Definite Clause Grammars Examine two important capabilities offered by DCG notation Extra arguments Extra tests  Discuss the status and limitations of definite clause grammars

More Related Content

PDF
Lecture: Context-Free Grammars
PPTX
Context Free Grammar
PPTX
Deterministic context free grammars &non-deterministic
PDF
Context free langauges
PPTX
Types of Language in Theory of Computation
PPT
Regular expressions and languages pdf
PPTX
NLP_KASHK:Context-Free Grammar for English
PPT
context free language
Lecture: Context-Free Grammars
Context Free Grammar
Deterministic context free grammars &non-deterministic
Context free langauges
Types of Language in Theory of Computation
Regular expressions and languages pdf
NLP_KASHK:Context-Free Grammar for English
context free language

What's hot (18)

PDF
Flat unit 3
ODP
Final formal languages
PDF
Setswana Tokenisation and Computational Verb Morphology: Facing the Challenge...
PDF
Unit i
PDF
Unit ii
PPTX
NLP_KASHK:Finite-State Morphological Parsing
PPT
Lecture 8
PPTX
Formal language
PPT
Class7
PPT
Theory of Automata
PPT
Lecture 3,4
PPTX
Unit v
ODP
Ldml - public
PPTX
Chomsky classification of Language
PDF
Mba ebooks ! Edhole
ODP
Ldml presentation
PDF
Chapter1 Formal Language and Automata Theory
PPT
Flat unit 3
Final formal languages
Setswana Tokenisation and Computational Verb Morphology: Facing the Challenge...
Unit i
Unit ii
NLP_KASHK:Finite-State Morphological Parsing
Lecture 8
Formal language
Class7
Theory of Automata
Lecture 3,4
Unit v
Ldml - public
Chomsky classification of Language
Mba ebooks ! Edhole
Ldml presentation
Chapter1 Formal Language and Automata Theory
Ad

Similar to Lecture 7: Definite Clause Grammars (20)

PPTX
PROLOG: Clauses Grammer In Prolog
PPTX
PROLOG: Clauses Grammer In Prolog
PPTX
natural language processing
PDF
ToC_M1L3_Grammar and Derivation.pdf
PPT
NLP Natural Language Processing 8th Chapter.ppt
PPT
Inteligencia artificial
PPTX
Natural Language processing Parts of speech tagging, its classes, and how to ...
PPT
NLP-my-lecture (3).ppt
PPT
ssNL11SyntaxandContext-free grammars.ppt
PPT
Natural Language Processing 9th Chapter.ppt
PPTX
Natural Language parsing.pptx
PDF
Stemming algorithms
PPTX
Dhdhddhd5. Syntactic analysis-Parsing.pptx
PPTX
5. Syntacticfffgffg analysis-Parsing.pptx
PPTX
Grammar Syntax(1).pptx
PDF
Segmenting dna sequence into words
PPTX
Regular-expressions in NLP and regular expression with example
PPTX
sentence patterns
PPTX
PROLOG: Clauses Grammer In Prolog
PROLOG: Clauses Grammer In Prolog
natural language processing
ToC_M1L3_Grammar and Derivation.pdf
NLP Natural Language Processing 8th Chapter.ppt
Inteligencia artificial
Natural Language processing Parts of speech tagging, its classes, and how to ...
NLP-my-lecture (3).ppt
ssNL11SyntaxandContext-free grammars.ppt
Natural Language Processing 9th Chapter.ppt
Natural Language parsing.pptx
Stemming algorithms
Dhdhddhd5. Syntactic analysis-Parsing.pptx
5. Syntacticfffgffg analysis-Parsing.pptx
Grammar Syntax(1).pptx
Segmenting dna sequence into words
Regular-expressions in NLP and regular expression with example
sentence patterns
Ad

More from CS, NcState (20)

PPTX
Talks2015 novdec
PPTX
Future se oct15
PPTX
GALE: Geometric active learning for Search-Based Software Engineering
PPTX
Big Data: the weakest link
PPTX
Three Laws of Trusted Data Sharing: (Building a Better Business Case for Dat...
PPTX
Lexisnexis june9
PPTX
Welcome to ICSE NIER’15 (new ideas and emerging results).
PPTX
Icse15 Tech-briefing Data Science
PPTX
Kits to Find the Bits that Fits
PPTX
Ai4se lab template
PPTX
Automated Software Enging, Fall 2015, NCSU
PPT
Requirements Engineering
PPT
172529main ken and_tim_software_assurance_research_at_west_virginia
PPTX
Automated Software Engineering
PDF
Next Generation “Treatment Learning” (finding the diamonds in the dust)
PPTX
Tim Menzies, directions in Data Science
PPTX
Goldrush
PPTX
Dagstuhl14 intro-v1
PPTX
Know thy tools
PPTX
The Art and Science of Analyzing Software Data
Talks2015 novdec
Future se oct15
GALE: Geometric active learning for Search-Based Software Engineering
Big Data: the weakest link
Three Laws of Trusted Data Sharing: (Building a Better Business Case for Dat...
Lexisnexis june9
Welcome to ICSE NIER’15 (new ideas and emerging results).
Icse15 Tech-briefing Data Science
Kits to Find the Bits that Fits
Ai4se lab template
Automated Software Enging, Fall 2015, NCSU
Requirements Engineering
172529main ken and_tim_software_assurance_research_at_west_virginia
Automated Software Engineering
Next Generation “Treatment Learning” (finding the diamonds in the dust)
Tim Menzies, directions in Data Science
Goldrush
Dagstuhl14 intro-v1
Know thy tools
The Art and Science of Analyzing Software Data

Recently uploaded (20)

PPTX
Big Data Technologies - Introduction.pptx
PDF
Approach and Philosophy of On baking technology
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPT
Teaching material agriculture food technology
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Encapsulation theory and applications.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
Spectroscopy.pptx food analysis technology
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
KodekX | Application Modernization Development
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Big Data Technologies - Introduction.pptx
Approach and Philosophy of On baking technology
NewMind AI Weekly Chronicles - August'25 Week I
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Diabetes mellitus diagnosis method based random forest with bat algorithm
Teaching material agriculture food technology
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Chapter 3 Spatial Domain Image Processing.pdf
Programs and apps: productivity, graphics, security and other tools
Encapsulation theory and applications.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Spectroscopy.pptx food analysis technology
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
The Rise and Fall of 3GPP – Time for a Sabbatical?
The AUB Centre for AI in Media Proposal.docx
KodekX | Application Modernization Development
Profit Center Accounting in SAP S/4HANA, S4F28 Col11

Lecture 7: Definite Clause Grammars

  • 1. Lecture 7: Definite Clause Grammars Theory Introduce context free grammars and some related concepts Introduce definite clause grammars , the Prolog way of working with context free grammars (and other grammars too) Exercises Exercises of LPN: 7.1, 7.2, 7.3 Practical work
  • 2. Context free grammars Prolog offers a special notation for defining grammars , namely DCGs or definite clause grammars So what is a grammar? We will answer this question by discussing context free grammars CFGs are a very powerful mechanism, and can handle most syntactic aspects of natural languages (such as English or Italian)
  • 3. Example of a CFG s  np vp np  det n vp  v np vp  v det  the det  a n  man n  woman v  shoots
  • 4. Ingredients of a grammar The  symbol is used to define the rules The symbols s , np , vp , det , n , v are called the non-terminal symbols The symbols in italics are the terminal symbols: the, a, man, woman, shoots s  np vp np  det n vp  v np vp  v det  the det  a n  man n  woman v  shoots
  • 5. A little bit of linguistics The non-terminal symbols in this grammar have a traditional meaning in linguistics: np : noun phrase vp : verb phrase det : determiner n : noun v : verb s : sentence
  • 6. More linguistics In a linguistic grammar, the non-terminal symbols usually correspond to grammatical categories In a linguistic grammar, the terminal symbols are called the lexical items , or simply words (a computer scientist might call them the alphabet )
  • 7. Context free rules The grammar contains nine context free rules A context free rule consists of: A single non-terminal symbol followed by  followed by a finite sequence of terminal or non-terminal symbols s  np vp np  det n vp  v np vp  v det  the det  a n  man n  woman v  shoots
  • 8. Grammar coverage Consider the following string: the woman shoots a man Is this string grammatical according to our grammar? And if it is, what syntactic structure does it have?
  • 9. Syntactic structure s vp np np det n v det n the woman shoots a man s  np vp np  det n vp  v np vp  v det  the det  a n  man n  woman v  shoots
  • 10. Parse trees Trees representing the syntactic structure of a string are often called parse trees Parse trees are important: They give us information about the string They gives us information about structure
  • 11. Grammatical strings If we are given a string of words, and a grammar, and it turns out we can build a parse tree, then we say that the string is grammatical (with respect to the given grammar) E.g., the man shoots is grammatical If we cannot build a parse tree, the given string is ungrammatical (with respect to the given grammar) E.g., a shoots woman is ungrammatical
  • 12. Generated language The language generated by a grammar consists of all the strings that the grammar classifies as grammatical For instance a woman shoots a man a man shoots belong to the language generated by our little grammar
  • 13. Recogniser A context free recogniser is a program which correctly tells us whether or not a string belongs to the language generated by a context free grammar To put it another way, a recogniser is a program that correctly classifies strings as grammatical or ungrammatical
  • 14. Information about structure But both in linguistics and computer science, we are not merely interested in whether a string is grammatical or not We also want to know why it is grammatical: we want to know what its structure is The parse tree gives us this structure
  • 15. Parser A context free parser correctly decides whether a string belongs to the language generated by a context free grammar And it also tells us what its structure is To sum up: A recogniser just says yes or no A parser also gives us a parse tree
  • 16. Context free language We know what a context free grammar is, but what is a context free language? Simply: a context free language is a language that can be generated by a context free grammar Some human languages are context free, some others are not English and Italian are probably context free Dutch and Swiss-German are not context free
  • 17. Theory vs. Practice So far the theory, but how do we work with context free grammars in Prolog? Suppose we are given a context free grammar. How can we write a recogniser for it? How can we write a parser for it? In this lecture we will look at how to define a recogniser
  • 18. CFG recognition in Prolog We shall use lists to represent strings [a,woman,shoots,a,man] The rule s  np vp can be thought as concatenating an np -list with a vp -list resulting in an s -list We know how to concatenate lists in Prolog: using append/3 So let`s turn this idea into Prolog
  • 19. CFG recognition using append/3 s (C):- np(A), vp(B), append(A,B,C). np (C):- det(A), n(B), append(A,B,C). vp (C):- v(A), np(B), append(A,B,C). vp (C):- v(C). det([the]). det([a]). n([man]). n([woman]). v([shoots]).
  • 20. CFG recognition using append/3 ?- s ([the,woman,shoots,a,man]). yes ?- s (C):- np(A), vp(B), append(A,B,C). np (C):- det(A), n(B), append(A,B,C). vp (C):- v(A), np(B), append(A,B,C). vp (C):- v(C). det([the]). det([a]). n([man]). n([woman]). v([shoots]).
  • 21. CFG recognition using append/3 ?- s (S). S = [the,man,shoots,the,man]; S = [the,man,shoots,the,woman]; S = [the,woman,shoots,a,man] … s (C):- np(A), vp(B), append(A,B,C). np (C):- det(A), n(B), append(A,B,C). vp (C):- v(A), np(B), append(A,B,C). vp (C):- v(C). det([the]). det([a]). n([man]). n([woman]). v([shoots]).
  • 22. CFG recognition using append/3 ?- np ([the,woman]). yes ?- np (X). X = [the,man]; X = [the,woman] s (C):- np(A), vp(B), append(A,B,C). np (C):- det(A), n(B), append(A,B,C). vp (C):- v(A), np(B), append(A,B,C). vp (C):- v(C). det([the]). det([a]). n([man]). n([woman]). v([shoots]).
  • 23. Problems with this recogniser It doesn`t use the input string to guide the search Goals such as np (A) and vp(B) are called with uninstantiated variables Moving the append/3 goals to the front is still not very appealing --- this will only shift the problem --- there will be a lot of calls to append/3 with uninstantiated variables
  • 24. Difference lists A more efficient implementation can be obtained by using difference lists This is a sophisticated Prolog technique for representing and working with lists Examples: [a,b,c]-[ ] is the list [a,b,c] [a,b,c,d]-[d] is the list [a,b,c] [a,b,c|T]-T is the list [a,b,c] X-X is the empty list [ ]
  • 25. CFG recognition using difference lists s (A-C):- np(A-B), vp(B-C). np (A-C):- det(A-B), n(B-C). vp (A-C):- v(A-B), np(B-C). vp (A-C):- v(A-C). det([the|W]-W). det([a|W]-W). n([man|W]-W). n([woman|W]-W). v([shoots|W]-W).
  • 26. CFG recognition using difference lists ?- s ([the,man,shoots,a,man]-[ ]). yes ?- s (A-C):- np(A-B), vp(B-C). np (A-C):- det(A-B), n(B-C). vp (A-C):- v(A-B), np(B-C). vp (A-C):- v(A-C). det([the|W]-W). det([a|W]-W). n([man|W]-W). n([woman|W]-W). v([shoots|W]-W).
  • 27. CFG recognition using difference lists ?- s (X-[ ]). S = [the,man,shoots,the,man]; S = [the,man,shoots,a,man]; … . s (A-C):- np(A-B), vp(B-C). np (A-C):- det(A-B), n(B-C). vp (A-C):- v(A-B), np(B-C). vp (A-C):- v(A-C). det([the|W]-W). det([a|W]-W). n([man|W]-W). n([woman|W]-W). v([shoots|W]-W).
  • 28. Summary so far The recogniser using difference lists is a lot more efficient than the one using append/3 However, it is not that easy to understand and it is a pain having to keep track of all those difference list variables It would be nice to have a recogniser as simple as the first and as efficient as the second This is possible: using DCGs
  • 29. Definite Clause Grammars What are DCGs? Quite simply, a nice notation for writing grammars that hides the underlying difference list variables Let us look at three examples
  • 30. DCGs: first example s --> np, vp. np --> det, n. vp --> v, np. vp --> v. det --> [the]. det --> [a]. n --> [man]. n --> [woman]. v --> [shoots].
  • 31. DCGs: first example ?- s ([a,man,shoots,a,woman],[ ]). yes ?- s --> np, vp. np --> det, n. vp --> v, np. vp --> v. det --> [the]. det --> [a]. n --> [man]. n --> [woman]. v --> [shoots].
  • 32. DCGs: first example ?- s (X,[ ]). S = [the,man,shoots,the,man]; S = [the,man,shoots,a,man]; … . s --> np, vp. np --> det, n. vp --> v, np. vp --> v. det --> [the]. det --> [a]. n --> [man]. n --> [woman]. v --> [shoots].
  • 33. DCGs: second example We added some recursive rules to the grammar… What and how many sentences does this grammar generate? What does Prolog do with this DCG? s --> s, conj, s. s --> np, vp. np --> det, n. vp --> v, np. vp --> v. det --> [the]. det --> [a]. n --> [man]. n --> [woman]. v --> [shoots]. conj --> [and]. conj --> [or]. conj --> [but].
  • 34. DCG without left-recursive rules s --> simple _ s, conj, s. s --> simple _ s. simple _ s --> np, vp. np --> det, n. vp --> v, np. vp --> v. det --> [the]. det --> [a]. n --> [man]. n --> [woman]. v --> [shoots]. conj --> [and]. conj --> [or]. conj --> [but].
  • 35. DCGs are not magic! The moral: DCGs are a nice notation, but you cannot write arbitrary context-free grammars as a DCG and have it run without problems DCGs are ordinary Prolog rules in disguise So keep an eye out for left-recursion!
  • 36. DCGs: third example We will define a DCG for a formal language A formal language is simple a set of strings Formal languages are objects that computer scientist and mathematicians define and study Natural languages are languages that human beings normally use to communicate We will define the language a n b n
  • 37. DCGs: third example s --> []. s --> l,s,r. l --> [a]. r --> [b]. ?- s ([a,a,a,b,b,b],[ ]). yes ?- s ([a,a,a,a,b,b,b],[ ]). no We will define the formal language a n b n
  • 38. DCGs: third example s --> []. s --> l,s,r. l --> [a]. r --> [b]. ?- s(X,[ ]). X = [ ]; X = [a,b]; X = [a,a,b,b]; X = [a,a,a,b,b,b] … . We will define the formal language a n b n
  • 39. Exercises LPN 7.1 LPN 7.2 LPN 7.3
  • 40. Summary of this lecture We explained the idea of grammars and context free grammars are We introduced the Prolog technique of using difference lists We showed that difference lists can be used to describe grammars Definite Clause Grammars is just a nice Prolog notation for programming with difference lists
  • 41. Next lecture More Definite Clause Grammars Examine two important capabilities offered by DCG notation Extra arguments Extra tests Discuss the status and limitations of definite clause grammars