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

how can I pass from DCG in Horn clauses with function \+

I want to translate a grammar rule like this, into a clause char(C) --> [C], { code_type(C, graph), \+ memberchk(C, "()") }. but does not work char(C,In,Out):- In=[C|Out], code_type(C, graph), \+ memberchk(C, "()").
Vincenzoni
  • 55
  • 4
0
votes
2 answers

PROLOG: Operator what is "-->"

i am quite new to prolog and i'd like an explanation about something if possible. What exactly does the "-->" prolog operator do? i can't seem to find an easy straight answer for that. and just one more thing... what's the difference between: …
0
votes
1 answer

Program that "mean" a parse tree in Prolog, don't work

I am studying DCG grammar and parse tree using the Ivan Bratko book: Programming fro Artificial Intelligence. On the book I found the following example that show a DCG grammar that also generate a parse tree and a meaning/2 predicate that is used to…
AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
0
votes
1 answer

Some doubts about how Prolog automatically convert DCG grammars int a set of rules

I am studying DCG grammar in Prolog using Ivan Bratko book: "Programming for Artificial Intelligence" and I am finding some problem to understand how Prolog automatically convert a DCG grammar into a set of Prolog rules. For example I have the…
AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
0
votes
2 answers

Define a dcg for palindromes over the alphabet {a,b} of 2 symbols a and b such that the number of a's is one less than the number of b's

I am trying to define a palindrome where the number of a's is one less than the number of b's. I cant seem to figure out how to write it properly please-->palindromes. palindromes-->[]. palindromes-->[a]. palindromes-->[b]. …
user1850254
  • 2,091
  • 3
  • 16
  • 17
0
votes
2 answers

Read file and build facts on swi-prolog

I need to read from a file where there are descriptions of facts, for example: id: GO:0000008 name: thioredoxin and I need build a fact with it, like as :"gene(0000008,thioredoxin)." ---added--- Hi, I have been searching info and I found the DCG…
Cristhiam
  • 1
  • 1
  • 3
0
votes
1 answer

Diagnostics messages for pascal parser

I'm writing a simple pascal parser and have specified some grammar rules like program_header --> program, id, leftparenthesis ... etc program --> [500] id --> [300] etc How would I go about if I wanted to display diagnostic messages to the user?…
Stayfrosty555
  • 23
  • 1
  • 5
0
votes
1 answer

DCG parser in Prolog for logic statements

Given the following grammar: BoolExpr ::= BoolConj { "or" BoolConj }. BoolConj ::= BoolLit { "and" BoolLit }. BoolLit ::= [ "not" ] BoolPosLit. BoolPosLit ::= "true"| "false"| "(" BoolExpr ")". I want to write a DCG parser for the grammar…
0
votes
1 answer

Prolog, error when querying a false statement

input :- read_line_to_codes(user_input, Input), string_to_atom(Input,Atoms), atomic_list_concat(Alist, ' ', Atoms), phrase(sentence(S), Alist), action(S). statement(Rule) --> [Noun, 'is', 'a', Object], { Rule =.. [Object, Noun]}. statement1(Rule)…
0
votes
1 answer

Parsing an integer in a string?

I was going through this example on DCG integer(I) --> digit(D0), digits(D), { number_codes(I, [D0|D]) }. digits([D|T]) --> digit(D), !, digits(T). digits([]) --> []. digit(D) --> …
ivt
  • 301
  • 3
  • 9
0
votes
3 answers

Prolog deciding a certain rule to choose depending on input

I've got this prolog problem I can't get around. What I'm trying to achieve is to assert FACT A, retract Fact B when I have input: take and assert Fact B and retract Fact A when I have input put. ie: :- dynamic s/2. :- dynamic s/3. s(P0, s(V, NP))…
macguy
  • 294
  • 3
  • 12
0
votes
1 answer

Infinite list of terminals – Prolog grammar rules

Is it possible to define an indefinite number of terminals in Prolog when working with grammar rules? The following example describes the…
Luka
  • 1,718
  • 3
  • 19
  • 29
0
votes
1 answer

Implementing an arithmetic parser in Prolog

I have the following Prolog code: expression-->first,operator,second. first-->[X]. operator-->['+'];['-']. second-->[X]. After compilation, the machine responds with "yes" in the command line for the following types of queries: | ?-…
Luka
  • 1,718
  • 3
  • 19
  • 29
0
votes
0 answers

Prolog- matching parts of a list

Possible Duplicate: Prolog- translating English to C We basically have an assignment where we're given a list that represent some notation in English, such as [add,3,to,5], and we need to take that and have it output the corresponding notation in…
T T
  • 261
  • 5
  • 16
0
votes
1 answer

Prolog simple program

I am writing a little Prolog program, which is expected to do something as following: ?-input([allan,is,a,name]). true. ?-input([Is,allan,a,name]). true. and here is my code: % Simple answering agent input(Text) :- phrase(sentence(S), Text),…
Allan Jiang
  • 11,063
  • 27
  • 104
  • 165