Question

I am studying DCG grammar and parse tree in Prolog using Ivan Bratko book: "Programming for Artificial Intelligence"

I have some doubt about my interpretation of this DCG grammar that generate a parse tree:

sentence(Number, sentence(NP, VP)) --> noun_phrase(Number, NP),
                           verb_phrase(Number, VP).

verb_phrase(Number, verb_phrase(Verb, NP)) --> verb(Number, Verb),
                               noun_phrase(_, NP).

noun_phrase(Number, noun_phrase(Det, Noun)) --> determiner(Number, Det),
                            noun(Number, Noun).

determiner(singular, determiner(a)) --> [a].
determiner(_,determiner(the)) --> [the].

noun(singular, noun(cat)) --> [cat].
noun(singular, noun(mouse)) --> [mouse].
noun(plural, noun(cats))  --> [cats].
noun(plural, noun(mice))  --> [mice].

verb(singular, verb(scares)) --> [scares].
verb(singular, verb(hates)) --> [hates].
verb(plural, verb(scare))  --> [scare].
verb(plural, verb(hate))  --> [hate].

So for example if I can execute the following query:

[debug]  ?- sentence(singular, Tree, [a, cat, scares, the, mice],[]).
Tree = sentence(noun_phrase(determiner(a), noun(cat)), verb_phrase(verb(scares), noun_phrase(determiner(the), noun(mice)))) 

that is TRUE and that generate a parse tree having sentence as root

I'm trying to reason about how the parsing tree is build only using the previous DCG grammar form and not about how Prolog translate this grammar into a set of rules (because, for me this thing is difficult and maybe because doing so I'd be adding a further step)

Is it a good thing read it by DCG grammar and not by the automatic converted rules?

So I read in this way:

A sentence is composed by a noun_phrase and by a verb_phrase so int the first "rule" of the DCG grammar I specify that sentence is the root of my tree (that represents a sentence in my subset of natural language) that have a noun_phrase as left child and a verb_phrase as right child

Then noun_phrase is defined as a determiner followed by a noun so noun_phrase is the root of another tree having determiner as left child and noun as right child

Then determiner is a tree composed by a single node: determiner(a) if it is TRUE that the found determiner is "a" or **determiner(the) if it is TRUE that the found determiner is "the"

Same reasoning for my *verb_phrase**

Is it correct?

Was it helpful?

Solution

The whole point of DCG form is that you don't need to look at the normalized form, so that is that.

So with DCG you end up with a representation that is fairly straight-forward to interpret for a human.

Yes, your interpretation is correct. Prolog seems to agree with you, as it gives you back what you expected. Assuming you don't see any warnings, you should be good to go. Maybe try to parse some edge cases and see what happens?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top