Questions tagged [dcg]

DCGs (Definite Clause Grammars) are a compact way to describe lists in Prolog.

DCGs (Definite Clause Grammars) are a compact way to describe lists in Prolog.

DCGs are usually associated with , but similar languages such as also include DCGs. They are called definite clause grammars because they represent a grammar as a set of definite clauses in first-order logic.

Some interesting contributions are available as SWI-Prolog packs, like

I like to think of DCGs as Attribute Grammars made practical.


References:

DCGs provide a threading state abstraction: don't break it


SWI-Prolog and double quotes

Many examples of DCGs on SO, in blogs and papers use the traditional and ISO standard conforming definition for double quotes. If you take such code and use it directly with SWI-Prolog it will sometimes fail because SWI uses by default double quoted syntax for its string type.

?- phrase("abc","abc").         % succeeds in other Prologs
ERROR: Type error: `list' expected, found `"abc"' (a string)

To make the examples work in SWI-Prolog set Prolog flags in .swiplrc or issue them directly on the top level.

:- set_prolog_flag(double_quotes, chars).   % or codes
:- set_prolog_flag(back_quotes, string).    % ISO conforming extension

Otherwise, to ensure the SWI-specific setting, proceed as in the following example.

:- module(course,
      [ courses//1,
        parse_courses/2
      ]).

:- [library(dcg/basics)].

:- set_prolog_flag(double_quotes, string).   % SWI's default setting
:- set_prolog_flag(back_quotes, codes).      % SWI's default setting

courses([Course|Courses]) -->
    course(Course),
    courses(Courses), !.
courses([]) --> [].

course(course(Course,Students)) -->
    string_without("\n", Course_codes),
    { string_codes(Course,Course_codes ) },
    "\n",
    students(Students),
    (
        empty_line
    ;
        []
    ).

students([Student|Students]) -->
    student(Student),
    students(Students).
students([]) --> [].

student(Student) -->
    spaces_or_tabs_plus,
    (
        (
            string_without("\n",Student_codes),
            { string_codes(Student,Student_codes) },
            "\n"
        )
    ;
        remainder(Student_codes),
        { string_codes(Student,Student_codes) }
    ).

spaces_or_tabs_plus -->
    space_or_tab,
    spaces_or_tabs_star.

spaces_or_tabs_star -->
    space_or_tab,
    spaces_or_tabs_star.
spaces_or_tabs_star --> [].

space_or_tab -->
    (
        "\s"
    |
        "\t"
    ).

empty_line --> "\n".

parse_courses(Codes,Courses) :-
    DCG = courses(Courses),
    phrase(DCG,Codes,Rest),
    assertion( Rest == [] ).

:- begin_tests(course).

:- set_prolog_flag(double_quotes, string).
:- set_prolog_flag(back_quotes, codes).

test(001) :-
    Input = "\c
        MATH2221\n\c
            \t201000001\n\c
            \t201000002\n\c
            \n\c
        MATH2251\n\c
            \t201000002\n\c
            \t201000003\n\c
            \n\c
        COMP2231\n\c
            \t201000003\n\c
            \t201000001\c
        ",
    string_codes(Input,Codes),
    parse_courses(Codes,Courses),

    assertion( Courses ==
        [
            course("MATH2221",["201000001","201000002"]),
            course("MATH2251",["201000002","201000003"]),
            course("COMP2231",["201000003","201000001"])
        ]
    ).

:- end_tests(course).

482 questions
57
votes
2 answers

What is the difference between ' and " in Prolog?

I am new to Prolog and noticed that ' and " give different behavior, but am curious as to why. Specifically, when loading a file, ?- ['test1.pl']. works, while ?- ["test1.pl"]. doesn't.
astay13
  • 6,857
  • 10
  • 41
  • 56
42
votes
10 answers

What are good starting points for someone interested in natural language processing?

Question So I've recently came up with some new possible projects that would have to deal with deriving 'meaning' from text submitted and generated by users. Natural language processing is the field that deals with these kinds of issues, and after…
kitsune
  • 11,516
  • 13
  • 57
  • 78
32
votes
4 answers

Implement the member predicate as a one-liner

Interview question! This is how you normally define the member relation in Prolog: member(X, [X|_]). % member(X, [Head|Tail]) is true if X = Head % that is, if X is the head of the list member(X, [_|Tail]) :- % or…
Claudiu
  • 224,032
  • 165
  • 485
  • 680
30
votes
7 answers

Flatten a list in Prolog

I've only been working with Prolog for a couple days. I understand some things but this is really confusing me. I'm suppose to write a function that takes a list and flattens it. ?- flatten([a,[b,c],[[d],[],[e]]],Xs). Xs = [a,b,c,d,e]. …
ToastyMallows
  • 4,203
  • 5
  • 43
  • 52
25
votes
3 answers

SWI-Prolog in Semantic Web

I would like to hear from people who have real world programming experience in using SWI-Prolog's semantic library. Edit: The reason for this question is, among the many people I talked to with prolog experience, most of them seem to have used it…
uncaught_exceptions
  • 21,712
  • 4
  • 41
  • 48
22
votes
5 answers

Read a file line by line in Prolog

I'd like to read a plain text file and apply a predicate to each line (the predicates contain write which does the output). How would I do that?
Igor Marvinsky
  • 387
  • 1
  • 4
  • 17
20
votes
7 answers

What is SML used for?

What are the uses of SML in the real word? Are its practical uses similar to that of Prolog?
CreamBun
18
votes
3 answers

Parsing in Prolog without cut?

I found this nice snippet for parsing lisp in Prolog (from here): ws --> [W], { code_type(W, space) }, ws. ws --> []. parse(String, Expr) :- phrase(expressions(Expr), String). expressions([E|Es]) --> ws, expression(E), ws, !, % single…
dnolen
  • 18,496
  • 4
  • 62
  • 71
16
votes
3 answers

Stack overflow in Prolog DCG grammar rule: how to handle large lists efficiently or lazily

I'm parsing a fairly simple file format consisting of a series of lines, each line having some space separated fields, that looks like this: l 0x9823 1 s 0x1111 3 l 0x1111 12 ⋮ I'm using SWI-Prolog. This is the DCG I have so far: :-…
Daniel Lyons
  • 22,421
  • 2
  • 50
  • 77
12
votes
2 answers

Remove incorrect subsequent solutions without once

I have a predicate that finds the correct solution but then goes on to find solutions which are not right. ?- data(D),data_threshold_nonredundantbumps(D,5,Bs),write(D). [3,6,7,8,2,4,5,6,9,4,7,3] D = [3, 6, 7, 8, 2, 4, 5, 6, 9|...], Bs = [bump([11],…
user27815
  • 4,767
  • 14
  • 28
12
votes
4 answers

Prolog - DCG parser with input from file

As part of a project I need to write a parser that can read a file and parse into facts I can use in my program. The file structure looks as follows: property = { el1 , el2 , ... }. What I want in the end…
Floris Devriendt
  • 2,044
  • 4
  • 24
  • 34
11
votes
3 answers

Parsing numbers with multiple digits in Prolog

I have the following simple expression…
ubuntudroid
  • 3,680
  • 6
  • 36
  • 60
10
votes
2 answers

What does the operator `-->` in Prolog do?

What does the --> operator do in Prolog and what is the difference between it and :-? I'm using SWI Prolog.
Joel Lindholm
  • 101
  • 1
  • 4
10
votes
4 answers

Prolog - handling binary data with DCGs

It seems to me one ought to be able to process binary data with DCGs on a list of bytes. To make it work generally, though, one has to use bitwise operations which means is/2 is involved, which means instantiation order is an issue, which may…
Daniel Lyons
  • 22,421
  • 2
  • 50
  • 77
9
votes
2 answers

Prolog : Combining DCG grammars with other restrictions

I'm very impressed by Prolog's DCG and how quickly I can produce all the possible structures that fit a particular grammar. But I'd like to combine this search with other constraints. For example, define a complex grammar and ask Prolog to generate…
interstar
  • 26,048
  • 36
  • 112
  • 180
1
2 3
32 33