% ltx2x.tex             Description of ltx2x program
\documentclass[11pt]{article}
\usepackage{url}
\usepackage{ltx2html}
\setlength{\textheight}{8.0in}
\setlength{\textwidth}{6.0in}
\setlength{\oddsidemargin}{0.25in}
\setlength{\evensidemargin}{0.25in}
\setlength{\marginparwidth}{0.6in}
\setcounter{secnumdepth}{4}
\setcounter{tocdepth}{4}
% define these to save some typing
\newcommand{\lx}{{\small\sl LTX2X}}
\newcommand{\ctab}{command table}
\newcommand{\Express}{{\sc Express}}
\newcommand{\ExpressA}{{\sc Express-A}}
\newcommand{\ExpressG}{{\sc Express-G}}
\newcommand{\ExpressI}{{\sc Express-I}}
\newenvironment{code}{\begin{small}}{\end{small}}
\newcommand{\keyword}[1]{\begin{small}\texttt{#1}\end{small}}
\newcommand{\keytext}[1]{\begin{small}\texttt{#1}\end{small}}
\newcommand{\file}[1]{\texttt{#1}}
\mltitle{LaTeX to X translator}
\begin{document}
\title{ \textsl{LTX2X:} A \protect\LaTeX\ to X Auto-tagger}
\author{Peter R. Wilson \\
        Catholic University of America\thanks{This work was performed
while a Guest Researcher at the National Institute of Standards and
Technology} \\
        Email: \texttt{pwilson@cme.nist.gov} }
\date{January 1997}

\bibliographystyle{unsrt}
\pagenumbering{roman}
\maketitle

\begin{abstract}
    \lx{} is a table-driven program that will replace \LaTeX\ commands
by user defined text. This report describes the beta version of the system.
\lx{} supports both a declaritive command style and an interpreted
procedural language tentatively called \ExpressA.
Details are given of the program functionality including examples.
System installation instructions are provided.
\end{abstract}

\tableofcontents
\listoftables

\clearpage
\pagenumbering{arabic}

\section{Introduction}  \label{sec:introduction}

    \LaTeX~\cite{LAMPORT94}, which is built on top of \TeX~\cite{KNUTH84a},
is a document tagging system
that is very popular in the academic and scientific publishing communities
because of the high quality typeset material that the system outputs
for normal text and especially for mathematics.

    In particular, many of the documents forming the 
International Standard ISO~ 10303, commonly referred to as STEP~\cite{STEPIS},
have been written using \LaTeX\ as
the document tagging language. Lately there have been moves towards converting
the STEP documents to embody SGML~\cite{GOLDFARB90}
rather than \LaTeX\ markup. This has led
to an interest in the automatic conversion from \LaTeX\ to SGML
documents.
The \lx\ system is an initial attempt to provide a generic capability for 
converting \LaTeX\ tags into other kinds of tags.

    The \lx\ system described below is in a beta release state. That is, there
is probably some more work to be done on it but experience from use is
needed to determine desirable additional functionality. However, the code
has been stable for some time.
Bug reports or suggested 
enhancements (especially if the suggestions are accompanied by working code)
are encouraged, as are constructive comments about this document.

    Essentially, \lx{} reads a file containing \LaTeX{} markup, 
replaces the \LaTeX{} commands by user-defined text, and writes the result 
out to another file. The program operates from a command table that specifies
the replacement text. In general, no programming knowledge or skills are
required to write a command table, which \lx{} will then interpret.
Some knowledge of \LaTeX{} is required, but no more than is necessary
for authoring a \LaTeX{} document.

    \lx{} has proved capable of performing such functions as:
\begin{itemize}
\item Conversion of documents marked up according to a specific \LaTeX{} 
      documentclass to documents tagged according to a specific SGML DTD.
\item Removal of \LaTeX{} commands to produce deTeXed source.
\item Conversion of simple \LaTeX{} documents to
  HTML~\cite{MUSCIANO96} tagged documents for publication on the 
  World Wide Web.
\end{itemize}

    The remainder of this introduction gives an overview of the \lx{} program.
The command table is described in more detail in section~\ref{sec:command-table}
and information on running the \lx{} program is provided in 
section~\ref{sec:program}. Section~\ref{sec:expressa} gives an
overview
of the \ExpressA{} language.\footnote{The overview is necessarily
rather brief as I am shortly moving to a new place of employment and
\ExpressA{} is the latest addition to the system.} 
Although the functionality available through the command table facility is 
suitable for many tasks, especially since an interpreter for the \ExpressA{} 
general programming language is included within \lx, 
section~\ref{sec:special} gives details on how the system can
be extended for cases where this proves to be inadequate.

    The report ends with several appendices. An example command table for
deTeXing a document is reproduced in~\ref{sec:detexing} and some of
the issues in converting from \LaTeX{} to HTML are discussed 
in~\ref{sec:htmling}. The known limitations
of \lx{} are listed in~\ref{sec:limitations} and a summary of the command
table facilities are given in~\ref{sec:summary}. Appendix~\ref{sec:install}
provides instructions on installing the \lx{} program, together with copyright
and warranty information. Finally, \ref{sec:ctabgrammar} and \ref{sec:expgrammar}provide grammars for the \ctab and \ExpressA, respectively.

\subsection{Overview}

    The intent of Leslie Lamport, the author of \LaTeX, was to provide a document
tagging system that enabled the capture of the logical structure of a document.
This system uses Donald Knuth's \TeX\ system as its typesetting engine~\cite{KNUTH84a},
and thus has an inherent capability for high quality typesetting.

    All \LaTeX\ commands are distinguished by starting with a backslash (\verb|\|).
Generally speaking, the name of a command is a string of alphabetic characters 
(e.g. \verb|\acommand|). Commands may take arguments. Required arguments are
enclosed in curly braces (i.e. \verb|{| and \verb|}|). Optional arguments
are enclosed in square brackets (i.e. \verb|[| and \verb|]|). The general syntax
for a command is the command name (preceded by a backslash) followed by the
argument list with a maximum\footnote{Under very unusual
circumstances this limit may be exceeded.} of nine arguments.

    The \lx\ program reads a \LaTeX\ document file and outputs a
transformation of this file. By default it outputs the normal text
while for each \LaTeX{} command and argument performs some
user-specified actions; typically these actions involve the output
of specific text corresponding to the particular command.
The actions are specified in a \emph{\ctab} file, written by the user, 
which is read into the \lx\ system before document processing is
begun. A \ctab\ consists of a listing of the \LaTeX\ commands of interest
together with the desired actiond for each of these commands and their
arguments.
Different effects may be easily obtained by changing the \ctab\
file. For example, a simple \ctab{} file may be written that will delete
all the \LaTeX\ commands from a document, resulting in a plain ASCII file
with no embedded markup.\footnote{To afficionados, 
this process is known as de-\TeX ing.} 
A more complex command table may be written that will replace \LaTeX\ tags 
with appropriate SGML tags.

    In some circles it is traditional to introduce a programming
language by providing an example program that prints `Hello world'.
In contrast, the following \ctab{} file called \file{bye.ct}, 
when used in conjunction
with a typical vanilla \LaTeX{} file, will transform the \LaTeX{} file
to a file that consists only of the words `Goodbye document'.

\begin{code}
\begin{verbatim}
C=        bye.ct   "Goodbye document" for ltx2x

TYPE= COMMAND
NAME= \documentclass
  START_TAG= "Goodbye document"
  PC_AT_END= NO_PRINT
END_TYPE
  
C= just in case a LaTeX v2.09 document
TYPE= COMMAND
NAME= \documentstyle
  START_TAG= "Goodbye document"
  PC_AT_END= NO_PRINT
END_TYPE
  
C= just in case there is no \documentclass/style command
TYPE= BEGIN_DOCUMENT
  START_TAG= "Goodbye document"
  PC_AT_END= NO_PRINT
END_TYPE

TYPE= OTHER_COMMAND
  PRINT_CONTROL= NO_PRINT
END_TYPE

TYPE= OTHER_BEGIN
  PRINT_CONTROL= NO_PRINT
END_TYPE

TYPE= OTHER_END
  PRINT_CONTROL= NO_PRINT
END_TYPE

END_CTFILE=  end of bye.ct
\end{verbatim}
\end{code}

    Essentially the \ctab{} instructs \lx{} what to print for each \LaTeX{}
command. A \ctab{} file consists of a series of commands, one per line
and introduced by a keyword such as \keyword{TYPE=}. Keywords are case
insensitive but by convention are written in upper case. Comments in
a \ctab{} are introduced by the keyword \keyword{C=}.

    The main body of a \ctab{} consists of the specification of
\LaTeX{} commands of interest and the actions to be taken for these.
Each specification commences with the keyword \keyword{TYPE=} and is
completed by the keyword \keyword{END\_TYPE}, the relevant actions
being listed between these two keywords.

    \lx{} treats some \LaTeX{} commands specially; among these are
\verb|\begin{document}| and \verb|\end{document}|. In a \ctab{}
these are specified by the types \keyword{TYPE= BEGIN\_DOCUMENT} and
\keyword{TYPE= END\_DOCUMENT}. The actions at
\verb|\begin{document}| are firstly to print the string `Goodbye document'
(specified in the line \keyword{START\_TAG= "Goodbye document"}) and
secondly to stop printing any output (specified in the line 
\keyword{PC\_AT\_END= NO\_PRINT}).

    By not specifying the \keyword{END\_DOCUMENT} entry, the default
action is used for the \verb|\end{document}| command.

    The \ctab{} entries for the commands \verb|\documentclass| and
\verb|\documentstyle| specify that, if either of these is in the
source document, then it is to be replaced by the text string
\keytext{"Goodbye document"}, and then all further printing is to
be switched off.

    The other three entries in the \ctab{} specify the actions for any
other kind of \LaTeX{} command. The keyword \keyword{OTHER\_BEGIN}
signifies a \LaTeX{} command of the form \verb|\begin{name}| and
\keyword{OTHER\_END} signifies a command of the form \verb|\end{name}|.
The keyword \keyword{OTHER\_COMMAND} signifies any other kind of
\LaTeX{} command (e.g., \verb|\acommand ... |). The actions declared
for these are all \keyword{PRINT\_CONTROL= NO\_PRINT} which shuts off
any printing of the command or its arguments. In the \ctab{} \file{bye.ct}
these are only included to prevent printing before the
\verb|\begin{document}|.

    To run \lx{} with the above \ctab, type the following (where \verb|>| 
is assumed to be the system prompt):
\begin{verbatim}
> ltx2x -f bye.ct input.tex output.tex
\end{verbatim}
where \file{bye.ct} is the name of the \ctab, and 
\file{input.tex} and \file{output.tex} are the names of the input \LaTeX{}
file and the resulting processed file respectively.


    As an example of a more useful \ctab\ file, the following one called
\file{decomm.ct} will remove all \LaTeX\ comments from a typical
\LaTeX{} source file.

\begin{code}
\begin{verbatim}
C=  decomm.ct  Command table file for ltx2x to de-comment LaTeX source

C= ------------------------------------ set newline characters
ESCAPE_CHAR= ?
NEWLINE_CHAR= N

C=   ----------------------------------- built in commands
TYPE= BEGIN_DOCUMENT
  START_TAG= "\begin{document}"
END_TYPE

TYPE= END_DOCUMENT
  START_TAG= "\end{document}"
END_TYPE

TYPE= BEGIN_VERB
  START_TAG= "\verb|"
END_TYPE

TYPE= END_VERB
  START_TAG= "|"
END_TYPE

TYPE= BEGIN_VERBATIM
  START_TAG= "\begin{verbatim}"
END_TYPE
\end{verbatim}
\verb|TYPE= END_VERBATIM| \\
\verb|  START_TAG= "\end{verbatim}"| \\
\verb|END_TYPE|
\begin{verbatim}
TYPE= LBRACE
  START_TAG= "{"
END_TYPE

TYPE= RBRACE
  START_TAG= "}"
END_TYPE

TYPE= PARAGRAPH
  START_TAG= "?N?N    "
END_TYPE

C= ------------------- define '\item' tags within lists

TYPE= BEGIN_LIST_ENV
NAME= itemize
  START_TAG= "\begin{itemize}"
  START_ITEM= "\item "
END_TYPE

TYPE= BEGIN_LIST_ENV
NAME= enumerate
  START_TAG= "\begin{enumerate}"
  START_ITEM= "\item "
END_TYPE

TYPE= BEGIN_LIST_ENV
NAME= description
  START_TAG= "\begin{description}"
  START_ITEM= "\item"
  START_ITEM_PARAM= "["
  END_ITEM_PARAM= "] "
END_TYPE

TYPE= END_LIST_ENV
  NAME= itemize
END_TYPE

TYPE= END_LIST_ENV
  NAME= enumerate
END_TYPE

TYPE= END_LIST_ENV
  NAME= description
END_TYPE

C=    --------------------- pass through all other LaTeX commands

TYPE= OTHER_COMMAND
END_TYPE

TYPE= OTHER_BEGIN
END_TYPE

TYPE= OTHER_END
END_TYPE

END_CTFILE= end of file decomm.ct
\end{verbatim}
\end{code} % end small

    In the above \ctab\ file, the first pair of commands (\keyword{ESCAPE\_CHAR=}
and \keyword{NEWLINE\_CHAR=}) define the character pair that are to be used to
signify a `newline' within a tag. An example of their use is later in the
file in the \keyword{PARAGRAPH} command type.

    As indicated above, \lx\ treats some \LaTeX\ commands specially. 
These are listed next in the
\ctab. The special \LaTeX{} commands are the begin and end of the 
\verb|document| and \verb|verbatim|
environments, together with the \verb|\verb| command, left and right braces,
the \verb*|\ | command, and the \lx{} \keyword{PARAGRAPH} specification.
There are default actions for these, but apart from the \verb*|\ | command
the defaults are not appropriate in this case. 
Above, the actions are to replace the
\LaTeX{} command by the string forming the \LaTeX{} command. The exception
is that paragraphs (the \keyword{PARAGRAPH} specification) should start 
with at least one blank line and be indented some spaces.

    The \LaTeX\ \verb|\item| command is used within lists. \lx\ has to be
told how to treat the \verb|\item| command within each kind of list. This
has been done above for the \verb|itemize|, \verb|enumerate| and 
\verb|description| environments.

    The final instructions in the \ctab\ file tell \lx\ to pass through the
text of all other commands and their arguments. The end of the \ctab\ file
is either the physical end of the file or the command \keyword{END\_CTFILE=},
whichever comes first. The \keyword{END\_CTFILE=} command acts like the
\keyword{C=} command in that arbitrary text can be put after the command.

    To use the \file{decomm.ct} \ctab{} to de-comment a \LaTeX\ file, type 
the following (where \verb|>| is assumed to be the system prompt):
\begin{verbatim}
> ltx2x -f decomm.ct input.tex output.tex
\end{verbatim}
where \file{input.tex} and \file{output.tex} are the names of the input \LaTeX\
file for de-commenting and the resulting de-commented version respectively.

\section{The \ctab\ file} \label{sec:command-table}

    By default, \lx\ does not output any \LaTeX\ comments. 
Otherwise, whenever it comes
across a \LaTeX\ command it looks at the data in the \ctab\ file to determine
what actions it should take. The two most typical actions are either
to print out the command as read in, or to replace the command by some (possibly
empty) text.

    Each line in a \ctab\ file is either blank or starts with a keyword followed
by one or more blanks. For
example, a comment in the file is a line that starts with \keyword{C= };
the remainder of the line is any comment text. Comments may be placed anywhere
in the file.


\subsection{Special print characters in tags}

    \lx\ is written in C~\cite{KERNIGHAN88}. 
The C language enables certain non-printing characters
to be defined. These are typically written in the form \verb|\c| where \verb|\|
is the C escape character and \verb|c|
is a particular character. \lx\ understands some of these special printing
characters and the \ctab\ enables these to be given non-default values.

    The default escape character (\verb|\|) may be redefined via the 
\keyword{ESCAPE\_CHAR=} command. For example,
\begin{code}
\begin{verbatim}
ESCAPE_CHAR= ?
\end{verbatim}
\end{code}
will make the question mark character the escape character. Typically, the escape 
character is changed in most \ctab s to avoid clashing with the \LaTeX{} \verb|\|
character.
The following commands
can be used to redefine the C special characters. Each of these commands takes
a single character as its value. If a relevant command is not given, then the
default value is used. 
\begin{description}
\item[\keyword{NEWLINE\_CHAR=}] a new line (default is \verb|n|) \\
\item[\keyword{HORIZONTAL\_TAB\_CHAR=}] horizontal tab  (default is \verb|t|) \\
\item[\keyword{VERTICAL\_TAB\_CHAR=}] vertical tab (default is \verb|v|) \\
\item[\keyword{BACKSPACE\_CHAR=}] backspace (default is \verb|b|) \\
\item[\keyword{CARRIAGE\_RETURN\_CHAR=}] carriage return (default is \verb|r|) \\
\item[\keyword{FORMFEED\_CHAR=}] formfeed (default is \verb|f|) \\
\item[\keyword{AUDIBLE\_ALLERT\_CHAR=}] beep the terminal (default is \verb|a|) \\
\item[\keyword{HEX\_CHAR=}] following characters form the hexadecimal number of 
      the character
                  to be printed (default is \verb|x|) (e.g. \verb|?xA3|) \\
\end{description}
    These command lines are all optional within a \ctab\ and their ordering is
immaterial. However, if any are present then they must be at the beginning of 
the \ctab.

    The above special characters are useful when specifying the replacement text
for \LaTeX\ commands.


\subsection{\protect\LaTeX\ command types}

    The commands for controlling the actions performed on \LaTeX\ commands
are enclosed between the command lines \keyword{TYPE= } and \keyword{END\_TYPE}, 
as below.
\begin{code}
\begin{verbatim}
TYPE= CommandType
  C= a possibly empty set of commands
END_TYPE
\end{verbatim}
\end{code}
where \keyword{CommandType} is an \lx\ keyword signifying the kind of \LaTeX\ 
command being specified.

\subsubsection{Built in command types}

    Some \LaTeX\ commands are pre-defined within \lx. Default actions
are provided for these but it is recommended that type specifications
for each of these commands be put in the \ctab{} anyway. The keywords for
these commands are:
\begin{description}
\item[\keyword{BEGIN\_DOCUMENT}] Corresponds to the \LaTeX\ command 
      \verb|\begin{document}|.
\item[\keyword{END\_DOCUMENT}] Corresponds to the \LaTeX\ command
      \verb|\end{document}|.
\item[\keyword{BEGIN\_VERBATIM}] Corresponds to the \LaTeX\ commands
      \verb|\begin{verbatim}| and \\
      \verb|\begin{verbatim*}|.
\item[\keyword{END\_VERBATIM}] Corresponds to the \LaTeX\ commands
      \verb|\end{verbatim}| and \verb|\end{verbatim*}|.
\item[\keyword{BEGIN\_VERB}] Corresponds to the \LaTeX\ commands
      \verb|\verb| and \verb|\verb*|, together with the succeeding character.
\item[\keyword{END\_VERB}] Corresponds to the appearance of the character
      that completes the \LaTeX\ commands
      \verb|\verb| and \verb|\verb*|.
\item[\keyword{LBRACE}] Corresponds to the \LaTeX\ left brace character
      \verb|{|.
\item[\keyword{RBRACE}] Corresponds to the \LaTeX\ right brace character
      \verb|}|.
\item[\keyword{BEGIN\_DOLLAR}] Corresponds to the \LaTeX\ \verb|$| symbol signalling the
   start of an in-text math formula.
\item[\keyword{END\_DOLLAR}] Corresponds to the \LaTeX\ \verb|$| symbol signalling the
   end of an in-text math formula.
\item[\keyword{PARAGRAPH}] Corresponds to the \LaTeX\ protocol of a blank
     line signalling the start/end of a paragraph.
\item[\keyword{SLASH\_SPACE}] Corresponds to the \LaTeX\ \verb*|\ | command.
\item[\keyword{OTHER\_COMMAND}] Corresponds to any \LaTeX\ command of the form
      \verb|\command| not specified elsewhere within the \ctab.
\item[\keyword{OTHER\_BEGIN}] Corresponds to any \LaTeX\ command of the form
      \verb|\begin{environment}| not specified elsewhere within the \ctab.
\item[\keyword{OTHER\_END}] Corresponds to any \LaTeX\ command of the form
      \verb|\end{environment}| not specified elsewhere within the \ctab.
\end{description}


    The ordering of these built in type specifications is immaterial. If
any of the above are not specified within the \ctab{} then \lx{} will use
their default action.
    With the exception of the \keyword{SLASH\_SPACE} command type, the
default action is to do nothing (i.e., produce no output). 
The default action for the \keyword{SLASH\_SPACE}
command type is to output a space.

\subsubsection{Optional command types}

    For the purposes of \lx, \LaTeX\ commands are divided into various classes.
The keywords for these clases, and the class descriptions, are listed below.

\begin{description}
\item[\keyword{TEX\_CHAR}] Corresponding to \LaTeX's special characters (with the exception
     of the \$, \{\ and \}\ characters).
\item[\keyword{CHAR\_COMMAND}] Corresponding to \LaTeX\ commands of the type
     \verb|\c| where \verb|c| is a single non-alphabetic character.
\item[\keyword{COMMAND}] Corresponding to \LaTeX\ commands of the type
     \verb|\command|, where \verb|command| is the name of the command (except for
      \verb|\begin|, \verb|\end| and \verb|\item|).
\item[\keyword{BEGIN\_ENV}] Corresponding to \LaTeX\ commands of the type
     \verb|\begin{environment}| where \verb|environment| is the name of the
      environment, except for those list environments whose bodies
      consist of \verb|\item| commands.
\item[\keyword{END\_ENV}] Corresponding to \LaTeX\ commands of the type
     \verb|\end{environment}|, with the same restrictions as for \verb|BEGIN_ENV|.
\item[\keyword{BEGIN\_LIST\_ENV}] Corresponding to \LaTeX\ commands of the type
     \verb|\begin{environment}| where \verb|environment| is the name of an
     environment whose body consists of \verb|\item| commands.
\item[\keyword{END\_LIST\_ENV}] Corresponding to \LaTeX\ commands of the type
     \verb|\end{environment}| to match \verb|BEGIN_LIST_ENV|.
\item[\keyword{VCOMMAND}] Corresponding to a \LaTeX{} \verb|\verb|-like command.
\item[\keyword{BEGIN\_VENV}] Corresponding to the start of a verbatim-like
     environment.
\item[\keyword{END\_VENV}] Corresponding to the end of a verbatim-like
     environment.
\item[\keyword{SECTIONING}] Corresponding to \LaTeX\ commands of the type
     \verb|\command|, where \verb|command| is a document sectioning command
      such as \verb|chapter| or \verb|subsection|.
\item[\keyword{SPECIAL}] Reserved for possible future use.
\item[\keyword{SPECIAL\_COMMAND}] Corresponding to the \verb|COMMAND| keyword,
     except that some special output processing is to be defined.
\item[\keyword{SPECIAL\_BEGIN\_ENV}] Corresponding to the \verb|BEGIN_ENV| keyword,
     except that some special output processing is to be defined.
\item[\keyword{SPECIAL\_END\_ENV}] Corresponding to the \verb|END_ENV| keyword, 
     except that some special output processing is to be defined.
\item[\keyword{SPECIAL\_BEGIN\_LIST}] Corresponding to the \verb|BEGIN_LIST_ENV| 
     keyword, 
     except that some special output processing is to be defined.
\item[\keyword{SPECIAL\_END\_LIST}] Corresponding to the \verb|END_LIST_ENV| keyword,
     except that some special output processing is to be defined.
\item[\keyword{SPECIAL\_SECTIONING}] Corresponding to the \verb|SECTIONING| keyword,
     except that some special output processing is to be defined.
\item[\keyword{\_PICTURE\_}] Corresponding to some of the \LaTeX\ picture
     drawing commands.
\item[\keyword{COMMAND\_...}] Corresponding to some of the \LaTeX{} commands
     whose arrangements of required and optional arguments are untypical.
\end{description}

    The ordering of these types within a \ctab\ is immaterial.

    Each of the above type specifications requires a \keyword{NAME=} command, 
whose value is the
name of the relevant command or environment being specified. For example, the
following is a (partial) specification of the figure environment and the caption
command.

\begin{code}
\begin{verbatim}
TYPE= BEGIN_ENV
NAME= figure
END_TYPE

TYPE= END_ENV
NAME= figure
END_TYPE

TYPE= COMMAND
NAME= \caption
END_TYPE
\end{verbatim}
\end{code}

\subsection{Command action tags}

    When \lx\ reads a \LaTeX\ command it performs the following actions:
\begin{enumerate}
\item Looks up the name of the command or environment in the \ctab. If
      it is not found, then the appropriate default type is used.
\item Sets the printing mode according to the \keyword{PC\_AT\_START=}
      command.
\item Performs the actions specified in the \ctab\ by the 
      \keyword{START\_TAG=} command.
\item Processes any specified arguments to the command.
\item Performs the actions specified in the \ctab\ by the \keyword{END\_TAG=}
      command.
\item Sets the printing mode according to the \keyword{PC\_AT\_END=} command.
\end{enumerate}

\begin{description}
\item[NOTES]:
\begin{enumerate}
\item Except for the default processing of \keyword{OTHER\_} types, it does not
      output the command itself.
\item If a tag action is not specified, then the default action is null (e.g.,
      nothing will appear in the output).
\end{enumerate}
\end{description}

    Within a \ctab\ all text strings for output are enclosed within double 
quotes. For example:
\begin{code}
\begin{verbatim}
START_TAG=     "Some "text" string\n another line of text."
\end{verbatim}
\end{code}

    Assuming that \verb|\n| means a newline, when this string action is performed
by \lx\ it will appear in the output file as:
\begin{code}
\begin{verbatim}
Some "text" string
another line of text.
\end{verbatim}
\end{code}

    A text string starts with the first double quote and ends with the last double
quote on the command line. A text string has to be written on a single line
within the \ctab. C language special print characters can be embedded within the
text string (e.g. the \verb|\n| for a newline in the above example). Remember
that the first section of the \ctab\ is used for specifying the 
particular \ctab\ version of these.

    If a text string is too long to fit comfortably on a single line in the
\ctab, it may be continued via the \keyword{STRING:} command. As many of these
can be used in succession as required (subject to internal limitations within \lx).

    For instance,
\begin{code}
\begin{verbatim}
START_TAG=     "Some "text" string\n"
  STRING: "another line of text."
\end{verbatim}
\end{code}
has the same effect as the previous example.

    The following specification is designed to write out the contents of the
\verb|\caption| command\footnote{Strictly speaking, the specification does not
do this exactly, but this simplified illustration will be corrected in 
the next sections.}, 
preceded by the word `CAPTION' and followed by at least one
blank line (assuming that the escape character has been set to \verb|?|). 

\begin{code}
\begin{verbatim}
TYPE= COMMAND
NAME= \caption
  START_TAG= "?n      CAPTION "
  END_TAG= "?n?n"
END_TYPE
\end{verbatim}
\end{code}
Assuming that somewhere in a \LaTeX\ file there is the command
\begin{code}
\begin{verbatim}
stuff
\caption{This is a caption.}
more stuff
\end{verbatim}
\end{code}
then the expected effect (see footnote) is
\begin{code}
\begin{verbatim}
stuff

    CAPTION This is a caption.

more stuff
\end{verbatim}
\end{code}

\subsection{Argument actions}

    \LaTeX\ commands can take arguments. The text for a required argument is
enclosed in curly braces, while the text for an optional argument is enclosed
in square brackets. \lx\ can be directed to perform actions at the start and end
of each argument. 

    The number of required arguments is specified by the command line 
\keyword{REQPARAMS=}
where the value of the command is a digit between 1 and 9 inclusive.

    \lx\ assumes that a command can have only one optional argument, and that this
is either first or last in the argument list. The potential presence of an
optional argument is indicated by the command line \keyword{OPT\_PARAM=}, 
where the
value is either the keyword \keyword{FIRST} (for first in the list) or 
\keyword{LAST} (for last in the list).

    The actions to be performed at the start and end of each required argument
are specified via the commands \keyword{START\_TAG\_1=} and \keyword{END\_TAG\_1=} 
for the first required argument, through \keyword{START\_TAG\_9=} and 
\keyword{END\_TAG\_9=} for the ninth argument.
The actions to be performed at the start and end of the optional argument
are specified by the command lines \keyword{START\_OPT=} and \keyword{END\_OPT=}.

    The argument delimiters (the braces or brackets) are not printed.

    In the simplest case, the action is to print a specified text string 
(enclosed in double quotes, and continued with \keyword{STRING:} commands 
if necessary). Other kinds of actions are also possible.
An unspecified tag defaults to doing no action.

\subsubsection{Print options}

\paragraph{Argument processing}

    By default, \lx\ processes (i.e. outputs as appropriate) 
the text of a argument.
Printing of the argument text may be disabled, if required. The command line
that controls argument printing is of the form \keyword{PRINT\_P1=} through
\keyword{PRINT\_P9=} for required arguments and 
\keyword{PRINT\_OPT=} for the optional
argument. The value of these commands is one from several keywords,
the most common being \keyword{NO\_PRINT}; this
switches off printing of the text of the indicated argument.
Default printing is resumed after the indicated argument.

    Continuing the caption example from earlier, we can now complete it. The full
syntax of the \LaTeX\ command is:
\begin{code}
\begin{verbatim}
\caption[optional table of contents entry]{Caption in the text}
\end{verbatim}
\end{code}
That is, it has one required argument, which prints the caption text both in
the body of the document and in the table of contents, unless the first optional
argument is present, in which case its value gets printed in the table of 
contents instead.

    Assume that an instance of the caption command in a document is:
\begin{code}
\begin{verbatim}
Some stuff
\caption[Short caption]{Long caption for the body of the text.}
More stuff
\end{verbatim}
\end{code}
Recall the previous \ctab\ caption specification. The actual output from 
processing this would be
\begin{code}
\begin{verbatim}
Some stuff

    CAPTION [Short caption]{Long caption for the body of the text.}

More stuff
\end{verbatim}
\end{code}
because, unless \lx\ is told that there are command arguments and how they should
be treated, it will just print them out together with their surrounding 
delimiters.

    The following \ctab\ entry will give more acceptable results.
\begin{code}
\begin{verbatim}
TYPE= COMMAND
NAME= \caption
  START_TAG= "?n      CAPTION "
  END_TAG= "?n?n"
  OPT_PARAM= FIRST
  PRINT_OPT= NO_PRINT
  REQPARAMS= 1
END_TYPE
\end{verbatim}
\end{code}

    For the above captioning instance, the output will now be:
\begin{code}
\begin{verbatim}
Some stuff

    CAPTION Long caption for the body of the text.

More stuff
\end{verbatim}
\end{code}

    The default print mode is to print text to the output file.

    The keywords that can be used to control argument printing are:
\begin{description}
\item[\keyword{NO\_PRINT}] Do not print anything.
\item[\keyword{TO\_SYSBUF}] Print to the \lx{} system buffer.
\item[\keyword{TO\_BUFFER num}] Print to the \lx{} buffer 
      number \keyword{num}.
\item[\keyword{TO\_FILE name}] Print to the file called \keyword{name}.
\item[\keyword{NO\_OP}] Skip all processing of the argument.
\end{description}
Note that even if the print mode is set to \keyword{NO\_PRINT}, the
argument text will still be processed. Only the \keyword{NO\_OP}
specification temporarly turns off the processing.


\paragraph{General printing}

    Just as the printing mode can be set for each argument, it can also be
set at the start and end of processing a \LaTeX{} command or environment.

    The specifications \keyword{PC\_AT\_START=} and \keyword{PC\_AT\_END=}
can be used to set the printing mode at the start of processing a command
and at the end, respectively. The keywords that can be used in these
specifications are:
\begin{description}
\item[\keyword{NO\_PRINT}] Do not print anything.
\item[\keyword{TO\_SYSBUF}] Print to the \lx{} system buffer.
\item[\keyword{TO\_BUFFER num}] Print to the \lx{} buffer 
      number \keyword{num}.
\item[\keyword{TO\_FILE name}] Print to the file called \keyword{name}.
\item[\keyword{RESET}] Reset the print mode back to what it was.
\end{description}

    Unlike the argument printing controls, the print mode is not
automatically reset. This has to be explicitly specified.

    As an example, assume that it is required to remove all \keytext{figure}
environments from a \LaTeX{} source and put them into a file on their own.
The following \ctab{} code could be used to accomplish this.
\begin{code}
\begin{verbatim}
TYPE= BEGIN_ENV
NAME= figure
  PC_AT_START= TO_FILE allfigs.tex
  START_TAG= "?n\begin{figure}"
END_TYPE

TYPE= END_ENV
NAME= figure
  START_TAG= "\end{figure}"
  PC_AT_END= RESET
END_TYPE
\end{verbatim}
\end{code}
When a \LaTeX{} \keytext{figure} environment is started, printing is switched
to go to the file called \file{allfigs.tex}. At the end of the \keytext{figure}
environment, the print mode is reset back to what it was before the environment
began. If at the first \keytext{figure} environment the \file{allfigs.tex} 
file did not exist, then \lx{} would create it automatically.

\paragraph{Read actions}

    As noted above, one of the actions that can be specified for a
\LaTeX{} comand's argument is to set the print mode for printing to a buffer
or a file. Similarly there are actions which will read from a buffer or
a file and print the contents. Within an argument tag these kinds of actions are
specified via the keyword \keyword{SOURCE:}. This can take one of several
values:
\begin{description}
\item[\keyword{SYSBUF}] Print the contents of the \lx{} system buffer.
\item[\keyword{BUFFER num}] Print the contents of the \lx{} buffer 
      number \keyword{num}.
\item[\keyword{FILE name}] Print the contents of the file called 
   \keyword{name}.
\end{description}

    In a previous example, the \LaTeX{} \keytext{figure} environments
were all written to the file \file{allfig.tex}. This file could be read
in again just before the end of the document so that all figures will be
typeset after everything else.
\begin{code}
\begin{verbatim}
TYPE= END_DOCUMENT
  END_TAG= "?n %  figures collected here by LTX2X ?n"
    SOURCE: FILE allfigs.tex
    STRING: "?n\end{document}?n"
END_TYPE
\end{verbatim}
\end{code}

    As another example of the use of the print actions consider the \LaTeX{}
\verb|\maketitle| command. This typesets the arguments of the \verb|\title|,
\verb|\author| and \verb|\date| commands, which must have been previously
specified but not necessarily in this ordering. Here is one way this can
be simulated using \lx.
\begin{code}
\begin{verbatim}
TYPE= COMMAND
NAME= \title
  START_TAG=
    RESET_BUFFER: 1
  REQPARAMS= 1
  PRINT_P1= TO_BUFFER 1
END_TYPE

TYPE= COMMAND
NAME= \author
  START_TAG=
    RESET_BUFFER: 2
  REQPARAMS= 1
  PRINT_P1= TO_BUFFER 2
END_TYPE

TYPE= COMMAND
NAME= \date
  START_TAG=
    RESET_BUFFER: 3
  REQPARAMS= 1
  PRINT_P1= TO_BUFFER 3
END_TYPE

TYPE= COMMAND
NAME= \maketitle
  START_TAG= "?n"
    SOURCE: BUFFER 1
    STRING: "?n?n"
    SOURCE: BUFFER 2
    STRING: "?n?n"
    SOURCE: BUFFER 3
    STRING: "?n?n"
  END_TAG=
    RESET_BUFFER: 1
    RESET_BUFFER: 2
    RESET_BUFFER: 3
END_TYPE
\end{verbatim}
\end{code}
For the \verb|\title| command, the print mode for its argument is set 
for printing to the buffer number~1. The single action at the start of
the command is to make sure that buffer~1 is empty (the line
\keyword{RESET\_BUFFER: 1}). The actions for the \verb|\author| and
\verb|\date| commands are similar, except that they print their argument
texts to buffers~2 and~3 respectively.

    The \verb|\maketitle| command takes no arguments, so all actions
must be placed under \keyword{START\_TAG=} and/or \keyword{END\_TAG=}.
There are a set of actions specified for \keyword{START\_TAG=}. Firstly
a newline is printed and this is followed by the contents of buffer~1
(i.e., the text of the argument of the \verb|\title| command).
Then two new lines are printed, followed by the contents of buffer~2
(the author). Finally another two newlines are printed, the contents
of buffer~3 (the date), and another two newlines. The actions
for \keyword{END\_TAG=} are to clear the contents of the three buffers.

    Just to extend the example, here is a specification for the
\LaTeX{} \verb|\thanks| command. \lx{} is not designed to do footnoting 
(as it does not do page breaking)
so instead the thanks text will be placed inside parentheses on a new line.
\begin{code}
\begin{verbatim}
TYPE= COMMAND
NAME= \thanks
  START_TAG= "?n ("
  REQPARAMS= 1
  END_TAG= ") "
END_TYPE
\end{verbatim}
\end{code}


    Given these \ctab{} specification and the following portion of a
\LaTeX{} document
\begin{code}
\begin{verbatim}
\date{29 February 2000}
\title{The Calculation of Leap Days\thanks{Originally published in JIR}}
\author{A. N. Other}
...
\maketitle
\end{verbatim}
\end{code}
then output from \lx{} will be:
\begin{code}
\begin{verbatim}
The Calculation of Leap Days
 (Originally published in JIR)

A. N. Other 

29 February 2000
\end{verbatim}
\end{code}
Note that as the \verb|\thanks| command appears within the argument of the
\verb|\title| command, it is written to the same place as the text of
the argument of \verb|\title|. Thus, it also gets written to the output file
when \verb|\maketitle| is processed.

\subsubsection{Print switching}

    There are individual actions that enable the printing destination
to be changed at will within the action set for any particular tag.
\begin{description}
\item[\keyword{SWITCH\_TO\_BUFFER: num}] Direct any following printing
  to the \lx{} buffer number \keyword{num}.
\item[\keyword{SWITCH\_TO\_FILE: name}] Direct any following printing
  to the file called \keyword{name}.
\item[\keyword{SWITCH\_TO\_SYSBUF}] Direct any following printing
  to the \lx{} system buffer.
\item[\keyword{SWITCH\_BACK:}] Undo the effect of the last 
  \keytext{SWITCH\_TO...} action.
\end{description}

    As an example of the utility of this type of action, consider
again the \LaTeX{} \verb|\maketitle| command. When \LaTeX{} processes
this command, it typesets the date as specified by the \verb|\date|
command, or if this has not been specified then it prints the current 
date instead. We can arrange for \lx{} to do something similar by
adding the following to the \ctab{} shown earlier for the \verb|\date|
and \verb|\maketitle| commands.
\begin{code}
\begin{verbatim}
TYPE= COMMAND
NAME= \documentclass
  OPT_PARAM= FIRST
  REQPARAMS= 1
  PRINT_OPT= NO_PRINT
  PRINT_P1= NO_PRINT
  START_TAG=
      c= Initialise buffer 3 to `Today'
    RESET_BUFFER: 3
    SWITCH_TO_BUFFER: 3
    STRING: "Today"
    SWITCH_BACK:
END_TYPE
\end{verbatim}
\end{code}
    At the start of the document, the above actions put the string 
\keytext{Today} into \keytext{BUFFER 3}, having first ensured that it is
empty. If the \LaTeX{} source includes a \verb|\date| command, then
the contents of the buffer will be overwritten, otherwise it will be
as initialised. In any event, when the \verb|\maketitle| command is
processed, the value output for the date will be either \keytext{Today}
or whatever the argument was of the \verb|\date| command.

\subsubsection{Notes on the use of buffers and files}

    Resetting a buffer or a file always has the effect of emptying it
of an prior contents.

    When printing from a buffer or a file, the entire contents are
printed. There is no limit to the number of times that a buffer
or a file can be used as a printing source.

    When printing to a buffer, the new strings are appended at
the end of the current contents of the buffer, at least until it
overflows. Unlike the behaviour of files,
this is independant of any intervening prints from the buffer.

    When printing to a file, the new strings are appended at the
end of the current contents of the file. However, if a file is printed
to after it has been printed from, the prior contents of the file
are lost, and the new string is added at the start of the file.
In general, it is safest to treat files as either read-only or write-only.



\subsubsection{User specified modes}

    Consider the \LaTeX{} command \verb|\\|. In normal text this signifies
that a line break must occur. In a \verb|tabular| environment, though,
it signifies the end of a row in a table. Suppose that in the \lx{}
procesing of a tabular environment it is required to start and end each
row with a vertical bar and to seperate each column also with a vertical
bar. However, in normal text a \verb|\\| command should just translate
into a newline. Just to complicate matters further, assume that in
an \verb|eqnarray| environment, the \verb|&| column seperator is to
be translated to some spaces, and that the string `(X)' is to be
put at the end of every row.

   In other words, we need to process some commands differently
according to where they appear in the LaTeX{} source. An \lx{} \ctab{}
provides this capability through mode setting and mode-dependent actions.
Here is an example of using this facility to solve the requirements 
outlined above.
\begin{code}
\begin{verbatim}
TYPE= BEGIN_ENV
NAME= tabular
  C= starting actions, etc., here
  END_TAG=
    SET_MODE: tabular
END_TYPE

TYPE= END_ENV
NAME= tabular
  START_TAG=
    RESET_MODE:
END_TYPE

TYPE= BEGIN_ENV
NAME= eqnarry
  C= starting actions, etc., here
  END_TAG=
    SET_MODE: eqn
END_TYPE

TYPE= END_ENV
NAME= eqnarray
  START_TAG= "    (X)?n"
    RESET_MODE:
END_TYPE

TYPE= TEX_CHAR
NAME= &
  START_TAG= "  |  "
IN_MODE= eqn
  START_TAG= "  "
END_MODE
END_TYPE

TYPE= CHAR_COMMAND
NAME= \\
  START_TAG= "?n"
IN_MODE= tabular
  START_TAG= " |?n"
    STRING: "     |  "    
END_MODE
IN_MODE= eqn
  START_TAG= "    (X)?n"
END_MODE
END_TYPE
\end{verbatim}
\end{code}

    Let us look at the specification for the \verb|\tabular| environment first.
The \keyword{END\_TAG=} action is specified by the single command line
\keyword{SET\_MODE:}~\keytext{tabular}, where \keytext{tabular} is any convenient
name for identifying a mode. Thus, this will set the mode to be \keytext{tabular}.
The action at the end of the environment is to reset the mode 
(\keyword{RESET\_MODE:})
to whatever its previous value was. It is assumed that the last row in any
\verb|tabular| environment is finished by \verb|\\|. Similar actions are
performed for the \verb|eqnarray| environment, except that the mode is called
\keytext{eqn} instead of \keytext{tabular}. The other difference is that it
is assumed that the last row is not ended by \verb|\\|, so the end of
the \verb|eqnarray| environment has to also act like the \verb|\\|.

    Turning now to the specification for the \verb|&| command, the first part
of the specification identifies the type and name of the \LaTeX{} command.
This is then followed by the mode-independent set of actions, 
which in this case
consists of printing a vertical bar with some spaces on either side of it.
Following these are any mode-dependent actions, bracketed between 
\keyword{IN\_MODE=} and \keyword{END\_MODE}. The value for \keyword{IN\_MODE=}
is the name of the relevent mode. In this case the only mode-dependent
action occurs when \keytext{MODE eqn} is in effect and it is to print
some spaces instead of the default spaces and vertical bar.

    The specification for the \verb|\\| command has its set of 
mode-independent default actions,
namely just to print a newline, and two sets of mode-dependent actions.
When the \keytext{tabular} mode is in effect, it prints some spaces, a vertical
bar, a newline, more spaces, a vertical bar, and finally some more spaces.
On the other hand, when the \keytext{eqn} mode is in effect, it prints some
spaces, the string `(X)' and a newline. If a mode is in effect
that is not defined within the specification (e.g., mode \keytext{anon})
it performs the default mode-independent actions.

    As a perhaps more practical example, the following \ctab{} code will 
convert simple \LaTeX{} \verb|tabular|
 environments to appropriate mark-up for HTML tables. It is assumed that
the \verb|tabular| environment is always within a \verb|table| environment.

    To set the perspective a little, here is the code for a simple table
in \LaTeX:
\begin{code}
\begin{verbatim}
\begin{table}[tbp]
\centering
\caption{A simple table typeset by \LaTeX.} \label{tab:lxtab}
\begin{tabular}{|l||r|r||r|r|} \hline
Stock & \multicolumn{2}{c||}{1994} & \multicolumn{2}{c|}{1995} \\ \cline{2-5}
      &  low    &  high  &   low  & high  \\ \hline
ABC   &  27     &  36    &   23   & 45     \\
DEF   &  53     &  72    &   19   & 54     \\
GHI   &  28     &  49    &   17   & 79     \\ \hline
\end{tabular}
\end{table}
\end{verbatim}
\end{code}
This will be typeset as shown in table~\ref{tab:lxtab}.
\begin{table}[tbp]
\centering
\caption{A simple table typeset by \LaTeX.} \label{tab:lxtab}
\begin{tabular}{|l||r|r||r|r|} \hline
Stock & \multicolumn{2}{c||}{1994} & \multicolumn{2}{c|}{1995} \\ \cline{2-5}
      &  low    &  high  &   low  & high  \\ \hline
ABC   &  27     &  36    &   23   & 45     \\
DEF   &  53     &  72    &   19   & 54     \\
GHI   &  28     &  49    &   17   & 79     \\ \hline
\end{tabular}
\end{table}

    The corresponding HTML code for the table after translation is:
\begin{latexonly}
\begin{code}
\begin{verbatim}
<p><center><table border>
<caption>A simple table typeset by LaTeX.</caption> <a name="tab:lxtab"></a>

<tr><td> Stock </td><td colspan=2> 1994 </td><td colspan=2> 1995 </tr>
<tr><td > 
       </td><td > low </td><td > high </td><td > low </td><td > high </tr>
<tr><td > ABC </td><td > 27 </td><td > 36 </td><td > 23 </td><td > 45 </tr>
<tr><td > 
DEF </td><td > 53 </td><td > 72 </td><td > 19 </td><td > 54 </tr>
<tr><td > 
GHI </td><td > 28 </td><td > 49 </td><td > 17 </td><td > 79 </tr>
<tr><td >
</table></center>
\end{verbatim}
\end{code}
\end{latexonly}

\begin{code}
\begin{htmlverbatim}
\ST{p}\ST{center}\ST{table border}
\ST{caption}A simple table typeset by LaTeX.\ET{caption} \ST{a name="tab:lxtab"}\ET{a}

\ST{tr}\ST{td} Stock \ET{td}\ST{td colspan=2} 1994 \ET{td}\ST{td colspan=2} 1995 \ET{tr}
\ST{tr}\ST{td} 
       \ET{td}\ST{td} low \ET{td}\ST{td} high \ET{td}\ST{td} low \ET{td}\ST{td} high \ET{tr}
\ST{tr}\ST{td} ABC \ET{td}\ST{td} 27 \ET{td}\ST{td} 36 \ET{td}\ST{td} 23 \ET{td}\ST{td} 45 \ET{tr}
\ST{tr}\ST{td} 
DEF \ET{td}\ST{td} 53 \ET{td}\ST{td} 72 \ET{td}\ST{td} 19 \ET{td}\ST{td} 54 \ET{tr}
\ST{tr}\ST{td} 
GHI \ET{td}\ST{td} 28 \ET{td}\ST{td} 49 \ET{td}\ST{td} 17 \ET{td}\ST{td} 79 \ET{tr}
\ST{tr}\ST{td}
\ET{table}\ET{center}
\end{htmlverbatim}
\end{code}

    In the HTML browser that I use this is displayed
approximately as shown for table~\ref{tab:httab}.

\begin{table}[tbp]
\centering
\caption{A simple table typeset after translation to HTML.} \label{tab:httab}
\begin{tabular}{||l|l|l|l|l||} \hline\hline
Stock & \multicolumn{2}{l||}{1994} & \multicolumn{2}{l|}{1995} \\ \hline
      &  low    &  high  &   low  & high  \\ \hline
ABC   &  27     &  36    &   23   & 45     \\
DEF   &  53     &  72    &   19   & 54     \\
GHI   &  28     &  49    &   17   & 79     \\ \hline \hline
\end{tabular}
\end{table}

    In HTML a table is enclosed between 
\keytext{\ST{table}} and \keytext{\ET{table}} tags.
Each row of the table is enclosed between 
\keytext{\ST{tr}} and \keytext{\ET{tr}} tags,
and each element in a row is enclosed between 
\keytext{\ST{td}} and \keytext{\ET{td}} tags.
Under certain circumstances the closing tags (i.e.,
those like \keytext{\ET{...}})
can be inferred by the HTML procesors and need
not be explicitly put into the source text. The equivalent HTML tags to a
\LaTeX{} \verb|\multicolumn{num}{col}{text}| command are\\
\keytext{\ST{td colspan=num} text \ET{td}}.
 
    The general actions that \lx{} has to perform in doing the \LaTeX{} to
HTML translation are:
\begin{itemize}
\item The start and end of a \verb|table| environment has to translate to
      the HTML start and end table tags \keytext{\ST{table}} and 
      \keytext{\ET{table}}.
      (Actually this also needs to handle the
      centering of the HTML table and drawing a border round it as well.)

\item The start and end of the \verb|tabular| environment has to translate
      into a row start 
      \keytext{\ST{tr}\ST{td}} and end \keytext{\ET{tr}}
      in the HTML table (and set the mode for the
      \LaTeX{} \verb|\\| command).

\item The \verb|\\| command must end one row of the HTML table and start the
      next row 
      (\keytext{\ET{tr}?n\ST{tr}\ST{td}}).
      It need not end a data element as
      this is automatically closed by the end of the row.

\item The \verb|&| column delimeter must end one HTML data element and start
      another one (i.e., \keytext{\ET{td}\ST{td}}).

\item The difficulty is in handling the \verb|\multicolumn| command. In the easy
      case the \LaTeX{} to HTML translation is: \\
      \keytext{... \Amp{}  text \Amp{} ...} maps to \\
      \keytext{... \ET{td}\ST{td} text \ET{td}\ST{td} ...}. \\
     However, when a multicolumn is involved the translation is \\
\verb|... & \multicolumn{N}{P}{text} & ...| maps to \\
\keytext{... \ET{td}\ST{td colspan=N} text \ET{td}\ST{td} ...}. \\
As there is no look-ahead in \lx{} we have to be careful about starting a
data element after a \verb|&| because at that point \lx{} cannot know
whether or not a \verb|\multicolumn| command comes next, or just an ordinary
data element.

\end{itemize}

    We solve this last problem partly by using buffers 
(numbers \keytext{8} and \keytext{9} in the specification below) 
as temporary storage, and partly by a subtle specification for the
\verb|\multicolumn| command.

\begin{latexonly}
\begin{code}
\begin{verbatim}
C=   start of a table
TYPE= BEGIN_ENV
NAME= table
  START_TAG= "<center><table border>"
  OPT_PARAM= FIRST
  C=  ignore the optional positioning argument
  PRINT_OPT= NO_PRINT
END_TYPE

C=  end a table
TYPE= END_ENV
NAME= table
  START_TAG= "</table></center>"
END_TYPE

C=  start a tabular
TYPE= BEGIN_ENV
NAME= tabular
  START_TAG= "?n<tr><td"
    RESET_BUFFER: 8
    RESET_BUFFER: 9
  OPT_PARAM= FIRST
  C=  ignore the optional positioning argument
  PRINT_OPT= NO_PRINT
  REQPARAMS= 1
  C=  ignore the column specification
  PRINT_P1= NO_OP
  END_TAG=
    SET_MODE: tabular
  PC_AT_END= TO_BUFFER 9
END_TYPE

C=  end a tabular
TYPE= END_ENV
NAME= tabular
  PC_AT_START= RESET
  START_TAG= ">"
    RESET_BUFFER: 8
    RESET_BUFFER: 9
    RESET_MODE:
END_TYPE

C=  we can do some processing of the \mutlicolumn command
TYPE= COMMAND
NAME= \multicolumn
  PC_AT_START= TO_BUFFER 8
  REQPARAMS= 2
  START_TAG_1= " colspan="
  PRINT_P2= NO_PRINT
  PC_AT_END= RESET
END_TYPE

C=  now for the end/start of a row
TYPE= CHAR_COMMAND
NAME= \\
  START_TAG= "<br>"
IN_MODE= tabular
  PC_AT_START= RESET
  START_TAG=
    SOURCE: BUFFER 8
    STRING: "> "
    RESET_BUFFER: 8
    SOURCE: BUFFER 9
  END_TAG= "</tr>?n<tr><td "
    RESET_BUFFER: 9
  PC_AT_END= TO_BUFFER 9
END_MODE
END_TYPE

C= and the column seperator
TYPE= TEX_CHAR
NAME= &
  PC_AT_START= RESET
  START_TAG= 
    SOURCE: BUFFER 8
    STRING: "> "
    RESET_BUFFER: 8
    SOURCE: BUFFER 9
  END_TAG= " </td><td "
    RESET_BUFFER: 9
  PC_AT_END= TO_BUFFER 9
END_TYPE
\end{verbatim}
\end{code}
\end{latexonly}

\begin{htmlverbatim}
C=   start of a table
TYPE= BEGIN\_ENV
NAME= table
  START\_TAG= "\ST{center}\ST{table border}"
  OPT\_PARAM= FIRST
  C=  ignore the optional positioning argument
  PRINT\_OPT= NO\_PRINT
END\_TYPE

C=  end a table
TYPE= END\_ENV
NAME= table
  START\_TAG= "\ET{table}\ET{center}"
END\_TYPE

C=  start a tabular
TYPE= BEGIN\_ENV
NAME= tabular
  START\_TAG= "?n\ST{tr}\LT{}td"
    RESET\_BUFFER: 8
    RESET\_BUFFER: 9
  OPT\_PARAM= FIRST
  C=  ignore the optional positioning argument
  PRINT\_OPT= NO\_PRINT
  REQPARAMS= 1
  C=  ignore the column specification
  PRINT\_P1= NO\_OP
  END\_TAG=
    SET\_MODE: tabular
  PC\_AT\_END= TO\_BUFFER 9
END\_TYPE

C=  end a tabular
TYPE= END\_ENV
NAME= tabular
  PC\_AT\_START= RESET
  START\_TAG= "\GT"
    RESET\_BUFFER: 8
    RESET\_BUFFER: 9
    RESET\_MODE:
END\_TYPE

C=  we can do some processing of the \verb|\multicolumn| command
TYPE= COMMAND
NAME= \verb|\multicolumn|
  PC\_AT\_START= TO\_BUFFER 8
  REQPARAMS= 2
  START\_TAG\_1= " colspan="
  PRINT\_P2= NO\_PRINT
  PC\_AT\_END= RESET
END\_TYPE

C=  now for the end/start of a row
TYPE= CHAR\_COMMAND
NAME= \verb|\\|
  START\_TAG= "\ST{br}"
IN\_MODE= tabular
  PC\_AT\_START= RESET
  START\_TAG=
    SOURCE: BUFFER 8
    STRING: "\GT{} "
    RESET\_BUFFER: 8
    SOURCE: BUFFER 9
  END\_TAG= "\ET{tr}?n\ST{tr}\LT{}td "
    RESET\_BUFFER: 9
  PC\_AT\_END= TO\_BUFFER 9
END\_MODE
END\_TYPE

C= and the column seperator
TYPE= TEX\_CHAR
NAME= \Amp
  PC\_AT\_START= RESET
  START\_TAG= 
    SOURCE: BUFFER 8
    STRING: "\GT "
    RESET\_BUFFER: 8
    SOURCE: BUFFER 9
  END\_TAG= " \ET{td}\LT{}td "
    RESET\_BUFFER: 9
  PC\_AT\_END= TO\_BUFFER 9
END\_TYPE
\end{htmlverbatim}

    Regarding the \verb|\multicolumn| specification, we state that as far as
\lx{} is concerned, it only has two required parameters, and that the action
for the second one is \keyword{NO\_PRINT}. The first argument is written to
buffer \keytext{8} after `colspan=' has first been put into it. \lx{} will treat
the actual third argument to the \verb|\multicolumn| as ordinary text, just
as if there was no \verb|\multicolumn| in the \LaTeX{} source. We use buffer
\keytext{9} for storing the text of a data element. When \lx{} processes
a \keytext{\Amp} column delimeter it first outputs the contents of
buffer \keytext{8} (the number of columns specification) and then appropriate
HTML characters. It then outputs the contents of buffer \keytext{9} (the
element text), finishes off the element and partially starts the next element.
Similar actions are performed at the start of the \keytext{tabular} environment
and at the end of each row in the table.

\subsection{Sectioning command types}

    \lx\ does some particular processing for sectioning command types. Although
\LaTeX\ can determine where any section of a document ends, other tagging systems
cannot always do this. They require both a `begin section' and an `end section'
tag. \lx\ can take account of the nesting depth of document sections and,
given appropriate specifications, can supply both `begin section' and `end section'
tags appropriately. This requires a little bit more in the way of specifications
than we have met so far.

    For a \keyword{SECTIONING} command type, the command line 
\keyword{SECTIONING\_LEVEL=}
must be included within the specification. The value of this command is a keyword
from the following list.
\begin{description}
\item[\keyword{PART}] For a sectioning command equivalent to the 
              \LaTeX\ \verb|\part| command.
\item[\keyword{CHAPTER}] For a sectioning command equivalent to the 
              \LaTeX\ \verb|\chapter| command.
\item[\keyword{SECT}] For a sectioning command equivalent to the 
              \LaTeX\ \verb|\section| command.
\item[\keyword{SUBSECT}] For a sectioning command equivalent to the 
              \LaTeX\ \verb|\subsection| command.
\item[\keyword{SUBSUBSECT}] For a sectioning command equivalent to the 
              \LaTeX\ \verb|\subsubsection| command.
\item[\keyword{PARA}] For a sectioning command equivalent to the 
              \LaTeX\ \verb|\paragraph| command.
\item[\keyword{SUBPARA}] For a sectioning command equivalent to the 
              \LaTeX\ \verb|\subparagraph| command.
\end{description}

    When a sectioning command is read from the \LaTeX\ source, \lx\ firstly
performs the \keyword{END\_TAG=} actions for any `lower level' sections
that this one is closing off. It then performs the \keyword{START\_TAG=} 
actions for the current command, and stores its own \keyword{END\_TAG=} actions for
later use. It then goes on and process any arguments as usual.
 The \keyword{END\_DOCUMENT} command automatically closes off any opened
sections.

    As an example, assume that some kind soul has supplied a \LaTeX\ style
file that makes the commands \verb|\clause| synonymous with \verb|\subsection|,
and \verb|\sclause| synonymous with \verb|\subsubsection|, etc. Also assume
that it is required to output start and end tags of the form 
\keytext{\ST{div.1}} and  \keytext{\ET{div.1}} 
for sections, \keytext{\ST{div.2}} for clauses, etc., and surround the
headings with tags \keytext{\ST{heading}} and \keytext{\ET{heading}}. 
Further, the first
optional argument is of no interest as the output is going to be
used by a processing system unable to automatically handle tables of contents.
Part of an appropriate \ctab\ for doing this is:

\begin{latexonly}
\begin{code}
\begin{verbatim}
TYPE= SECTIONING
NAME= \section
  SECTIONING_LEVEL= SECT
  START_TAG= "?n?n<div.1>?n"
  END_TAG= "?n</div.1>"
  OPT_PARAM= FIRST
  PRINT_OPT= NO_PRINT
  REQPARAMS= 1
  START_TAG_1= "<heading>"
  END_TAG_1= "</heading>?n"
END_TYPE

TYPE= SECTIONING
NAME= \clause
  SECTIONING_LEVEL= SUBSECT
  START_TAG= "?n?n<div.2>?n"
  END_TAG= "?n</div.2>"
  OPT_PARAM= FIRST
  PRINT_OPT= NO_PRINT
  REQPARAMS= 1
  START_TAG_1= "<heading>"
  END_TAG_1= "</heading>?n"
END_TYPE
\end{verbatim}
\end{code}
\end{latexonly}
\begin{htmlverbatim}
TYPE= SECTIONING
NAME= \verb|\section|
  SECTIONING\_LEVEL= SECT
  START\_TAG= "?n?n\ST{div.1}?n"
  END\_TAG= "?n\ET{div.1}"
  OPT\_PARAM= FIRST
  PRINT\_OPT= NO\_PRINT
  REQPARAMS= 1
  START\_TAG\_1= "\ST{heading}"
  END\_TAG\_1= "\ET{heading}?n"
END\_TYPE

TYPE= SECTIONING
NAME= \verb|\clause|
  SECTIONING\_LEVEL= SUBSECT
  START\_TAG= "?n?n\ST{div.2}?n"
  END\_TAG= "?n\ET{div.2}"
  OPT\_PARAM= FIRST
  PRINT\_OPT= NO\_PRINT
  REQPARAMS= 1
  START\_TAG\_1= "\ST{heading}"
  END\_TAG\_1= "\ET{heading}?n"
END\_TYPE
\end{htmlverbatim}

    An example output resulting from this \ctab\ (if it had been applied to this
document) is:
\begin{latexonly}
\begin{code}
\begin{verbatim}
...
</div.2>
</div.1>

<div.1>
<heading>The command table file</heading>

    By default, ...
\end{verbatim}
\end{code}
\end{latexonly}
\begin{htmlverbatim}
...
\ET{div.2}
\ET{div.1}

\ST{div.1}
\ST{heading}The command table file\ET{heading}

    By default, ...
\end{htmlverbatim}

\subsection{List environment types}

    In \LaTeX\ the use of the \verb|\item| command is restricted to within a
list environment. The typeset appearance of an \verb|\item| typically depends
on the particular environment in which it is used. \lx\ has a limited capability
of modifying its \verb|\item| tagging output. It can also provide an `end item'
tag for those tagging systems that require such a thing.

    For such list environments, identified by the command type keyword 
\keyword{BEGIN\_LIST\_ENV},
the following command lines should be included within the type specification.
\begin{description}
\item[\keyword{START\_ITEM=}] Actions to be performed at the start 
   of each \verb|\item| command in the list.
\item[\keyword{END\_ITEM=}] Actions to be performed after processing all 
    the \verb|\item|'s text.
\item[\keyword{START\_ITEM\_PARAM=}] Actions to be performed at the start of an 
   \verb|\item|'s optional argument text.
\item[\keyword{END\_ITEM\_PARAM=}] Actions to be performed at the end of an
   \verb|\item|'s optional argument text.
\end{description}
As usual, an unspecified tag defaults to no actions.

    For example, assume that we are not interested in tagging the end of an item,
but we do want to mark each item in an \verb|itemize| environment with the lowercase
letter `o', each \verb|enumerate| item with `(N)' and put a colon after the
optional argument in a description environment. Also, each item should have some 
indentation from the left hand margin.
    
\begin{code}
\begin{verbatim}
TYPE= BEGIN_LIST_ENV
NAME= itemize
  START_ITEM= "?n    o "
END_TYPE

TYPE= END_LIST_ENV
NAME= itemize
END_TYPE

TYPE= BEGIN_LIST_ENV
NAME= enumerate
  START_ITEM= "?n    (N) "
END_TYPE

TYPE= END_LIST_ENV
NAME= enumerate
END_TYPE

TYPE= BEGIN_LIST_ENV
NAME= description
  START_ITEM= "?n  "
  END_ITEM_PARAM= " : "
END_TYPE

TYPE= END_LIST_ENV
NAME= description
END_TYPE
\end{verbatim}
\end{code}

    With the above commands, this \LaTeX{} text:
\begin{verbatim}
\begin{description}
\item[An example]
  \begin{itemize}
  \item the first item;
  \item the second item.
  \end{itemize}
\end{description}
\end{verbatim}
will be transformed into:
\begin{verbatim}
  An example :
    o the first item;
    o the second item.
\end{verbatim}

\subsection{Character types}

    \LaTeX\ treats some characters specially. These special characters are: 
\verb|#|, 
\verb|$|,  
\verb|%|, 
\verb|&|, 
\verb|~|, 
\verb|_|, 
\verb|^|,
\verb|\|,
\verb|{|,
\verb|}|,
and, under some circumstances, also the 
character \verb|@|. 
 \lx\ recognizes these special characters and, if directed, will
perform specified actions; otherwise it treats them as it treats any alphanumeric
character, which is just to print it.

    It has already been stated that commands for the left and right braces
(i.e. \verb|{| and \verb|}|) must be given 
within the \ctab\ as command types
\keyword{LBRACE}, \keyword{RBRACE} respectively.
The dollar symbol (\$) must also be specified via the two command types
\keyword{BEGIN\_DOLLAR} and \keyword{END\_DOLLAR}. Here is an
example of replacing the dollar signs by tags intended to indicate
the start and end of a mathematical phrase.
\begin{latexonly}
\begin{code}
\begin{verbatim}
TYPE= BEGIN_DOLLAR
  START_TAG= "<math>"
END_TYPE

TYPE= END_DOLLAR
  START_TAG= "</math>"
END_TYPE
\end{verbatim}
\end{code}
\end{latexonly}
\begin{htmlverbatim}
TYPE= BEGIN\_DOLLAR
  START\_TAG= "\ST{math}"
END\_TYPE

TYPE= END\_DOLLAR
  START\_TAG= "\ET{math}"
END\_TYPE
\end{htmlverbatim}

    Commands for the other special \LaTeX\ characters are specified with the
\keyword{TEX\_CHAR} command type keyword. 

The characters \verb|_| (underscore) and \verb|^| (caret) are used in
\LaTeX{} math mode to indicate subscripting and superscripting respectively. The
following will replace \verb|^| by \keytext{\ST{sup}}, print the superscript
text (which must be enclosed in braces\footnote{It is good practice to
always enclose superscript and subscript text in braces, even though
\TeX\ does not always require this.}) and at the end close with
\keytext{\ET{sup}}.
\begin{latexonly}
\begin{code}
\begin{verbatim}
TYPE= TEX_CHAR
NAME= ^
  START_TAG= "<sup>"
  REQPARAMS= 1
  END_TAG= "</sup>"
END_TYPE
\end{verbatim}
\end{code}
\end{latexonly}

\begin{htmlverbatim}
TYPE= TEX\_CHAR
NAME= \verb|^|
  START\_TAG= "\ST{sup}"
  REQPARAMS= 1
  END\_TAG= "\ET{sup}"
END\_TYPE
\end{htmlverbatim}

Given the above specifications, then \verb|$(2^{15} - 1)$| will be transformed
into \\
\keytext{\ST{math}(2\ST{sup}15\ET{sup} - 1)\ET{math}}.

\subsection{Verbatim like types}

    The command type \keyword{VCOMMAND} is for the procesing of \LaTeX{}
\verb|\verb|-like commands where the argument of the command is to be
typeset as-is. For example, there might be a command called \verb|\url|
which takes one argument which is meant to be an Internet URL. If the
application was the conversion of a \LaTeX{} document to HTML, then the
following specification could be useful.
\begin{latexonly}
\begin{code}
\begin{verbatim}
TYPE= VCOMMAND
NAME= \url
  REQPARAMS= 1
  PRINT_P1= TO_BUFFER 7
  START_TAG=
    RESET_BUFFER: 7
  END_TAG= "<a href=""
    SOURCE: BUFFER 7
    STRING: "">"
    SOURCE: BUFFER 7
    STRING: "</a>"
    RESET_BUFFER: 7
END_TYPE
\end{verbatim}
\end{code}
\end{latexonly}
\begin{htmlverbatim}
TYPE= VCOMMAND
NAME= \verb|\url|
  REQPARAMS= 1
  PRINT\_P1= TO\_BUFFER 7
  START\_TAG=
    RESET\_BUFFER: 7
  END\_TAG= "\LT{}a href=""
    SOURCE: BUFFER 7
    STRING: ""\GT"
    SOURCE: BUFFER 7
    STRING: "\ET{a}"
    RESET\_BUFFER: 7
END\_TYPE
\end{htmlverbatim}

If the \LaTeX{} source included:
\begin{code}
\begin{verbatim}
... obtainable from 
\url{http://www.cdrom.com/pub/tex}
\end{verbatim}
\end{code}
then the resulting \lx{} output would be:
\begin{latexonly}
\begin{code}
\begin{verbatim}
... obtainable from 
<a href="http://www.cdrom.com/pub/tex">http://www.cdrom.com/pub/tex</a>
\end{verbatim}
\end{code}
\end{latexonly}
\begin{htmlverbatim}
... obtainable from 
\ST{a href="http://www.cdrom.com/pub/tex"}http://www.cdrom.com/pub/tex\ET{a}
\end{htmlverbatim} 
which, if this was then read via an appropriate browser, a link to the URL 
\begin{htmlonly}
<a href="http://www.cdrom.com/pub/tex">http://www.cdrom.com/pub/tex</a>
\end{htmlonly}
would be automatically established.

    Similarly \verb|verbatim|-like environments can also be specified with
the types \keyword{BEGIN\_VENV} and \keyword{END\_VENV}. 
For example, the \file{html.sty} package defines three \LaTeX{}
environments for documents that might be converted from \LaTeX{} tagging
to HTML tagging. One of these, \verb|latexonly| is for \LaTeX{} code
that is not to occur in the HTMLed document and another is \verb|htmlonly|
which contains HTML code that is required for an HTML version
of the document but which is not to appear in the \LaTeX ed document.
The third one is \verb|rawhtml| which is for HTML code to be output
verbatim to the HTML document source.
These could be simulated by:
\begin{code}
\begin{verbatim}
TYPE= BEGIN_VENV
NAME= latexonly
  PC_AT_START= NO_PRINT
END_TYPE

TYPE= END_VENV
NAME= latexonly
  PC_AT_END= RESET
END_TYPE

TYPE= BEGIN_ENV
NAME= htmlonly
END_TYPE

TYPE= END_ENV
NAME= htmlonly
END_TYPE

TYPE= BEGIN_VENV
NAME= rawhtml
END_TYPE

TYPE= END_VENV
NAME= rawhtml
END_TYPE
\end{verbatim}
\end{code}


\subsection{Odd command types}

    The majority of commands in \LaTeX{} that take optional arguments
have only a single optional argument that is either immediately after
the command or after all the required arguments. There are, however, some
commands that do not fit this pattern. This set of command types enables
at least some of these `odd' commands to be handled.

    The command type keyword is of the form \keytext{COMMAND\_code}, where
\keytext{code} indicates the type and ordering of the arguments. The
\keytext{code} is composed from combinations of the letters \keytext{O}
(for an optional argument) and \keytext{P} (for a required parameter (i.e.,
argument)). The ordering of these letters in the \keytext{code} specifies
the type and ordering of the command's arguments.

    The `odd' command types are:
\begin{description}
\item[\keyword{COMMAND\_OOP}] Corresponding to a \LaTeX{} command of the form \\
\verb|\com[OptParam][OptParam]{ReqParam}|.
For example, the \verb|\makebox|
command falls into this category.

\item[\keyword{COMMAND\_OOOPP}] Corresponding to a \LaTeX{} command of the form \\
\verb|\com[OptParam][OptParam][OptParam]{ReqParam}{ReqParam}|.
For example, the \verb|\parbox|
command falls into this category.

\item[\keyword{COMMAND\_OPO}] Corresponding to a \LaTeX{} command of the form \\
\verb|\com[OptParam]{ReqParam}[OptParam]|.
For example, the \verb|\RequirePackage| and \verb|\LoadClass|
commands fall into this category.

\item[\keyword{COMMAND\_POOOP}] Corresponding to a \LaTeX{} command of the form \\
\verb|\com{ReqParam}[OptParam][OptParam][OptParam]{ReqParam}|.

\item[\keyword{COMMAND\_POOP}] Corresponding to a \LaTeX{} command of the form \\
\verb|\com{ReqParam}[OptParam][OptParam]{ReqParam}|.
For example, the \verb|\newcommand| and its companion commands
fall into this category.

\item[\keyword{COMMAND\_POOPP}] Corresponding to a \LaTeX{} command of the form \\
\verb|\com{ReqParam}[OptParam][OptParam]{ReqParam}{ReqParam}|.
For example, the \verb|\newenvironment| and its companion command
fall into this category.
\end{description}

    As usual, the command name is required, as are any actions. However,
it is not necessary to specify the number of required arguments (i.e.
\keyword{REQPARAMS=}) nor the position of the optional argument (i.e. 
\keyword{OPT\_PARAM=}), as \lx\ already has this information. The tag actions
are according to the argument ordering given in the \keytext{code} and are
specified by the required argument tags (e.g. \keytext{START\_TAG\_n=} and
\keytext{END\_TAG\_n=}). 
Do not use any of the command lines for optional arguments. Argument
actions are controlled in the usual manner.

    A typical example of the use of these commands is to supress any processing
of the \LaTeX{} \verb|\newcommand| and its ilk. For example:
\begin{code}
\begin{verbatim}
TYPE= COMMAND_POOP
NAME= \providecommand
  PRINT_P1= NO_OP
  PRINT_P2= NO_OP
  PRINT_P3= NO_OP
  PRINT_P4= NO_OP
END_TYPE

TYPE= COMMAND_POOPP
NAME= \renewenvironment
  PRINT_P1= NO_OP
  PRINT_P2= NO_OP
  PRINT_P3= NO_OP
  PRINT_P4= NO_OP
  PRINT_P5= NO_OP
END_TYPE
\end{verbatim}
\end{code}    


\subsection{Other command types}

    The \keytext{OTHER\_} command types (\keyword{OTHER\_COMMAND}, 
\keyword{OTHER\_BEGIN} 
and \keyword{OTHER\_END})
are very limited in what can be affected. Basically, these provide for
default printing actions if the corresponding \LaTeX\ command has not been
identified elsewhere in the \ctab.

    If there are no commands within the specification, the name of the command and
all its arguments will be printed verbatim.

    The command lines \keyword{START\_TAG=} and \keyword{END\_TAG=} 
cause the corresponding
actions to be performed before and after the name of the command is printed. Any
arguments are printed verbatim. 

    The command line \keyword{PRINT\_CONTROL=} with a value of \keyword{NO\_PRINT}
causes the command name not to be printed, nor any arguments that \lx\ may 
find associated with the command.

\subsection{Picture types}

    The \keytext{\_PICTURE\_} command types differ from all the other types in
\lx, just as they do in \LaTeX. In \LaTeX\ some of the picture drawing commands 
take arguments of the form \verb|(number, number)|, representing a coordinate
pair,  as well as the usual
required arguments enclosed in curly braces and possibly an optional 
argument enclosed in square brackets. Within \lx, commands that take coordinate
arguments are treated specially in the \ctab.

    Generally speaking, the \lx\ command types are of the form 
\keytext{PICTURE\_code},
where \keytext{code} indicates the type and ordering of the arguments. The
\keytext{code} is composed from combinations of the letters \keytext{C} (for a
coordinate argument), \keytext{O} (for an optional argument) and \keytext{P} (for
a required argument). For example, \keyword{PICTURE\_PCOP} indicates a picture
command that has a required argument, followed by a coordinate argument,
followed by an optional argument and finally another required argument.

    The provided picture types are:
\begin{description}
\item[\keyword{BEGIN\_PICTURE\_CC}] Corresponding to a \LaTeX\ command of the form \\
     \verb|\begin{PictureEnv}(coords)(coords)|, where the final coordinate argument
      is optional.
\item[\keyword{PICTURE\_CCPP}] Corresponding to a \LaTeX\ command of the form \\
     \verb|\com(coords)(coords){ReqParam}{ReqParam}|. 
     For example, the \verb|\multiput| command falls into this category.
\item[\keyword{PICTURE\_CO}]  Corresponding to a \LaTeX\ command of the form \\
     \verb|\com(coords)[OptParam]|.
     For example, the standard \LaTeX\ \verb|\oval| command falls into 
     this category.
\item[\keyword{PICTURE\_COP}]  Corresponding to a \LaTeX\ command of the form \\
     \verb|\com(coords)[OptParam]{ReqParam}|.
     For example, the \verb|\makebox| and \verb|\framebox| commands fall 
     into this category.
\item[\keyword{PICTURE\_CP}]  Corresponding to a \LaTeX\ command of the form \\
     \verb|\com(coords){ReqParam}|.
     For example, the \verb|\put|, \verb|\line| and \verb|\vector| commands 
     fall into this category.
\item[\keyword{PICTURE\_OCC}]  Corresponding to a \LaTeX\ command of the form \\
     \verb|\com[OptParam](coords)(coords)|.
     For example, the \verb|\graphpaper| command from the \verb|graphpap|
     package falls into this category.
\item[\keyword{PICTURE\_OCCC}]  Corresponding to a \LaTeX\ command of the form \\
     \verb|\com[OptParam](coords)(coords)(coords)|.
     For example, the \verb|\qbezier| command falls into this category.
\item[\keyword{PICTURE\_OCO}]  Corresponding to a \LaTeX\ command of the form \\
     \verb|\com[OptParam](coords)[OptParam]|.
     For example, the \verb|\oval| command from the \verb|pict2e| package
     falls into this category.
\item[\keyword{PICTURE\_PCOP}]  Corresponding to a \LaTeX\ command of the form \\
     \verb|\com{ReqParam}(coords)[OptParam]{ReqParam}|.
     For example, the \verb|\dashbox| and \verb|\savebox| commands fall
     into this category.
\item[\keyword{END\_PICTURE}]  Corresponding to a \LaTeX\ command of the form \\
    \verb|\end{PictureEnv}|.
\end{description}

    As usual, the command name is required, as are any actions. However,
it is not necessary to specify the number of required arguments (i.e.
\keyword{REQPARAMS=}) nor the position of the optional argument (i.e. 
\keyword{OPT\_PARAM=}), as \lx\ already has this information. The tag actions
are according to the argument ordering given in the \keytext{code} and are
specified by the required argument tags (e.g. \keytext{START\_TAG\_n=} and
\keytext{END\_TAG\_n=}). 
Do not use any of the command lines for optional arguments. Argument
actions controlled in the usual manner.

    As an example, the following specifications within a \ctab\ should be
sufficient to ensure that any picture commands in a source file are 
not passed through to the output file.

\begin{code}
\begin{verbatim}
TYPE= BEGIN_PICTURE_CC
NAME= picture
PRINT_P1= NO_PRINT
PRINT_P2 = NO_PRINT
END_TYPE

TYPE= PICTURE_CP
NAME= \put
PRINT_P1= NO_PRINT
PRINT_P2= NO_OP
END_TYPE

TYPE= PICTURE_CCPP
NAME= \multiput
PRINT_P1= NO_PRINT
PRINT_P2= NO_PRINT
PRINT_P3= NO_OP
PRINT_P4= NO_OP
END_TYPE

TYPE= PICTURE_PCOP
NAME= \savebox
PRINT_P1= NO_OP
PRINT_P2= NO_PRINT
PRINT_P3= NO_OP
PRINT_P4= NO_OP
END_TYPE

TYPE= PICTURE_OCC
NAME= \graphpaper
PRINT_P1= NO_OP
PRINT_P2= NO_PRINT
PRINT_P3= NO_PRINT
END_TYPE

TYPE= PICTURE_OCCC
NAME= \qbezier
PRINT_P1= NO_OP
PRINT_P2= NO_PRINT
PRINT_P3= NO_PRINT
PRINT_P4= NO_PRINT
END_TYPE

TYPE= END_PICTURE
NAME= picture
END_TYPE
\end{verbatim}
\end{code}

\begin{description}
\item[NOTE 1:] The action \keyword{NO\_OP} cannot be applied to an
 argument that is a coordinate pair.

\item[NOTE 2:] As \lx\ is essentially limited to printing actions, 
and cannot actually process any
\LaTeX\ picture drawing commands, the suppression of picture printing is 
probably the most usual use of the picture commands.
\end{description}

\subsection{Special command types}

    The \keytext{SPECIAL\_} commands, namely \keyword{SPECIAL\-\_COMMAND}, 
\keyword{SPECIAL\-\_BEGIN\-\_ENV},
 \keyword{SPECIAL\-\_BEGIN\-\_LIST}, 
\keyword{SPECIAL\-\_END\-\_ENV},
\keyword{SPECIAL\-\_END\-\_LIST} and
\keyword{SPECIAL\-\_SECTIONING}, are provided for cases where some special kind of
output processing is required that is not built into \lx. In order to implement
any commands of these types, it is necessary to modify the internals of \lx\
and recompile the source code. This is not recommended.

\subsection{File inclusion}

    A \ctab\ file can include other \ctab\ files. In turn an included
file can recursively include other \ctab\ files. The file inclusion command
line is
\begin{code}
\begin{verbatim}
INCLUDE= FileName
\end{verbatim}
\end{code}
where \keyword{FileName} is the name of the \ctab\ file to be included. The effect
is that the above line is replaced by the contents of \keyword{FileName}.

    For example, assume that there are three \ctab\ files called respectively
\file{detex.ct}, \file{detex.l2x} and \file{detex.fl}. The contents of these 
files are:
\begin{code}
\begin{verbatim}
C=  ----------file detex.ct
...
INCLUDE= detex.l2x
...
C= ----------end of detex.ct
END_CTFILE=        end of detex.ct
\end{verbatim}
\end{code}
and for \file{detex.l2x} as:
\begin{code}
\begin{verbatim}
C= ---------- file detex.l2x

TYPE= COMMAND
NAME= \lx
  START_TAG= "LTX2X"
END_TYPE

INCLUDE= detex.fl

TYPE= COMMAND
NAME= \ctab
  START_TAG= "command table"
END_TYPE

C= ---------- end of file detex.l2x
END_CTFILE=         end of file detex.l2x
\end{verbatim}
\end{code}
and lastly \file{detex.fl} is:
\begin{code}
\begin{verbatim}
C= ---------- file detex.fl

TYPE= COMMAND
NAME= \fl
  START_TAG= "FLaTTeN"
END_TYPE

C= ---------- end file detex.fl
END_CTFILE=         end file detex.fl
\end{verbatim}
\end{code}
    Then, as far as \lx\ is concerned, the original \file{detex.ct} file is 
treated as though it had been written as:
\begin{code}
\begin{verbatim}
C=  ----------file detex.ct
...
C= ---------- file detex.l2x

TYPE= COMMAND
NAME= \lx
  START_TAG= "LTX2X"
END_TYPE

C= ---------- file detex.fl

TYPE= COMMAND
NAME= \fl
  START_TAG= "FLaTTeN"
END_TYPE

C= ---------- end file detex.fl

TYPE= COMMAND
NAME= \ctab
  START_TAG= "command table"
END_TYPE

C= ---------- end of file detex.l2x
...
C= ----------end of detex.ct
END_CTFILE=        end of detex.ct
\end{verbatim}
\end{code}

    Note that nasty things will happen if you have a cycle of inclusions. That is,
you must not have anything similar to file \file{A} including file \file{B} which
includes file \file{C} which in turn includes either file \file{A} or \file{B}.

\subsection{Interpreter commands} \label{sec:intcom}

    \lx\ includes an interpreter for a procedural programming language
that is based on the ISO international standard \Express{} information
modeling language~\cite{EBOOK, EXPRESSIS}. At the moment the
programming language within \lx{} is anonymous, but for ease
of reference I will call it \ExpressA 
(\Express{} -Almost? -Approximate? -Anonymous?). 
The \ExpressA{} language is described later in section~\ref{sec:expressa}, 
but for now it is sufficient to know the commands that signify
the start and end of this code.

    The command \keyword{CODE\_SETUP=} indicates the commencement of
code to be run before any document processing occurs. The
\keyword{END\_CODE} command signifies the end of this code block.
This block should be placed in the \ctab{} before any other commands
except for the \keytext{ESCAPE...} commands, if any. This block
can contain variable declarations, function and procedure declarations,
and statements.

    Code consisting purely of statements can be placed anywhere that
a tagging action may be specified. These statements are enclosed
between a \keyword{CODE:} and \keyword{END\_CODE} pair of commands.

    The \ExpressA{} language is described in detail in \ref{sec:expressa}, 
but to give a flavour of it here 
is a simple possible application. It has been noted that \lx{} will find
difficulty in processing the contents of the \LaTeX{} \keytext{picture}
environment. The following portions of a \ctab{} write the contents
of a \keytext{figure} environment to an external file and uses the programming
language to keep a count of the number of figures so processed.

\begin{code}
\begin{verbatim}
c=  declare and initialise a variable
CODE_SETUP=
  LOCAL
    fignum : INTEGER;
  END_LOCAL;
  fignum := 0;
END_CODE

c= write figure contents to an external file
TYPE= BEGIN_ENV
NAME= figure
  OPT_PARAM= FIRST
  PRINT_OPT= NO_PRINT
  PC_AT_START= TO_FILE figs.tmp
  START_TAG=
    CODE:
      fignum := fignum + 1;           -- increment figure counter
      println;                        -- print a blank line
      println('%%% FIGURE ', fignum); -- write counter as a LaTeX comment
    END_CODE
    STRING: "\begin{figure}"
END_TYPE

c= close figure environment, back to normal output, and output
c= text indicating that a figure should be here
TYPE= END_ENV
NAME= figure
  PC_AT_START= RESET
  START_TAG=
    SWITCH_TO_FILE: figs.tmp
    STRING: "\end{figure}?n?n"
    SWITCH_BACK:
    CODE:
      println;
      println('PLACE FOR FIGURE ', fignum);
      println;
   END_CODE
END_TYPE
\end{verbatim}
\end{code}

\section{The LTX2X program} \label{sec:program}

    \lx\ is written using flex and bison. The resulting C code should
compile on any system. More details are given later, but for the end-user
the next section describes how to run the program, assuming that is available
on your system.

\subsection{Running LTX2X}

    The syntax for running the compiled version of \lx\ is:
\begin{verbatim}
ltx2x [-c] [-f table-file] [-p number] [-w] [-D dir_cat_char]
      [-P path_seperators] [-S]
      [-i number] [-l number] [-t] [-y number] [-C] [-E] 
      input-file output-file
\end{verbatim}
where elements in square brackets are options. The options fall into
two groups, one for the casual user and the other for those who may be
interested in the internals of \lx. The first group of options includes:
\begin{description}
\item[{\tt -c}] By default, \lx\ ignores all \LaTeX\ comments in the input
     file. This option causes \lx\ to write the comments to the output file.

\item[{\tt -f}] By default, \lx\ reads the \ctab\ from a file called
     \verb|ltx2x.ct|. If the required \ctab\ is in a file with another name this
     option is used to change from the default file. For example,
\begin{verbatim}
> ltx2x in.tex out.l2x
\end{verbatim}
reads a \ctab\ from \verb|ltx2x.ct|, while
\begin{verbatim}
> ltx2x -f detex.ct in.tex out.l2x
\end{verbatim}
reads a \ctab\ from file \verb|detex.ct|.

\item[{\tt -p}] This option causes \lx\ to  `pretty print' the output file
     (as far as it is able to). The \verb|number| is required and it indicates 
     the desired maximum number
     of characters per output line. If this is considered to be too small,
     then \lx\ chooses a value. Note that pretty printing is only applied
     to the source file --- not to any replacement tags. That is, it
     only tries to format the running text from the source file.

\item[{\tt -w}] By default, \lx\ outputs source white space just at it reads it.
     This option causes \lx\ to collapse any amount of contiguous white space
     to a single space. The \verb|-p| option includes the \verb|-w| option.

\item[{\tt -D}] The value of this option is the character that the operating system
    uses to catenate directory names to form a path (see~\ref{sec:search}). 
    The default value is a
    slash (i.e. \verb|/|). The default could be changed to a backslash, for example,
    by \verb|-D \|.

\item[{\tt -P}] The environment variable (see~\ref{sec:search})
   contains a list of directories (also known
   as path names). In the operating system that I use, these are separated by
   the colon (\verb|:|) character which, together with the semi-colon
   and space characters, form
   the \lx\ default separators. The path separator characters can be changed 
   with this option.
   For example, \verb|-P :| will make the separators be a colon
   or a space (space is automatically included in the separator list).

\item[{\tt -S}] This option enables the source level debugger 
   (see~\ref{sec:sld}) for any embedded \ExpressA{} code.
\end{description}

The second group of options are principally for those who might be
extending the \lx{} system.

\begin{description}
\item[{\tt -i}] This produces information that may be useful for debugging
     the \ExpressA{} interpreter. \verb|number| is an integer 
     between 1 and 9 inclusive.
     The greater the number, the more diagnostics are generated.

\item[{\tt -l}] This produces information that may be useful for debugging
     the \lx{} program. \verb|number| is an integer between 1 and 9 inclusive.
     The greater the number, the more diagnostics are generated.

\item[{\tt -t}] This generates diagnostics related to the processing of
    the \ctab\ file.

\item[{\tt -y}] Like the \verb|l| option, but produces diagnostic information
     from the parser (this is actually a null option, but may be useful
     in the future).

\item[{\tt -C}] Disable any interpreter debugging information during
     the code generation pass. This is not necessary unless the {\tt -i}
     option is used.

\item[{\tt -E}] Disable any interpreter debugging information during
     the code execution processing.This is not necessary unless the {\tt -i}
     option is used.

\end{description}




    \lx\ first reads the specified \ctab\ file, together with any included files,
looking first in the current directory, then in the directories specified by
the environment variable (if it exists).
 It then reads the \verb|input-file| from the current directory,
performs the actions specified in the \ctab\ and outputs the results to
the \verb|output-file|.

    Three other files are also generated.
\begin{itemize}
\item \verb|ltx2xct.lis| --- This contains a human-readable form of the internal
     representation of the \ctab. It may be useful if there are any errors
     in the original \ctab.
\item \verb|ltx2x.err| --- This contains any error or warning messages generated
     by \lx. It also contains any diagnostic information that may have been
     requested via a command line option.
\item \verb|interp.csg| --- This contains a human readable listing of the
     internal byte code generated by the \ExpressA{} interpreter.
\end{itemize}

    When \lx\ is running normally it prints out a counter to the terminal
indicating how many hundreds of input source file lines it has processed.
Lack of such output is an indication that the program may be in a loop and
chewing up CPU cycles to no avail. In this case, stop the program and examine
the output for indications of where the trouble is occurring.

    A limited number of errors are allowed when processing the \ctab\
and the input \LaTeX\ file before \lx\ gives up and quits. In
particular, if it is reading a \ctab\ file that includes another file, say one
called \file{zilch},
that it cannot read, it prints the following message to the user's terminal.
\begin{verbatim}
Can't open file zilch
Enter new file name, or I to ignore, or Q to quit
: 
\end{verbatim}
A {\tt Q} (or {\tt q}) response stops \lx\ from any further processing. An
{\tt I} (or {\tt i}) response causes \lx\ to stop looking for the included file 
and continue processing the current file. Any other response is taken to be
the name of an included file, which \lx\ then tries to read. If it fails, then
the above message is repeated. The user is given a limited number of opportunities
to identify a readable file before \lx\ quits altogether with this message:
\begin{verbatim}
Last attempt. Can't open file zilch. I'm giving up.
\end{verbatim}


    Regarding performance, the time taken by \lx\ to process a document does
not appear to be significantly different from the time to \LaTeX\ the 
same document.

\subsubsection{Directory searching} \label{sec:search}

    The program employs a search algorithm to find files that are not
in the current directory. It first looks in the current directory and
if a file of the given name is found, then that is used. If the file
is not found, then it searches for it among directories that are
specified in a system environment variable. This variable specifies
a list of pathnames, where the directories forming the path are
combined using a catenation character. For example, \verb|dir1/dir2/dir3|
could be a pathname, where the slash (\verb|/|) is the catenation character.
If it is looking for file \verb|afile.txt| it will catenate the file name
to the path name (e.g. \verb|dir1/dir2/dir3/afile.txt|) and look for
that. The pathnames in the list are separated by another character
(in fact it can be one from a list of characters). For example here
is a list of two pathnames; \verb|dir1/dir2;dir1/dir4|, where the semi-colon
(\verb|;|) is the pathname separator.

    By default, the program uses a slash (\verb|/|) as the directory
catenation character and the pathname separators can be a space, or
a colon or a semi-colon (i.e., any of \verb*| :;|). All these characters
can be altered via the program command line options, and should be set to
match the conventions of your operating system.

    The environment variable used by the program is LTX2XTABLES. 
On the operating system that I use, I set this in my login file like:
\begin{verbatim}
setenv LTX2XTABLES .:/dir1/dir2:/dir3/dir4
\end{verbatim}
Your system may have different conventions. Note that if the environment
variable is not set, only files in the current directory are considered.


\subsection{System components}

    The system consists of five main components --- a lexer, a parser, a
library of support functions and \ctab\ parsing code, a 
user-defined library of functions, and an interpreter for the \ExpressA{}
language.

\subsubsection{The lexer}

    The lexer is generated by flex~\cite{LEVINE92} 
(a more functional version of lex~\cite{LESK75}). 
The source for the lexer is in file \file{l2x.l}.
Its principal function is to read a \LaTeX\
source file and recognize \LaTeX\ commands. In general, it passes off the
relevant command tokens to the parser for performing appropriate actions.

    However, the lexer does do some processing of the source itself.
\begin{itemize}
\item It recognizes \LaTeX\ comments (i.e. any text starting with a percent
      sign and ending with a newline). It silently ignores these comments, unless
      run with the \verb|-c| option.
\item It recognizes the `standard' \LaTeX\ verbatims. It will write out the
      appropriate tags at the start and end of the verbatim text, while writing
      the verbatim text directly to the output file, bypassing the parser.
      It does not distinguish between the unstarred and starred
      versions of the verbatims.
\item Newlines and whitespace are directly written to the output file, again
      bypassing the parser. The \verb|-p| and \verb|-w| options control the
      final appearance of whitespace, newlines and paragraphing.
\item It recognizes the command \verb*|\ | i.e. a backslash followed by a
      space, and writes the tags defined in the \verb|SLASH_SPACE| specification
      directly to the output file.
\end{itemize}

    The lexer is designed to recognize four kinds of \LaTeX\ commands.
\begin{itemize}
\item Commands of the form \verb|\begin{environment}|
\item Commands of the form \verb|\end{environment}|
\item Any other command of the form \verb|\command|
\item As special cases, commands that are 'verbatim-like' or `verb-like'.
\end{itemize}
When it finds a command, it looks up the command or environment name in the
\ctab\ and sends the appropriate token and its \ctab\ location to the
parser. As a special case the contents of verbatim-like environments and
the argument of verb-like coommands are processesd within
the lexer and not sent to the parser.


\subsubsection{The parser}

    The parser is generated by bison~\cite{LEVINE92} 
(a more powerful version of YACC~\cite{JOHNSON75}). 
The source for the parser is in file \file{l2x.y}.
Essentially it defines a very simple
grammar for a \LaTeX\ document. That is, the grammar is limited to
generic kinds of commands and command arguments. It does not understand
the `meaning' of any of the commands or arguments.

    When the parser receives a token from the lexer it tries to match it with
one of the grammar rules, performing the actions specified by the \ctab.
Here is an extract from the parser grammar file, \file{l2x.y},
for a \LaTeX\ command that has
two required arguments followed by an optional argument.

\begin{code}
\begin{verbatim}
l2xComm2Opt: COMMAND_2_OPT
        {
          start_with_req($1);
        }
        ReqParam
        {
          action_p_p1($1,1);
        }
        ReqParam
        {
          action_p_opt($1,2);
        }
        OptParam
        {
          action_last_opt($1);
        }
        ;
\end{verbatim}
\end{code}

    The actions are enclosed in braces, and are interspersed with the elements
of the grammar.

    The token \verb|COMMAND_2_OPT| indicates that the lexer has found a command
that takes two required arguments followed by an optional argument. 
The parser then performs some
actions. The \verb|start_with_req| function is the standard \lx\ function
for the first action in a command production where the final argument
is optional. The \verb|$1| refers to the
location of the particular command in the \ctab, and its value is passed to the
parser by the lexer.

    The parser then expects a required argument (i.e. \verb|{|, token \verb|LBRACE|)
as the start of the required argument, followed by the text of the argument and
finished off by a right brace (i.e. \verb|}|, token \verb|RBRACE|); the grammar
for all of this is specified in the production called \verb|ReqParam|). If it
finds these it performs some further actions, otherwise it reports an
error.
In this case the action is defined by the function \verb|action_p_p1|,
which is the standard action performed between two required arguments
(the second argument in the function call specifies the Pth argument that
has been recognized). Another required argument is then expected.
In this case the action is defined by the function \verb|action_p_opt|,
which is the standard action performed between the end of the Pth required argument
and the start of an optional argument. It then looks for an optional
argument, the grammar for which is specified in the production called
\verb|OptParam|. The final action is specified by the standard function
\verb|action_last_opt| for finishing off a command that ends with an
optional argument.

    The grammar for a command that that has two required arguments, 
and possibly an
initial optional argument is similar:

\begin{code}
\begin{verbatim}
l2xComm2: COMMAND_2
        {
          start_with_opt($1);
        }
        OptParam
        {
          action_opt_first($1);
        }
        ReqParam
        {
          action_p_p1($1,1);
        }
        ReqParam
        {
          action_last_p($1,2);
        }
        ;
\end{verbatim}
\end{code}

\subsubsection{The support libraries}

    Source code for the C main program and support functions is in file
\file{l2xlib.c}. 
    The main program is responsible for reading in the \ctab\ and calling the
lexer and parser to do the appropriate processing.
    The file also contains a variety of support functions that are, or could 
be, used in the lexer, parser, action library, or user-defined library.

    The standard actions for the grammar are contained in file \file{l2xacts.c}.

\subsubsection{The user-defined library}

    The intent of this library is that masochistic users can define their
own functions for use within \lx\ when processing their \verb|SPECIAL_|
commands, without having to modify the \lx\ support or action libraries.

    Source code for the user-defined library should be maintained in a file
called \file{l2xusrlb.c} and a corresponding header file called \file{l2xusrlb.h}.

\subsubsection{The \ExpressA{} interpreter}

    The \ExpressA{} interpreter is based on algorithms originally developed
by Ronald Mak~\cite{MAKR91} for interpreting Pascal. His original
algorithms have been modified and extended to cater for \ExpressA.
The interpreter module has a minimal interface with the rest of the
\lx{} system, and could easily be modified to be a stand-alone program
(in fact it started that way in the first place). The interface
between \lx{} and the interpreter is confined to the small
\file{l2xistup.c} file.


\section{The \ExpressA{} programming language} \label{sec:expressa}

    \Express{} is a language for information modeling and includes both
declarative and procedural aspects~\cite{EBOOK}. There are also two other
companion languages called respectively \ExpressG{} and \ExpressI. The former
of these is a graphical form of the declaritive aspects of \Express, and the
later is an instiation and test case specification language. These languages
are either ISO international standards~\cite{EXPRESSIS} or on the way
to becoming so~\cite{EXPRESSITR}.

    Certain of the procedural aspects of \Express{} and \ExpressI{} are relevent
to the \lx{} concepts and so, together with some other reasons,
it seemed appropriate to provide an interpreter for a similar language for
use within \lx. \ExpressA{} provides a major subset of the \Express{} procedural
language, together with some Pascal-like additions for input and output.
Of particular note, strings are a built-in type in \ExpressA. The language
also supports three-valued logic and the concept of an `indeterminate'
value of any type.

    Earlier I gave an example \ctab{} to replace the text of a
\LaTeX{} document with the words `Goodbye document'. Here is
an \ExpressA{} program that outputs `Goodbye document'.
\begin{code}
\begin{verbatim}
println('Goodbye document');
END_CODE
\end{verbatim}
\end{code}

    The following gives a brief overview of \ExpressA. For more details
consult Schenck \& Wilson~\cite{EBOOK}.

\subsection{Basic elements}

    \ExpressA{} is a case-insensitive language and uses the ASCII character
set. Two kinds of comments are supported --- an end of line comment,
which starts with a \verb|--| pair and continues until the end of the current
line --- and an extended comment. An extended comment starts with a
\verb|(*| pair and is ended by a matching \verb|*)| pair; extended comments
may be nested.

    The language contains many reserved words, some of which are only
applicable to the \Express{} and \ExpressI{} languages.

    Identifiers are composed of an initial letter, possibly followed by
any number of letters, digits, and the underscore character.

    Literals are self defining constant values. An integer literal consists
of one or more digits, the first of which shall not be zero. Real numbers
start with one or more digits, followed by a decimal point. Further
digits may occur after the point, and finaly there may be an exponent
in the `e' notation format (e.g., \verb|123.456e-78|).

    A string literal is any sequence of characters enclosed by single
quote marks. If a single quote mark is meant to form part of the string,
two quote marks must be used at that point.

    Logical literals consists of one of these keywords: \keyword{FALSE},
\keyword{UNKNOWN} or \keyword{TRUE}.

    \ExpressA{} also includes some other constants. \keyword{PI} stands
for the value of the mathematical constant $\pi$ (3.1415\ldots), 
and \keyword{CONST\_E}
stands for the value of the mathematical constant $e$ (2.7182\ldots), 
the base of natural logarithms. The special token \verb|?| stands for
an indeterminate value of any type. The three constants
\keyword{THE\_DAY}, \keyword{THE\_MONTH} and \keyword{THE\_YEAR} are integer
values for the current date holding the day of the month (1--31), the
month of the year (1--12) and the year (four digits), respectively.


\subsection{Data types}

    \ExpressA{} is a typed language. The simple data types are: \keyword{INTEGER},
\keyword{REAL}, \keyword{STRING} and \keyword{LOGICAL}.

    The aggregation data types are \keyword{ARRAY}, \keyword{BAG}, \keyword{LIST},
and \keyword{SET}. The array data type is of a fixed size and must have
declared lower and upper bounds (index range), such as \verb|ARRAY [-7:10] OF|.
The other aggregate data types are dynamic in size, but may have lower and
upper bounds specified for the number of elements, such as \verb|SET [2:5] OF|,
meaning a set that should have between two and five members. For the
dynamic aggregates the upper bound may be given as \verb|?|, which means
an unlimited upper bound, such as \verb|LIST [2:?] OF|. If a bound
specification is absent, then the dynamic aggregate can hold from zero
to any number of elements.\footnote{The dynamic aggregates may not
be fully implemented due to lack of time.}

    Aggregates are one dimensional, but can be chained together for 
multi-dimensional aggregates, like 
\begin{code}
\begin{verbatim}
ARRAY [1:4] OF LIST OF INTEGER;
\end{verbatim}
\end{code}

    The enumeration data type is a parenthesised comma seperated list of
identifiers. These identifiers represent the values of the enumerated type;
for instance
\begin{code}
\begin{verbatim}
ENUMERATION OF (red, green, blue)
\end{verbatim}
\end{code}

    A defined data type is one declared and named by the user using the
\keyword{TYPE} and \keyword{END\_TYPE} construct. For example
\begin{code}
\begin{verbatim}
TYPE length = REAL; END_TYPE;
TYPE crowd_size = INTEGER; END_TYPE;
TYPE signal_colour = ENUMERATION OF (red, amber, green); END_TYPE;
\end{verbatim}
\end{code}

  An entity data type consists of a list of attributes and their types, enclosed
in a \keyword{ENTITY} and \keyword{END\_ENTITY} pair. An entity type
is named.
\begin{code}
\begin{verbatim}
ENTITY an_ent;
  auditorium_width : length;
  audience         : crowd_size;
  title            : STRING;
  profit           : REAL;
END_ENTITY;
\end{verbatim}
\end{code}

   \ExpressA{} provides for algorithms in the form of functions and procedures.

A \keyword{FUNCTION} is an algorithm that operates on parameters and returns
a single resultant value of a specified data type. An invocation of a function
in an expression evaluates to the resultant value at the point of invocation.
For example:
\begin{code}
\begin{verbatim}
FUNCTION func (par1 : INTEGER; par2 : STRING) : STRING;
  LOCAL
    str : STRING;
    -- other variable declarations
  END_LOCAL;
  -- the algorithm statements are here
  RETURN(str);
END_FUNCTION;
\end{verbatim}
\end{code}
Note that the parameters are typed.

    A \keyword{PROCEDURE} is an algorithm that receives parameters from the 
point of invocation and operates on them in some manner. Changes to the parameters
within the procedure are only reflected to the point of invocation
when the formal parameter is preceded by the keyword \keyword{VAR}.
For example:
\begin{code}
\begin{verbatim}
PROCEDURE proc (par1 : INTEGER; VAR par2 : STRING);
  -- local declarations and the algorithm statements
END_PROCEDURE;
\end{verbatim}
\end{code}
Note that the parameters are typed. In this case the value of \keytext{par2}
may be changed.

    Variables are declared in a local block, enclosed by the keywords
\keyword{LOCAL} and \keyword{END\_LOCAL}. A variable declaration consists
of an identifer and its type, such as:
\begin{code}
\begin{verbatim}
LOCAL
  str    : STRING;
  e1, e2 : an_ent;     -- e1 and e2 are both of type an_ent
  e3     : an_ent;     -- so is e3
  num    : INTEGER;
  col    : signal_colour;
  matrix : ARRAY [1:15] OF ARRAY [1:15] OF REAL;
END_LOCAL;
\end{verbatim}
\end{code}

   The above declarations must be in the following order:
\begin{enumerate}
\item \keyword{ENTITY} and/or \keyword{TYPE} declarations
\item \keyword{FUNCTION} and/or \keyword{PROCEDURE} declarations
\item a \keyword{LOCAL} declaration block
\end{enumerate}

    After the above can come any number of statements.

\subsection{Statements}

    \ExpressA{} supports the following statements:
\begin{itemize}
\item Null statement
\item Assignment statement
\item Call statement
\item \keyword{BEGIN} \ldots \keyword{END} compound statement
\item \keyword{CASE} \ldots \keyword{END\_CASE} statement
\item \keyword{IF} \ldots \keyword{THEN} \ldots \keyword{ELSE} 
      \ldots \keyword{END\_IF} statement
\item \keyword{REPEAT} \ldots \keyword{WHILE} \ldots \keyword{UNTIL} \ldots
      \keyword{END\_REPEAT} statement. This also includes the
      \keyword{ESCAPE} and \keyword{SKIP} statements
\item \keyword{RETURN} statement
\end{itemize}

    All the above statements are completed by a \keytext{;} (semicolon).
The \emph{null} statement just consists of a semicolon.

    The \emph{assignment} statement is used to assign an instance to a local
variable or parameter. The data types must be compatible.
\begin{code}
\begin{verbatim}
LOCAL
  a, b, c : REAL;
END_LOCAL;
...
  a := 2.3E-6;
  b := a;
  a := -27.0;
  c := 33.3*b;
\end{verbatim}
\end{code}

    The \emph{call} statement invokes a procedure or a function. The actual
parameters provided with the call must agree in number, order and type
with the formal parameters specified in the procedure or function
declaration. The supplied parameter values must be assignment compatible
with the formal parameters. This is an example of calling the \ExpressA{}
defined \keyword{INSERT} procedure which takes three parameters:
\begin{code}
\begin{verbatim}
INSERT(my_list, list_element, 0);
\end{verbatim}
\end{code}

    The \emph{compound} statement consists of one or more statements enclosed
between a \keyword{BEGIN} and \keyword{END} pair. The enclosed statements
are treated as a single statement.
\begin{code}
\begin{verbatim}
...
  BEGIN
    a := 2.3e-7;
    b := a;
    c := b*33.3;
  END;
\end{verbatim}
\end{code}

    The \emph{case} statement is a means of selectively executing statements
based on the value of an expresion.
\begin{code}
\begin{verbatim}
LOCAL
  a : INTEGER;
  x, y : REAL;
END_LOCAL;
...
  a := 2;
  x := 21.9;
  CASE 2*a OF
    1         : x := SIN{x};
    2         : x := SQRT(x);
    3         : x := LOG(x);
    4         : x := COS(x);  -- this is executed
    5, 6      : y := y**x;
    OTHERWISE : x := 0.0;
  END_CASE;
\end{verbatim}
\end{code}
The integer expression following the \keyword{CASE} keyword is evaluated. 
The result is compared to the values of the case labels and the 
statement following the first matching label is executed. Execution then
continues at the statement following the \keytext{END\_CASE;}. If no
label matches, then no statements within the case block are executed,
except if an \keyword{OTHERWISE} label is included, which will match
anything. All other labels are examined before looking for the
\keyword{OTHERWISE}.

    The \emph{if \ldots then \ldots else} statement allows the conditional
execution of statements depending on the value of a \keyword{LOGICAL}
expression. When the expression evaluates to \keyword{TRUE} the statement(s)
following the \keyword{THEN} are executed, after which control passes
to the statement following the closing \keyword{END\_IF}. When the logical
expression evaluates to \keyword{FALSE} or \keyword{UNKNOWN} the \keyword{THEN}
statements are jumped over and execution starts at the statement(s) following
the \keyword{ELSE} keyword if present, or at the statement following
the \keyword{END\_IF} keyword.
\begin{code}
\begin{verbatim}
IF a > 20 THEN
  b := a + 2;
  c := c - 1;
ELSE
  IF a > 10 THEN
    b := a + 1;
  ELSE
    c := c + 1;
  END_IF;
END_IF;
\end{verbatim}
\end{code}

    The \emph{repeat} statement is used to control the conditional repetition
of a series of statements. The control conditions are:
\begin{itemize}
\item finite iteration until an integer expression reaches a specified
      value;
\item \keyword{WHILE} a logical condition is \keyword{TRUE};
\item \keyword{UNTIL} a logical condition is \keyword{TRUE}.
\end{itemize}
\begin{code}
\begin{verbatim}
REPEAT i := 100 TO 0 BY -7 WHILE r >= 0.0 UNTIL err < 1.0e-8;
  ...
  r := ...;
  err := ...;
END_REPEAT;
\end{verbatim}
\end{code}
At entry to the \keyword{REPEAT} statement the iteration variable is 
initialized to the first bound. If the variable less than or equal to the
\keyword{TO} bound and the increment is positive, or the variable is less
than the \keyword{TO} bound and the increment is negative, processing
jumps to after the \keyword{END\_REPEAT}, otherwise processing continues.
The \keyword{WHILE} condition is checked and if 
\keyword{TRUE} then the statements in the body are executed. After
these have been executed the \keyword{UNTIL} condition is checked. If this
is not \keyword{TRUE} then processing continues by incrementing the iteration
variable by either unity or by the \keyword{BY} value if present. The whole
process then starts again with the checking of the iteration variable
against the \keyword{TO} bound.

   All three types of controls are optional. If none are given then the
\keyword{REPEAT} statement will loop for ever. The \emph{escape} statement
causes an immediate transfer out of the \keyword{REPEAT} statement
in which it occurs. The \emph{skip} statement causes a jump to the
end of the \keyword{REPEAT} statement in which it occurs (i.e., to the point
where the \keyword{UNTIL} expression is tested).
\begin{code}
\begin{verbatim}
REPEAT UNTIL (a = 1);
  ...
  IF a = 0 THEN 
    ESCAPE;
  END_IF;
  ...
  IF a > 10 THEN
    SKIP;
  END_IF;
  ...
  ...
-- SKIP transfers control to here
END_REPEAT;
 -- ESCAPE transfers control to here
\end{verbatim}
\end{code}

    The \emph{return} statement terminates the execution of a \keyword{FUNCTION}
or \keyword{PROCEDURE}. The \keyword{RETURN} statement within a function
must specify an expression, the value of which is the value returned
by the function. A \keyword{RETURN} in a procedure must not specify an
expression.
\begin{code}
\begin{verbatim}
RETURN(a <> b);  -- example for within a function
RETURN;          -- example for within a procedure
\end{verbatim}
\end{code}

\subsection{Expressions}

    Expressions are combinations of operators, operands and function calls
which are evaluated to produce a value. The simplest expression is
either a literal value or the name of a variable.

\subsubsection{Arithmetic operators}

    The arithmetic operators act on number values and produce a number
result. If any operand
is indeterminate (i.e., \verb|?|) then the result is also
indeterminate. The operators are:
\begin{description}
\item[Unary] The operators \verb|+| and \verb|-|, the latter of which
negates its following operand.
\item[Binary] Addition (\verb|+|), 
   subtraction (\verb|-|), 
   multiplication (\verb|*|), 
   real division (\verb|/|), 
   exponentiation (\verb|**|),
   integer division (\keyword{DIV}),
    and modulo (\keyword{MOD}).
\end{description}

\subsubsection{Relational operators}

    The result of a relational expression is a \keyword{LOGICAL} value.
If either operand is indeterminate, the expression evaluates to
\keyword{UNKNOWN}.

\begin{description}
\item[Value comparison] Equal (\verb|=|), 
     not equal (\verb|<>|),
     greater than (\verb|>|),
     less than (\verb|<|),
     greater than or equal (\verb|>=|), and
     less than or equal (\verb|<=|).

\item[Membership] The \keyword{IN} operator tests an item for membership
in a dynamic aggregate (e.g., \keytext{IF fred IN mylist THEN ...}).

\item[Matching] The \keyword{LIKE} operator compares a string against a pattern,
evaluating to \keyword{TRUE} if they match. The pattern characters are:
  \begin{itemize}
  \item \verb|@| Matches any letter.
  \item \verb|^| Matches any upper-case letter.
  \item \verb|?| Matches any character.
  \item \verb|&| Matches remainder of string.
  \item \verb|#| Matches any digit.
  \item \verb|$| Matches a substring terminated by a space character 
                 or end-of-string.
  \item \verb|*| Matches any number of characters.
  \item \verb|\| Begins a pattern escape sequence.
  \item \verb|!| Negation character (used with the other characters).
  \item Any other character matches itself.
  \end{itemize}
  
  Some examples:
  \begin{itemize}
  \item \verb|'The quick red fox' LIKE '$$$$'| is \keyword{TRUE}.
  \item \verb|'Page 231' LIKE '$ ###'| is \keyword{TRUE}.
  \item \verb|'Page 27' LIKE 'Page ###'|  is \keyword{FALSE}.
  \item \verb|'\aaaa' LIKE '\\aaaa'| is \keyword{TRUE}.
  \item \verb|'\aaaa' LIKE '\aaaa'| is \keyword{FALSE}.
  \item \verb|'aaaa' LIKE 'a@@a'| is \keyword{TRUE}.
  \end{itemize}

\end{description}

\subsubsection{Logical operators}

    The logical operators produce a logical result. Except for the
\keyword{NOT} operator which takes one logical operand (e.g., \keytext{NOT op}), 
they take two logical operands (e.g., \keytext{op1 XOR op2}).

    The evaluation of the \keyword{NOT} operator is given in table~\ref{tab:not}.

\begin{table}
\centering
\caption{The NOT logical operator} \label{tab:not}
\begin{tabular}{|c|c|} \hline
Operand value & Result value \\ \hline
\keyword{TRUE} & \keyword{FALSE} \\
\keyword{UNKNOWN} & \keyword{UNKNOWN} \\
\keyword{FALSE} & \keyword{TRUE} \\ \hline
\end{tabular}
\end{table}

    The evaluation of the \keyword{AND}, \keyword{OR} and \keyword{XOR} operators 
is given in table~\ref{tab:andorxor}.

\begin{table}
\centering
\caption{The AND, OR and XOR logical operators} 
\label{tab:andorxor}
\begin{tabular}{|c|c||c|c|c|} \hline
Op1            & Op2           & Op1 \keyword{AND} Op2 & Op1 \keyword{OR} Op2 & Op1 \keyword{XOR} Op2 \\ \hline
\keyword{TRUE} & \keyword{TRUE}       & \keyword{TRUE}    & \keyword{TRUE}    & \keyword{FALSE} \\
\keyword{TRUE} & \keyword{UNKNOWN}    & \keyword{UNKNOWN} & \keyword{TRUE}    & \keyword{UNKNOWN} \\
\keyword{TRUE} & \keyword{FALSE}      & \keyword{FALSE}   & \keyword{TRUE}    & \keyword{TRUE} \\
\keyword{UNKNOWN} & \keyword{TRUE}    & \keyword{UNKNOWN} & \keyword{TRUE}    & \keyword{UNKNOWN} \\
\keyword{UNKNOWN} & \keyword{UNKNOWN} & \keyword{UNKNOWN} & \keyword{UNKNOWN} & \keyword{UNKNOWN} \\
\keyword{UNKNOWN} & \keyword{FALSE}   & \keyword{FALSE}   & \keyword{UNKNOWN} & \keyword{UNKNOWN} \\
\keyword{FALSE} & \keyword{TRUE}      & \keyword{FALSE}   & \keyword{TRUE}    & \keyword{TRUE} \\
\keyword{FALSE} & \keyword{UNKNOWN}   & \keyword{FALSE}   & \keyword{UNKNOWN} & \keyword{UNKNOWN} \\
\keyword{FALSE} & \keyword{FALSE}     & \keyword{FALSE}   & \keyword{FALSE}   & \keyword{FALSE} \\ \hline
\end{tabular}
\end{table}

\subsubsection{Miscellaneous}

\begin{description}

\item[Function call] A function may be called without the result necessarily
being assigned to a variable. If \keytext{fun} is a function with two
arguments (for simplicitly integer arguments) and returning a logical
value, then
\begin{code}
\begin{verbatim}
log := fun(i1, i2);
fun(i3, 24*i4);
\end{verbatim}
\end{code}
are both legitimate calls.


\item[Dot operator] The dot operator is used to access an attribute from an 
entity. If \keytext{ent} is an \keyword{ENTITY} type with an attribute
\keytext{att}, then \keytext{ent.attr} evaluates to the value of the
\keytext{attr} attribute within the \keytext{ent}.

\item[String operators] \mbox{} \\
 The \verb|+| operator takes two strings as
its operands and evaluates to the string that is the concatenation of its
operands. For example:
\begin{code}
\begin{verbatim}
str1 := 'string1';
str2 := 'string2';
str1 := str1 + str2;
-- str1 = 'string1string2'   is TRUE
\end{verbatim}
\end{code}

    The substring operator \verb|[i1:i2]| is a postfix operator that when
applied to a string, evalutes to the string whose characters are composed
of the \verb|i1|'th through the \verb|i2|'th characters, inclusively,
of its operand. Note that \verb|i2| must be greater than or equal to
\verb|i1|, and both must be within the limits of the number of characters
in the string. For example:
\begin{code}
\begin{verbatim}
str1 := 'string';
str2 := str1[2:4];
str1 := str1 + str2;
-- str1 = 'tristring'   is TRUE
\end{verbatim}
\end{code}

\item[Aggregate operators] \mbox{} \\
    The index operator \verb|[i]| is a postfix operator that can be applied
to an aggregate operand; the expression evaluates to the value of the
aggregate at the index position. For example, if \keytext{lagg} is a
list of integers:
\begin{code}
\begin{verbatim}
insert(lagg, 20, 0);
insert(lagg, 40, 0);
insert(lagg, 60, 0);
insert(lagg, 80, 0);
-- lagg[2] = 60    is TRUE
\end{verbatim}
\end{code}

\item[Interval expression] \mbox{} \\
    An \emph{interval expression} is a \keyword{LOGICAL} expression 
consisting of three operands and two operators. It has the form:
\begin{code}
\begin{verbatim}
{ low op1 test op2 high }
\end{verbatim}
\end{code}
where \keytext{op1} and \keytext{op2} are either of the two relational
operators \verb|<| or \verb|<=|, and \keytext{low}, \keytext{test} and
\keytext{high} are expressions of the same type. The interval expression
is equivalent to:
\begin{code}
\begin{verbatim}
((low op1 test) AND (test op2 high))
\end{verbatim}
\end{code}
The value of the interval expression is given by
\begin{enumerate}
\item If any operand is indeterminate, then it evauates to \keyword{UNKNOWN}.
\item If either of the logical relationships evaluates to \keyword{FALSE},
      then it evauates to \keyword{FALSE}.
\item If both logical relationships evalute to \keyword{TRUE},
      then it evauates to \keyword{TRUE}.
\item Otherwise it evaluates to \keyword{UNKNOWN}.
\end{enumerate}
For example:
\begin{code}
\begin{verbatim}
i := 10;
{1 <= i < 20}  -- is TRUE
{1 <= i < 10}  -- is FALSE
i := ?;
{1 <= i < 10}  -- is UNKNOWN
\end{verbatim}
\end{code}


\end{description}


\subsection{Built in procedures and functions}

\subsubsection{Procedures}

    The following procedures are an integral part of \ExpressA. They are
shown as signatures to inidicate the data types of the formal parameters.
For convenience, \keytext{GENERIC} is used to indicate any type.

\begin{itemize}
\item \keytext{INSERT (VAR L:LIST OF GENERIC; E:GENERIC; P:INTEGER)} \\
      \keyword{INSERT} inserts the element \keytext{E} into a list
\keytext{L} at position \keytext{P}. The insertion follows the existing
element at \keytext{P}, so that if \keytext{P=0}, \keytext{E} will
become the first element.

\item \keytext{REMOVE (VAR L:LIST OF GENERIC; P:INTEGER)} \\
     \keyword{REMOVE} modifies the list \keytext{L} by deleting the
element at position \keytext{P}.

\item \keytext{SYSTEM (V:STRING)} \\
     \keyword{SYSTEM} passes the string \keytext{V} to the operating system.
This is typically used to get the operating system to perform some action.

\item \keytext{ READ(VAR V1, V2,...:GENERIC)}, \keytext{READLN(VAR V1, V2,...:GENERIC)} \\
     These two procedures are similar to the Pascal procedures of the same
     name and put data from standard input into the variable(s)
     \keytext{V1}, etc.

     The argument is a comma-seperated list of variables. The variables may be
     of different types, but the types are limited to 
     \keyword{INTEGER},
     \keyword{REAL},
     \keyword{LOGICAL}, and
     \keyword{STRING}.
     The procedure gets the next value of the variable's type from standard input
     and assigns it to the variable. An integer is recognised as a set of digits,
     optinally preceeded by a sign. A real is in either decimal or scientific
     notation (e.g., \verb|12.34| or \verb|1.234e1|). A logical is \verb|TRUE|, 
     \verb|FALSE| or \verb|UNKNOWN| (case independent, so \verb|TRUE| could also 
     be \verb|tRuE|). A string is
     any non-empty set of characters ended by white space (e.g., \verb*|string |
     is one string but \verb*|ball of str8 string | is four strings). The difference
     between \keyword{READ} and \keyword{READLN} is that the former performs the
     actions described above, while the latter will discard any remaining characters
     in the input line after processing its arguments.
     

\item \keytext{ WRITE(format)}, \keytext{WRITELN(format)} \\
     These two procedures are similar to the Pascal procedures of the same name.
     They write data to standard output.

     The \keytext{format} consists of a comma-seperated list of variables with
     optional spacing specifications. The variable types may be 
     \keyword{INTEGER},
     \keyword{REAL},
     \keyword{LOGICAL}, or
     \keyword{STRING}.
     The \keyword{LOGICAL} and \keyword{STRING} types take no spacing declarations.
     An \keyword{INTEGER} variable can take one optional space specification which
     is an integer number specifying the minimum field width for printing the value
     (e.g., \keytext{int:6} to specify a minimum field width of 6 characters).
     A \keyword{REAL} variable can take two optional space specifications. The
     first is the field width and the second is the number of digits to be printed
     (e.g., \keytext{r:10:5} for printing with a field width of 10 characters
      and to a pecision of 5 digits). For example:
\begin{code}
\begin{verbatim}
BEGIN_LOCAL
  int : INTEGER;
  r   : REAL;
  log : LOGICAL;
  str : STRING;
END_LOCAL;
  int := 23;
  r := 23.0;
  log := true;
  str := 'This is a string.';
WRITE('Example', int, r:10:5, ' ', log, ' ', str);
\end{verbatim}
\end{code}
will produce:
\begin{code}
\begin{verbatim}
Example      23     23.000 TRUE This is a string.
\end{verbatim}
\end{code}

    The difference between \keyword{WRITE} and \keyword{WRITELN} is that the latter
    will end the output line after it has output the values of its arguments.
    (\keyword{WRITELN} need take no arguments, in which case it justs ends the current
     output line).
     
\item \keytext{ PRINT(format)}, \keytext{PRINTLN(format)} \\
     These \keyword{PRINT} procedures are the same as the
     \keyword{WRITE} procedures, except that they send the data
      to the current \lx{} output destination.

\end{itemize}

\subsubsection{Functions}

    The following functions are supplied as part of \ExpressA. They are
exhibited as signatures to show the formal parameters. For convenience, 
\keytext{NUMBER} is being used to denote either an \keyword{INTEGER} or a
\keyword{REAL} number.

\begin{itemize}
\item \keytext{ABS (V:NUMBER) : NUMBER;} \\
      \keyword{ABS} returns the absolute value of its argument.
\item \keytext{COS (V:NUMBER) : REAL;} \\
       Returns the cosine of an an angle specified in radians.
\item \keytext{EOF () : LOGICAL;} \\
       Returns \keyword{TRUE} if the next character from standard input
       is `end-of-file', otherwise it returns \keyword{FALSE}.
\item \keytext{EOLN () : LOGICAL;} \\
       Returns \keyword{TRUE} if the next character from standard input
       is `end-of-line', otherwise it returns \keyword{FALSE}.
\item \keytext{EXISTS (V:GENERIC) : LOGICAL;} \\
       The function \keyword{EXISTS} returns \keyword{FALSE} if its
     argument is indeterminate or does not exist, otherwise it returns
     \keyword{TRUE}.
\item \keytext{EXP (V:NUMBER) : REAL;} \\
      Returns $e$ (the base of natural logarithms (\keyword{CONST\_E})) 
      raised to the power of \keytext{V}.
\item \keytext{HIBOUND (V:AGGREGATE OF GENERIC) : INTEGER;} \\
      \keyword{HIBOUND} returns the declared upper index of an \keyword{ARRAY}
       or the declared upper bound of a 
       \keyword{BAG}, \keyword{LIST} or \keyword{SET}.
\item \keytext{HIINDEX (V:AGGREGATE OF GENERIC) : INTEGER;} \\
      \keyword{HIINDEX} returns the declared upper index of an \keyword{ARRAY}
       or the number of elements in a 
       \keyword{BAG}, \keyword{LIST} or \keyword{SET}.
\item \keytext{LENGTH (V:STRING) : INTEGER;} \\
       Returns the number of characters in its argument.
\item \keytext{LOBOUND (V:AGGREGATE OF GENERIC) : INTEGER;} \\
      \keyword{LOBOUND} returns the declared lower index of an \keyword{ARRAY}
       or the declared lower bound of a 
       \keyword{BAG}, \keyword{LIST} or \keyword{SET}.
\item \keytext{LOG (V:NUMBER) : REAL;} \\
       Returns the natural logarithm of its argument.
\item \keytext{LOG2 (V:NUMBER) : REAL;} \\
       Returns the base 2 logarithm of its argument.
\item \keytext{LOG10 (V:NUMBER) : REAL;} \\
       Returns the base 10 logarithm of its argument.
\item \keytext{LOINDEX (V:AGGREGATE OF GENERIC) : INTEGER;} \\
      \keyword{LOINDEX} returns the declared lower index of an \keyword{ARRAY}
       or the value 1 for a 
       \keyword{BAG}, \keyword{LIST} or \keyword{SET}.

       The \keytext{..INDEX} functions are useful for iterating over
       aggregates. For example, if \keytext{lagg} is a list of integer,
       then all the elements can be printed out as a comma-seperated
       list enclosed in parentheses by:
\begin{code}
\begin{verbatim}
writeln;
write('lagg = (');
REPEAT i := LOINDEX(lagg) TO HIINDEX(lagg);
  IF (i = HIINDEX(lagg)) THEN write(lagg[i]:1);
  ELSE write(lagg[i]:1, ', ');
  END_IF;
END_REPEAT;
writeln(')');
\end{verbatim}
\end{code}
\item \keytext{NVL (V:GENERIC; SUBS:GENERIC) : GENERIC;} \\
       If the argument \keytext{V} exists then it is returned, otherwise
       the argument \keytext{SUBS} is returned. Both arguments must be
       of the same type.
\item \keytext{ODD (V:INTEGER) : LOGICAL;} \\
       Returns \keyword{TRUE} or \keyword{FALSE} depending on whether or not
       its argument is odd or even.
\item \keytext{REXPR (V:STRING; E:STRING) : LOGICAL;} \\
       This function tests whether the \keytext{V} string parameter
       matches a regular expression \keytext{E}. \keyword{REXPR}
       returns \keyword{TRUE} if there is a match, \keyword{FALSE}
       if there is not a match, or \keyword{UNKNOWN} if the regular
       expression is ill-formed.

       In the regular expression, most characters stand for themselves,
       but \verb|\| can be used to escape any of the meta-characters.
       \begin{itemize}
       \item The meta-characters \keytext{(} and \keytext{)} are used for
       grouping sub-expressions. 
       \item \verb?|? between expressions means one or the other.
       \item \keytext{+} following an expression means match one or more times.
       \item \keytext{*} following an expression means match zero or more times.
       \item \keytext{?} following an expression means match zero or one times.
       \item \verb|[...]| is an expression indicating that any of the enclosed
       characters are acceptable. 
       \item \verb|[^...]| is an expression indicating that any characters
       except those enclosed are acceptable.
       \item Within a bracket expression a range of characters can be
       specified by providing the first and last with a seperating hyphen.
       For instance, \keytext{[a-zA-Z]} will match any alphabetic character.
       \end{itemize}

       Some examples:
       \begin{itemize}
       \item \verb|[a-zA-Z]+| match one or more letters.
       \item \verb|[0-9]+.[0-9]+([eE][\-\+]?[0-9]+)?| match a floating
             point number \\
             (e.g., \verb|1.23e-27| or \verb|0.987|)
       \item \verb|[a-zA-Z][0-9a-zA-Z_]*| match an \ExpressA{} variable.
       \item \verb|[^0-9a-zA-Z]| match anything except letters or digits.
       \item \verb?(I|i)(F|f)? case insensitive match for the word \verb|IF|.
       \end{itemize}

\item \keytext{ROUND (V:NUMBER) : INTEGER;} \\
       Returns the nearest integer to its argument value.
\item \keytext{SIN (V:NUMBER) : REAL;} \\
       Returns the sine of an an angle specified in radians.
\item \keytext{SIZEOF (V:AGGREGATE OF GENERIC) : INTEGER;} \\
       \keyword{SIZEOF} returns the number of elements in its argument.
       When \keytext{V} is an \keyword{ARRAY} this is the declared number
       of elements. When \keytext{V} is a 
       \keyword{BAG}, \keyword{LIST} or \keyword{SET} this is the actual
       number of elements.
\item \keytext{SQRT (V:NUMBER) : REAL;} \\
       Returns the square root of its argument.
\item \keytext{TAN (V:NUMBER) : REAL;} \\
       Returns the tangent of an an angle specified in radians.
\item \keytext{TRUNC (V:NUMBER) : INTEGER;} \\
       Chops off any decimal part of its argument, returning the corresponding
       integer value.

\end{itemize}


\subsection{Source level debugger} \label{sec:sld}

    The \ExpressA{} interpreter includes a source level debugger for
use when your code appears to be misbehaving. When in operation the
debugger will prompt for a command to be entered. It understands
the following commands.

\begin{itemize}
\item \keytext{<return>} Continue processing.
\item \keyword{break} \keytext{<number>} Place a breakpoint at the statement
  on line \keytext{<number>}.
\item \keyword{break} Print the line numbers of all the breakpoints.
\item \keyword{unbreak} \keytext{<number>} Remove the breakpoint from line
  \keytext{<number>}.
\item \keyword{unbreak} Remove all breakpoints.
\item \keyword{trace} Turn on statement tracing.
\item \keyword{untrace} Turn off statement tracing.
\item \keyword{entry} Turn on tracing of entry to procedures and functions.
\item \keyword{unentry} Turn off entry tracing.
\item \keyword{exit} Turn on tracing of exits from procedures and functions.
\item \keyword{unexit} Turn off exit tracing.
\item \keyword{traceall} Turn on all tracing.
\item \keyword{untraceall} Turn off all tracing.
\item \keyword{stack} Turn on display of the runtime stack accesses.
\item \keyword{unstack} Turn off stack display.
\item \keyword{step} Turn on single-stepping.
\item \keyword{unstep} Turn off single stepping.
\item \keyword{fetch} \keytext{<variable>}  Print data fetches for 
    \keytext{<variable>}.
\item \keyword{store} \keytext{<variable>}   Print data stores for 
    \keytext{<variable>}.
\item \keyword{watch} \keytext{<variable>}   Print both data fetches and
    stores for  \keytext{<variable>}.
\item \keyword{watch} Print the names of all variables being watched.
\item \keyword{unwatch} \keytext{<variable>}  Remove the watch from
      \keytext{<variable>}.
\item \keyword{unwatch} Remove all watches.
\item \keyword{show} \keytext{<expression>} Print the value of 
       the \ExpressA{} expresion \keytext{<expression>}. The variables
       in the expression must have been declared in the \ExpressA{} code.
        For example:
\begin{code}
\begin{verbatim}
show (23.0 + LOG(num))/(PI*r**2)
\end{verbatim}
\end{code}
\item \keyword{assign} \keytext{<variable := expression>} Assign the value
    of \keytext{<expression>} to the \ExpressA{} variable \keytext{<variable>}.
    For example:
\begin{code}
\begin{verbatim}
assign num := SIN(theta/300.0) 
\end{verbatim}
\end{code}
\item \keyword{where} Print the current line number and the text of the
       next statement to be executed.
\item \keyword{kill} Terminate the execution of the \lx{} program.
\end{itemize}

\subsection{Example \ExpressA{} code}

    The following demonstrates most of the functionality of \ExpressA.
Most of this is not particularly interesting, except possibly for the
algorithms for calculating the date of Easter and for generating
magic squares.

\begin{code}
\begin{verbatim}
      c=        fun.ct  Test of CODE ltx2x

CODE_SETUP=
  ENTITY ent;
    attr1, attr3 : INTEGER;
    attr2 : STRING;
  END_ENTITY;

  TYPE joe = INTEGER;
  END_TYPE;

  TYPE colour = ENUMERATION OF (red, blue, green);
  END_TYPE;


PROCEDURE easter;
(* calculates the date of Easter for the present year 
   The algorithm can be applied to any year between 
   1900 and 2099 inclusive, but if so, then the year
   should be checked to ensure that it is within this range. *)
  LOCAL
    n, a, b, m, q, w : INTEGER;
    day : INTEGER;
    month : STRING;
  END_LOCAL;

  n := THE_YEAR - 1900;
  a := n MOD 19;
  b := (7*a + 1) DIV 19;
  m := (11*a + 4 - b) MOD 29;
  q := n DIV 4;
  w := (n + q + 31 - m) MOD 7;
  day := 25 - m - w;
  month := 'April';
  IF (day < 1) THEN
    month := 'March';
    day := day + 31;
  END_IF;
  writeln('In