[comp.text.tex] bnf macros

hugh@phoibos.cs.kun.nl (Hugh Osborne) (12/04/90)

A while back I posted a bnf.sty file to the net, in which an environment
is defined for printing context free grammars. A few people were kind
enough to report bugs (most of which I couldn't find a really
satisfactory solution for). For those of you who are using these macros
I am now posting this BUG REPORT.




1). The BNF macros cannot deal with environments that are started by one of
the optional parameters and ended by another. For example the following
example (due to Florence Maraninchi of LGI-IMAG Campus, BP 53X, 38041
Grenoble cedex, FRANCE) does not work.

This is a structural problem, that I do not have time to consider at the
moment. If anyone else solves it, will they please let me know?

________________________________________________________________________________
\newenvironment{tabu}{\begin{tabular}{ll}[t]}{\end{tabular}}

\begin{grammar}
        [(colon){\begin{tabu} : & }]
        [(period){\end{tabu}}]
        [(semicolon){\\ $\mid$ &  }]
        [(comma){  }]
        [(quote){\begin{bf}}{\end{bf}}]

        toto : expression.

        expression: constante; idf;expression operateur expression.

        operateur: "+" ; "-" ; "*" ; "/".

        constante: "0" ; "1" ; "2" ; "3" ; "4".

\end{grammar}

________________________________________________________________________________

2). The above example also made me aware of another problem that has
not yet been reported. If you try and include a command with an optional
parameter in one of the optional parameters of the grammar environment,
LaTeX will get horribly confused. For example, a grammar environment
such as

--------------------------------------------------------------------------------

\begin{grammar}
         [(colon){\rule[.5\lineskip]{10pt}{\lineskip}}]
 wrong : won't work.
\end{grammar}

won't work. This is due to TeX' macro expansion mechanism, and an
elegant solution would require a fairly major overhaul, which I don't
have time for at the moment.

A hack is to change the syntax of the optional commands of the grammar
environment so that the optional commands are delimited by some other
symbol than "[" and "]". If, for example, we were to choose "!" as our
(left _and_ right) delimiter, then lines 63, 65, 66 and 69 of the 
original BNF macros would have to be changed. They are currently
...............................................................................

\def\@dogrammarchars{
  \@ifnextchar [{\@getchardef}{\@defaultchardefs}}

\def\@getchardef[(#1)#2]{
  \@dogrammarchar{#1}[#2]
  \@dogrammarchars}

\def\@dogrammarchar#1[#2]{
  \csname if@#1\endcsname\else
  \csname @def#1\endcsname{#2}\csname @#1true\endcsname\fi}
...............................................................................

and would have to become
................................................................................

\def\@dogrammarchars{
  \@ifnextchar !{\@getchardef}{\@defaultchardefs}}
%              ^

\def\@getchardef!(#1)#2!{
%               ^      ^
  \@dogrammarchar{#1}!#2!
%                    ^  ^
  \@dogrammarchars}

\def\@dogrammarchar#1!#2!{
%                    ^  ^
  \csname if@#1\endcsname\else
  \csname @def#1\endcsname{#2}\csname @#1true\endcsname\fi}
................................................................................

(sorry to belabour the obvious, but to be on the safe side, the "^" are
only to indicate where the changes have taken place)

The optional parameters may not now contain any "!" symbols, or else
we're back where we started from.

3). Thanks to David Wright (wright@tasis.eecs.utas.edu.au) and Arto
Viitanen (av@uta.fi) for the next one.

I included a couple of spaces in the BNF macros, where I shouldn't have,
which leads to spurious spaces being produced in the output. (Dave
Wright's example was "->" appearing as "- >", and Arto Viitanen's was 
"<terminal>" appearing as "<terminal >". The solution is to remove the
extra spaces, on lines 22 and 24. These were
................................................................................
    \catcode`\<\active
    \def<{{\@deactifygrammarchars \@first#1}}
%                                ^
    \catcode`\>\active
    \def>{{\@deactifygrammarchars \@second#1}}}
%                                ^
\endgroup
................................................................................

The "^" symbols indicate the spaces that should be removed.
                                  Hugh.