Is there a prolog language grammar, or something close to it that is generally used as a reference? I am using SWI-prolog, so one for that flavor would be nice to have, otherwise a general prolog language grammar/specification works as well.
-
Prolog syntax was simple for the times when the language was started. But it has a feature that make it not amenable to an usual context free grammar: operators can be declared - even dynamically. I have a very simple implementation [here](https://github.com/CapelliC/IL/blob/master/parse.cpp), if you want to take a look – CapelliC Aug 27 '14 at 11:54
-
@CapelliC: Prolog had operators right from the beginning. And first Prologs, Prolog 0 and Prolog I, had an entirely different syntax. Not sure what time you are referring here to. – false Aug 27 '14 at 12:53
3 Answers
Since 1995, there is an ISO/IEC standard for Prolog: ISO/IEC 13211-1:1995. It also contains a grammar defining Prolog's syntax which consists of two levels:
Token level: (6.4 Tokens, 6.5 Processor character set)
These are defined by regular expressions and using the longest input match/eager consumer rule/greedy match/maximal munch like many languages of that era. In the words of the standard (6.4):
A token shall not be followed by characters such thatThis way of defining tokens is typical for programming languages originating in the 1970s.
concatenating the characters of the token with these
characters forms a valid token as specified by the above
syntax.NOTES
1 This is the eager consumer rule:123.e
defines the tokens123 . e
. Alayout text
is sometimes necessary to
separate two tokens.
The token level is of particular importance to Prolog's syntax, because term
, or read term
is first defined as a sequence of tokens:
term (* 6.4 *)
= { token (* 6.4 *) } ;
read term (* 6.4 *)
= term (* 6.4 *) , end (* 6.4 *) ;
Many tokens contain an optional layout text sequence
in the beginning. But never at the end. Also note that to determine the end (that is the finishing period), a lookahead to the next character is required. In a tokenizer written in Prolog, this would be realized with peek_char/1
.
Only after identifying a term on this level, the actual grammar comes into play. See the 8.14.1.1 Description of read_term/3
. Of course, an implementation might do it differently, as long as it behaves "as if".
Syntax level: (6.2 Prolog text and data, 6.3 Terms)
These definitions rely on the full context-free grammar formalism plus a few context sensitive constraints.
Conformance
As to conformance of implementations,
see this table. SWI always differed in many idiosyncratic ways: both on the token level and on the syntax level. Even operator syntax is (for certain cases) incompatible with other systems and the standard. That is, certain terms are read differently. Since SWI7, SWI now differs even for canonical syntax. Try writeq('.'(1,[])).
This should yield [1]
, but SWI7 produces some error.
For conforming implementations see sicstus-prolog (version 4.3) and gnu-prolog.

- 10,264
- 13
- 101
- 209
For SWI-Prolog in particular, things are a bit "complicated". It has never strictly conformed to ISO, and the current development version (SWI-Prolog 7 and beyond) has departed even further from ISO compliance. The development version is at the moment the only "actively" maintained version, meaning that soon you might expect bugs not to be removed from SWI-Prolog 6.
As for reference, you will have to read the manual and hope to figure out what is right and what is not. The information is all there, even if it is not super neatly organized.
This is where you might start:
http://www.swi-prolog.org/pldoc/man?section=syntax
The books recommended:
http://www.swi-prolog.org/pldoc/man?section=intro
are actually something that you can't completely circumvent, unfortunately (I would be glad if someone proves me wrong). Get at least one of the three listed there. Sterling & Shapiro, 1986 is for example a good starting point. The online tutorial at http://www.learnprolognow.org/ is also quite good.
Something else: in "The Craft of Prolog" by Richard O'Keefe you can find the full implementation of a Prolog tokenizer, written in Prolog (10.7, pp 337-354). I don't know if this would serve your purpose.
And some advice: make the effort to install the current development version if you are going to use SWI-Prolog. It is fairly easy on Linux (no idea how it goes on MacOS in practice, but I doubt it is more complicated).
-
1"has departed from strict ISO compliance in few places": SWI was never strictly conforming – false Aug 27 '14 at 11:06