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

Package "dcg_util"

Title:DCG utility predicates
Rating:
(1/1)
Latest version:0.3.1
SHA1 sum:a79b6d5b777cca325aa2e11ac5ce15e420971ef5
Author:Michael Hendricks <michael@ndrix.org>
Maintainer:Michael Hendricks <michael@ndrix.org>
Packager:Michael Hendricks <michael@ndrix.org>
Home page:http://github.com/mndrix/dcg_util
Download URL:https://github.com/mndrix/dcg_util/archive/v0.3.1.zip

Reviews

Write a review or add a rating.
A practical tool, and very instructive also. Just a downside: it exports the non terminal *eos*//0, currently conflicting with library(dcg/basics). I've been forced to comment out the declaration.
Sunday 04 September 2016, Carlo Capelli

Details by download location

VersionSHA1#DownloadsURL
0.3.0658dc4024e2a198fbe6f9feb8284d2a5064bd5d21https://github.com/mndrix/dcg_util/archive/v0.3.0.zip
0.3.135c6233e1f64a3989598fc920edaeb96bcf1baaf115https://github.com/mndrix/dcg_util.git
56291213ed951a83bd3283c73946bcba0a470ccc4https://github.com/mndrix/dcg_util/archive/v0.3.1.zip
a79b6d5b777cca325aa2e11ac5ce15e420971ef532https://github.com/mndrix/dcg_util/archive/v0.3.1.zip

Synopsis

:- use_module(library(dcg_util)).
ho(ho) --> "ho".
comma --> ", ".

?- phrase(exactly(3,ho,Matches),`hohoho`).
Matches = [ho, ho, ho].

?- phrase(list(ho,comma,Santa),`ho, ho, ho`).
Santa = [ho, ho, ho].

?- phrase(list(ho,comma,[ho,ho,ho]),Text).
Text = `ho, ho, ho`.

Description

This module is a collection of predicates and combinators for working with Prolog's definite clause grammars (DCG). As much as possible, I've tried to make these rules symmetric so that you can use them for both parsing and generating.

Most of these predicates factor out patterns that I've noticed in my DCGs. Some of them provide regex-like quantifiers similar to *, *?, {2}, etc.

Example

To match a single, lowercase word one might write a DCG like this. It consumes as many lowercase letters as possible. On backtracking, it gives back one letter at a time:

lowercase(C) -->
    [C],
    { between(0'a,0'z,C) }.

word([X|Xs]) -->
    lowercase(X),
    word(Xs).
word([]) -->
    [].

Unfortunately, that mixes list operations, lowercase matching and search order. It's more difficult to read than we'd like. Because of the complexity, it's also easy to make a mistake.

By using this library's predicate greedy//2 we end up with something cleaner:

word(Word) -->
    greedy(lowercase, Word).

Installation

Using SWI-Prolog 7.1 or later:

?- pack_install(dcg_util).

This module uses semantic versioning.

Source code available and pull requests accepted at http://github.com/mndrix/dcg_util

author
- Michael Hendricks <michael@ndrix.org>
license
- unlicense

Contents of pack "dcg_util"

Pack contains 14 files holding a total of 12.0K bytes.