Did you know ... | Search Documentation: |
Packs (add-ons) for SWI-Prolog |
Title: | DCG utility predicates |
---|---|
Rating: | |
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 |
Version | SHA1 | #Downloads | URL |
---|---|---|---|
0.3.0 | 658dc4024e2a198fbe6f9feb8284d2a5064bd5d2 | 1 | https://github.com/mndrix/dcg_util/archive/v0.3.0.zip |
0.3.1 | 35c6233e1f64a3989598fc920edaeb96bcf1baaf | 115 | https://github.com/mndrix/dcg_util.git |
56291213ed951a83bd3283c73946bcba0a470ccc | 4 | https://github.com/mndrix/dcg_util/archive/v0.3.1.zip | |
a79b6d5b777cca325aa2e11ac5ce15e420971ef5 | 32 | https://github.com/mndrix/dcg_util/archive/v0.3.1.zip |
:- 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`.
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.
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).
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
Pack contains 14 files holding a total of 12.0K bytes.