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.
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:
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".
These definitions rely on the full context-free grammar formalism plus a few context sensitive constraints.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With