.. $Id: index.txt a2119c07028f 2008-10-27 mtnyogi $
.. 
.. Copyright © 2008 Bruce Frederiksen
.. 
.. Permission is hereby granted, free of charge, to any person obtaining a copy
.. of this software and associated documentation files (the "Software"), to deal
.. in the Software without restriction, including without limitation the rights
.. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
.. copies of the Software, and to permit persons to whom the Software is
.. furnished to do so, subject to the following conditions:
.. 
.. The above copyright notice and this permission notice shall be included in
.. all copies or substantial portions of the Software.
.. 
.. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
.. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
.. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
.. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
.. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
.. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
.. THE SOFTWARE.

restindex
    crumb: Pyke Syntax
    page-description:
        The syntax of Pyke's three different kinds of source files.
    /description
    section-pages: , kfb_syntax, krb_syntax/index, kqb_syntax
    format: rest
    encoding: utf8
    output-encoding: utf8
    include: yes
    initialheaderlevel: 2
/restindex

uservalues
    filedate: $Id: index.txt a2119c07028f 2008-10-27 mtnyogi $
/uservalues

===========
Pyke Syntax
===========

Source Files
============

Pyke has three different kinds of source files for the three main types of
`knowledge bases`_:

#. *Knowledge Fact Base* (KFB) files for `fact bases`_.
#. *Knowledge Rule Base* (KRB) files for `rule bases`_.
#. *Knowledge Question Base* (KQB) files for `question bases`_.

Each type of source file ends in a different file suffix: ``.kfb``,
``.krb`` or ``.kqb``.

Place all of these source files into a directory structure.  Then include this
directory as an argument to the `knowledge_engine.engine`_ constructor.
This will recursively search your directory for these three types of source
files, compile them, and load them into the engine.  How you organize these
files into subdirectories is up to you -- the directory structure does not
matter to Pyke.

The ``.kfb`` and ``.kqb`` files are compiled into Python pickles_ with
``.fbc`` and ``.qbc`` suffixes.

The ``.krb`` files are compiled into up to three ``.py`` source files.
The names of these ``.py`` files are the same as the ``.krb`` file, but with
different endings:

- ``_fc`` (if there are any forward-chaining_ rules)
- ``_bc`` (if there are any backward-chaining_ rules) and
- ``_plans`` (if any of the backward-chaining rules have a plan_)

These ``.py`` files are then automatically imported to define the rule base.
This causes Python to compile them into ``.pyc`` or ``.pyo`` files.

Subsequent runs of the `knowledge_engine.engine`_ constructor only recompile
the Pyke source files that have changed since the last time they were compiled.

The name of each knowledge base is the filename of the Pyke source file with
the suffix removed.  This must be a legal Python identifier.

Syntax Legend
==============

To describe this syntax, the following punctuation is used:

'*any_chars*'
   Required punctuation or keyword: *any_chars*.
*a* | *b*
   Alternation: *a* or *b*.
[*a*]
   Optional *a*.
{*a*}
   One or more *a*'s.  **But** it is understood that if *a* ends in a comma,
   the last comma is optional.
IDENTIFIER
   Any legal Python identifier.  Example: *foobar*
NUMBER
   Any legal Python integer or floating point literal.
   Examples: *123*, *3.14*.
STRING
   Any legal Python string literal.
   Examples: *'Hi Mom!'*, *u"Hi Dad!\\n"*, *r'''don't gobble my \\'s!'''*,
   *ur"""leave \\'s alone!"""*.
TEXT
   Only used in KQB files.  This signifies any text (any characters) other
   than the delimiter characters containing the ``TEXT``.
PARAMETRIZED_TEXT
   Only used in KQB files.  This signifies any text (any characters) through
   the end of the line and all text on subsequent lines that are indented at
   least as much as the first ``PARAMETRIZED_TEXT`` character on the first
   line.  All ``PARAMETRIZED_TEXT`` is treated as a `string.Template`_ and
   may include ``$IDENTIFIER`` or ``${IDENTIFIER}`` parameters.  All other
   ``$`` characters must be doubled (``$$``).
REGEXP_TEXT
   Only used in KQB files.  This signifies any text (any characters) excluding
   an unescaped backslash (``\``) at the end.  These are given to the Python's
   ``re`` module as regular expressions and must follow Python's
   `regular expression syntax`_.
NL
   One or more newlines.
INDENT
   The following text must be indented to a higher level (more) than the
   previous text.
DEINDENT
   The following text must be indented one less level than the previous text.

Lexical Structure
=======================

The lexical structure is much like Python.  Like Python, indenting is
significant.  It uses the same commenting, line continuation and literal
formats for strings and numbers (but doesn't use complex numbers).  It also
uses the same rules for forming identifiers.

The two notable exceptions to Python conventions are:

#. Identifiers may be used as strings, without requiring quotes.

   - So ``foobar`` is the same as ``'foobar'``.

#. Singleton tuples do not require a trailing comma.

   - So ``(1)`` is the same as ``(1,)``.

