Question

I'm new to Prolog and I have just started looking around. I read the Define Clause Grammar chapter on both Simply Logical and Learn Prolog now!, so now I wanted to get started with some exercise but I'm stuck. I have to read from a file with this syntax

setName = {elemen1, element2,..., elementN}.
element1: element2 > element3.

Now I have read that when you define a DCG you have a parser for free, so I wanted to do that to get the data from my file to the Prolog program.

My problem is that in all the examples I have read they always provide a basic dictionary like

article --> [the]

but I cannot do that because I don't know what is going to be written in the file.

Any suggestions?

Was it helpful?

Solution

In SWI-Prolog, consider using library(dcg/basics). It provides building-blocks that you can use in your DCG. Focus on a clear declarative description of what the contents of the file look like, state this with a DCG. Then use phrase_from_file/2 from library(pio) to apply the DCG to a file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top