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

Prolog - How can obtain the prefix form of a math calculation given its usual form?

I'm new in prolog and I would like to program a calculator. For that I need to program a predicate that process the given arithmetic expression, written in the usual form (infix form), such that to obtain its prefix form. The elements of the…
user7141599
0
votes
1 answer

Writing grammars with prolog

Is it possible to use Prolog to define a grammar for a non-natural language, for example, SQL? If so, please guide me to some starting point.
Great
  • 95
  • 1
  • 4
0
votes
1 answer

Find the elements on even level of a binary tree in Prolog

I'm trying to find all the elements that are placed on even level in a binary tree and place them in a list. This is what i tried: concat([],L,L). concat([H|T], L, [L|LRez]) :- concat(T,L,LRez). find(R, t(_, R, _), 0). find(X, t(S, R, D),P) :- X <…
Andy
  • 155
  • 1
  • 13
0
votes
1 answer

Prolog type errors with DCG library functions

I'm trying to write a DCG for a command interface. The idea is to read a string of input, split it on spaces, and hand the resulting list of tokens to a DCG to parse it into a command and arguments. The result of parsing should be a list of terms…
DCE
  • 1
  • 1
0
votes
1 answer

prolog - bst tree to list

I am pracising dcg technique, but I have a problem. I am going to implement converting BST tree to list (each possible) and in reversed side - list ot bst (each possible). (List means set in real here). However, this grammar should be ok, but it…
user6023611
0
votes
0 answers

How can I test a phrase with a context free grammar writing with DCG prolog

I have a context free grammar which I have written in DCG prolog. I also have some example phrases in the language. I want to test each example phrase with the context free grammar in order to verify that the context free grammar is correct. Please…
fenimi
  • 21
  • 4
0
votes
1 answer

Have Prolog return a rule that applies

I'm interested in incrementally parsing the sequence as the terms come in one at a time. This requires that I'm able to determine which of the rewrite rules can consume the current input. So, conceptually I am looking for something like the…
user1048677
  • 229
  • 3
  • 13
0
votes
1 answer

How to bind variable using DCG in prolog

Based on the code in the pics, How can I bind those unbounded variables with processing the structure? For example, how can I let the X=1,Y=2 for the example in first pic. a more clear example: I asked another question to be more specific. if it…
SwordW
  • 592
  • 2
  • 6
  • 20
0
votes
1 answer

Having trouble understanding Prolog DCG

This is a new following up question of https://stackoverflow.com/questions/34170191/confused-about-how-to-use-in-prolog?noredirect=1#comment56112107_34170191 I tried to make the code in 9.7 more powerful. So I decide to add a new grammar…
SwordW
  • 592
  • 2
  • 6
  • 20
0
votes
1 answer

Meaning of --> in Prolog

I am a novice in Prolog. Recently I saw a piece of code. I want to know the meaning of some parts of it and how to use it. the code as below: MatrixTconcat([]) --> []. matrix_tconcat([Cs|Css]) --> row_rows_rest_(Cs,Css,Mss), …
Lin Jiayin
  • 499
  • 1
  • 6
  • 11
0
votes
1 answer

Reducing Complex DCGs Prolog

How do I reduce a DCG rule like this dtv(P1^P2^P3^Q1) using apply(X^P,X,P)? I'm trying to describe the semantics of different grammatical components and I'm using lambda calculus. This is what I have for ditransitive verbs, however, the second…
fenimi
  • 21
  • 4
0
votes
0 answers

Syntax error in the representation of DCG in Prolog?

I am writing a grammar for a game simulation using DCG of prolog. I assume that the logic is correct by I get the following syntax errors . program --> envsetup, commands. envsetup --> boardsize, obstacles, itinerary, character. boardsize -->…
user1680944
  • 537
  • 2
  • 13
  • 23
0
votes
2 answers

Prolog - List concatenation in Trees

I have to write a predicate ListInTree(T,X) which is true when T is a tree with a list in each node, and X is the concatenation of all the lists (assuming to visit in Pre-order). I really can't understand how to use the recursion to visit all the…
0
votes
1 answer

How to represent optionality and 0 or more in Prolog rules?

I am trying to write a rule in order to represent 0 or more using recursion: The rule in EBNF is the following: translation_unit => external_declaration { external_declaration} My guess is the following: translation_unit(Xs0,Xs) :- …
user1680944
  • 537
  • 2
  • 13
  • 23
0
votes
1 answer

Stuck on translating a cfg to dcg

I'm trying to teach myself prolog and implementing an interpreter for a simple arithmetic cfg: --> number --> ( ) --> + --> -…
joeblog
  • 1,101
  • 11
  • 17