summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/latex/simplebnf/simplebnf-doc.tex
blob: 1181d3f2033aff3e80097fcd5565a6b39714d751 (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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
\documentclass[11pt]{article}
\usepackage{geometry}
\geometry{a4paper,top=1in,bottom=1in,left=1.2in,right=1.2in}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Font setup %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Works for pdfLaTeX, XeLaTeX, and LuaLaTeX
\usepackage{libertine}

% Math/Mono font setup
\ExplSyntaxOn
\bool_if:nTF { \sys_if_engine_xetex_p: || \sys_if_engine_luatex_p: }
  {
    % Load mathtools before unicode-math
    \usepackage{mathtools}
    \usepackage[
      math-style=ISO,
      warnings-off={mathtools-colon, mathtools-overbracket},
    ]{unicode-math}
    \setmathfont{LibertinusMath-Regular.otf}[Scale = MatchUppercase]
    \setmathfont{Garamond-Math.otf}[
      Scale = MatchUppercase,
      range = {\Coloneq}
    ]

    \setmonofont{Inconsolatazi4}[
      Extension = .otf,
      UprightFont = *-Regular,
      BoldFont = *-Bold,
      Scale=MatchLowercase,
      AutoFakeSlant = 0.2,
    ]
  }
  {
    \usepackage[libertine]{newtxmath}
    \usepackage{inconsolata}
  }
\ExplSyntaxOff

% Some fancy symbols (prevent being overwritten by math fonts)
\AtBeginDocument{%
  \usepackage[
    only, fatsemi, fatslash, talloblong, rightarrowtriangle
  ]{stmaryrd}%
}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Presentations %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage{tcolorbox}
\tcbuselibrary{listings,breakable}
\tcbset{listing engine=listings, colframe=black, colback=white, size=small}

\NewTCBListing { example } { !O{} !s }
  {
    sharp corners,
    IfBooleanTF = { #2 }
      { listing above text }
      { listing side text },
    fontlower = \small,
    #1
  }

\NewTCBListing { listing } { !O{} }
  { sharp corners, listing only, #1 }


\NewDocumentEnvironment {exampleside} {}
  { \tcblisting{listing side text,righthand width=.5\textwidth} }
  { \endtcblisting }

\NewDocumentEnvironment { presentcommand } { b }
  {%
    \vspace*{0.5\baselineskip}\noindent\fbox{%
    \begin{minipage}{\dimexpr\textwidth-2\fboxsep-2\fboxrule}
      #1
    \end{minipage}}\vspace*{0.5\baselineskip}
  }
  { }


\NewDocumentCommand \cmd { m } {\texttt{\char`\\#1}}
\NewDocumentCommand \env { m m }
  {
    \texttt{%
      \char`\\begin\{#1\} \textrm{#2}%
      \char`\\end\{#1\}%
    }%
  }


\usepackage{tabularray}
\UseTblrLibrary{booktabs}
% \fakeverb
\usepackage{codehigh}


\newcommand*{\secref}[1]{\hyperref[{#1}]{section~\ref*{#1} -- \emph{\nameref*{#1}}}}
\newcommand*{\subsecref}[1]{\hyperref[{#1}]{subsection~\ref*{#1} -- \emph{\nameref*{#1}}}}
\newcommand*{\subsubsecref}[1]{\hyperref[{#1}]{\nameref*{#1}}}
\newcommand*{\parref}[1]{\hyperref[{#1}]{\nameref*{#1}}}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% simplebnf %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage{simplebnf}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Miscellaneous %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage{hologo}
\newcommand*{\XeLaTeX}{\hologo{XeLaTeX}}
\newcommand*{\LuaLaTeX}{\hologo{LuaLaTeX}}

% ``Make sure it comes *last* of your loaded packages"
\usepackage[pdfborder={0 0 0}, colorlinks=true]{hyperref}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Metadata %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\title{%
  \textsf{simplebnf} --- A simple package to format Backus-Naur form%
  \footnote{This file describes v1.0.0.}}
\author{Jay Lee\footnote{E-mail: %
  \href{mailto:jaeho.lee@snu.ac.kr}{\texttt{jaeho.lee@snu.ac.kr}}}}
\date{2023/11/25}


\begin{document}
\maketitle


\vfill
This package provides a simple way for typesetting grammars in Backus-Naur form (BNF).
It features a flexible configuration system, allowing for the customization of the domain-specific language (DSL) used in typesetting the grammar.
Additionally, the package comes with sensible defaults.

Below is the metagrammar of the DSL as defined in this package, which is typeset using the package itself.\footnote{The code is shown in \nameref{sec:appendix}.}
\begin{tcolorbox}[sharp corners]
  \begin{center}
    \begin{bnf}(
      prod-delim = ;;;,
      new-line-delim = !,
      single-line-delim = ?,
      comment = //,
      relation = {:::=|:in:},
      relation-sym-map =
        {
          {:::=} = $\Coloneqq$,
          {:in:} = $\in$,
        },
    )[
      colspec = lrcll,
      column{2} = {mode=dmath},
      column{4} = {mode=text, font=\ttfamily},
    ]
      G // Gramar :::=
      ! $P$ // production
      ! $P \fatsemi {}$ // production w/ a trailing delimiter
      ! $P \fatsemi G$ // production sequence
    ;;;
      P // Production :::= $L \rightarrowtriangle R$
    ;;;
      L // LHS :::=
      ! $v$ // metavariable
      ! $v\!\fatslash\,c$ // annotated metavariable
    ;;;
      R // RHS :::=
      ! $\talloblong$ // delimiter
      ! $A \talloblong R$ // alternative sequence
    ;;;
      A // Alternative :::=
      ! $f$ // syntactic form
      ! $f\!\fatslash\,c$ // annotated syntactic form
    ;;;
      \fatsemi // Prod. delimiter :::=
      ! ;; // default symbol
      ! $\cdots$ // user-defined
    ;;;
      \rightarrowtriangle // Rule relation :::=
      ! ::= // $\Coloneqq$
      ! -> // $\to$
      ! \texttt{\char`\\in} // $\in$
      ! $\cdots$ // user-defined
    ;;;
      \fatslash // Annot. symbol :::=
      ! : // default symbol
      ! $\cdots$ // user-defined
    ;;;
      \talloblong // Alt. delimiter :::=
      ! | // new-line delimiter
      ! || // single-line delimiter
      ! $\cdots$ // user-defined
    ;;;
      v, f, c :in: \textsf{\TeX{} tl} // valid \TeX{} token lists
    ;;;
    \end{bnf}
  \end{center}
\end{tcolorbox}
\vfill

\section{Tutorial for the impatient}\label{sec:tutorial}
Typesetting a grammar is as simple as typing the BNF grammar in the \verb/bnf/ environment:
\begin{example}[righthand width=.52\linewidth]
\begin{center}
\begin{bnf}
  $\tau$ : \textsf{Type} ::=
  | \texttt{num} : numbers
  | \texttt{str} : strings
  ;;
  $e$ : \textsf{Expr} ::=
  | $x$ : variable
  | $n$ : numeral
  | \texttt{$e$ + $e$} : addition
  | \texttt{$e$ * $e$} : multiplication
  | \texttt{$e$ \textasciicircum{} $e$} : concatenation
  | \texttt{len($e$)} : length
  | \texttt{let $x$ = $e_1$ in $e_2$} : definition
\end{bnf}
\end{center}
\end{example}

Typically, each column in the BNF grammar has the same font style.
In the example above, the comments in the first column, e.g., \textsf{Type}, is typeset in sans-serif, and the second column in math mode, e.g., $\tau$.
The column for the right-hand side is (mostly) typeset in typewriter font with a bit of non-terminals like $e$ typeset in math mode.

\textsf{simplebnf} provides a straightforward way to customize the font styles for each column\footnote{Note that you must provide a manual alignment specification with the key \texttt{colspec}.}:
\begin{listing}
\begin{bnf}[
  colspec = {llcll},
  column{1} = {font = \sffamily},
  column{2} = {mode = dmath},
  column{4} = {font = \ttfamily},
]
  \tau : Type ::=
  | num : numbers
  | str : strings
  ;;
  e : Expr ::=
  | $x$ : variable
  | $n$ : numeral
  | $e$ + $e$ : addition
  | $e$ * $e$ : multiplication
  | $e$ \textasciicircum{} $e$ : concatenation
  | len($e$) : length
  | let $x$ = $e_1$ in $e_2$ : definition
\end{bnf}
\end{listing}
If you find yourself using the same configuration repeatedly, you can fix the desired configuration using \cmd{SetBNFLayout}.
Once set using \cmd{SetBNFLayout}, the configuration is applied to all subsequent \verb/bnf/ environments:
\begin{listing}
% Some where above...
\SetBNFLayout{
  colspec = {llcll},
  column{1} = {font = \sffamily},
  column{2} = {mode = dmath},
  column{4} = {font = \ttfamily},
}
% ...
\begin{bnf}
  \tau : Type ::=
  | num : numbers
  | str : strings
  ;;
  e : Expr ::=
  | $x$ : variable
  | $n$ : numeral
  | $e$ + $e$ : addition
  | $e$ * $e$ : multiplication
  | $e$ \textasciicircum{} $e$ : concatenation
  | len($e$) : length
  | let $x$ = $e_1$ in $e_2$ : definition
\end{bnf}
\end{listing}

Since these customizations are provided by the backend \textsf{tabularray}\footnote{\url{https://github.com/lvjr/tabularray}}, consult its documentation for more details.
The corresponding command is \cmd{SetTblrInner}.

For more advanced customization, such as changing default delimiters and symbols, continue to \secref{sec:config}.

\section{Configuration}\label{sec:config}
While the default configuration should be sufficient for most use cases, some grammars may require using different delimiters and symbols.
For example, the language itself may contain \verb/->/ as a syntactic form, which conflicts with one of the default symbols for the rule relation.

To this end, \textsf{simplebnf} provides a number of options to configure the DSL used to typeset the grammar.
Some examples of customizable symbols include the delimiters for productions and alternatives, the relation symbol between the left-hand side and the right-hand side, the annotation symbol, and more.

The default configuration is shown in \autoref{tab:config}.
\begin{longtblr}[
  caption = {Default key-values for the configuration of \textsf{simplebnf}.},
  label = {tab:config}
]{
  colspec={ll},
  row{2-Z}={font=\ttfamily}
}
  \toprule
  Key & Default value \\
  \midrule
  prod-delim & ;;\\
  new-line-delim & \fakeverb{\|}\\
  single-line-delim & //\\
  comment & :\\
  relation & \fakeverb{{::=|->|:in:}}\\
  relation-sym-map & {
    \{\\
    \quad\fakeverb{{::=} = {\ensuremath{\Coloneqq}},}\\
    \quad\fakeverb{{->} = {\ensuremath{\to}},}\\
    \quad\fakeverb{{:in:} = {\ensuremath{\in}},}\\
    \}
  }\\
  or-sym & \fakeverb{$|$}\\
  prod-sep & 2pt\\
  row-sep & 0pt\\
  \bottomrule
\end{longtblr}

The key-value list can be provided as an optional argument to the \verb/bnf/ environment, wrapped in \verb/()/.
The optional key-value list for the layout---wrapped in \verb/[]/---should be provided after the configuration key-value list.
See \subsecref{subsec:layout} for more details.

An exmaple of customizing the configuration is shown below:
\begin{example}*
\begin{center}
\begin{bnf}(
  prod-delim = {--},
  new-line-delim = {\&},
  single-line-delim = {\&\&},
  comment = {//},
  relation-sym-map =
    {
      {->} = {\ensuremath{\hookrightarrow}},
      {:in:} = {\ensuremath{\in}},
    },
  or-sym = {},
)[
  colspec = {llcll},
  column{1} = {font = \sffamily},
  column{2} = {mode = dmath},
  column{4} = {font = \ttfamily},
]
  \tau // Type ->
  & num // numbers
  & str // strings
  --
  e // Expr ->
  & $x$ // variable
  & $n$ // numeral
  & $e$ + $e$ // addition
  & $e$ * $e$ // multiplication
  & $e$ \textasciicircum{} $e$ // concatenation
  & len($e$) // length
  & let $x$ = $e_1$ in $e_2$ // definition
\end{bnf}
\end{center}
\end{example}


Furthermore, the configuration can be set globally using \cmd{SetBNFConfig}, similar to \cmd{SetBNFLayout}:
\begin{listing}
% Some where above...
\SetBNFConfig{
  prod-delim = {--},
  new-line-delim = {\&},
  single-line-delim = {\&\&},
  comment = {//},
  relation-sym-map =
    {
      {->} = {\ensuremath{\hookrightarrow}},
      {:in:} = {\ensuremath{\in}},
    },
  or-sym = {},
}
% ...
\begin{bnf}[
  colspec = {llcll},
  column{1} = {font = \sffamily},
  column{2} = {mode = dmath},
  column{4} = {font = \ttfamily},
]
  \tau // Type ->
  & num // numbers
  & str // strings
  --
  e // Expr ->
  & $x$ // variable
  & $n$ // numeral
  & $e$ + $e$ // addition
  & $e$ * $e$ // multiplication
  & $e$ \textasciicircum{} $e$ // concatenation
  & len($e$) // length
  & let $x$ = $e_1$ in $e_2$ // definition
\end{bnf}
\end{listing}


\subsection{Production delimiter}
The default production delimiter \verb/prod-delim/ is \verb/;;/.
This will separate different productions in the same grammar.

\subsection{New-line alternative delimiter}
The default new-line alternative delimiter \verb/new-line-delim/ is \verb/\|/, which will actually match verbatim \verb/\|/ in the grammar.
This will separate different alternatives into different lines in the same production.

\subsection{Single-line alternative delimiter}
The default single-line alternative delimiter \verb/single-line-delim/ is \verb|//|.
The difference between the single-line alternative delimiter and the new-line alternative delimiter is that the former will not add a new line after the delimiter.

For consecutive alternatives delimited by the single-line alternative delimiter, only the last of them can be annotated.

Note that the single-line alternative delimiter must not contain the new-line alternative delimiter as a substring.

\subsection{Annotation delimiter}
The default annotation delimiter \verb/comment/ is \verb/:/.
This will separate the syntactic form and the annotation within the alternative.

\subsection{Rule relations and the symbol map}
\textsf{simplebnf} provides a \verb/::=/, \verb/->/, and \verb/:in:/ for the rule relation between the left-hand side and the right-hand side.
Each of them is mapped to a symbol in the \verb/relation-sym-map/ key-value list; By default, they are mapped to $\Coloneqq$ (\verb/\ensuremath{\Coloneqq}/), $\to$ (\verb/\ensuremath{\to}/), and $\in$ (\verb/\ensuremath{\in}/), respectively.

To provide a custom relation symbol, provide the desired symbol and the mapping to the \verb/relation-sym-map/ key-value list and to the \verb/relation/.

\subsection{Or symbol}
The default or symbol \verb/or-sym/ is \verb/$|$/.
Probably the most common use case for this option is to remove the or symbol altogether by setting it to \verb/{}/.

\subsection{Production separation}
The default production separation \verb/prod-sep/ is \verb/2pt/.
This will add a vertical space between the productions.

\subsection{Row separation}
The default row separation \verb/row-sep/ is \verb/0pt/.
This will add a vertical space between the alternatives in the same production.

\subsection{Layout}\label{subsec:layout}
As mentioned in \secref{sec:tutorial}, the layout of the \verb/bnf/ environment can be customized per-enviroment using the key-value list in the optional argument wrapped in \verb/[]/.
When provided, these should be provided after the configuration key-value list, which is wrapped in \verb/()/.

The optional key-value list for the layout is passed to the backend \textsf{tabularray} package.

An example of customizing the layout is shown below:
\begin{example}[righthand width=.53\linewidth]
\begin{center}
\begin{bnf}(
  prod-delim = {--},
  or-sym = {},
  prod-sep = 5pt,
)[
  colspec = {|[2pt, gray5]llcll},
  column{1} = {font = \sffamily},
  column{2} = {mode = dmath},
  column{4} = {font = \ttfamily},
  column{5} = {font = \itshape},
  cell{9}{4} = {cmd = \fbox},
  row{-} = {bg = azure9},
]
  \tau : Type ::=
  | num : numbers
  | str : strings
  --
  e : Expr ::=
  | $x$ : variable
  | $n$ : numeral
  | $e$ + $e$ : addition
  | $e$ * $e$ : multiplication
  | $e$ \textasciicircum{} $e$ : concatenation
  | len($e$) : length
  | let $x$ = $e_1$ in $e_2$ : definition
\end{bnf}
\end{center}
\end{example}


\section{Caveats}
The choices of delimiters and symbols should be carefully made, in order to avoid conflicts with the grammar of the target language and our meta-language, \TeX{}.

Another subtlety to consider while customizing delimiters, is that certain delimiters not contain the others as a substring.
For instance, as mentioned in \secref{sec:config}, the single-line alternative delimiter must not contain the new-line alternative delimiter as a substring.

When using custom OpenType fonts with the \verb/unicode-math/ package in \XeLaTeX{} or \LuaLaTeX{}, beware of the fact that many fonts lack the \verb/\Coloneqq/ symbol, $\Coloneqq$.
The easiest way to fix this is to actually use a font that provides the symbol, such as \verb/Garamond-Math/, which comes with the \TeX{}Live installation\footnote{\fakeverb{\Coloneq} is not a typo of \fakeverb{\Coloneqq}.}.
\begin{listing}
\setmathfont{Garamond-Math.otf}[
  Scale = MatchUppercase,
  range = {\Coloneq}
]
\end{listing}


\section{The deprecated \texttt{bnfgrammar} environment}
Prior to version 1.0.0, the package provided a sole environment \verb/bnfgrammar/ for typesetting grammars.
While it is still available, it is now deprecated and will be removed in a future release.
Using it will result in a deprecation warning.

To migrate to the new \verb/bnf/ environment, simply replace the \verb/bnfgrammar/ environment with \verb/bnf/, and replace \verb/\in/ with \verb/:in:/ and \verb/||/ with \verb|//|.
Moreover, the new environment does not center the grammar by default, so wrapping it in a \verb/center/ environment is necessary to reproduce the same output.
While there will be some differences in the spacings and the font styles, it is an easy fix.
See \secref{sec:tutorial} for more details.

Below is an example of using the deprecated \verb/bnfgrammar/ environment, left here for historical reasons.
\begin{example}
  \begin{bnfgrammar}
    a : Variables \in \textit{Vars}
    ;;
    expr : Expressions ::=
      expr + term
    | term
    ;;
    term ::= term * a || a
  \end{bnfgrammar}
\end{example}


\section*{Appendix}\label{sec:appendix}
The following is the code used to typeset the metagrammar of the \textsf{simplebnf} DSL in the first page.
\begin{listing}[breakable]
\begin{bnf}(
  prod-delim = ;;;,
  new-line-delim = !,
  single-line-delim = ?,
  comment = //,
  relation = {:::=|:in:},
  relation-sym-map =
    {
      {:::=} = $\Coloneqq$,
      {:in:} = $\in$,
    },
)[
  colspec = lrcll,
  column{2} = {mode=dmath},
  column{4} = {mode=text, font=\ttfamily},
]
  G // Gramar :::=
  ! $P$ // production
  ! $P \fatsemi {}$ // production w/ a trailing delimiter
  ! $P \fatsemi G$ // production sequence
;;;
  P // Production :::= $L \rightarrowtriangle R$
;;;
  L // LHS :::=
  ! $v$ // metavariable
  ! $v\!\fatslash\,c$ // annotated metavariable
;;;
  R // RHS :::=
  ! $\talloblong$ // delimiter
  ! $A \talloblong R$ // alternative sequence
;;;
  A // Alternative :::=
  ! $f$ // syntactic form
  ! $f\!\fatslash\,c$ // annotated syntactic form
;;;
  \fatsemi // Prod. delimiter :::=
  ! ;; // default symbol
  ! $\cdots$ // user-defined
;;;
  \rightarrowtriangle // Rule relation :::=
  ! ::= // $\Coloneqq$
  ! -> // $\to$
  ! \texttt{\char`\\in} // $\in$
  ! $\cdots$ // user-defined
;;;
  \fatslash // Annot. symbol :::=
  ! : // default symbol
  ! $\cdots$ // user-defined
;;;
  \talloblong // Alt. delimiter :::=
  ! | // new-line delimiter
  ! || // single-line delimiter
  ! $\cdots$ // user-defined
;;;
  v, f, c :in: \textsf{\TeX{} tl} // valid \TeX{} token lists
;;;
\end{bnf}
\end{listing}

\end{document}