Is there a prolog grammar / specification?

Is there a prologue grammar or something close to it that is usually used as a reference? I use a SWI prolog, so it would be nice to have one for this taste, otherwise the general grammar / specification of the prolog language also works.

+7
prolog iso-prolog grammar
source share
3 answers

Since 1995, there is an ISO / IEC standard for Prolog: ISO / IEC 13211-1: 1995. It also contains a grammar defining the Prolog syntax, which consists of two levels:

Token Level: ( 6.4 Tokens, 6.5 CPU Character Set )

They are defined by regular expressions and use the longest rule of input / impatient consumer / greedy matching / maximum munk, like many languages ​​of that era. In the words of the standard (6.4):

And the token should not be accompanied by symbols, so when concatenating the token symbols with these symbol symbols, a valid token is formed, as indicated above. syntax

NOTES
1 This is the right consumer rule: 123.e defines tokens
123 . e
123 . e . A layout text sometimes it is necessary to separate two tokens.

This method of defining tokens is typical of programming languages ​​created in the 1970s.

Token level is of particular importance to the Prolog syntax, because a term or read term first defined as a sequence of tokens:

 term (* 6.4 *) = { token (* 6.4 *) } ; read term (* 6.4 *) = term (* 6.4 *) , end (* 6.4 *) ; 

Most tokens contain an optional layout text sequence at the beginning. But never at the end. Also note that to determine the end (i.e., the end period), you need to look at the next character. In a tokenizer written in Prolog, this will be implemented using peek_char/1 .

Only after defining the term at this level does the actual grammar come into play. See 8.14.1.1 Description of read_term/3 . Of course, an implementation can do it differently if it behaves "as if".

Syntax Level: (6.2 Prolog Text and Data, 6.3 Terms)

These definitions rely on full context-free grammar formalism plus a few context-sensitive restrictions.

Conformity

Regarding conformance of implementations, see this table . SWI has always been distinguished by many features: both at the marker level and at the syntax level. Even the operator syntax ( for certain cases ) is incompatible with other systems and the standard. That is, some terms are read differently. Since SWI7, SWI is now different even for canonical syntax . Try writeq('.'(1,[])). This should give [1] , but SWI7 creates some error.

For relevant implementations see sicstus-prolog (version 4.3) and gnu-prolog .

+4
source share

For SWI-Prolog, in particular, everything is a bit "complicated." It never strictly complied with ISO, and the current development version (SWI-Prolog 7 and later) deviated even further from ISO compliance. The development version is currently the only "active" version, which means that soon you can expect that errors will not be removed from SWI-Prolog 6.

As a reference, you will need to read the manual and hope to find out what is right and what is not. Information is there, even if it is not super ordered.

Here you can start:

http://www.swi-prolog.org/pldoc/man?section=syntax

Recommended books:

http://www.swi-prolog.org/pldoc/man?section=intro

in fact, something that you cannot completely get around, unfortunately (I would be glad if someone proves to me that I'm wrong). Get at least one of the three listed there. Sterling and Shapiro, 1986, are, for example, a good starting point. The online tutorial at http://www.learnprolognow.org/ is also good.

Something else: in Richard O'Keefe's The Craft of Prolog, you can find the full implementation of the Prolog tokenizer written in Prolog (10.7, pp. 337-354). I do not know if this will serve your purpose.

And some tips: make the effort to install the current development version if you intend to use SWI-Prolog. This is pretty easy on Linux (I don’t know how it works on MacOS in practice, but I doubt it is more complicated).

+3
source share

At least there is an ISO standard (see its creator page ).

+2
source share

All Articles