📄 bnf.doc
字号:
% <assignment>& -> & <non-macro assignment> | <macro assignment>\\% <equals> & ->& <optional spaces> | <optional spaces>"="_{12}\\% <def>&->&"\def"|"\gdef"|"\edef"|"\xdef"\\% <normal integer>&->&<integer constant>\\% &&|<integer constant><one optional space>\\% &&|"'"_{12}<octal constant><one optional space>\\% &&|`"`_{12}<hexadecimal constant><one optional space>\\% &&|"`"_{12}<character token><one optional space>\\% <S>&->& "a" <B>\\% <S>&->&\Empty\\% <B>&->& "b" <B>\\% <B>&->&{@ a{@ b{@ c{@ d{@ e<B>}}}}}\\% <B>&->& ["c"{@"ab"}"d"] | {@ {@"aba"}["d"]{@"baab"}}% \end{eqnarray*}%\end{bnf}%\end{verbatim}%\begin{bnf}% \begin{eqnarray*}% <assignment>& -> & <non-macro assignment> | <macro assignment>\\% <equals> & ->& <optional spaces> | <optional spaces>"="_{12}\\% <def>&->&"\def"|"\gdef"|"\edef"|"\xdef"\\% <normal integer>&->&<integer constant>\\% &&|<integer constant><one optional space>\\% &&|"'"_{12}<octal constant><one optional space>\\% &&|`"`_{12}<hexadecimal constant><one optional space>\\% &&|"`"_{12}<character token><one optional space>\\% <S>&->& "a" <B>\\% <S>&->&\Empty\\% <B>&->& "b" <B>\\% <B>&->&{@ a{@ b{@ c{@ d{@ e<B>}}}}}\\% <B>&->& ["c"{@"ab"}"d"] | {@ {@"aba"}["d"]{@"baab"}}% \end{eqnarray*}%\end{bnf}%%\StopEventually{}%\section{Implementation of the long forms}%The "NonTerminal" environment is easy to implement.%We set nonterminal letters in roman by default, that is, maths family~0,%and make sure that spaces are respected. The boolean is there to%make the checking of the short forms easier later. Certain characters%can be excluded from the text of a nonterminal, and an error message%issued if they are encountered.% \begin{macrocode}\newif\ifnonterminal\def\NonTerminal{\left\langle\obeyspaces\ControlSpaces \nonterminaltrue\NonTerminalStyle}\def\endNonTerminal{\right\rangle}\def\NonTerminalStyle{\fam0 }{\obeyspaces\gdef\ControlSpaces{\let =\ }}% \end{macrocode}%%The "Star", "Optional" and "Bracket" environments are all similar.%A small amount of extra space has been introduced round each,%to improve their appearance.% \begin{macrocode}\def\Star{\,\left\{} \def\endStar{\right\}\,}\def\Optional{\,\left[} \def\endOptional{\right]\,}\def\Bracket{\,\left(} \def\endBracket{\right)\,}% \end{macrocode}%%The single symbols have easy definitions.% \begin{macrocode}\def\OR{\mathop{\left|\right.}\nolimits}\def\Production{\mathrel{\longrightarrow}}\def\Yields{\mathrel{\Longrightarrow}}\def\Empty{\varepsilon}% \end{macrocode}%%By far the most difficult effect to produce is that of a terminal.%We could make "\Terminal" expand to "\verb*", but that prevents us%from including extra text before and after the terminal string.%Thus a definition similar to that of "\verb*" from NFSS is included.% \begin{macrocode}\begingroup \catcode`\`=\active \gdef\TerminalFont{\tt \catcode96\active \def`{\leavevmode\kern\z@\char96 }}\endgroup\begingroup \catcode`\~=\active \lccode`\~=`\^^M \lowercase{\endgroup \gdef\Terminal{\relax\PreTerminal \ifmmode \hbox \else \leavevmode\null \fi \bgroup \TerminalFont \catcode`~\active \def~{\egroup\@latexerr{Terminal string ended by end of line.}\@ehc}% \let\do\@makeother \dospecials \@sTerminal}}\def\@sTerminal#1{% \catcode`#1\active \lccode`\~`#1% \lowercase{\def~{\egroup\PostTerminal}}}%% \end{macrocode}%The default leading and trailing text is defined to be empty.% \begin{macrocode}\def\PreTerminal{} \def\PostTerminal{}% \end{macrocode}%%\section{Implementation of the short forms}%%Our first task is to make $<$ and $>$ available as mathematical symbols.% \begin{macrocode}\mathchardef\lt="313C \mathchardef\gt="313E% \end{macrocode}%The next task is more subtle, and at first puzzled the author when he%tried to make a preliminary version of these macros work.%The problem is that we are making "-" into an active character.%If we do this we find that it tries to expand in text as well, and so%we make sure that it only expands in maths mode.%Our objective is to make "->" expand to $\Production$, but here is%where things go wrong. In plain \TeX, the control sequence%"\longrightarrow" is defined to expand to a "\relbar" and a "\rightarrow".%In turn, a relbar expands to yield a "-" again, and so we are in an%infinite loop.%%The solution seems to be to abandon the definition of "\relbar" in%plain \TeX\ and produce our own. The same applies to "=" and "\Relbar".%We also make a hyphen available for use within nonterminals.% \begin{macrocode}\mathchardef\HYPHEN="2D \mathchardef\MINUS="2200\mathchardef\Relbar="303D \def\relbar{\mathrel{\smash{\MINUS}}}\mathchardef\EQUALS="303D% \end{macrocode}%%We next define "\bnf" to set the mathcodes of the characters we wish to%use to the active value \verb|"8000|. Thus in text they will behave as%normal characters, but in mathematics they will expand as though they were%active characters. We also allow "bnf" to be an environment.% \begin{macrocode}\def\mathactive{"8000}\def\bnf{% \mathcode`"=\mathactive \mathcode`[=\mathactive \mathcode`\]=\mathactive \mathcode`(=\mathactive \mathcode`\)=\mathactive \mathcode`|=\mathactive \mathcode`-=\mathactive \mathcode`<=\mathactive \mathcode`\>=\mathactive \mathcode`@=\mathactive \mathcode`==\mathactive \mathcode96 \mathactive}\def\endbnf{}% \end{macrocode}%%Several characters are not allowed to appear in nonterminal names,%and so we define an error message to warn the user.% \begin{macrocode}\def\NotInNonTerminal{\errmessage{Not allowed in a non-terminal}}% \end{macrocode}%%Several of our mathactive characters have similar definitions;%we use this fact to allow some condensation of our file.% \begin{macrocode}\def\NotNTdef#1#2{\gdef#1{\ifnonterminal\NotInNonTerminal\else#2\fi}}% \end{macrocode}%%The expansion text of all our mathactive characters must be defined%inside a group in which they are all active.% \begin{macrocode}\begingroup \catcode`"\active \catcode`[\active \catcode`\]\active \catcode`(\active \catcode`\)\active \catcode`|\active \catcode`-\active \catcode`<\active \catcode`>\active \catcode`@\active \catcode`=\active \catcode96 \active% \end{macrocode}%First we have the two short forms of "\Terminal".% \begin{macrocode} \gdef"{\Terminal"}% \gdef`{\Terminal`}%% \end{macrocode}%Next a block of definitions of characters which are illegal inside%nonterminals.% \begin{macrocode} \NotNTdef[\Optional \NotNTdef]\endOptional \NotNTdef(\Bracket \NotNTdef)\endBracket \NotNTdef|\OR \NotNTdef<\NonTerminal% \end{macrocode}%The end of a nonterminal has to be defined directly, of course.% \begin{macrocode} \gdef>{\endNonTerminal}%% \end{macrocode}%A "-" character in a nonterminal is just a hyphen, but otherwise it%could possibly be a production arrow.% \begin{macrocode} \gdef-{% \ifnonterminal \def\Nnext{\HYPHEN}% \else \def\Nnext{\futurelet\Next\SeeIfProdn}% \fi\Nnext}% \end{macrocode}%The "=" character, on the other hand, should not appear in a nonterminal.%Otherwise, it could be a yields arrow.% \begin{macrocode} \gdef={% \ifnonterminal \def\Nnext{\NotInNonTerminal}% \else \def\Nnext{\futurelet\Next\SeeIfYields}% \fi\Nnext}%% \end{macrocode}%The "@" character should examine the next character to see whether it should%be given a special treatment.% \begin{macrocode} \gdef@{% either an escape char or a star group \ifnonterminal \def\Nnext{\NotInNonTerminal}% \else \def\Nnext{\futurelet\Next\SeeIfSpecial}% \fi\Nnext}%\endgroup% \end{macrocode}%%All that remains now is to see that the checks used above%are defined. First we have the check for a production.% \begin{macrocode}\def\SeeIfProdn{% \if\noexpand\Next\noexpand>% \def\Nnext{\Production\@gobble}% \else \def\Nnext{\MINUS}% \fi \Nnext}% \end{macrocode}%Second we have the check for a yields.% \begin{macrocode}\def\SeeIfYields{% \if\noexpand\Next\noexpand>% \def\Nnext{\Yields\@gobble}% \else \def\Nnext{\EQUALS}% \fi\Nnext}% \end{macrocode}%Finally the "@" character must produce a "Star" environment unless it%is immediately followed by "<" or~">".% \begin{macrocode}\def\SeeIfSpecial{% \if\noexpand\Next\noexpand<% \def\Nnext{\lt\@gobble}% \else \if\noexpand\Next\noexpand>% \def\Nnext{\gt\@gobble}% \else \def\Nnext{\Star\bgroup\aftergroup\endStar\aftergroup\egroup}% \fi \fi\Nnext}% \end{macrocode}%\Finale%\endinput%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Dr M J Piff %% e-mail:%% Department of Pure Mathematics %%%% University of Sheffield %% M.Piff@sheffield.ac.uk%% Hicks Building %% M.Piff@pa.shef.ac.uk%% Hounsfield Road %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% SHEFFIELD S3 7RH %% Telephone: SHEFFIELD (0742) 768555%% England %% Ext. 4431%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -