Joshua Levy writes:
Hello,
I have a ciao question. I wish to use the prolog reader to parse input from a string, rather than from a stream (a facility provided by read_from_chars/2 in SICStus). The {atom,string}2term predicates do this for simple terms, but do not correctly parse many things such as "(a,b,c)", "{a,b,c}", "foo((a; b))". (At least under v1.7p93, which I'm running.) Are there any other facilities for this?
Thanks for your help.
Joshua
Dear Joshua,
Yes, the implementation is not complete, as the documentation reads. We are working on a better version, based on communicating Prolog streams with character strings, and then calling the standart Prolog reader. In the meantime, a (valid) kludge is this one:
------------------------------------------------------- :- module(stt, [str2trm/2], []).
:- use_module(library(format)). :- use_module(library(system)). :- use_module(library(read)).
str2trm(String, Term):- mktemp(stringtotermXXXXXX, TempFileName), open(TempFileName, write, TempStreamWrite), format(TempStreamWrite, "~s.~n", [String]), close(TempStreamWrite), open(TempFileName, read, TempStreamRead), read(TempStreamRead, Term), close(TempStreamRead), delete_file(TempFileName). -------------------------------------------------------
Of course it creates, opens, writes to, reads from, deletes a file, which may be a performance problem if it is called many times. But it works nicely otherwise:
?- stt("a", T). T = a ? yes
?- stt("a(1)", T). T = a(1) ? yes
?- stt("(1,r,t)", T). T = (1,r,t) ? yes
?- stt("{1,r;t}", T). T = {1,r;t} ? yes
?- stt("[{1,r;t}, foo(([{}];(a,b,c)))]", T). T = [{1,r;t},foo(([{}];a,b,c))] ? yes
MCL & the Ciao Prolog Team
___________________________________________________ Real programmers can write FORTRAN in any language.