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


Note: When using DCGs with SWI-Prolog be aware of The string type and its double quoted syntax

In other words many examples of DCGs here, in blogs and papers use the traditional Prolog definition for double quotes. If you take such code and use it with SWI-Prolog it will sometimes fail.

To avoid this problem there are Prolog flags for changing how double quotes and back quotes are defined.

Example of Prolog DCG that sets Prolog flags.

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

:- [library(dcg/basics)].

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

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

469 questions
51
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,137
  • 8
  • 36
  • 52
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,098
  • 13
  • 54
  • 77
30
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
  • 206,738
  • 150
  • 445
  • 651
27
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
  • 3,975
  • 5
  • 40
  • 50
24
votes
2 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
  • 20,440
  • 4
  • 37
  • 48
21
votes
4 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
  • 377
  • 1
  • 4
  • 16
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
19
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,116
  • 3
  • 58
  • 69
17
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
  • 21,545
  • 2
  • 48
  • 73
13
votes
1 answer

A prologue for Prolog?

In case you write Prolog programs regularly you probably have your own library of predicates you always rely on. Some Prolog systems come with a rich set of predefined predicates and some not. Some systems have libraries but they are mostly…
false
  • 10,182
  • 12
  • 93
  • 182
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,687
  • 11
  • 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
  • 1,954
  • 3
  • 21
  • 32
11
votes
3 answers

Parsing numbers with multiple digits in Prolog

I have the following simple expression…
ubuntudroid
  • 3,068
  • 5
  • 32
  • 54
10
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
  • 22,620
  • 31
  • 101
  • 161
9
votes
4 answers

Recursive Prolog predicate for reverse / palindrome

Can I get a recursive Prolog predicate having two arguments, called reverse, which returns the inverse of a list: Sample query and expected result: ?- reverse([a,b,c], L). L = [c,b,a]. A recursive Prolog predicate of two arguments called…
sam
  • 91
  • 1
  • 2
1
2 3
31 32