title: How to use DCG in Prolog
body: <p>So I'm currently trying to achieve something similar to this tree, using a text file containing courses and the student ID's of anyone attending it.</p>
  
  <pre><code>courses(
      [
       ('MATH2221',
        [
         201000001,
         201000002
        ]
       ),
  
       ('MATH2251',
        [
         201000002,
         201000003
        ]
       ),
  
       ('COMP2231',
        [
         201000003,
         201000001
        ]
       )
      ]
  ).
  </code></pre>
  
  <p>The text file I'm parsing from looks like this:</p>
  
  <pre><code>MATH2221
         201000001
         201000002
  
  MATH2251
         201000002
         201000003
  
  COMP2231
         201000003
         201000001
  </code></pre>
  
  <p>I read online that using DCGs is probably the best way to go about this as every student starts with a tab so ascii value '9' and then courses are separated by 2 nl characters. I'm really lost on prolog and I'm only going to post what I have that is currently working cause everything else is a mess. Does anyone have any advice at all or can at least help me understand what a DCG is?</p>
  
  <pre><code>:- debug.
  :- [library(dcg/basics)].
  
  load:-
      open('courses.txt',read,Stream),
           read,
           close(Stream).
  
  read:-
      open('courses.txt',read,In),
      repeat,
      read_line_to_codes(In,X),write(X), nl,
      (X=end_of_file,!,
      nl); fail.
  </code></pre>