# Syntax of SWI Prolog anywhere to be found?

**URL:** <https://swi-prolog.discourse.group/t/syntax-of-swi-prolog-anywhere-to-be-found/862>\
**Category:** Help!\
**Created:** [June 26, 2019, 2:16pm UTC](https://swi-prolog.discourse.group/t/syntax-of-swi-prolog-anywhere-to-be-found/862 "2019-06-26T14:16:25Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![adimetrius](https://avatars.discourse-cdn.com/v4/letter/a/e79b87/32.png) [@adimetrius](https://swi-prolog.discourse.group/u/adimetrius)\
**Post date:** [June 26, 2019, 2:16pm UTC](https://swi-prolog.discourse.group/t/syntax-of-swi-prolog-anywhere-to-be-found/862/1 "2019-06-26T14:16:25Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [June 26, 2019, 2:22pm UTC](https://swi-prolog.discourse.group/t/syntax-of-swi-prolog-anywhere-to-be-found/862/2 "2019-06-26T14:22:15Z")

</div>

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](https://stackoverflow.com/tags/iso-prolog/info), 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](https://webstore.ansi.org/Standards/INCITS/INCITSISOIEC132111995R2017).

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  
> form `f(Al,... ,An)` where each argument `Ai` 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

```

---

<div class="post-metadata">

**Author:** ![adimetrius](https://avatars.discourse-cdn.com/v4/letter/a/e79b87/32.png) [@adimetrius](https://swi-prolog.discourse.group/u/adimetrius)\
**Post date:** [June 26, 2019, 2:34pm UTC](https://swi-prolog.discourse.group/t/syntax-of-swi-prolog-anywhere-to-be-found/862/3 "2019-06-26T14:34:04Z")

</div>

Shame… mess… How CAN one have a language without a strict formal definition?  
Can you disclose what formal notation does ISO standard use?

---

<div class="post-metadata">

**Author:** ![adimetrius](https://avatars.discourse-cdn.com/v4/letter/a/e79b87/32.png) [@adimetrius](https://swi-prolog.discourse.group/u/adimetrius)\
**Post date:** [June 26, 2019, 2:54pm UTC](https://swi-prolog.discourse.group/t/syntax-of-swi-prolog-anywhere-to-be-found/862/4 "2019-06-26T14:54:36Z")

</div>

Ok, well at least some formalism…

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [June 26, 2019, 3:01pm UTC](https://swi-prolog.discourse.group/t/syntax-of-swi-prolog-anywhere-to-be-found/862/5 "2019-06-26T15:01:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![CapelliC](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/capellic/32/17_2.png) [@CapelliC](https://swi-prolog.discourse.group/u/CapelliC)\
**Post date:** [June 26, 2019, 3:40pm UTC](https://swi-prolog.discourse.group/t/syntax-of-swi-prolog-anywhere-to-be-found/862/6 "2019-06-26T15:40:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![peter.ludemann](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/peter.ludemann/32/48_2.png) [@peter.ludemann](https://swi-prolog.discourse.group/u/peter.ludemann)\
**Post date:** [June 26, 2019, 4:21pm UTC](https://swi-prolog.discourse.group/t/syntax-of-swi-prolog-anywhere-to-be-found/862/7 "2019-06-26T16:21:50Z")

</div>

_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.)

---

<div class="post-metadata">

**Author:** ![adimetrius](https://avatars.discourse-cdn.com/v4/letter/a/e79b87/32.png) [@adimetrius](https://swi-prolog.discourse.group/u/adimetrius)\
**Post date:** [June 26, 2019, 4:48pm UTC](https://swi-prolog.discourse.group/t/syntax-of-swi-prolog-anywhere-to-be-found/862/8 "2019-06-26T16:48:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [June 26, 2019, 4:53pm UTC](https://swi-prolog.discourse.group/t/syntax-of-swi-prolog-anywhere-to-be-found/862/9 "2019-06-26T16:53:20Z")

</div>

> [@adimetrius](#):
>
> from what I understood on SWI site, strings can contain \0x00, which suggests some kind of syntax for it.

Not the definitive answer but often useful [The SWI-Prolog syntax](https://www.swi-prolog.org/pldoc/man?section=syntax)

The one page I seem to live in is [Character Escape Syntax](https://www.swi-prolog.org/pldoc/man?section=charescapes)

Also check out [write\_canonical/1](https://www.swi-prolog.org/pldoc/man?predicate=write_canonical/1) and [write\_term/2](https://www.swi-prolog.org/pldoc/man?predicate=write_term/2)
