Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9051911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:01:52+00:00 2026-06-16T13:01:52+00:00

As part of a project I need to write a parser that can read

  • 0

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 is:

property(el1).
property(el2).
...

I read my file like this:

main :-
       open('myFile.txt', read, Str),
       read_file(Str,Lines),
       close(Str),
       write(Lines), nl.

read_file(Stream,[]) :-
                       at_end_of_stream(Stream).

read_file(Stream,[X|L]) :-
                          \+ at_end_of_stream(Stream),
                          read(Stream,X),
                          parse(X),            % Here I call upon my parser.
                          read_file(Stream,L).

Now I have read in several books and online about DCG, but they all explain the same simple examples where you can generate sentences like “the cat eats the bat” etc… When I want to use it for the above example I fail miserably.

What I did manage was “parsing” the underneath line:

property = el1.

to

property(el1).

with this:

parse(X) :-
           X =.. List,    % Reason I do this is because X is one atom and not a list.
           phrase(sentence(Statement), List),
           asserta(Statement).

sentence(Statement) --> ['=', Gender, Person] , { Statement =.. [Gender, Person] }.

I don’t even know if I’m using the dcg in a correct way here, so any on help on this would be appreciated. Now the problem I having is, how to do this with multiple elements in my list, and how to handle ‘{‘ and ‘}’.
What I really want is a dcg that can handle these types of sentences (with more than 2 elements): Sentence split in parts

Now I know many people around here refer to the libraries dcg_basics and pio when it comes to dcgs. However, I have an additional problem that when I try to use the library I receive the error:

ERROR: (c:/users/ldevriendt/documents/prolog/file3.pl:3):
      Type error: `text' expected, found `http/dcg_basics'
Warning: (c:/users/ldevriendt/documents/prolog/file3.pl:3):
      Goal (directive) failed: user:[library(http/dcg_basics)]

when I do this:

:- [library(http/dcg_basics)].

Additional info:

  • I use the program: SWI-Prolog-Editor on a Windows environment.

Any help on this would be appreciated!

EDIT: The aim of this is question is to learn more about DCG and its use in parsers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T13:01:53+00:00Added an answer on June 16, 2026 at 1:01 pm

    as long as your file is in plain Prolog syntax, you’re advised to use Prolog term IO. Fully structured terms are read with a single call. Using a DCG its’ way more complicate, and a bit less efficient (not sure here, should measure, but read(Term) invokes a Prolog parser implemented in C…) See this other question, that uses the very same format (at least, you could check if some other guy got an answer here on SO about your same assignment…)

    edit after comments…

    You’re right that DCG are the right way to handle general parse in Prolog.
    Arguments in DCG productions can be seen as semantic attributes, thus programming DCG can be seen as providing a working semantic analysis on the input (see Attribute Grammar, an important technique -also- in language engineering).

    And indeed the presented examples can perfectly well be solved without the hacks required with term IO.

    Here it is:

    :- use_module(library(pio)).  % autoload(ed), added just for easy browsing
    :- use_module(library(dcg/basics)).
    
    property(P) -->
        b, "my props", b, "=", b, "{", elS(Es) , b, "}", b,
        { P =.. [property|Es] }.
    
    elS([E|Es]) --> el(E), b, ("," -> elS(Es) ; {Es = []}).
    el(N) --> number(N).
    el(S) --> csym(S). % after Jeremy Knees comment...
    b --> blanks.
    
    %   parse a C symbol
    csym(S) -->
        [F], { code_type(F, csymf) },
        csym1(Cs),
        !, { atom_codes(S, [F|Cs]) }.
    
    csym1([C|Cs]) -->
        [C], { code_type(C, csym) },
        csym1(Cs).
    csym1([]) --> [].
    

    with that, we have

    ?- phrase(property(P), "my props = {1,2,3}").
    P = property(1, 2, 3).
    

    Thanks to library(pureio) we can apply semantic programming to Prolog streams, and be rewarded of the same behaviour of phrase/2.

    more

    This other answer show a practical way to implement an expression calculator with operator resolution, and lazy evaluation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As part of my project I need to use some Unamanged code dlls in
As part of an ASP.NET MVC4 project I need to be able to read
As part of an upcoming project at my university, I need to write a
I am a part of TFS project. We use Visual Studio 2005. It was
As part of a pet project of mine, I need to test the performance
I am trying to write Java code as a part of my project to
To conditionally enable a part of an autotooled project, I need to check whether
As part of my project I need to download a Zipped folder from a
In my project, I need to write a sql query but I have an
I'm going to write my first non-Access project, and I need advice on choosing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.