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
2 answers

Categorise List in Prolog

Alright so I am coding a parser for arithmetic equations. I get the input in a list, e.g. "10+20" = [49,48,43,50,48] and then I convert all the digits to there corresponding numbers e.g. [49,48,43,50,48] = [1,0,43,2,0] and from there I want to put…
WhaleFanny
  • 77
  • 3
  • 9
0
votes
2 answers

Check a sentence for right syntax and get the semantic

i'm new to prolog and I try to program a answering machine. At first I like to figure out for what was asked, and to check correct syntax. question(P) --> [where],[is], article(G,K,N), location(P,G,K,N). location(P,G,K,N) --> [P], {member(P,…
Karl Adler
  • 15,780
  • 10
  • 70
  • 88
0
votes
2 answers

DCG(Definite clause grammar) in Prolog

I am trying to make a DCG in prolog so that I create a sentence based on some predicates. I have two pieces of information = properties of objects ("Charles is a man.") and relations between objects ("Charles is the father of William.") The task is…
user1306283
0
votes
4 answers

Prolog - Do plain rules have better performance than lists?

I have a set of DCG rules (in this case german personal pronouns): % personal pronoun (person, case, number, genus) ppers(1,0,sg,_) --> [ich]. ppers(1,1,sg,_) --> [meiner]. ppers(1,2,sg,_) --> [mir]. ppers(1,3,sg,_) --> [mich]. ppers(2,0,sg,_) -->…
magnattic
  • 12,638
  • 13
  • 62
  • 115
0
votes
1 answer

simple dcg lexer

Hi i'm writing a simple lexer :- module(lekser, [lekser/3]). lekser(Tokens) --> white_space, ( ( "{", !, { Token = tkLBrace } ; "}", !, { Token = tkRBrace } ; ")", !, {…
whd
  • 1,819
  • 1
  • 21
  • 52
0
votes
1 answer

Replace term with a variable in Prolog

I have a DCG in Prolog that I can query like so: q(Tree, [name, of, company], []). and get a response that shows me the path taken to parse the query: Tree = q(['company (Class)', 'name (Attribute)']) Now I would like to pose a query such…
Radek
  • 3,913
  • 3
  • 42
  • 37
0
votes
3 answers

Use Prolog to get data model paths from language rules?

I want to build a semi-natural language interface for a data warehouse. A simple data model looks for example like this: Company - attribute 'name' - reference to 'Departments' Department - attribute 'type' - reference to 'Employees' Employee -…
Radek
  • 3,913
  • 3
  • 42
  • 37
-1
votes
3 answers

how to print the whole route

Such as for go(a, d). i want it to print the route as well such as route a, route b, route, c and route d door(a, b). door(b, c). door(c, d). door(b, e). door(e, f). door(e, g). go(FromRoom, ToRoom):- door(FromRoom,ToRoom). go(FromRoom, ToRoom)…
user1232622
  • 253
  • 1
  • 2
  • 6
-1
votes
3 answers

DCG: hidden argument across all Rules?

.in DCG is there a way to have hidden argument i.e. argument is passed in the top rule , but I dont mention it in the rest of the rules , but i still have access to it. S(Ctx,A,B) --> ... R1(A) --> .... R2(A) --> ..R5(A), { write(Ctx) } R3(A)…
sten
  • 7,028
  • 9
  • 41
  • 63
-1
votes
1 answer

prolog - DCG transformation

What does E -> T mean ? Variable E implies variable T ? This the code which is linked : e(TS,R) :- t(TS,R). Thanks for your help
wallace27
  • 35
  • 5
-1
votes
1 answer

How to extract a prefix given a suffix in a DCG

pc --> [ta] -> [tb] -> [tc] . If I have tc , how do I get [ta,tb] put into a variable? If I have the tail end of a list defined in a definite clause, how do I get the preceeding elements as a list of unknown length? I am hoping that approach can be…
-1
votes
1 answer

Write a Prolog DCG grammar that handle the mean of a sentence and also build its parse tree

I have this DCG grammar that understand and agree phrases like: [john, paints] and [john, likes, mary] managing the semantic meaning directly into the DCG grammar by the use of parameters sentence2(VP) --> noun_phrase2(Actor), …
AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
-1
votes
1 answer

Some doubts about how work this DCG grammar for subset of natural language in Prolog

I am studying DCG grammar for natural language processing using Prolog and I have some doubts about if I have understand it correctly or if I am missing something. This is my DCG grammar: sentence2(VP) --> noun_phrase2(Actor), …
AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
-1
votes
1 answer

Some doubts related about how work a meaning predicate that interprets the parse tree of a DCG grammar in Prolog

I am studying Prolog DCG grammar* and **parse tree on the Ivan Bratko book: "Programming for Artificial Intelligence" I am finding some difficulties with the following example that provide a DCG grammar that create a parse tree from a string that…
AndreaNobili
  • 40,955
  • 107
  • 324
  • 596
-1
votes
1 answer

Explicitly failing a context free grammar sentence parse

I have a context free grammar sentence parser which functions correctly except for the fact that when you either type in something that doesn't exist in its knowledge base or doesn't conform to the grammar it will remain in an infinite loop. I…
user2211776
  • 239
  • 1
  • 2
  • 11
1 2 3
32
33