term_string/2 results in a syntax error if the string starts with '?'

15 views
Skip to first unread message

Till Hofmann

unread,
Jan 5, 2017, 5:33:16 AM1/5/17
to SWI-Prolog
I'm trying to convert a string that starts with a '?' into a term:

?- term_string(T, "?x").
ERROR
: Syntax error: Operator expected
ERROR
:
ERROR
: ** here **
ERROR
: ?x .

I would expect the result to be the same as with atom_string/2:

?- atom_string(A, "?x").
A
= '?x'.

I guess the error occurs because term_string actually parses the string, as the documentation mentions:
If String is instantiated, it is parsed and the result is unified with Term
 
However, I still wouldn't expect a syntax error as result. Is this a bug or is my expectation wrong?

Raivo Laanemets

unread,
Jan 5, 2017, 5:40:36 AM1/5/17
to SWI-Prolog
It's a syntax error directly on console too:

?- A = ?x.
ERROR: Syntax error: Operator expected
ERROR: A = 
ERROR: ** here **
ERROR: ?x .

Atom must be quoted if it contains any special characters.

Till Hofmann

unread,
Jan 5, 2017, 5:44:24 AM1/5/17
to SWI-Prolog


On Thursday, January 5, 2017 at 11:40:36 AM UTC+1, Raivo Laanemets wrote:
It's a syntax error directly on console too:

?- A = ?x.
ERROR: Syntax error: Operator expected
ERROR: A = 
ERROR: ** here **
ERROR: ?x .

Atom must be quoted if it contains any special characters.

Yes, and I would expect term_string/2 to quote the atom appropriately, just like atom_string/2 does.

Jan Wielemaker

unread,
Jan 5, 2017, 7:03:21 AM1/5/17
to Till Hofmann, SWI-Prolog
term_string/2 is (de)serialization of a Prolog term to/from text (a
string). That means the text must have valid Prolog syntax. If you
want an atom, use atom_string/2. If you want an atom as fallback,
use catch/3 around term_string, as in:

catch(term_string(Term, String),
error(syntax_error(_),_),
atom_string(Term, String)).

I can't see many cases where this is a smart idea ...

Cheers --- Jan

>
> --
> You received this message because you are subscribed to the Google
> Groups "SWI-Prolog" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to swi-prolog+...@googlegroups.com
> <mailto:swi-prolog+...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/swi-prolog.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages