% These are the BNF style macros. In the documentation it is assumed
% that you will place these in a file called "bnf.sty"


\def\@actifygrammarchars{\catcode`\ \active\catcode`\:\active\catcode`\,\active\catcode`\;\active\catcode`\.\active\catcode`\<\active\catcode`\>\active}
\def\@deactifygrammarchars{\@makeother\ \@makeother\:\@makeother\,\@makeother\;\@makeother\.\@makeother\<\@makeother\>}

\begingroup
  \@actifygrammarchars
   \gdef\@defcolon#1{
    \catcode`\:\active
    \def:{\def\newline{\hfill\break\hspace*{2em}}{\@deactifygrammarchars #1}}}
   \gdef\@defcomma#1{
    \catcode`\,\active
    \def,{\def\newline{\hfill\break\hspace*{4em}}{\@deactifygrammarchars #1}}}
   \gdef\@defsemicolon#1{
    \catcode`\;\active
    \def;{\def\newline{\hfill\break\hspace*{2em}}{\@deactifygrammarchars #1}}}
   \gdef\@defperiod#1{
    \catcode`\.\active
    \def.{\def\newline{\vskip10pt plus10pt minus5pt}{\@deactifygrammarchars #1}}}
   \gdef\@defnonterminal#1{
    \def\@first##1##2{##1}
    \def\@second##1##2{##2}
    \catcode`\<\active
    \def<{{\@deactifygrammarchars \@first#1}}
    \catcode`\>\active
    \def>{{\@deactifygrammarchars \@second#1}}}
\endgroup

\def\quotesymbol{\begingroup\@makeother\""\endgroup}
\def\lessthan{\begingroup\@makeother\<<\endgroup}
\def\greaterthan{\begingroup\@makeother\>>\endgroup}

\newif\if@string
\@stringfalse
\begingroup
  \catcode`\"\active
  \gdef\@defquote#1{
    \def\@openquote##1##2{##1}
    \def\@closequote##1##2{##2}
	\catcode`\"\active
    \def"{\if@string\@closequote#1\endgroup\@stringfalse
          \else\@openquote#1\begingroup\@deactifygrammarchars\@stringtrue\fi}}
\endgroup

\newif\if@colon
\newif\if@semicolon
\newif\if@period
\newif\if@comma
\newif\if@quote
\newif\if@nonterminal

\newenvironment{grammar}%
  {\begin{quote}%
   \@colonfalse%
   \@semicolonfalse%
   \@periodfalse%
   \@commafalse%
   \@quotefalse%
   \@nonterminalfalse%
   \@dogrammarchars}%
  {\@deactifygrammarchars%
   \end{quote}}

\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}

\def\@defaultchardefs{
   \if@quote\else\@defquote{{``}{''}}\fi%
   \if@nonterminal\else\@defnonterminal{<>}\fi%
   \if@colon\else\@defcolon{:\\}\fi%
   \if@semicolon\else\@defsemicolon{;\\}\fi%
   \if@period\else\@defperiod{.\\}\fi%
   \if@comma\else\@defcomma{, }\fi}%

