# Phrase\_from\_file vs phrase

**URL:** <https://swi-prolog.discourse.group/t/phrase-from-file-vs-phrase/2925>\
**Category:** Help!\
**Created:** [September 12, 2020, 12:44pm UTC](https://swi-prolog.discourse.group/t/phrase-from-file-vs-phrase/2925 "2020-09-12T12:44:33Z")\
**Posts on this page:** 1\
**Showing post:** 18

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [September 13, 2020, 12:40pm UTC](https://swi-prolog.discourse.group/t/phrase-from-file-vs-phrase/2925/18 "2020-09-13T12:40:58Z")

</div>

> [@remy.s](#):
>
> But may I ask: why are you using the combination “read\_stream\_to\_codes(…), phrase(…)” rather than phrase\_from\_file?

Good question.

In one place I read that all JSON line ends have to have both the CR and LF. Without `newline(posix)` IIRC the CR is dropped and thus the JSON is considered invalid. See: [Preserving CR with LF when converting text files to character codes](https://swi-prolog.discourse.group/t/preserving-cr-with-lf-when-converting-text-files-to-character-codes/2591)

However in looking at the JSON [spec](http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf), it seems that the CR is not required so I will have to see if my parser is following the correct spec.

But you are correct that in most places I would simply use phrase\_from\_file/2,3 and you could use it in your code.

* * *

In an eariler version of this post I noted optimizations as the reason for not using phrase\_from\_file/2,3 but have since rewrote that part; so leaving this part as it is still of use with regards to DCGs in general.

If you read my example JSON parser ([post](https://swi-prolog.discourse.group/t/phrase-from-file-vs-phrase/2925/7)) and [notes](https://swi-prolog.discourse.group/t/wiki-discussion-dcg-and-phrase-3/735/7) you will see that my example code has some optimizations that I should not have done during development per my rules of development.

```prolog
object([0'{|T1],T) -->
   "{",
   (
      'ws*'(T1,[0'}|T])
   ;
      'member_first'(T1,[0'}|T])
   ).

```

---

_[View the full topic](https://swi-prolog.discourse.group/t/phrase-from-file-vs-phrase/2925)._
