Home Contents Index Summary Previous Next

2.15 Syntax Notes

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:

2.15.1 ISO Syntax Support

SWI-Prolog offers ISO compatible extensions to the Edinburgh syntax.

2.15.1.1 Character Escape Syntax

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
Alert character. Normally the ASCII character 7 (beep).

\b
Backspace character.

\c
No output. All input characters up to but not including the first non-layout character are skipped. This allows for the specification of pretty-looking long lines. For compatibility with Quintus Prolog. Not supported by ISO. Example:


format('This is a long line that would look better if it was \c
       split across multiple physical lines in the input')

\<RETURN>
No output. Skips input till the next non-layout character or to the end of the next line. Same intention as \c but ISO compatible.

\f
Form-feed character.

\n
Next-line character.

\r
Carriage-return only (i.e. go back to the start of the line).

\t
Horizontal tab-character.

\v
Vertical tab-character (ASCII 11).

\x23
Hexadecimal specification of a character. 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
Octal character specification. The rules and remarks for hexadecimal specifications apply to octal specifications too, but the maximum allowed number of octal digits is 3.

\<character>
Any character immediately preceded by a \ 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').

2.15.1.2 Syntax for non-decimal numbers

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.