summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/backnaur/backnaur.dtx
blob: bf04cc42ba0c40821fea079390d426c45d7d02eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
% \iffalse meta-comment
%
% backnaur.dtx
% 4 December 2012
%
%    Documented LaTeX file for the backnaur package.
%
%    Run LaTeX on backnaur.ini to make the package's
%    backnaur.sty file.
%
%    Run LaTeX on this file to produce the package 
%    documentation.
%
% Copyright (C) 2012 by Adrian P Robson
%     adrian.robson@nepsweb.co.uk
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either 
% version 1.3c of this license or (at your option) any later
% version. The latest version of this license is in
%    http://www.latex-project.org/lppl.txt
%
% This work has the LPPL maintenance status `maintained'.
% 
% The Current Maintainer of this work is Adrian Robson.
%
% This work consists of the files backnaur.dtx and 
%                                 backnaur.ins
% and the derived files           backnaur.sty and 
%                                 backnaur.pdf
% -----------------------------------------------------------
% \fi
%
% \iffalse
%<package>\NeedsTeXFormat{LaTeX2e}[2011/06/27]
%<package>\ProvidesPackage{backnaur}
%<package>[2012/12/12 1.1 Typesetting for Backus-Naur Form (BNF) definitions]
%<*driver>
\documentclass[10pt,a4paper]{ltxdoc}
\usepackage{backnaur}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{textcomp}
\usepackage{bigstrut}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\OnlyDescription       % comment out for code implementation
\begin{document}
\DocInput{backnaur.dtx}
\end{document}
%</driver>
% \fi
%
%^^A-------------------------------------------------------------------
%^^A Change log...
%^^A--------------
%^^A
%^^A 15 November 2012
%^^A The original was based on a non-packaged version that was used 
%^^A in 2008 for the LaTeX editions of the author's foggy times 
%^^A reports.
%    \changes{v1.0}{2012/11/15}
%            {Initial version of the package.}
%^^A
%^^A 3 December 2012
%^^A Add tstt option and generally rewrite documentation
%    \changes{v1.1}{2012/12/12}
%            {Added {\tt tstt} option, and improved documentation.}
%^^A-------------------------------------------------------------------
% 
%^^A \CheckSum{0} %^^A no checksum for development
% \CheckSum{59}
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
% \GetFileInfo{backnaur.sty}
%
% \title{The \textsf{backnaur} package\thanks{
%   This work replaces \emph{Writing BNF Notation in LaTeX}, 
%   which described a non-package method of BNF typesetting.
%   This document corresponds to \textsf{backnaur}~\fileversion, dated~\filedate.}}
%
% \author{Adrian P. Robson\thanks{\texttt{adrian.robson@nepsweb.co.uk}}}
% \date{12 December 2012}
%
% \maketitle
%
%^^A-------------------------------------------------------------------
%^^A Start report body
%^^A-------------------------------------------------------------------
%
% \section{Introduction}
%
% The \textsf{backnaur} package typesets Backus-Naur Form (BNF) definitions. 
% It creates aligned lists of productions, with numbers if required.
% It can also print  in line BNF expressions using math mode. 
%
% Backus-Naur Form is a notation for defining context free grammars.
% It is used to describe such things as programming languages, communication protocols 
% and command syntaxes, but it can be useful whenever a rigorous definition of language is needed.
%
% \section{BNF Definitions}
%
% The following is a BNF definition of a semicolon separated list:
% \begin{bnf*}
% \bnfprod{list}
%         {\bnfpn{listitems}  \bnfor \bnfes}\\
% \bnfprod{listitems}
%         {\bnfpn{item}  \bnfor \bnfpn{item}  
%          \bnfsp  \bnfts{;}  \bnfsp \bnfpn{listitems}}\\
% \bnfprod{item}
%         { \bnftd{description of item}}
% \end{bnf*}
%
% \noindent
% Here, $\bnfpo$ signifies \emph{produces}, $\mid$ is an \emph{or} operator, $\langle$...$\rangle$
% are \emph{production names}, 
% and $\bnfes$ represents the \emph{empty string}.
% However, some BNF users prefer alternative terminologies, 
% where $\bnfpo$  stands for \emph{is defined as}, 
% $\langle$...$\rangle$ is a \emph{category name} or \emph{nonterminal}, 
% and $\bnfes$ is refered to as \emph{null} or \emph{empty}.
%
% The above definition was created with the following code:
%
%\begin{verbatim}
%\usepackage{backnaur}
%...
%\begin{bnf*}
%   \bnfprod{list}
%           {\bnfpn{listitems} \bnfor \bnfes}\\
%   \bnfprod{listitems}
%           {\bnfpn{item} \bnfor \bnfpn{item}  
%            \bnfsp \bnfts{;} \bnfsp \bnfpn{listitems}}\\
%   \bnfprod{item}
%           {\bnftd{description of item}}
%\end{bnf*}
%
%\end{verbatim}
%
% \noindent
% Each BNF production is defined by a |\bnfprod| command, which has two arguments 
% giving its left and right sides.
% The right hand side of each production is specified with the commands described in
% \S\ref{commandDesc}.
% Terminal (|\bnfts{;}|) and nonterminal (|\bnfpn{item}|), elements are separated 
% by spaces (|\bnfsp|) and OR symbols (|\bnfor|).
% The |\bnfes| command gives the symbol for the empty string.
%
% \section{Package Commands}\label{commandDesc}
%
% \subsection{Loading and options}\label{packopts}
%
% The package is loaded with
%
% \medskip\noindent
% \hspace{2.5em}|\usepackage{backnaur}| \\
% or\\
% \hspace*{2.5em}|\usepackage[|<options>|]{backnaur}|
% \medskip
%
% \noindent
% Possible options are
%
% \vspace{-1.5ex}
% \begin{tabbing}
% xxxxxxxxx \=\kill
% |perp|   \>The empty string symbol is $\perp$\\
% |epsilon| \>  The empty string symbol is $\epsilon$\\
% |tstt| \>  Terminal string typeface is typewriter
% \end{tabbing}
% \vspace{-1ex}
%
% \noindent
% The defaults are: the empty string symbol is $\lambda$, 
% and the terminal string typeface is normal (roman).
% 
% \subsection{Environments}
%
% \DescribeEnv{bnf} 
% BNF productions are defined in a |bnf| or |bnf*| environment, 
% which respectively  give numbered and unnumbered lists of productions.
% \DescribeEnv{bnf*}
% \begin{quote}
% \begin{minipage}{0.5\linewidth}
%   |\begin{bnf}|\\
%   \hspace*{1em}<list of productions>\\
%   |\end{bnf}|
% \end{minipage}
%   \begin{minipage}{0.45\linewidth}
%   |\begin{bnf*}|\\
%   \hspace*{1em}<list of productions>\\
%   |\end{bnf*}|
% \end{minipage}
% \end{quote}
%
% \subsection{Productions}
%
% \DescribeMacro{\bnfprod}
% A production is defined by |\bnfprod|, which takes two arguments: 
% \begin{quote}
%   |\bnfprod{|<production name>|}{|<production definition>|}|
% \end{quote}	
%
% \subsection{Production definitions}
%
% The following commands are used to compose the right hand side of a production.
% They are deployed in the second argument of the |\bnfprod| command.
%
% \DescribeMacro{\bnfpn}
% The |\bnfpn| command generates a production name.
% It takes a single argument that is the name. 
% It is used as follows:
% \begin{quote}
% \begin{minipage}{0.6\linewidth}
%   |\bnfpn{list item}|
% \end{minipage}
% \begin{minipage}{0.3\linewidth}
%   $\bnfpn{list item}$
% \end{minipage}
% \end{quote}
%
% \DescribeMacro{\bnftm}
% There are three types of terminal item: a literal string, a descriptive phrase and an empty string.
% \DescribeMacro{\bnftd}
% A literal terminal string is specified by the |\bnftm| command, which takes a single argument.
% \DescribeMacro{\bnfes}
% The |\bnftd| command generates a descriptive phrase, as an alternative to a literal string.  
% The |\bnfes| command generates a token that represents the empty string. 
% This is normally $\bnfes$, but it can be changed as a package option (see \S\ref{packopts}).
% \begin{quote}
% \begin{minipage}{0.6\linewidth}
%   |\bnfts{terminal}|\\
%   |\bnftd{description}|\\
%   |\bnfes|
% \end{minipage}
% \begin{minipage}{0.3\linewidth}
%   $\bnfts{terminal}$\\
%   $\bnftd{description}$\\
%   $\bnfes$
% \end{minipage}
% \end{quote}
%
% \DescribeMacro{\bnfsk}
% Some literal terminal strings can be abbreviated with the `skip' token,
% which is generated by the |\bnfsk| command.
% This substitutes for a sequence of terminal characters.
% It is used like this:
% \begin{quote}
%   \begin{minipage}{0.6\linewidth}
%   |\bnfts{A} \bnfsk \bnfts{Z}|
%   \end{minipage}
% \begin{minipage}{0.3\linewidth}
%   $\bnfts{A} \bnfsk \bnfts{Z}$
% \end{minipage}
% \end{quote}
%
% \DescribeMacro{\bnfor}
% All items are separated by an OR or a space. 
% The |\bnfor| command generates the OR symbol, and the |\bnfsp| command introduces a space.
% \DescribeMacro{\bnfsp} 
% A space can be considered equivalent to an AND operator.
% \begin{quote}
% \begin{minipage}{0.6\linewidth}
%   |\bnfpn{abc} \bnfor \bnfts{xzy}|\\
%   |\bnfpn{abc} \bnfsp \bnfts{xzy}|
% \end{minipage}
% \begin{minipage}{0.3\linewidth}
%   $\bnfpn{abc} \bnfor \bnfts{xzy}$\\
%   $\bnfpn{abc} \bnfsp \bnfts{xzy}$
% \end{minipage}
% \end{quote}
%
% \subsection{Inline expressions}
%
% The package's definition commands can be typeset inline using maths mode, 
% so the expression |$\bnfpn{name}$| will give $\bnfpn{name}$.
%
% \DescribeMacro{\bnfpo}
% The |\bnfpo| command is provided so that the production operator $\bnfpo$ 
% can be printed independently from the |bnf| environment if required.
% The |\bnfprod| command cannot be used inline.
%
% \DescribeMacro{\bnfpo}
% The |\bnfpo| command is provided so that the production operator $\bnfpo$ 
% can be printed independently from the |bnf| environment if required.
% The |\bnfprod| command cannot be used inline.
%
% \DescribeMacro{\bnfpo}
% The |\bnfprod| command cannot be used inline.
% So the |\bnfpo| command is provided so that the production operator $\bnfpo$ 
% can be printed independently from the |bnf| environment if required.
%
% \subsection{Command summary}
%
% The commands that can be used to define a BNF production in a |bnf| or |bnf*| environment 
% are as follows:
%
% \begin{center}
% \begin{tabular}{lll}
% \hline
% Command         & Operator             & Outcome               \bigstrut \\
% \hline
% |\bnfpn{}| & production name      & $\bnfpn{name}$        \bigstrut[t] \\
% |\bnfor|   & OR operator          & $\bnfor$               \\
% |\bnfsk|   & skip                 & $\bnfsk$               \\
% |\bnfsp|   & space/AND operator   & $\bnfsp$ \\
% |\bnfes|   & empty string         & $\bnfes$              \\
% |\bnfts{}| & terminal string      & $\bnfts{terminal}$     \\
% |\bnftd{}| & terminal description & $\bnftd{description}$ \\
% |\bnfpo|   & production operator  & $\bnfpo$              \bigstrut[b] \\
% \hline
% \end{tabular}
% \end{center}
%
% \section{Example}\label{example}
%
% A more significant example is the following definition of a $\bnfpn{sentence}$, 
% where $\bnfpn{cchar}$ are countable characters, and $\bnfpn{ichar}$ are characters 
% that should be ignored:
%
% \begin{samepage}
% \small
%\begin{verbatim}
%\begin{bnf*}
%   \bnfprod{sentence}
%      {\bnfpn{start} \bnfsp \bnfpn{rest} \bnfsp \bnfts{.}}\\
%   \bnfprod{start}  
%      {\bnfpn{space} \bnfor \bnfes}\\
%   \bnfprod{rest}   
%      {\bnfpn{word} \bnfsp \bnfpn{space} \bnfsp \bnfpn{rest} 
%       \bnfor \bnfpn{word} \bnfor \bnfes}\\
%   \bnfprod{word}  
%      {\bnfpn{wchar} \bnfsp \bnfpn{word} \bnfor \bnfpn{wchar}}\\
%   \bnfprod{space}  
%      {\bnfpn{schar} \bnfsp \bnfpn{space} \bnfor \bnfpn{schar}}\\
%   \bnfprod{wchar}
%      {\bnfpn{cchar}  \bnfor \bnfpn{ichar} }\\
%   \bnfprod{cchar}
%      {\bnfts{A} \bnfsk \bnfts{Z} \bnfor \bnfts{a} \bnfsk \bnfts{z} 
%       \bnfor \bnfts{0} \bnfsk \bnfts{9} \bnfor 
%       \bnfts{\textquotesingle}}\\
%   \bnfprod{ichar}  
%      {-}\\
%   \bnfprod{schar}
%      {\bnfts{`\hspace{1em}'} \bnfor \bnfts{!} \bnfor \bnfts{"} 
%       \bnfor \bnfts{(} \bnfor \bnfts{)} \bnfor \bnfts{\{} \bnfor
%       \bnfts{\}} \bnfor \bnfts{:} \bnfor \bnfts{;} \bnfor \bnfts{?}
%       \bnfor \bnfts{,}}\\
%\end{bnf*}
%\end{verbatim}
% \end{samepage}
%
% \noindent
% This creates the following BNF definition:
% \begin{bnf*}
% \bnfprod{sentence}
%    {\bnfpn{start} \bnfsp \bnfpn{rest} \bnfsp \bnfts{.}}\\
% \bnfprod{start}  
%    {\bnfpn{space} \bnfor \bnfes}\\
% \bnfprod{rest}   
%    {\bnfpn{word} \bnfsp \bnfpn{space} \bnfsp \bnfpn{rest} 
%     \bnfor \bnfpn{word} \bnfor \bnfes }\\
% \bnfprod{word}  
%    {\bnfpn{wchar} \bnfsp \bnfpn{word} \bnfor \bnfpn{wchar}}\\
% \bnfprod{space}  
%    {\bnfpn{schar} \bnfsp \bnfpn{space} \bnfor \bnfpn{schar}}\\
% \bnfprod{wchar}
%    {\bnfpn{cchar}  \bnfor \bnfpn{ichar} }\\
% \bnfprod{cchar}
%    {\bnfts{A} \bnfsk \bnfts{Z} \bnfor \bnfts{a} \bnfsk 
%     \bnfts{z} \bnfor \bnfts{0} \bnfsk \bnfts{9} \bnfor 
%     \bnfts{\textquotesingle} }\\
% \bnfprod{ichar}  
%    {-}\\
% \bnfprod{schar}
%    {\bnfts{`\hspace{1em}'} \bnfor \bnfts{!} \bnfor \bnfts{"} 
%     \bnfor \bnfts{(}  \bnfor \bnfts{)} \bnfor \bnfts{\{} 
%     \bnfor \bnfts{\}} \bnfor \bnfts{:} \bnfor \bnfts{;}  
%     \bnfor \bnfts{?}  \bnfor \bnfts{,} }
% \end{bnf*}
%
%
%^^A-------------------------------------------------------------------
%^^A Start code implementation
%^^A-------------------------------------------------------------------
%
% \StopEventually{}
%^^A \StopEventually{\PrintChanges}
%^^A    -> Run pdfLaTeX backnaur.dtx
%^^A           makeindex -s gglo.ist -o backnaur.gls backnaur.glo
%^^A           pdfLaTeX backnaur.dtx
%^^A \StopEventually{\PrintIndex}
%^^A    -> Run pdfLaTeX backnaur.dtx
%^^A           makeindex -s gind.ist -o backnaur.ind backnaur.idx
%^^A           pdfLaTeX backnaur.dtx
%
% \section{Implementation}
%
% The implementation of |backnaur.sty| uses only \LaTeX{} commands. 
%
% \subsection{Environments}\label{environ}
%
% There are two environments defined:
%
% \begin{environment}{bnf}
%    A production list with numbers. It is simply implemented as an |eqnarray|.
%    Surplus space is removed with an |\ignorespacesafterend| and |%| characters.
%    \begin{macrocode}
\newenvironment{bnf}%
{\begin{eqnarray}}%
{\end{eqnarray}\ignorespacesafterend}
%    \end{macrocode}
% \end{environment}
%
% \begin{environment}{bnf*}
%    A lists without numbers. It is implemented with an |eqnarray*|, 
%    and extra space is removed as in the |bnf| environment above. 
%    \begin{macrocode}
\newenvironment{bnf*}%
{\begin{eqnarray*}}%
{\end{eqnarray*}\ignorespacesafterend}
%    \end{macrocode}
% \end{environment}
%
% \subsection{Definition Commands}
%
% All of these commands run in the math mode supplied by the 
% |bnf| and |bnf*| environments (see \S\ref{environ}). 
%
% \begin{macro}{\bnfpn}
%    Production name / nonterminal.
%    \begin{macrocode}
\newcommand{\bnfpn}[1]{\langle \textnormal{#1} \rangle}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\bnfor}
%    OR operator.
%    \begin{macrocode}
\newcommand{\bnfor}{\; \mid \;}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\bnfsp}
%    Space.
%    \begin{macrocode}
\newcommand{\bnfsp}{\;}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\bnfes}
%    Empty string symbol. This is the default if no package options are given.
%    \begin{macrocode}
\newcommand{\bnfes}{\lambda}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\bnfts}\label{codebnfts}
%    \changes{v1.1}{2012/12/12}{Support for {\tt tstt} option added}
% \begin{macro}{\bnf@tsfont}
%    Terminal string. The font is controlled by the |\bnf@tsfont| macro,
%    and this macro can be redefined by the |tstt| option (see \S\ref{codeoptions}).
%    \begin{macrocode}
\newcommand\bnf@tsfont[1]{\textrm{#1}}
\newcommand{\bnfts}[1]{\textnormal{\bnf@tsfont{#1}}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\bnfpo}
%    Terminal description.
%    \begin{macrocode}
\newcommand{\bnftd}[1]{\textnormal{\it #1}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\bnfsk}
%    Skip symbol.
%    \begin{macrocode}
\newcommand{\bnfsk}{\dots}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\bnfpo}
%    Production operator.
%    \begin{macrocode}
\newcommand{\bnfpo}{\models}
%    \end{macrocode}
% \end{macro}
%
% \subsection{Production Commands}
%
% \begin{macro}{\bnfprod}
%    A Backus-Naur Form production. 
%    This command is designed to exploit the behaviour the |equarray| used in 
%    the |bnf| and |bnf*| environments. 
%    In particular, the |\bnfpo| character will be vertically aligned. 
%    \begin{macrocode}
\newcommand{\bnfprod}[2]{\bnfpn{#1} & \bnfpo & #2}
%    \end{macrocode}
% \end{macro}
%
% \subsection{Options}\label{codeoptions}
%
% \begin{macro}{tstt}
%    \changes{v1.1}{2012/12/12}{New option declared}
% \begin{macro}{\bnf@tsfont}
%    The |tstt| option redefines |\bnf@tsfont| to change the 
%    terminal string font used in the |\bnfts| command (see \S\ref{codebnfts}):
%    \begin{macrocode}
\DeclareOption{tstt}{\renewcommand\bnf@tsfont[1]{\texttt{#1}}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
% \begin{macro}{perp}
% \begin{macro}{epsilon}
% \begin{macro}{\bnfes}
%    The |perp| and |epsilon| options redefine |\bnfes|, 
%    which is the empty string command:
%    \begin{macrocode}
\DeclareOption{perp}{\renewcommand{\bnfes}{\perp}}
\DeclareOption{epsilon}{\renewcommand{\bnfes}{\epsilon}}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
%
%\noindent
% Finally, the options that have been selected are executed:
%    \begin{macrocode}
\ProcessOptions\relax
%    \end{macrocode}
%
% \Finale
\endinput