Yes, I know I am reviving a topic.
How do you suggest to parse a line-oriented text file, where each line must end with a newline?
Here are different options I tried. I strongly suspect I am making a very basic mistake somewhere…
First, I thought that I can use sequence//5 with an empty Start, and both Sep and End a newline, like this:
phrase(sequence("", line_dcg, "\n", "\n", List), Input)
This does not work for me. The problem seems to be that the Sep and the End are the same. Using *
and ‘|’ so that it is easier to read:
?- phrase(sequence("", integer, "*", "*", L), `1*2*`).
false.
?- phrase(sequence("", integer, "|", "*", L), `1|2*`).
L = [1, 2].
I guess sequence//3 suffers from the same problem?
?- phrase(( sequence(integer, "|", L), "*" ), `1|2*`).
L = [1, 2].
?- phrase(( sequence(integer, "*", L), "*" ), `1*2*`).
false.
Finally, using sequence//2 with an “element DCG” that consumes the end-of-line:
line(X) --> integer(X), "*".
and then:
?- phrase(sequence(line, L), `1*2*`).
L = [1, 2] ;
false.
… and I guess that for parsing, I should cut after phrase?
?- phrase(sequence(line, L), `1*2*`), !.
L = [1, 2].