Tutorial by Examples

One of the most elementary DCG nonterminals is ... //0, which can be read as "anything at all": ... --> [] | [_], ... . It can be used to describe a list Ls that contains the element E via: phrase(( ..., [E], ... ), Ls)
DCGs can be used for parsing. Best of all, the same DCG can often be used to both parse and generate lists that are being described. For example: sentence --> article, subject, verb, object. article --> [the]. subject --> [woman] | [man]. verb --> [likes] | [enjoys]. object ...
Extra goals enable to add processing to DCG clauses, for example, conditions that the elements of the list must satisfy. The extra goals are observed between curly braces at the end of a DCG clause. % DCG clause requiring an integer int --> [X], {integer(X)}. Usage: ?- phrase(int, [3]). t...
The extra arguments add results to predicates of a DCG clause, by decorating the derivation tree. For example, it's possible to create a algebraic grammar that computes the value at the end. Given a grammar that supports the operation addition: % Extra arguments are passed between parenthesis afte...

Page 1 of 1