Currently I am playing with DCG in Prolog to parse an XML file. I was able to get the following code snippet which is able to parse a simple XML such as:
<author> <name> <f> arthur </f>
<m> conan </m>
<l> doyle </l>
</name>
<bday> <d> 22 </d>
<m> 5 </m>
<y> 1859 </y>
</bday>
</author>
<author> <name> <f> william </f>
<l> shakespeare </l>
</name>
<bday> <d> 23 </d>
<m> 4 </m>
<y> 1564 </y>
</bday>
</author>
$
and the DCG is defined as:
xml([E]) --> element(E).
xml([E|L]) --> element(E), xml(L).
element(E) --> begintag(N), elements(L), endtag(N), {E =.. [N|L]}.
elements(L) --> xml(L).
elements([E]) --> [E].
begintag(N) --> ['<', N, '>'].
endtag(N) --> ['<', '/', N, '>'].
Would someone please illustrate how the DCG works in this case? I am having a really hard time trying to understand the arguments in the DCG (e.g. [E] in xml([E]); [E|L] in xml([E|L]). ) Thank you!