Prolog DCG for parsing escaped sequences

Saturday, July 12, 2014

I need to parse the string ^borrow$ ^\$500$ into the list [borrow, $500]. The grammar I wrote so far is



:- use_module(library(dcg/basics)).

write_list([]).
write_list([H|T]) :- atom_codes(S, H), write(S), nl, write_list(T).

% Grammar.
tags([Tag|Rest]) --> string(_), tag(Tag), tags(Rest).
tags([]) --> string(_).
tag(Tag) --> "^", tag_contents(Tag), "$".
tag_contents(Tag) --> string(Tag).


Which works when I don't have \$ inside a token:



?- phrase(tags(T), "^pisica$ ^catel$"), write_list(T).
pisica
catel
?- phrase(tags(T), "^borrow$ ^\\$500$"), write_list(T).
borrow
\


What is the best practice for parsing this kind of escaped sequences with Prolog DCGs?







http://ift.tt/TZfoxf