Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a prolog language grammar/spec?

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.

like image 723
Lance Avatar asked Aug 27 '14 03:08

Lance


1 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 that
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 tokens
123 . e. A layout text is sometimes necessary to
separate two tokens.

This way of defining tokens is typical for programming languages originating in the 1970s.

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.

like image 114
false Avatar answered Oct 12 '22 22:10

false