next up previous contents index
Next: Two differences with other Up: Definite Clause Grammars Previous: Translation of Definite Clause   Contents   Index


Definite Clause Grammar predicates

The library predicates of XSB that support DCGs are the following:

phrase(+Phrase, ?List)

This predicate is true iff the list List can be parsed as a phrase (i.e. sequence of terminals) of type Phrase. Phrase can be any term which would be accepted as a nonterminal of the grammar (or in general, it can be any grammar rule body), and must be instantiated to a nonvariable term at the time of the call; otherwise an error message is sent to the standard error stream and the predicate fails. This predicate is the usual way to commence execution of grammar rules.

If List is bound to a list of terminals by the time of the call, then the goal corresponds to parsing List as a phrase of type Phrase; otherwise if List is unbound, then the grammar is being used for generation.

phrase(+Phrase, ?List, ?Rest)

This predicate is true iff the segment between the start of list List and the start of list Rest can be parsed as a phrase (i.e. sequence of terminals) of type Phrase . In other words, if the search for phrase Phrase is started at the beginning of list List, then Rest is what remains unparsed after Phrase has been found. Again, Phrase can be any term which would be accepted as a nonterminal of the grammar (or in general, any grammar rule body), and must be instantiated to a nonvariable term at the time of the call; otherwise an error message is sent to the standard error stream and the predicate fails.

Predicate phrase/3 is the analogue of call/1 for grammar rule bodies, and provides a semantics for variables in the bodies of grammar rules. A variable X in a grammar rule body is treated as though phrase(X) appeared instead, X would expand into a call to phrase(X, L, R) for some lists L and R.

expand_term(+Term1, ?Term2)

This predicate is used to transform terms that appear in a Prolog program before the program is compiled or consulted. The standard transformation performed by expand_term/2 is that when Term1 is a grammar rule, then Term2 is the corresponding Prolog clause; otherwise Term2 is simply Term1 unchanged. If Term1 is not of the proper form, or Term2 does not unify with its clausal form, predicate expand_term/2 simply fails.

Users may override the standard transformations performed by predicate expand_term/2 by defining their own compile-time transformations. This can be done by defining clauses for the predicate term_expansion/2. When a term Term1 is read in when a file is being compiled or consulted, expand_term/2 calls term_expansion/2 first; if the expansion succeeds, the transformed term so obtained is used and the standard grammar rule expansion is not tried; otherwise, if Term1 is a grammar rule, then it is expanded using dcg/2; otherwise, Term1 is used as is. Note that predicate term_expansion/2 must be defined in the XSB's default read-in module ( usermod) and should be loaded there before the compilation begins.

'C'(?L1, ?Terminal, ?L2)

This predicate generally is of no concern to the user. Rather it is used in the transformation of terminal symbols in grammar rules and expresses the fact that L1 is connected to L2 by the terminal Terminal. This predicate is needed to avoid problems due to source-level transformations in the presence of control primitives such as cuts ( '!'/0), or if-then-elses ( '->'/2) and is defined by the single clause:
'C'([Token|Tokens], Token, Tokens).
The name 'C' was chosen for this predicate so that another useful name might not be preempted.

dcg(+DCG_Rule, ?Prolog_Clause)
dcg
Succeeds iff the DCG rule DCG_Rule translates to the Prolog clause Prolog_Clause. At the time of call, DCG_Rule must be bound to a term whose principal functor is '->'/2 or else the predicate fails. dcg/2 must be explicitly imported from the module dcg.


next up previous contents index
Next: Two differences with other Up: Definite Clause Grammars Previous: Translation of Definite Clause   Contents   Index
Baoqiu Cui
2000-04-23