Did you know ... Search Documentation:
Pack edcgs -- README.md

Extended DCGs

Extend Definite Clause Grammars (DCG) for Prolog by an additional argument to automatically store the parsing tree.

Synopsis

:- use_module(library(edcgs_expand)).

:- op(1200, xfx, ==>).

% Extended DCGs get expanded to hold additional
%   argument with syntax tree.
sentence ==> noun_phrase, verb_phrase.
noun_phrase ==> determiner, noun.
verb_phrase ==> ( verb ; verb, noun_phrase ).
noun ==> [boy] ; [boys] ; [apple] ; [apples].
determiner ==> [the].
verb ==> [eat].
verb ==> [eats].

main :-
  phrase(sentence(Tree), [the, boy, eats, the, apples]),
  print_term(Tree, [indent_arguments(2)]).

% prints:
%   sentence([
%     noun_phrase([
%       determiner(the),
%       noun(boy) ]),
%     verb_phrase([
%       verb(eats),
%       noun_phrase([
%         determiner(the),
%         noun(apples) ]) ]) ])

Installation

This pack is available from the add-on registry of SWI-Prolog.

It can be installed with pack_install/1:

?- pack_install(edcgs).

Requirements

Only for development purposes the tap pack is needed:

?- pack_install(tap).

Usage

In most cases you simply want to automatically expand all EDCGs to regular DCGs. To do so, simply call:

:- use_module(library(edcgs_expand)).
% and later the definition of EDCGs

Additionally you can manually call the predicates to translate an EDCG rule:

?- use_module(library(edcgs)).
?- edcg_rule_to_dcg_rule((sentence ==> noun_phrase, verb_phrase), DCG).
DCG = (sentence(X) --> ( ... )).