Grammar
=======

The overall structure of a **pacc grammar** is very simple. First, there
is an optional *preamble* enclosed in braces: ``{}``. Then one or more
*rule*\s.

The **preamble** contains **C** source code, and is copied verbatim to
the beginning of **pacc**\'s output file. Typically it will comprise a
few ``#include`` statements.

Rules
-----

A **rule** consists of a *name*, an optional *type*, and a *definition*.
The first rule is the *start* rule of the grammar: valid utterances of the
language defined by the grammar match the start rule.

The **name** follows the same lexical rules as **C** identifier names:
it must start with an alphabetic character or underscore, and remaining
characters are alphabetic, numeric, or underscores. The namespace of
**pacc** rule names is entirely separate from any **C** program,
however, and there are no reserved words.

The **type**, if present, is introduced by a double colon, and can be any
**C** type name that is in scope, provided that name can be constructed
with identifiers and the ``*`` character. Thus, ``struct`` types are
supported, but function pointers are not. If the type is omitted,
the rule has the same type as the previous rule. (**Warning**: this means
that rearranging the rules in a grammar can change the meaning of the
grammar!) If the first rule has no explicit type, its type is ``int``.

The **definition** is introduced with a left arrow, which is followed by
one or more *matcher*\s, combined with *operator*\s.

Matchers
--------

**Matcher**\s are the fundamental building blocks of *definition*\s.

Literal: ``"string"``
  A string enclosed in double quotes matches exactly that string.

Character class: ``[a-z0-9]``
  A series of characters in square brackets matches any single one of
  those characters, which are written as in **C**, e.g. ``[\t\n]``
  matches a tab or a newline. A character range can be expressed with a
  hyphen, e.g. ``[a-z]`` matches any lower case letter. A literal hyphen
  may be matched by making it the first or last character in the class,
  e.g. ``[- _]`` matches a hyphen, a space, or an underscore. A literal
  right square bracket may be matched by preceding it with a backslash,
  e.g. ``[[\]]`` matches a left or right square bracket. (In the
  **pacc-0.0** release, character classes were not complete. Character
  escapes were buggy, and a “character” was in fact a byte, i.e. ASCII
  coding was assumed.  This is now fixed in the development version.)

Call: ``rule_name``
  A bare word names another *rule* (it is an error if there is no such
  rule defined in this **pacc** grammar). It matches anything that the
  named rule matches.

  
Any: ``.``
  The period character matches any single character. (UTF-8 coding is
  assumed.)

Operators
---------

**pacc** has the following operators, which are shown with their
precedence, in order from 5 to 0. In the descriptions that follow, ``e``
and ``f`` are arbitrary **pacc** expressions; ``c`` is an arbitrary
**C** expression.

Parentheses: ``(e)`` (5)
  Parentheses are used for grouping.

Repetition: ``e? e* e+`` (4)
  ``e?`` matches ``e`` optionally (zero or one occurrences); ``e*``
  matches zero or more repetitions of ``e``; and ``e+`` matches one or
  more repetitions of ``e``.

Guards: ``&e !e &{c}`` (3)
  ``&e`` requires that the input matches ``e`` at this point, but it
  does not consume any input. ``!e`` requires that the input does *not*
  match ``e``, and again does not consume anything. ``&{ ... }`` is a
  *semantic guard*: the braces hold an arbitrary **C** expression of
  type ``int``. The expression is evaluated; the guard succeeds if its
  value is non-zero.

Binding: ``name:e`` (3)
  Matches whatever ``e`` matches, and binds the *value* of ``e`` to
  ``name``, which is then in scope till the end of this *alternative*,
  for use in *semantic guards* and *expressions*. See the `next
  chapter`_ for more details.

Bound literal: ``"string":e`` (3)
  Matches if ``e`` matches and the *value* of ``e`` (which must be of
  type ``char *``) is equal to ``string``.

Sequencing:  ``e f`` (2)
  Matches ``e`` followed by ``f``.

Empty: ``ε`` (2)
  An empty sequence. Epsilon ``ε`` (U+03B5) represents an empty
  sequence; it can always be omitted, but its use can improve
  readability.

Value: ``→ {c}`` (1)
  Defines the *value* of a *sequence*; ``c`` is an arbitrary **C**
  expression which must have the same type as the *rule*. If ``c`` is a
  name (**C** identifier rules) or a decimal integer, the braces are
  optional.  Otherwise, the arrow is optional. (In **pacc-0.0**, the
  braces are only optional when ``c`` is an identifier.)

Alternation: ``e / f`` (0)
  If ``e`` matches, the alternation matches; otherwise, if ``f``
  matches, the alternation matches. Priority is important: ``e`` is
  always tried before ``f``. 

.. _next chapter: 03
