Where can I find EBNF or some other formal syntax definition for SWI Prolog itself? I can’t seem to find it on the site. I also can’t believe that a language with an environment goes without a formal syntax definition.
Sorry to tell you that to the best of my knowledge there is no EBNF for SWI-Prolog.
For ISO Prolog there is the standard, but it is not free and does not use EBNF; it uses a variation close to BNF. I purchased my copy of the standard from ANSI Webstore.
Here is an example of the syntax rules from the ISO standard.
6.3.3 Compound terms - functional notation
Every compound term can be expressed in functional
notation. When the principal functor is an operator, it can
also be expressed in operator notation (6.3.4). When the
principal functor is./2
it can also be expressed in list
notation (6.3.5), and sometimes it can be expressed as a
double quoted list (6.3.7). When the principal functor is
{}/1
it can also be expressed as a curly bracketed term
(6.3.6).Functional notation is a subset of the Prolog syntax in
which all compound terms can be expressed.A compound term written in functional notation has the
formf(Al,... ,An)
where each argumentAi
is an arg
and they are separated by,
(comma).
term = atom, open ct, arg list, close
Abstract: f(l) f l
Priority: 0
arg list = arg ;
Abstract: a a
arg list = arg, comma, arg list ;
Abstract: a,l a l
Shame… mess… How CAN one have a language without a strict formal definition?
Can you disclose what formal notation does ISO standard use?
Ok, well at least some formalism…
Part of the syntax could be in a nice EBNF, but Prolog has dynamic operators and you cannot capture that using an EBNF. That doesn’t mean the (ISO) Prolog syntax isn’t unambiguously defined. It does mean that EBNF isn’t powerful enough and that the context matters.
Yes, this also means that developing an IDE for Prolog is hard. The fact that programs are syntactically equivalent to data and that there are no keywords make it even worse. The only thing you can do easily is tokenizing. More high level support quickly requires reasoning about the program.
SWI-Prolog ships with library(prolog_colour) that can do semantic highlighting for you, both for an entire file or a single clause in the context of a file. That is what is being used for the built-in PceEmacs editor, SWISH (the web interface) and rendering sources using HTML.
Nice question.
I would love to have a DCG instead of an EBNF.
Many years ago I implemented my own lexer/parser in c++, using the high level description by Clocksin, Mellish, was funny enough.
The Craft of Prolog section 10.7 is “Tokenising Prolog”. This could probably be turned into a combined tokenizer/parser. The only advantage of having an EBNF formalism is that it can be easily proved to be unambiguous. (But EBNF would be rather tricky for Prolog; an attribute grammar would probably work but proving the grammar is unambiguous could be challenging.)
Dear colleagues, thank you for your answers.
What I need at the moment is answers to pretty simple questions relating to enclosing character sequences in “”, ', `, treatment of newline inside of those, formats of comments, character literals. I want to know whether there are restrictions for what can/has to be in the beginning of a line. I am working on a “subset” of a tokenizer. And also, from what I understood on SWI site, strings can contain \0x00, which suggests some kind of syntax for it. That’s why I set out looking for EBNF or some other formalism defining the syntax of the language.
Not the definitive answer but often useful The SWI-Prolog syntax
The one page I seem to live in is Character Escape Syntax
Also check out write_canonical/1 and write_term/2