سؤال

I need to export to a file a Prolog program expressed using an arbitrary term representation in Java. The idea is that a Prolog interpreter should be able to consult the generated file afterwards. My question is about the correct way to write in the file Java Strings representing atom terms.

For example, if the string has a space in the middle, it should be surrounded by single quotes in the file:

 hello world   becomes   'hello world'

And the exporter should take into consideration characters that should be escaped:

 '   becomes  '\''

Could someone point me to the place were these rules are specified?, and: Can I assume that these rules are respected by major Prolog implementors? (I mean, a Prolog program generated following these rules would be correctly parsed by most Prolog interpreters?).

هل كانت مفيدة؟

المحلول

The precise place for this is the standard, ISO/IEC 13211-1:1995, quoted_token (* 6.4.2 *). See this answer how to get it for USD 30.

The precise syntax is quite complex due to a lot of extras like continuation lines and the like. If you are only writing atoms that should be read by Prolog, things are a bit easier. Also in that situation, you could always quote, which makes writing again a bit simpler.

Some things to be aware of:

Only simple spaces may occur as layout in a quoted atom. All other spaces need to be escaped like \t, \n (abrftnv). Many systems accept also other layout but they differ to each other in very tiny details.

Backslash and quote must be escaped.

Characters outside the printable ASCII range depend on the PCS supported by a system. In a conforming system, the accompanying documentation should define how the additional characters (extended characters) are classified. Documentation quality varies on a wide range.

In any case, test your interface also with GNU-Prolog from 1.4.1 upwards. To date, no differences are known between GNU 1.4.1+ and the standard as far as syntax is concerned.

Here are some 240+ syntax related test cases. Please report any oversight!

نصائح أخرى

A practical hint: if you issue a writeq with your Prolog, with data you need to know about, you'll get quotes around when required.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top