# When using a DCG on a binary file can the position be changed?

**URL:** <https://swi-prolog.discourse.group/t/when-using-a-dcg-on-a-binary-file-can-the-position-be-changed/5770>\
**Category:** Help!\
**Created:** [September 14, 2022, 1:00pm UTC](https://swi-prolog.discourse.group/t/when-using-a-dcg-on-a-binary-file-can-the-position-be-changed/5770 "2022-09-14T13:00:33Z")\
**Posts on this page:** 1\
**Showing post:** 4

<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 25, 2022, 9:13am UTC](https://swi-prolog.discourse.group/t/when-using-a-dcg-on-a-binary-file-can-the-position-be-changed/5770/4 "2022-09-25T09:13:44Z")

</div>

Thanks. 🙂

In the early parts of parsing of a PE file using DCGs was working because the data was laid out sequentially. Latter the data has pointers to null terminated structures (think null terminated strings) which requires either keeping multiple offsets (think the value for seek/4) and jumping forward and backward in the data so lazy list (think phrase\_from\_file/3) are out. Then to keep track of the different offsets for each DCG is making the code harder to understand for a novice.

One option I did not try but I think would work would be to push the data such as offset onto a stack and when accessing data at a new offset would push a new context so that the DCG works as expected. For those who are comfortable with such the code would make sense but as I want to reserve the option to release the code for public use would rather make it much simpler for the everyday programmer and setting the offset with a seek function/method is common knowledge in parsing binary data so for now will not use DCGs and instead base it on

> [@Parsing of binary data](https://swi-prolog.discourse.group/t/parsing-of-binary-data/1370):
>
> This is related to this post [Making a large prolog based knowledge base for bioinformatics/epidemiology](https://swi-prolog.discourse.group/t/making-a-large-prolog-based-knowledge-base-for-bioinformatics-epidemiology/1368) Here are some example of the predicates used when parsing binary data files. [binary.pl](https://swi-prolog.discourse.group/uploads/short-url/2qMLZeeMcgB5VXFaVTdbNtzbHrY.pl) (9.9 KB) get\_byte(Stream,Address,Word) :- seek(Stream,Address,bof,\_New\_address), get\_byte(Stream,Word). get\_byte(Stream,Address0,Address,Byte) :- get\_byte(Stream,Address0,Byte), Address is Address0 + 16'01. get\_word(Stream,Endian,Word) :- get\_byte(Stream,Byte\_0), get\_byte(Stream,By…

---

_[View the full topic](https://swi-prolog.discourse.group/t/when-using-a-dcg-on-a-binary-file-can-the-position-be-changed/5770)._
