\documentclass[11pt,letterpaper]{article}

% \usepackage{footmisc}

\usepackage{syntax}
\renewcommand\litleft{``\bgroup\ttfamily}
\renewcommand\litright{\egroup''}

\usepackage{geometry}
\geometry{margin=0.75in}

\usepackage{indentfirst}
\usepackage{amsmath}
\usepackage{float}
\floatstyle{boxed}
\restylefloat{figure}

\title{Format for GUI definition files}
% \date{}
\author{}

\begin{document}

\noindent
\begin{flushright}
  \normalsize \rm \today
\end{flushright}

\noindent
\begin{center}{
  \Large \sc Format For GUI Definition Files
}\end{center}

\pagestyle{empty}
\thispagestyle{empty}

\section{Lexical structure}

Whitespace is ignored (except in bare strings; see below). Both kinds of
C99/C++/Java comment (single-line ``\texttt{//}'' and multiline ``\texttt{/*
*/}'') are supported.

Apart from single-character punctuation marks, there are three different kinds
of tokens in the syntax:

\begin{description}

  \item[ID] An identifier. As usual, this must be a letter or underscore
    followed by letters, numbers, and/or underscores. (This is a subset of the
    Java identifiers; in particular, Java also allows dollar signs. We reserve
    those for constructing unique internal identifiers.)

  \item[STRING] A string, in double quotation marks. Recognized escapes:
    \begin{description}
      \item[\texttt{$\backslash$"}] A literal double quotation mark; does not
        end the string.
      \item[\texttt{$\backslash\backslash$}] A literal backslash; will not
        escape a quotation mark.
      \item[\texttt{$\backslash$}\synt{end-of-line}] Skips the end of the line
        and all leading whitespace in the next line. Allows for long lines to
        be continued neatly:

        \begin{verbatim}
        This is a very long string, which we can continue on the next line \
        and not even worry about indentation.
        \end{verbatim}
    \end{description}

    Any unrecognized escape sequence will be left alone (including the
    backslash) to allow the underlying language to process it.

  \item[BARESTRING] In certain circumstances, the quotes around a string are
    optional. A bare string always starts on a non-whitespace character, and
    continues up to the first \lit{,} or \lit{;}. Quotation marks are always
    necessary for any string that:
    \begin{itemize}
      \item contains any of \lit{,}, \lit{;} and \lit{"},
      \item spans more than one line, or
      \item begins with whitespace.
    \end{itemize}
\end{description}

\section{Grammar}

\setlength\grammarindent {8em}

\begin{figure}
\begin{grammar}
  <file> ::= <attr>$^*$ [<controller>] <statement>$^*$
  
  <statement> ::= <widget> $\mid$ <accum> $\mid$ <attr>

  <widget> ::= "ID" [<specifier>] <block>
    \alt "ID" <specifier> (`,' <specifier>)$^+$ <attrblock>

  <specifier> ::= <declarator> $\mid$ "STRING"

  <declarator> ::= "ID" ["STRING"]

  <block> ::= `;' $\mid$ `{' <statement>$^*$ `}'
  
  <accum> ::= <declarator> <block> 
    \alt <declarator> (`,' <declarator>)$^+$ <attrblock>

  <attrblock> ::= `;' $\mid$ `{' <attr>$^*$ `}'
  
  <attr> ::= "ID" `:' <attrvalue> (`,' <attrvalue>)$^*$ `;'

  <attrvalue> ::= "STRING" $\mid$ "BARESTRING"
  
  <controller> ::= `\%controller' `{' <handler>$^+$ `}'

  <handler> ::= "ID" "ID" (`,' "ID")$^*$ `;'

\end{grammar}
\caption{The full formal grammar, in extended Backus-Naur form.}
\end{figure}

\begin{enumerate}

\item \begin{grammar}
    <file> ::= <attr>$^*$ [<controller>] <statement>$^*$
  \end{grammar}

  A file is a series of statements. There can also be one controller block,
  which must appear before all widget statements.

\item \begin{grammar}
    <statement> ::= <widget> $\mid$ <accum> $\mid$ <attr>
  \end{grammar}

  There are three kinds of statement:

  \begin{description}
    \item[Widget statement] Defines one or more new widgets, either at the top
      level or as a child of another widget.
    \item[Accumulation statement] Adds more attributes to one or more existing
      widgets.
    \item[Attribute statement] Adds an attribute to a widget or widgets, or,
      at top level, declares an option or flag.
  \end{description}

\item \begin{grammar}
    <widget> ::= "ID" [<specifier>] <block>
      \alt "ID" <specifier> (`,' <specifier>)$^+$ <attrblock>

    <specifier> ::= <declarator> $\mid$ "STRING"

    <declarator> ::= "ID" ["STRING"]
  \end{grammar}

  A widget statement is in identifier for the widget type, followed by zero or
  more specifiers separated by commas, followed by a block. If more than one
  specifier is given, the block is an attribute block and can therefore contain
  no widget statements.
  
  Each specifier may be a string with no identifier or a declarator, which
  includes an identifier and optionally a string. The string, if present, will
  be used as the value of the \texttt{label} attribute, exactly as if it had
  been declared as such within the block.
  
  A widget statement with no declarators is an anonymous widget statement; it
  creates a widget with no name, which therefore cannot be accessed directly
  either within the definition file or in user code.

\item \begin{grammar}
    <block> ::= `;' $\mid$ `{' <statement>$^*$ `}'
    
    <attrblock> ::= `;' $\mid$ `{' <attr>$^*$ `}'
  \end{grammar}
  
  A block can be empty, in which case it is only a semicolon, or it's a series
  of statements within curly braces. (An empty pair of braces is equivalent to
  a bare semicolon.)

  An attribute block is a block containing only attribute statements.
  
\item \begin{grammar}
    <accum> ::= <declarator> <block> 
      \alt <declarator> (`,' <declarator>)$^+$ <attrblock>
  \end{grammar}

  \emph{\textbf{N.B.}} Every declarator in an accumulation statement
  \emph{must} have appeared earlier in the same block. This is the only way to
  distinguish an anonymous widget statement from an accumulation statement with
  only one declarator (without knowing what the valid widget types are called),
  so this restriction is actually part of the grammar, so that the grammar is
  not ambiguous. (This does mean the grammar is not context-free, but only in
  this limited instance; the parser is made only somewhat more complicated
  because of this.)
  
  An accumulation statement specifies additional attributes for the widget(s)
  with the given identifier(s). If only one declarator is given, additional
  child widgets can also be specified.
  
\item \begin{grammar}
    <attr> ::= "ID" `:' <attrvalue> (`,' <attrvalue>)$^*$ `;'

    <attrvalue> ::= "STRING" $\mid$ "BARESTRING"
  \end{grammar}

  Attribute statements have the form ``\texttt{attribute:\ value, value,
  value;}''. The attribute name is an identifier, the values are
  optionally-quoted strings, and the statement ends in a semicolon.

\item \begin{grammar}
    <controller> ::= `\%controller' `{' <handler>$^+$ `}'

    <handler> ::= "ID" "ID" (`,' "ID")$^*$ `;'
  \end{grammar}

  The event handlers are declared in the controller block, which begins with
  the keyword \lit*{\%controller}. Each handler statement is simply the name of
  the handler type (such as \texttt{Action} or \texttt{KeyPress}), followed by
  one or more comma-separated identifiers and a terminating semicolon.

\end{enumerate}
  
\end{document}
