Did you know ... Search Documentation:
Packs (add-ons) for SWI-Prolog

Package "dcg4pt"

Title:DCG for Parse Trees
Rating:Not rated. Create the first rating!
Latest version:1.0.0
SHA1 sum:eef8bcbe25e80699179bd353673fe7dd766fccc8
Author:Falco Nogatz <fnogatz@gmail.com>
Maintainer:Falco Nogatz <fnogatz@gmail.com>
Packager:Falco Nogatz <fnogatz@gmail.com>
Home page:https://github.com/fnogatz/dcg4pt
Download URL:https://github.com/fnogatz/dcg4pt/releases/*.zip
Requires:tap

Reviews

No reviews. Create the first review!.

Details by download location

VersionSHA1#DownloadsURL
1.0.0eef8bcbe25e80699179bd353673fe7dd766fccc878https://github.com/fnogatz/dcg4pt/archive/v1.0.0.zip
0.2.0fc9c125dd22e92d21af0b01a10884dc6e37196d53https://github.com/fnogatz/dcg4pt.git
e7eecd6cc6b9d2d18a1eb46aae669419690882b02https://github.com/fnogatz/dcg4pt.git
b94767fc41bb2b837e28e22ba2055d7de9c69bea18https://github.com/fnogatz/dcg4pt.git
0.1.6c536778e701e3f914029cf27a42affbced09839b1https://github.com/fnogatz/dcg4pt.git

DCGs for Parse Trees

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

Synopsis

:- use_module(library(dcg4pt/expand)).

% Extended DCGs get expanded to hold an additional
%   argument with parse tree.
sentence --> noun_phrase(N), verb_phrase(N).
noun_phrase(N) --> determiner, noun(N).
verb_phrase(N) --> ( verb(N) ; verb(N), noun_phrase(_) ).
noun(sg) --> [boy]  ; [apple].
noun(pl) --> [boys] ; [apples].
determiner --> [the].
verb(sg) --> [eats].
verb(pl) --> [eat].

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(dcg4pt).

Requirements

Only for development purposes the tap pack is needed:

?- pack_install(tap).

DCG Expansion

In most cases you simply want to automatically expand all given DCGs with the additional parse tree argument. To do so, simply call:

:- use_module(library(dcg4pt/expand)).
% and later the definition of DCGs

Additionally you can manually call the predicates to translate a DCG4PT rule:

?- use_module(library(dcg4pt)).
?- dcg4pt_rule_to_dcg_rule((sentence --> noun_phrase, verb_phrase), DCG).
DCG = (sentence(X) --> ( ... )).

Usage

You can use the generated predicates like normal DCGs, besides that they provide an additional argument to hold the parse tree. It is automatically added as the very last argument of the DCG body.

Bound Arguments

library(dcg4pt) has been implemented to provide a tool that generates a parse tree from a given input list but also the other way around, i.e., to generate a list based on a parse tree. So you can also use it this way:

?- Tree = noun_phrase([determiner(the), noun(boy)]),
   phrase(noun_phrase(N, Tree), List).
Tree = noun_phrase([determiner(the), noun(boy)]),
N = sg,
List = [the, boy] .

Sequences

library(dcg4pt) provides a built-in `sequence(+Quantifier, :Body)` DCG body to resolve sequences of Body with the quantifiers '?', '+', and '*' as known from regular expressions. Their occurrences are represented as (possibly empty) lists in the parse tree:

:- use_module(library(dcg4pt/expand)).

single --> [a].
list --> sequence('*', single).
non_empty_list --> sequence('+', single).
optional --> sequence('?', single).

main :-
  phrase(list(Tree), [a, a, a]),
  print_term(Tree, [indent_arguments(2)]).

% prints:
%   list([
%     single(a),
%     single(a),
%     single(a) ])

With a free variable given as the parse tree, the possibilities are generated beginning with the smallest solution:

?- phrase(non_empty_list(Tree), List).
Tree = non_empty_list([single(a)]),
List = [a] ;
Tree = non_empty_list([single(a), single(a)]),
List = [a, a] ;
...

Contents of pack "dcg4pt"

Pack contains 9 files holding a total of 15.7K bytes.