SlideShare une entreprise Scribd logo
1  sur  41
Télécharger pour lire hors ligne
Lecture 7: Definite Clause Grammars


                                                       •  Theory
                                                         – Introduce context free grammars and
                                                           some related concepts
© Patrick Blackburn, Johan Bos & Kristina Striegnitz




                                                         – 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
© Patrick Blackburn, Johan Bos & Kristina Striegnitz




                                                       •  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
© Patrick Blackburn, Johan Bos & Kristina Striegnitz




                                                         vp → v
                                                         det → the
                                                         det → a
                                                         n → man
                                                         n → woman
                                                         v → shoots
Ingredients of a grammar

                                                       •  The → symbol is used to      s → np vp
                                                          define the rules             np → det n
                                                       •  The symbols s, np, vp,
                                                                                       vp → v np
© Patrick Blackburn, Johan Bos & Kristina Striegnitz




                                                          det, n, v are called the
                                                                                       vp → v
                                                          non-terminal symbols
                                                                                       det → the
                                                       •  The symbols in italics are
                                                          the terminal symbols:        det → a
                                                            the, a, man,
                                                                                       n → man
                                                            woman, shoots
                                                                                       n → woman
                                                                                       v → shoots
A little bit of linguistics

                                                       •  The non-terminal symbols in this
                                                          grammar have a traditional meaning in
                                                          linguistics:
© Patrick Blackburn, Johan Bos & Kristina Striegnitz




                                                         – 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
© Patrick Blackburn, Johan Bos & Kristina Striegnitz




                                                       •  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                  s → np vp
                                                          nine context free rules               np → det n
                                                       •  A context free rule consists of:
                                                                                                vp → v np
© Patrick Blackburn, Johan Bos & Kristina Striegnitz




                                                          –  A single non-terminal symbol
                                                                                                vp → v
                                                          –  followed by →
                                                                                                det → the
                                                          –  followed by a finite sequence of
                                                             terminal or non-terminal symbols
                                                                                                det → a
                                                                                                n → man
                                                                                                n → woman
                                                                                                v → shoots
Grammar coverage

                                                       •  Consider the following string:

                                                         the woman shoots a man
© Patrick Blackburn, Johan Bos & Kristina Striegnitz




                                                       •  Is this string grammatical according to
                                                          our grammar?
                                                       •  And if it is, what syntactic structure
                                                          does it have?
Syntactic structure
                                                                                                  s → np vp
                                                                      s                           np → det n
                                                                                                  vp → v np
                                                                                                  vp → v
                                                                              vp
© Patrick Blackburn, Johan Bos & Kristina Striegnitz




                                                                                                  det → the
                                                                                                  det → a
                                                                                                  n → man
                                                             np                          np       n → woman
                                                                                                  v → shoots


                                                       det        n       v        det        n

                                                       the woman shoots            a      man
Parse trees

                                                       •  Trees representing the syntactic
                                                          structure of a string are often called
                                                          parse trees
© Patrick Blackburn, Johan Bos & Kristina Striegnitz




                                                       •  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
© Patrick Blackburn, Johan Bos & Kristina Striegnitz




                                                          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
© Patrick Blackburn, Johan Bos & Kristina Striegnitz




                                                        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
© Patrick Blackburn, Johan Bos & Kristina Striegnitz




                                                          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
© Patrick Blackburn, Johan Bos & Kristina Striegnitz




                                                       •  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
© Patrick Blackburn, Johan Bos & Kristina Striegnitz




                                                          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
© Patrick Blackburn, Johan Bos & Kristina Striegnitz




                                                          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
© Patrick Blackburn, Johan Bos & Kristina Striegnitz




                                                          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
© Patrick Blackburn, Johan Bos & Kristina Striegnitz




                                                          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).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz




                                                          vp(C):- v(C).
                                                          det([the]).    det([a]).
                                                          n([man]).      n([woman]).     v([shoots]).
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).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz




                                                            vp(C):- v(C).
                                                            det([the]).    det([a]).
                                                            n([man]).      n([woman]).     v([shoots]).

                                                       ?- s([the,woman,shoots,a,man]).
                                                       yes
                                                       ?-
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).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz




                                                            vp(C):- v(C).
                                                            det([the]).    det([a]).
                                                            n([man]).      n([woman]).     v([shoots]).

                                                       ?- s(S).
                                                       S = [the,man,shoots,the,man];
                                                       S = [the,man,shoots,the,woman];
                                                       S = [the,woman,shoots,a,man]
                                                       …
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).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz




                                                            vp(C):- v(C).
                                                            det([the]).    det([a]).
                                                            n([man]).      n([woman]).     v([shoots]).

                                                       ?- np([the,woman]).
                                                       yes
                                                       ?- np(X).
                                                       X = [the,man];
                                                       X = [the,woman]
Problems with this recogniser

                                                       •  It doesn`t use the input string to guide
                                                          the search
                                                       •  Goals such as np(A) and vp(B) are
© Patrick Blackburn, Johan Bos & Kristina Striegnitz




                                                          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
© Patrick Blackburn, Johan Bos & Kristina Striegnitz




                                                          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).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz




                                                       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(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).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz




                                                       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).

                                                       ?- s([the,man,shoots,a,man]-[ ]).
                                                       yes
                                                       ?-
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).
© Patrick Blackburn, Johan Bos & Kristina Striegnitz




                                                       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).

                                                       ?- s(X-[ ]).
                                                       S = [the,man,shoots,the,man];
                                                       S = [the,man,shoots,a,man];
                                                       ….
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
© Patrick Blackburn, Johan Bos & Kristina Striegnitz




                                                          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
© Patrick Blackburn, Johan Bos & Kristina Striegnitz




                                                          difference list variables
                                                       •  Let us look at three examples
DCGs: first example
                                                       s --> np, vp.
                                                       np --> det, n.
                                                       vp --> v, np.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz




                                                       vp --> v.
                                                       det --> [the].   det --> [a].
                                                       n --> [man].     n --> [woman].   v --> [shoots].
DCGs: first example
                                                       s --> np, vp.
                                                       np --> det, n.
                                                       vp --> v, np.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz




                                                       vp --> v.
                                                       det --> [the].           det --> [a].
                                                       n --> [man].             n --> [woman].   v --> [shoots].

                                                       ?- s([a,man,shoots,a,woman],[ ]).
                                                       yes
                                                       ?-
DCGs: first example
                                                       s --> np, vp.
                                                       np --> det, n.
                                                       vp --> v, np.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz




                                                       vp --> v.
                                                       det --> [the].          det --> [a].
                                                       n --> [man].            n --> [woman].   v --> [shoots].

                                                       ?- s(X,[ ]).
                                                       S = [the,man,shoots,the,man];
                                                       S = [the,man,shoots,a,man];
                                                       ….
DCGs: second example
                                                       s --> s, conj, s.   s --> np, vp.
                                                       np --> det, n.      vp --> v, np.    vp --> v.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz




                                                       det --> [the].      det --> [a].
                                                       n --> [man].        n --> [woman].     v --> [shoots].
                                                       conj --> [and].     conj --> [or].   conj --> [but].


                                                        •  We added some recursive rules to the grammar…
                                                        •  What and how many sentences does this grammar
                                                           generate?
                                                        •  What does Prolog do with this DCG?
DCG without left-recursive rules

                                                       s --> simple_s, conj, s.
                                                       s --> simple_s.
                                                       simple_s --> np, vp.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz




                                                       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
© Patrick Blackburn, Johan Bos & Kristina Striegnitz




                                                          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
© Patrick Blackburn, Johan Bos & Kristina Striegnitz




                                                          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 anbn
DCGs: third example
                                                                                      s --> [].
                                                       •  We will define the formal   s --> l,s,r.
                                                          language anbn               l --> [a].
© Patrick Blackburn, Johan Bos & Kristina Striegnitz




                                                                                      r --> [b].

                                                       ?- s([a,a,a,b,b,b],[ ]).
                                                       yes
                                                       ?- s([a,a,a,a,b,b,b],[ ]).
                                                       no
DCGs: third example
                                                                                      s --> [].
                                                       •  We will define the formal   s --> l,s,r.
                                                          language anbn               l --> [a].
© Patrick Blackburn, Johan Bos & Kristina Striegnitz




                                                                                      r --> [b].

                                                       ?- s(X,[ ]).
                                                       X = [ ];
                                                       X = [a,b];
                                                       X = [a,a,b,b];
                                                       X = [a,a,a,b,b,b]
                                                       ….
© Patrick Blackburn, Johan Bos & Kristina Striegnitz




                                             •  LPN 7.3
                                             •  LPN 7.2
                                             •  LPN 7.1
                                                          Exercises
Summary of this lecture

                                                       •  We explained the idea of grammars
                                                          and context free grammars are
                                                       •  We introduced the Prolog technique of
© Patrick Blackburn, Johan Bos & Kristina Striegnitz




                                                          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
© Patrick Blackburn, Johan Bos & Kristina Striegnitz




                                                            •  Extra arguments
                                                            •  Extra tests
                                                         – Discuss the status and limitations of
                                                           definite clause grammars

Contenu connexe

En vedette

Ch11 input output systems
Ch11 input output systemsCh11 input output systems
Ch11 input output systemsissbp
 
Os2 2
Os2 2Os2 2
Os2 2issbp
 
Os10 2
Os10 2Os10 2
Os10 2issbp
 
Theory of Automata and formal languages Unit 5
Theory of Automata and formal languages Unit 5Theory of Automata and formal languages Unit 5
Theory of Automata and formal languages Unit 5Abhimanyu Mishra
 
Pattern detection in mealy machine
Pattern detection in mealy machinePattern detection in mealy machine
Pattern detection in mealy machineAnimesh Chaturvedi
 
Automated Web Service Change Management (AWSCM) A tool published at IEEE Clou...
Automated Web Service Change Management (AWSCM) A tool published at IEEE Clou...Automated Web Service Change Management (AWSCM) A tool published at IEEE Clou...
Automated Web Service Change Management (AWSCM) A tool published at IEEE Clou...Animesh Chaturvedi
 
Class6
 Class6 Class6
Class6issbp
 
0227 regularlanguages
 0227 regularlanguages 0227 regularlanguages
0227 regularlanguagesissbp
 
Pumping Lemma and Regular language or not?
Pumping Lemma and Regular language or not?Pumping Lemma and Regular language or not?
Pumping Lemma and Regular language or not?Animesh Chaturvedi
 
Software Engineering Unit 1
Software Engineering Unit 1Software Engineering Unit 1
Software Engineering Unit 1Abhimanyu Mishra
 
Fundamental question and answer in cloud computing quiz by animesh chaturvedi
Fundamental question and answer in cloud computing quiz by animesh chaturvediFundamental question and answer in cloud computing quiz by animesh chaturvedi
Fundamental question and answer in cloud computing quiz by animesh chaturvediAnimesh Chaturvedi
 
Theory of Automata and formal languages unit 1
Theory of Automata and formal languages unit 1Theory of Automata and formal languages unit 1
Theory of Automata and formal languages unit 1Abhimanyu Mishra
 
Os3 2
Os3 2Os3 2
Os3 2issbp
 
Theory of Automata and formal languages Unit 3
Theory of Automata and formal languages Unit 3Theory of Automata and formal languages Unit 3
Theory of Automata and formal languages Unit 3Abhimanyu Mishra
 
Theory of automata and formal languages Unit 4
Theory of automata and formal languages Unit 4Theory of automata and formal languages Unit 4
Theory of automata and formal languages Unit 4Abhimanyu Mishra
 

En vedette (20)

Os2
Os2Os2
Os2
 
Ch11 input output systems
Ch11 input output systemsCh11 input output systems
Ch11 input output systems
 
Os2 2
Os2 2Os2 2
Os2 2
 
Design1
Design1Design1
Design1
 
Os5
Os5Os5
Os5
 
Os10 2
Os10 2Os10 2
Os10 2
 
Theory of Automata and formal languages Unit 5
Theory of Automata and formal languages Unit 5Theory of Automata and formal languages Unit 5
Theory of Automata and formal languages Unit 5
 
Cspc final
Cspc finalCspc final
Cspc final
 
Pattern detection in mealy machine
Pattern detection in mealy machinePattern detection in mealy machine
Pattern detection in mealy machine
 
Automated Web Service Change Management (AWSCM) A tool published at IEEE Clou...
Automated Web Service Change Management (AWSCM) A tool published at IEEE Clou...Automated Web Service Change Management (AWSCM) A tool published at IEEE Clou...
Automated Web Service Change Management (AWSCM) A tool published at IEEE Clou...
 
Class6
 Class6 Class6
Class6
 
Os3
Os3Os3
Os3
 
0227 regularlanguages
 0227 regularlanguages 0227 regularlanguages
0227 regularlanguages
 
Pumping Lemma and Regular language or not?
Pumping Lemma and Regular language or not?Pumping Lemma and Regular language or not?
Pumping Lemma and Regular language or not?
 
Software Engineering Unit 1
Software Engineering Unit 1Software Engineering Unit 1
Software Engineering Unit 1
 
Fundamental question and answer in cloud computing quiz by animesh chaturvedi
Fundamental question and answer in cloud computing quiz by animesh chaturvediFundamental question and answer in cloud computing quiz by animesh chaturvedi
Fundamental question and answer in cloud computing quiz by animesh chaturvedi
 
Theory of Automata and formal languages unit 1
Theory of Automata and formal languages unit 1Theory of Automata and formal languages unit 1
Theory of Automata and formal languages unit 1
 
Os3 2
Os3 2Os3 2
Os3 2
 
Theory of Automata and formal languages Unit 3
Theory of Automata and formal languages Unit 3Theory of Automata and formal languages Unit 3
Theory of Automata and formal languages Unit 3
 
Theory of automata and formal languages Unit 4
Theory of automata and formal languages Unit 4Theory of automata and formal languages Unit 4
Theory of automata and formal languages Unit 4
 

Plus de CS, NcState

Talks2015 novdec
Talks2015 novdecTalks2015 novdec
Talks2015 novdecCS, NcState
 
GALE: Geometric active learning for Search-Based Software Engineering
GALE: Geometric active learning for Search-Based Software EngineeringGALE: Geometric active learning for Search-Based Software Engineering
GALE: Geometric active learning for Search-Based Software EngineeringCS, NcState
 
Big Data: the weakest link
Big Data: the weakest linkBig Data: the weakest link
Big Data: the weakest linkCS, NcState
 
Three Laws of Trusted Data Sharing: (Building a Better Business Case for Dat...
Three Laws of Trusted Data Sharing:(Building a Better Business Case for Dat...Three Laws of Trusted Data Sharing:(Building a Better Business Case for Dat...
Three Laws of Trusted Data Sharing: (Building a Better Business Case for Dat...CS, NcState
 
Lexisnexis june9
Lexisnexis june9Lexisnexis june9
Lexisnexis june9CS, NcState
 
Welcome to ICSE NIER’15 (new ideas and emerging results).
Welcome to ICSE NIER’15 (new ideas and emerging results).Welcome to ICSE NIER’15 (new ideas and emerging results).
Welcome to ICSE NIER’15 (new ideas and emerging results).CS, NcState
 
Icse15 Tech-briefing Data Science
Icse15 Tech-briefing Data ScienceIcse15 Tech-briefing Data Science
Icse15 Tech-briefing Data ScienceCS, NcState
 
Kits to Find the Bits that Fits
Kits to Find  the Bits that Fits Kits to Find  the Bits that Fits
Kits to Find the Bits that Fits CS, NcState
 
Ai4se lab template
Ai4se lab templateAi4se lab template
Ai4se lab templateCS, NcState
 
Automated Software Enging, Fall 2015, NCSU
Automated Software Enging, Fall 2015, NCSUAutomated Software Enging, Fall 2015, NCSU
Automated Software Enging, Fall 2015, NCSUCS, NcState
 
Requirements Engineering
Requirements EngineeringRequirements Engineering
Requirements EngineeringCS, NcState
 
172529main ken and_tim_software_assurance_research_at_west_virginia
172529main ken and_tim_software_assurance_research_at_west_virginia172529main ken and_tim_software_assurance_research_at_west_virginia
172529main ken and_tim_software_assurance_research_at_west_virginiaCS, NcState
 
Automated Software Engineering
Automated Software EngineeringAutomated Software Engineering
Automated Software EngineeringCS, NcState
 
Next Generation “Treatment Learning” (finding the diamonds in the dust)
Next Generation “Treatment Learning” (finding the diamonds in the dust)Next Generation “Treatment Learning” (finding the diamonds in the dust)
Next Generation “Treatment Learning” (finding the diamonds in the dust)CS, NcState
 
Tim Menzies, directions in Data Science
Tim Menzies, directions in Data ScienceTim Menzies, directions in Data Science
Tim Menzies, directions in Data ScienceCS, NcState
 
Dagstuhl14 intro-v1
Dagstuhl14 intro-v1Dagstuhl14 intro-v1
Dagstuhl14 intro-v1CS, NcState
 
The Art and Science of Analyzing Software Data
The Art and Science of Analyzing Software DataThe Art and Science of Analyzing Software Data
The Art and Science of Analyzing Software DataCS, NcState
 

Plus de CS, NcState (20)

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

Dernier

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024The Digital Insurer
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 

Dernier (20)

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 

Lecture 7: Definite Clause Grammars

  • 1. Lecture 7: Definite Clause Grammars •  Theory – Introduce context free grammars and some related concepts © Patrick Blackburn, Johan Bos & Kristina Striegnitz – 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 © Patrick Blackburn, Johan Bos & Kristina Striegnitz •  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 © Patrick Blackburn, Johan Bos & Kristina Striegnitz vp → v det → the det → a n → man n → woman v → shoots
  • 4. Ingredients of a grammar •  The → symbol is used to s → np vp define the rules np → det n •  The symbols s, np, vp, vp → v np © Patrick Blackburn, Johan Bos & Kristina Striegnitz det, n, v are called the vp → v non-terminal symbols det → the •  The symbols in italics are the terminal symbols: det → a the, a, man, n → man woman, shoots n → woman v → shoots
  • 5. A little bit of linguistics •  The non-terminal symbols in this grammar have a traditional meaning in linguistics: © Patrick Blackburn, Johan Bos & Kristina Striegnitz – 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 © Patrick Blackburn, Johan Bos & Kristina Striegnitz •  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 s → np vp nine context free rules np → det n •  A context free rule consists of: vp → v np © Patrick Blackburn, Johan Bos & Kristina Striegnitz –  A single non-terminal symbol vp → v –  followed by → det → the –  followed by a finite sequence of terminal or non-terminal symbols det → a n → man n → woman v → shoots
  • 8. Grammar coverage •  Consider the following string: the woman shoots a man © Patrick Blackburn, Johan Bos & Kristina Striegnitz •  Is this string grammatical according to our grammar? •  And if it is, what syntactic structure does it have?
  • 9. Syntactic structure s → np vp s np → det n vp → v np vp → v vp © Patrick Blackburn, Johan Bos & Kristina Striegnitz det → the det → a n → man np np n → woman v → shoots det n v det n the woman shoots a man
  • 10. Parse trees •  Trees representing the syntactic structure of a string are often called parse trees © Patrick Blackburn, Johan Bos & Kristina Striegnitz •  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 © Patrick Blackburn, Johan Bos & Kristina Striegnitz 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 © Patrick Blackburn, Johan Bos & Kristina Striegnitz 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 © Patrick Blackburn, Johan Bos & Kristina Striegnitz 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 © Patrick Blackburn, Johan Bos & Kristina Striegnitz •  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 © Patrick Blackburn, Johan Bos & Kristina Striegnitz 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 © Patrick Blackburn, Johan Bos & Kristina Striegnitz 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 © Patrick Blackburn, Johan Bos & Kristina Striegnitz 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 © Patrick Blackburn, Johan Bos & Kristina Striegnitz 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). © Patrick Blackburn, Johan Bos & Kristina Striegnitz vp(C):- v(C). det([the]). det([a]). n([man]). n([woman]). v([shoots]).
  • 20. 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). © Patrick Blackburn, Johan Bos & Kristina Striegnitz vp(C):- v(C). det([the]). det([a]). n([man]). n([woman]). v([shoots]). ?- s([the,woman,shoots,a,man]). yes ?-
  • 21. 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). © Patrick Blackburn, Johan Bos & Kristina Striegnitz vp(C):- v(C). det([the]). det([a]). n([man]). n([woman]). v([shoots]). ?- s(S). S = [the,man,shoots,the,man]; S = [the,man,shoots,the,woman]; S = [the,woman,shoots,a,man] …
  • 22. 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). © Patrick Blackburn, Johan Bos & Kristina Striegnitz vp(C):- v(C). det([the]). det([a]). n([man]). n([woman]). v([shoots]). ?- np([the,woman]). yes ?- np(X). X = [the,man]; X = [the,woman]
  • 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 © Patrick Blackburn, Johan Bos & Kristina Striegnitz 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 © Patrick Blackburn, Johan Bos & Kristina Striegnitz 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). © Patrick Blackburn, Johan Bos & Kristina Striegnitz 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(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). © Patrick Blackburn, Johan Bos & Kristina Striegnitz 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). ?- s([the,man,shoots,a,man]-[ ]). yes ?-
  • 27. 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). © Patrick Blackburn, Johan Bos & Kristina Striegnitz 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). ?- s(X-[ ]). S = [the,man,shoots,the,man]; S = [the,man,shoots,a,man]; ….
  • 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 © Patrick Blackburn, Johan Bos & Kristina Striegnitz 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 © Patrick Blackburn, Johan Bos & Kristina Striegnitz difference list variables •  Let us look at three examples
  • 30. DCGs: first example s --> np, vp. np --> det, n. vp --> v, np. © Patrick Blackburn, Johan Bos & Kristina Striegnitz vp --> v. det --> [the]. det --> [a]. n --> [man]. n --> [woman]. v --> [shoots].
  • 31. DCGs: first example s --> np, vp. np --> det, n. vp --> v, np. © Patrick Blackburn, Johan Bos & Kristina Striegnitz vp --> v. det --> [the]. det --> [a]. n --> [man]. n --> [woman]. v --> [shoots]. ?- s([a,man,shoots,a,woman],[ ]). yes ?-
  • 32. DCGs: first example s --> np, vp. np --> det, n. vp --> v, np. © Patrick Blackburn, Johan Bos & Kristina Striegnitz vp --> v. det --> [the]. det --> [a]. n --> [man]. n --> [woman]. v --> [shoots]. ?- s(X,[ ]). S = [the,man,shoots,the,man]; S = [the,man,shoots,a,man]; ….
  • 33. DCGs: second example s --> s, conj, s. s --> np, vp. np --> det, n. vp --> v, np. vp --> v. © Patrick Blackburn, Johan Bos & Kristina Striegnitz det --> [the]. det --> [a]. n --> [man]. n --> [woman]. v --> [shoots]. conj --> [and]. conj --> [or]. conj --> [but]. •  We added some recursive rules to the grammar… •  What and how many sentences does this grammar generate? •  What does Prolog do with this DCG?
  • 34. DCG without left-recursive rules s --> simple_s, conj, s. s --> simple_s. simple_s --> np, vp. © Patrick Blackburn, Johan Bos & Kristina Striegnitz 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 © Patrick Blackburn, Johan Bos & Kristina Striegnitz 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 © Patrick Blackburn, Johan Bos & Kristina Striegnitz 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 anbn
  • 37. DCGs: third example s --> []. •  We will define the formal s --> l,s,r. language anbn l --> [a]. © Patrick Blackburn, Johan Bos & Kristina Striegnitz r --> [b]. ?- s([a,a,a,b,b,b],[ ]). yes ?- s([a,a,a,a,b,b,b],[ ]). no
  • 38. DCGs: third example s --> []. •  We will define the formal s --> l,s,r. language anbn l --> [a]. © Patrick Blackburn, Johan Bos & Kristina Striegnitz r --> [b]. ?- s(X,[ ]). X = [ ]; X = [a,b]; X = [a,a,b,b]; X = [a,a,a,b,b,b] ….
  • 39. © Patrick Blackburn, Johan Bos & Kristina Striegnitz •  LPN 7.3 •  LPN 7.2 •  LPN 7.1 Exercises
  • 40. Summary of this lecture •  We explained the idea of grammars and context free grammars are •  We introduced the Prolog technique of © Patrick Blackburn, Johan Bos & Kristina Striegnitz 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 © Patrick Blackburn, Johan Bos & Kristina Striegnitz •  Extra arguments •  Extra tests – Discuss the status and limitations of definite clause grammars