14. Grammar rules

The second goal of this tutorial reads Markdown text. For that, Prolog has a special notation called definite clause grammars, or DCG for short. You write the grammar of a language, and Prolog can read sentences with it. It can also write them.

A tiny language

Let's start with a language of commands for a game, such as go north or take the key. For now, a sentence is a list of words, written as atoms.

command --> [go], direction.
command --> [take, the], thing.

direction --> [north].
direction --> [south].

thing --> [key].
thing --> [lamp].

Read --> as "can be". A command can be the word go followed by a direction, or the words take the followed by a thing.

  • A list such as [go] matches those words in the sentence.
  • A name such as direction is another grammar rule.
  • A comma means "followed by".

phrase(Rule, Words) is true when Words is a sentence of Rule.

The first query says the sentence is correct. The second one says it is not. The third query is the interesting one. With an unknown sentence, Prolog writes every sentence of the language, one per answer.

Getting a meaning

A grammar rule can have arguments, like any predicate. Use them to say what the sentence means.

The same grammar works in both directions. The first query reads words and gives the meaning take(lamp). The second query starts from the meaning go(south) and gives the words. A parser and a generator from one piece of code. This is the reason why Prolog has been used for language processing since the beginning.

How it works

A grammar rule is just a normal Prolog clause with two extra arguments. listing/1 shows how C#Prolog turned the rules into clauses.

  • The first extra argument is the list of words before the rule reads.
  • The second one is the list of words that are left after it.

Because of these two hidden arguments, we write a grammar rule with two slashes, such as command//1. It means command with one argument that you can see. As a normal predicate, it is command/3.

The listing shows the clause command(go(A), B, C). It reads go from the start of B, then lets direction/3 read from the rest, and C is what is left at the end. phrase/2 calls the rule with the whole sentence and asks that nothing is left.

So you can call a grammar rule directly with the two lists, or use phrase/3 to get the words that are left.

Normal goals in braces

Inside a grammar rule, a goal in { } runs as a normal Prolog goal. It reads no words. Use it to compute something, or to check a word.

This grammar accepts take one keys too. Fixing that is an exercise below.

Recursive grammars

A grammar rule can use itself, just like a predicate. This one reads one or more commands joined by and.

Again, the same grammar reads and writes.

Try it

  1. Add drop the key to the commands, with the meaning drop(key).
  2. Add the directions east and west, and list all the commands with ?- phrase(command(C), Words).
  3. Go back to the box in "Normal goals in braces". Change count//1 and thing//1 there, so that take one keys and take two key are not correct sentences. You can pass the count to thing.
An unhandled error has occurred. Reload 🗙