Note that there are some explanatory texts on larger screens.

plurals
  1. POStack overflow in Prolog DCG grammar rule: how to handle large lists efficiently or lazily
    primarykey
    data
    text
    <p>I'm parsing a fairly simple file format consisting of a series of lines, each line having some space separated fields, that looks like this:</p> <pre><code>l 0x9823 1 s 0x1111 3 l 0x1111 12 ⋮ </code></pre> <p>I'm using SWI-Prolog. This is the DCG I have so far:</p> <pre class="lang-prolog prettyprint-override"><code>:- consult(library(pure_input)). load_trace(Filename, Traces) :- phrase_from_file(trace_file_phrase(Traces), Filename). trace_file_phrase([]) --&gt; []. trace_file_phrase([T|Ts]) --&gt; trace_phrase(T), trace_file_phrase(Ts). trace_phrase(access(Type, Address, SinceLast)) --&gt; access_type(Type), space, address(Address), space, nat(SinceLast), newline. access_type(load) --&gt; "l". access_type(store) --&gt; "s". address(Number) --&gt; "0x", hexnum(Number). hexdigit(N) --&gt; digit(N). hexdigit(10) --&gt; "a". hexdigit(11) --&gt; "b". hexdigit(12) --&gt; "c". hexdigit(13) --&gt; "d". hexdigit(14) --&gt; "e". hexdigit(15) --&gt; "f". hexnum(N) --&gt; hexdigit(D), hexnum(D, N). hexnum(N, N) --&gt; []. hexnum(A, N) --&gt; hexdigit(D), { A1 is A*16 + D }, hexnum(A1, N). newline --&gt; "\n". space --&gt; " ". %% the following two productions are courtesy of Lars Mans at %% https://stackoverflow.com/questions/3279822/parsing-numbers-with-multiple-digits-in-prolog digit(0) --&gt; "0". digit(1) --&gt; "1". digit(2) --&gt; "2". digit(3) --&gt; "3". digit(4) --&gt; "4". digit(5) --&gt; "5". digit(6) --&gt; "6". digit(7) --&gt; "7". digit(8) --&gt; "8". digit(9) --&gt; "9". nat(N) --&gt; digit(D), nat(D,N). nat(N,N) --&gt; []. nat(A,N) --&gt; digit(D), { A1 is A*10 + D }, nat(A1, N). </code></pre> <p>As mentioned in the comment, I cribbed the number handling from <a href="https://stackoverflow.com/questions/3279822/parsing-numbers-with-multiple-digits-in-prolog">Parsing numbers with multiple digits in Prolog</a>.</p> <p>The problem I'm running into is some of these files are large, like, on the order of 5-10 MB. The default stack in SWI-Prolog is insufficient for this, and parsing these files is taking substantial time, on the order of 5-15 seconds. I have several questions about this situation:</p> <ol> <li>Where is the efficiency problem in this code? I think it's either in <code>trace_file_phrase//1</code> or <code>nat//1</code> but these are just hunches.</li> <li>If the problem is lists, is there a better way to handle lists with DCGs than this?</li> <li>How does one, in general, diagnose and treat performance problems with DCGs such as this?</li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload