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
7
votes
1 answer

Parsing with DCGs in Scheme (without Prolog)?

Lots of Prolog-in-Scheme implementations are out there. E.g. Kanren, Schelog. Apparently in "Paradigms of AI Programming" Norvig implements Prolog-to-Lisp compiler in Lisp in order to use Definite Clause Grammars. But is there a simpler cleaner way?…
Mark Bolusmjak
  • 23,606
  • 10
  • 74
  • 129
7
votes
3 answers

Parsing inflected non-word-order languages (e.g. Latin)

Taking an example from the Introduction to Latin Wikiversity, consider the sentence: the sailor gives the girl money We can handle this in Prolog with a DCG fairly elegantly with this pile of rules: sentence(s(NP, VP)) --> noun_phrase(NP),…
Daniel Lyons
  • 22,421
  • 2
  • 50
  • 77
6
votes
3 answers

prolog Searching the Lists

I am trying to compare the lists. Given function(List1,List2) and List1 has length N and List 2 has length M and N>M. I want to check if any permutation of List2 happens to be the first M characters of…
forreal
  • 63
  • 3
6
votes
2 answers

Using a prolog DCG to find & replace - code review

I came up w/ the following code to replace all occurences of Find w/ Replace in Request & put the answer in Result. This is using a DCG, so they are all lists of character codes. The predicate that client code would use is…
Ashley
  • 829
  • 1
  • 5
  • 16
6
votes
1 answer

How to read a csv file into a list of lists in SWI prolog where the inner list represents each line of the CSV?

I have a CSV file that look something like below: i.e. not in Prolog format james,facebook,intel,samsung rebecca,intel,samsung,facebook Ian,samsung,facebook,intel I am trying to write a Prolog predicate that reads the file and returns a list that…
6
votes
1 answer

Problems with Prolog's DCG

The project is about translating semi-natural language to SQL tables. The code: label(S) --> label_h(C), {atom_codes(A, C), string_to_atom(S, A)}, !. label_h([C|D]) --> letter(C), letters_or_digits(D), !. letters_or_digits([C|D]) -->…
Igor
  • 2,673
  • 5
  • 33
  • 39
6
votes
4 answers

Concatting a list of strings in Prolog

I'm writing a Lisp to C translator and I have a problem with handling strings. This is a code that transforms an unary Lisp function to a C equivalent: define(F) --> fun_unary(F), !. fun_unary(F) --> "(define (", label(Fun), spaces, label(Arg1),…
Igor
  • 2,673
  • 5
  • 33
  • 39
6
votes
4 answers

Question - formal language in prolog

I am trying to build a DCG which recognizes all lists which match this form : a^n b^2m c^2m d^n. I have written up the following rules: s --> []. s --> ad. ad --> a, ad, d. ad --> bc. bc --> b, b, bc, c, c. bc --> []. a --> [a]. b --> [b]. c -->…
Simon
  • 4,999
  • 21
  • 69
  • 97
6
votes
3 answers

Prolog - Generate alternating symbols on backtrack: [a] ; [a,b]; [a,b,a]; [a,b,a,b]

I've wrapped my mind a lot and couldn't figure it out. Is it possible to make a script that with backtrack generates lists in this format: [a] [a,b] [a,b,a] [a,b,a,b] ... I've made one that generates two elements at a time but my head started to…
6
votes
3 answers

how to split a sentence in swi-prolog

I am trying my hands on SWI-Prolog in win xp. I am trying to understand how to split a sentence in Prolog into separate atoms. Ex : Say I have a sentence like this : "this is a string" Is there any way to get individual words to get stored in a…
JPro
  • 6,292
  • 13
  • 57
  • 83
6
votes
1 answer

Lenses in Prolog via DCG, possible or not?

Was playing around with lenses in Prolog. Lenses are a kind of microscope that allow to zoom into a structure and do some reads or writes in a functional fashion. Basically my point of departure was the following modelling of setters and declarative…
user502187
6
votes
1 answer

Where I can find an exhaustive book about "Definite Clause Grammar" in Prolog?

I wanna learn something more about Definite Clause Grammar in Prolog and I'm searching some book and tutorial online. I've already saw something on "Learn Prolog Now", "The art of Prolog" and a tutorial on Swi-Prolog, but none of those tells much…
s.dallapalma
  • 1,225
  • 1
  • 12
  • 35
6
votes
3 answers

Is there a way or an algorithm to convert DCG into normal definite clauses in Prolog?

I am a newbie in Prolog, and I am trying to understand how a grammar can be translated into a normal definite clause from a DCG. I understood that DCG notation is just syntactic sugar for normal definite clauses in Prolog. I started to depict some…
user1680944
  • 537
  • 2
  • 13
  • 23
6
votes
2 answers

Parse To Prolog Variables Using DCG

I want to parse a logical expression using DCG in Prolog. The logical terms are represented as lists e.g. ['x','&&','y'] for x ∧ y the result should be the parse tree and(X,Y) (were X and Y are unassigned Prolog variables). I implemented it and…
jules
  • 1,897
  • 2
  • 16
  • 19
6
votes
2 answers

How can I create this DCG in Prolog?

I want to create a DCG that languages like this get accepted: c bbbcbbb bbacbba abacaba aababacaababa As you can see this means that there is a specific order of a and b, then one c and then again the exact same order as before the c. If these…
Waylander
  • 825
  • 2
  • 12
  • 34
1 2
3
32 33