SWI-Prolog uses standard `Edinburgh' syntax. A description of this syntax can be found in the Prolog books referenced in the introduction. Below are some non-standard or non-common constructs that are accepted by SWI-Prolog:
0'<char>
C
is a lower case character one can use
between(0'a, 0'z, C)
.
/* ... /* ... */ ... */
/* ... */
comment statement can be nested. This is
useful if some code with /* ... */
comment statements in it
should be commented out.
SWI-Prolog offers ISO compatible extensions to the Edinburgh syntax.
The
processor character set specifies the class of each character used for
parsing Prolog source text. The character classes are compatible to the
ISO-latin-1 character set. The style_check/1
flag charset
can be used to provide warnings on the use of
unquoted variables and atoms holding non-ASCII characters.
Within quoted atoms (using single quotes: '<atom>'
special characters are represented using escape-sequences. An escape
sequence is lead in by the backslash (
)
character. The list of escape sequences is compatible with the ISO
standard, but contains one extension and the interpretation of
numerically specified characters is slightly more flexible to improve
compatibility.
\
\a
\b
\c
format('This is a long line that would look better if it was \c split across multiple physical lines in the input') |
\<RETURN>
\c
but ISO compatible.
\f
\n
\r
\t
\v
\x23
23
is just an
example. The `x' may be followed by a maximum of 2 hexadecimal digits.
The closing \
is optional. The code \xa\3
emits the character 10 (hexadecimal `a') followed by `3'. The code \x201
emits 32 (hexadecimal `20') followed by `1'. According to ISO, the
closing
\
is obligatory and the number of digits is unlimited. The
SWI-Prolog definition allows for ISO compatible specification, but is
compatible with other implementations.
\40
\<character>
\
and
not covered by the above escape sequences is copied verbatim. Thus, '\\'
is an atom consisting of a single \
and '\''
and ''''
both describe the atom with a single '
.
Character escaping is only available if the
current_prolog_flag(character_escapes, true)
is active
(default). See current_prolog_flag/2.
Character escapes conflict with writef/2
in two ways: \40
is interpreted as decimal 40 by writef/2,
but character escapes handling by read has already interpreted as 32 (40
octal). Also, \l
is translated to a single `l'. It is
advised to use the more widely supported format/[2,3]
predicate instead. If you insist upon using writef/2,
either switch character_escapes
to
false
, or use double \\
, as in writef('\\l')
.
SWI-Prolog implements both Edinburgh and ISO representations for
non-decimal numbers. According to Edinburgh syntax, such numbers are
written as <radix>'<number>
,
where <radix> is a number between 2 and 36. ISO defines
binary, octal and hexadecimal numbers using
0[bxo]<number>
. For example: A is 0b100 \/ 0xf00
is a valid expression. Such numbers are always unsigned.