| 1 |
\documentclass[11pt]{article} |
| 2 |
\usepackage{geometry} |
| 3 |
\geometry{a4paper,top=1in,bottom=1in,left=1.2in,right=1.2in} |
| 4 |
|
| 5 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 6 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Font setup %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 7 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 8 |
|
| 9 |
\usepackage{microtype} |
| 10 |
|
| 11 |
% Works for pdfLaTeX, XeLaTeX, and LuaLaTeX |
| 12 |
\usepackage{libertine} |
| 13 |
|
| 14 |
% Math/Mono font setup |
| 15 |
\ExplSyntaxOn |
| 16 |
\bool_if:nTF { \sys_if_engine_xetex_p: || \sys_if_engine_luatex_p: } |
| 17 |
{ |
| 18 |
% Load mathtools before unicode-math |
| 19 |
\usepackage{mathtools} |
| 20 |
\usepackage[ |
| 21 |
math-style=ISO, |
| 22 |
warnings-off={mathtools-colon, mathtools-overbracket}, |
| 23 |
]{unicode-math} |
| 24 |
\setmathfont{LibertinusMath-Regular.otf}[Scale = MatchUppercase] |
| 25 |
\setmathfont{Garamond-Math.otf}[ |
| 26 |
Scale = MatchUppercase, |
| 27 |
range = {\Coloneq} |
| 28 |
] |
| 29 |
|
| 30 |
\setmonofont{Inconsolatazi4}[ |
| 31 |
Extension = .otf, |
| 32 |
UprightFont = *-Regular, |
| 33 |
BoldFont = *-Bold, |
| 34 |
Scale=MatchLowercase, |
| 35 |
AutoFakeSlant = 0.2, |
| 36 |
] |
| 37 |
} |
| 38 |
{ |
| 39 |
\usepackage[libertine]{newtxmath} |
| 40 |
\usepackage{inconsolata} |
| 41 |
} |
| 42 |
\ExplSyntaxOff |
| 43 |
|
| 44 |
% Some fancy symbols (prevent being overwritten by math fonts) |
| 45 |
\AtBeginDocument{% |
| 46 |
\usepackage[ |
| 47 |
only, fatsemi, fatslash, fatbslash, talloblong, rightarrowtriangle |
| 48 |
]{stmaryrd}% |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 53 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Presentations %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 54 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 55 |
|
| 56 |
\usepackage{tcolorbox} |
| 57 |
\tcbuselibrary{listings,breakable} |
| 58 |
\tcbset{listing engine=listings, colframe=black, colback=white, size=small} |
| 59 |
|
| 60 |
\NewTCBListing { example } { !O{} !s } |
| 61 |
{ |
| 62 |
sharp corners, |
| 63 |
IfBooleanTF = { #2 } |
| 64 |
{ listing above text } |
| 65 |
{ listing side text }, |
| 66 |
fontlower = \small, |
| 67 |
#1 |
| 68 |
} |
| 69 |
|
| 70 |
\NewTCBListing { listing } { !O{} } |
| 71 |
{ sharp corners, listing only, #1 } |
| 72 |
|
| 73 |
|
| 74 |
\NewDocumentEnvironment {exampleside} {} |
| 75 |
{ \tcblisting{listing side text,righthand width=.5\textwidth} } |
| 76 |
{ \endtcblisting } |
| 77 |
|
| 78 |
\NewDocumentEnvironment { presentcommand } { b } |
| 79 |
{% |
| 80 |
\vspace*{0.5\baselineskip}\noindent\fbox{% |
| 81 |
\begin{minipage}{\dimexpr\textwidth-2\fboxsep-2\fboxrule} |
| 82 |
#1 |
| 83 |
\end{minipage}}\vspace*{0.5\baselineskip} |
| 84 |
} |
| 85 |
{ } |
| 86 |
|
| 87 |
|
| 88 |
\NewDocumentCommand \cmd { m } {\texttt{\char`\\#1}} |
| 89 |
\NewDocumentCommand \env { m m } |
| 90 |
{ |
| 91 |
\texttt{% |
| 92 |
\char`\\begin\{#1\} \textrm{#2}% |
| 93 |
\char`\\end\{#1\}% |
| 94 |
}% |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
\usepackage{tabularray} |
| 99 |
\UseTblrLibrary{booktabs} |
| 100 |
% \fakeverb |
| 101 |
\usepackage{codehigh} |
| 102 |
|
| 103 |
|
| 104 |
\newcommand*{\secref}[1]{\hyperref[{#1}]{section~\ref*{#1} -- \emph{\nameref*{#1}}}} |
| 105 |
\newcommand*{\subsecref}[1]{\hyperref[{#1}]{subsection~\ref*{#1} -- \emph{\nameref*{#1}}}} |
| 106 |
\newcommand*{\subsubsecref}[1]{\hyperref[{#1}]{\nameref*{#1}}} |
| 107 |
\newcommand*{\parref}[1]{\hyperref[{#1}]{\nameref*{#1}}} |
| 108 |
|
| 109 |
|
| 110 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 111 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% simplebnf %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 112 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 113 |
|
| 114 |
\usepackage{simplebnf} |
| 115 |
|
| 116 |
|
| 117 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 118 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Miscellaneous %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 119 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 120 |
|
| 121 |
\usepackage{hologo} |
| 122 |
\newcommand*{\XeLaTeX}{\hologo{XeLaTeX}} |
| 123 |
\newcommand*{\LuaLaTeX}{\hologo{LuaLaTeX}} |
| 124 |
|
| 125 |
% ``Make sure it comes *last* of your loaded packages" |
| 126 |
\usepackage[pdfborder={0 0 0}, colorlinks=true]{hyperref} |
| 127 |
|
| 128 |
|
| 129 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 130 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Metadata %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 131 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
| 132 |
|
| 133 |
\title{% |
| 134 |
\textsf{simplebnf} --- A simple package to format Backus-Naur form% |
| 135 |
\footnote{This file describes v1.1.0.}} |
| 136 |
\author{Jay Lee\footnote{E-mail: % |
| 137 |
\href{mailto:[email protected]}{\texttt{[email protected]}}}} |
| 138 |
\date{2026/05/19} |
| 139 |
|
| 140 |
|
| 141 |
\begin{document} |
| 142 |
\maketitle |
| 143 |
|
| 144 |
|
| 145 |
\vfill |
| 146 |
This package provides a simple way for typesetting grammars in Backus-Naur form (BNF). |
| 147 |
It features a flexible configuration system, allowing for the customization of the domain-specific language (DSL) used in typesetting the grammar. |
| 148 |
Additionally, the package comes with sensible defaults. |
| 149 |
|
| 150 |
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}.} |
| 151 |
\def\colonincolon{:in:} |
| 152 |
\def\doubleslash{//} |
| 153 |
\def\doublebslash{\char`\\\char`\\} |
| 154 |
\def\metabnfcomment{\char`\\bnfcomment} |
| 155 |
\begin{tcolorbox}[sharp corners] |
| 156 |
\begin{center} |
| 157 |
\begin{bnf}( |
| 158 |
prod-delim = ;;;, |
| 159 |
new-line-delim = !, |
| 160 |
single-line-delim = ?, |
| 161 |
comment = //, |
| 162 |
relation = {:::=|:in:}, |
| 163 |
relation-sym-map = |
| 164 |
{ |
| 165 |
{:::=} = $\Coloneqq$, |
| 166 |
{:in:} = $\in$, |
| 167 |
}, |
| 168 |
)[ |
| 169 |
colspec = lrcll, |
| 170 |
column{2} = {mode=dmath}, |
| 171 |
column{4} = {mode=text, font=\ttfamily}, |
| 172 |
] |
| 173 |
G // Grammar :::= |
| 174 |
! $P$ // production |
| 175 |
! $P \fatsemi {}$ // production w/ a trailing delimiter |
| 176 |
! $P \fatsemi G$ // production sequence |
| 177 |
! \texttt{\metabnfcomment\char`{$c$\char`}} // full-line comment |
| 178 |
! \texttt{\metabnfcomment\char`{$c$\char`}\,$P$} // commented production |
| 179 |
;;; |
| 180 |
P // Production :::= $L \rightarrowtriangle R$ |
| 181 |
;;; |
| 182 |
L // LHS :::= |
| 183 |
! $v$ // metavariable |
| 184 |
! $v\!\fatslash\,c$ // commented metavariable |
| 185 |
;;; |
| 186 |
R // RHS :::= |
| 187 |
! $A$ // alternative |
| 188 |
! $A \talloblong R$ // alternative sequence |
| 189 |
;;; |
| 190 |
A // Alternative :::= |
| 191 |
! $f$ // syntactic form |
| 192 |
! $f\fatbslash A$ // line-broken syntactic form |
| 193 |
! $f\!\fatslash\,c$ // commented syntactic form |
| 194 |
;;; |
| 195 |
\fatsemi // Prod. delimiter :::= |
| 196 |
! ;; // default symbol |
| 197 |
! $\cdots$ // user-defined |
| 198 |
;;; |
| 199 |
\rightarrowtriangle // Rule relation :::= |
| 200 |
! ::= // $\Coloneqq$ |
| 201 |
! -> // $\to$ |
| 202 |
! \colonincolon // $\in$ |
| 203 |
! $\cdots$ // user-defined |
| 204 |
;;; |
| 205 |
\fatslash // Annot. symbol :::= |
| 206 |
! : // default symbol |
| 207 |
! $\cdots$ // user-defined |
| 208 |
;;; |
| 209 |
\talloblong // Alt. delimiter :::= |
| 210 |
! | // new-line delimiter |
| 211 |
! \doubleslash // single-line delimiter |
| 212 |
! $\cdots$ // user-defined |
| 213 |
;;; |
| 214 |
\fatbslash // Line-break delim :::= |
| 215 |
! \doublebslash // default symbol |
| 216 |
! $\cdots$ // user-defined |
| 217 |
;;; |
| 218 |
v, f, c :in: \textsf{\TeX{} tl} // valid \TeX{} token lists |
| 219 |
;;; |
| 220 |
\end{bnf} |
| 221 |
\end{center} |
| 222 |
\end{tcolorbox} |
| 223 |
\vfill |
| 224 |
|
| 225 |
\section{Tutorial for the impatient}\label{sec:tutorial} |
| 226 |
Typesetting a grammar is as simple as typing the BNF grammar in the \verb/bnf/ environment: |
| 227 |
\begin{example}[righthand width=.52\linewidth] |
| 228 |
\begin{center} |
| 229 |
\begin{bnf} |
| 230 |
$\tau$ : \textsf{Type} ::= |
| 231 |
| \texttt{num} : numbers |
| 232 |
| \texttt{str} : strings |
| 233 |
;; |
| 234 |
$e$ : \textsf{Expr} ::= |
| 235 |
| $x$ : variable |
| 236 |
| $n$ : numeral |
| 237 |
| \texttt{$e$ + $e$} : addition |
| 238 |
| \texttt{$e$ * $e$} : multiplication |
| 239 |
| \texttt{$e$ \textasciicircum{} $e$} : concatenation |
| 240 |
| \texttt{len($e$)} : length |
| 241 |
| \texttt{let $x$ = $e_1$ in $e_2$} : definition |
| 242 |
\end{bnf} |
| 243 |
\end{center} |
| 244 |
\end{example} |
| 245 |
|
| 246 |
Typically, each column in the BNF grammar has the same font style. |
| 247 |
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$. |
| 248 |
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. |
| 249 |
|
| 250 |
\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}.}: |
| 251 |
\begin{listing} |
| 252 |
\begin{bnf}[ |
| 253 |
colspec = {llcll}, |
| 254 |
column{1} = {font = \sffamily}, |
| 255 |
column{2} = {mode = dmath}, |
| 256 |
column{4} = {font = \ttfamily}, |
| 257 |
] |
| 258 |
\tau : Type ::= |
| 259 |
| num : numbers |
| 260 |
| str : strings |
| 261 |
;; |
| 262 |
e : Expr ::= |
| 263 |
| $x$ : variable |
| 264 |
| $n$ : numeral |
| 265 |
| $e$ + $e$ : addition |
| 266 |
| $e$ * $e$ : multiplication |
| 267 |
| $e$ \textasciicircum{} $e$ : concatenation |
| 268 |
| len($e$) : length |
| 269 |
| let $x$ = $e_1$ in $e_2$ : definition |
| 270 |
\end{bnf} |
| 271 |
\end{listing} |
| 272 |
If you find yourself using the same configuration repeatedly, you can fix the desired configuration using \cmd{SetBNFLayout}. |
| 273 |
Once set using \cmd{SetBNFLayout}, the configuration is applied to all subsequent \verb/bnf/ environments: |
| 274 |
\begin{listing} |
| 275 |
% Some where above... |
| 276 |
\SetBNFLayout{ |
| 277 |
colspec = {llcll}, |
| 278 |
column{1} = {font = \sffamily}, |
| 279 |
column{2} = {mode = dmath}, |
| 280 |
column{4} = {font = \ttfamily}, |
| 281 |
} |
| 282 |
% ... |
| 283 |
\begin{bnf} |
| 284 |
\tau : Type ::= |
| 285 |
| num : numbers |
| 286 |
| str : strings |
| 287 |
;; |
| 288 |
e : Expr ::= |
| 289 |
| $x$ : variable |
| 290 |
| $n$ : numeral |
| 291 |
| $e$ + $e$ : addition |
| 292 |
| $e$ * $e$ : multiplication |
| 293 |
| $e$ \textasciicircum{} $e$ : concatenation |
| 294 |
| len($e$) : length |
| 295 |
| let $x$ = $e_1$ in $e_2$ : definition |
| 296 |
\end{bnf} |
| 297 |
\end{listing} |
| 298 |
|
| 299 |
Since these customizations are provided by the backend \textsf{tabularray}\footnote{\url{https://github.com/lvjr/tabularray}}, consult its documentation for more details. |
| 300 |
The corresponding command is \cmd{SetTblrInner}. |
| 301 |
|
| 302 |
For more advanced customization, such as changing default delimiters and symbols, continue to \secref{sec:config}. |
| 303 |
|
| 304 |
\section{Configuration}\label{sec:config} |
| 305 |
While the default configuration should be sufficient for most use cases, some grammars may require using different delimiters and symbols. |
| 306 |
For example, the language itself may contain \verb/->/ as a syntactic form, which conflicts with one of the default symbols for the rule relation. |
| 307 |
|
| 308 |
To this end, \textsf{simplebnf} provides a number of options to configure the DSL used to typeset the grammar. |
| 309 |
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 comment symbol, and more. |
| 310 |
|
| 311 |
The default configuration is shown in \autoref{tab:config}. |
| 312 |
\begin{longtblr}[ |
| 313 |
caption = {Default key-values for the configuration of \textsf{simplebnf}.}, |
| 314 |
label = {tab:config} |
| 315 |
]{ |
| 316 |
colspec={ll}, |
| 317 |
row{2-Z}={font=\ttfamily} |
| 318 |
} |
| 319 |
\toprule |
| 320 |
Key & Default value \\ |
| 321 |
\midrule |
| 322 |
prod-delim & ;;\\ |
| 323 |
new-line-delim & \fakeverb{|}\\ |
| 324 |
single-line-delim & //\\ |
| 325 |
line-break-delim & \fakeverb{\\\\}\\ |
| 326 |
comment & :\\ |
| 327 |
relation & \fakeverb{{::=|->|:in:}}\\ |
| 328 |
relation-sym-map & { |
| 329 |
\{\\ |
| 330 |
\quad\fakeverb{{::=} = {\ensuremath{\Coloneqq}},}\\ |
| 331 |
\quad\fakeverb{{->} = {\ensuremath{\to}},}\\ |
| 332 |
\quad\fakeverb{{:in:} = {\ensuremath{\in}},}\\ |
| 333 |
\} |
| 334 |
}\\ |
| 335 |
or-sym & \fakeverb{$|$}\\ |
| 336 |
prod-sep & 2pt\\ |
| 337 |
row-sep & 0pt\\ |
| 338 |
\bottomrule |
| 339 |
\end{longtblr} |
| 340 |
|
| 341 |
The key-value list can be provided as an optional argument to the \verb/bnf/ environment, wrapped in \verb/()/. |
| 342 |
The optional key-value list for the layout---wrapped in \verb/[]/---should be provided after the configuration key-value list. |
| 343 |
See \subsecref{subsec:layout} for more details. |
| 344 |
|
| 345 |
An exmaple of customizing the configuration is shown below: |
| 346 |
\begin{example}* |
| 347 |
\begin{center} |
| 348 |
\begin{bnf}( |
| 349 |
prod-delim = {--}, |
| 350 |
new-line-delim = {\&}, |
| 351 |
single-line-delim = {\&\&}, |
| 352 |
comment = {;}, |
| 353 |
relation-sym-map = |
| 354 |
{ |
| 355 |
{->} = {\ensuremath{\hookrightarrow}}, |
| 356 |
{:in:} = {\ensuremath{\in}}, |
| 357 |
}, |
| 358 |
or-sym = {}, |
| 359 |
)[ |
| 360 |
colspec = {llcll}, |
| 361 |
column{1} = {font = \sffamily}, |
| 362 |
column{2} = {mode = dmath}, |
| 363 |
column{4} = {font = \ttfamily}, |
| 364 |
] |
| 365 |
\tau ; Type -> |
| 366 |
& num ; numbers |
| 367 |
& str ; strings |
| 368 |
-- |
| 369 |
e ; Expr -> |
| 370 |
& $x$ ; variable |
| 371 |
& $n$ ; numeral |
| 372 |
& $e$ + $e$ ; addition |
| 373 |
& $e$ * $e$ ; multiplication |
| 374 |
& $e$ \textasciicircum{} $e$ ; concatenation |
| 375 |
& len($e$) ; length |
| 376 |
& let $x$ = $e_1$ in $e_2$ ; definition |
| 377 |
\end{bnf} |
| 378 |
\end{center} |
| 379 |
\end{example} |
| 380 |
|
| 381 |
|
| 382 |
The configuration can also be set globally using \cmd{SetBNFConfig}, similar to \cmd{SetBNFLayout}: |
| 383 |
\begin{listing} |
| 384 |
% Some where above... |
| 385 |
\SetBNFConfig{ |
| 386 |
prod-delim = {--}, |
| 387 |
new-line-delim = {\&}, |
| 388 |
single-line-delim = {\&\&}, |
| 389 |
comment = {;}, |
| 390 |
relation-sym-map = |
| 391 |
{ |
| 392 |
{->} = {\ensuremath{\hookrightarrow}}, |
| 393 |
{:in:} = {\ensuremath{\in}}, |
| 394 |
}, |
| 395 |
or-sym = {}, |
| 396 |
} |
| 397 |
% ... |
| 398 |
\begin{bnf}[ |
| 399 |
colspec = {llcll}, |
| 400 |
column{1} = {font = \sffamily}, |
| 401 |
column{2} = {mode = dmath}, |
| 402 |
column{4} = {font = \ttfamily}, |
| 403 |
] |
| 404 |
\tau ; Type -> |
| 405 |
& num ; numbers |
| 406 |
& str ; strings |
| 407 |
-- |
| 408 |
e ; Expr -> |
| 409 |
& $x$ ; variable |
| 410 |
& $n$ ; numeral |
| 411 |
& $e$ + $e$ ; addition |
| 412 |
& $e$ * $e$ ; multiplication |
| 413 |
& $e$ \textasciicircum{} $e$ ; concatenation |
| 414 |
& len($e$) ; length |
| 415 |
& let $x$ = $e_1$ in $e_2$ ; definition |
| 416 |
\end{bnf} |
| 417 |
\end{listing} |
| 418 |
|
| 419 |
|
| 420 |
\subsection{Production delimiter} |
| 421 |
The default production delimiter \verb/prod-delim/ is \verb/;;/. |
| 422 |
This will separate different productions in the same grammar. |
| 423 |
|
| 424 |
\subsection{New-line alternative delimiter} |
| 425 |
The default new-line alternative delimiter \verb/new-line-delim/ is \verb/\|/, which will actually match verbatim \verb/\|/ in the grammar. |
| 426 |
This will separate different alternatives into different lines in the same production. |
| 427 |
|
| 428 |
\subsection{Single-line alternative delimiter} |
| 429 |
The default single-line alternative delimiter \verb/single-line-delim/ is \verb|//|. |
| 430 |
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. |
| 431 |
|
| 432 |
For consecutive alternatives delimited by the single-line alternative delimiter, only the last of them can be commented. |
| 433 |
|
| 434 |
Note that the single-line alternative delimiter must not contain the new-line alternative delimiter as a substring. |
| 435 |
|
| 436 |
\subsection{Line-break delimiter} |
| 437 |
The line-break delimiter \verb/line-break-delim/ defaults to \verb/\\/. |
| 438 |
It forces a manual line break inside a single alternative of the right-hand side, aligning the continuation under the RHS column without repeating the relation or the or-symbol. |
| 439 |
|
| 440 |
Example: |
| 441 |
\begin{example}* |
| 442 |
\begin{center} |
| 443 |
\begin{bnf} |
| 444 |
$e$ : Expr ::= |
| 445 |
| verylongtoken verylongtoken verylongtoken verylongtoken \\ |
| 446 |
continued tokens still part of the same alternative : long form |
| 447 |
\end{bnf} |
| 448 |
\end{center} |
| 449 |
\end{example} |
| 450 |
|
| 451 |
\subsection{Comment delimiter} |
| 452 |
The default comment delimiter \verb/comment/ is \verb/:/. |
| 453 |
This will separate the syntactic form and the comment within the alternative. |
| 454 |
|
| 455 |
\subsection{Rule relations and the symbol map} |
| 456 |
\textsf{simplebnf} provides a \verb/::=/, \verb/->/, and \verb/:in:/ for the rule relation between the left-hand side and the right-hand side. |
| 457 |
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. |
| 458 |
|
| 459 |
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/. |
| 460 |
|
| 461 |
\subsection{Or symbol} |
| 462 |
The default or symbol \verb/or-sym/ is \verb/$|$/. |
| 463 |
Probably the most common use case for this option is to remove the or symbol altogether by setting it to \verb/{}/. |
| 464 |
|
| 465 |
\subsection{Production separation} |
| 466 |
The default production separation \verb/prod-sep/ is \verb/2pt/. |
| 467 |
This will add a vertical space between the productions. |
| 468 |
|
| 469 |
\subsection{Row separation} |
| 470 |
The default row separation \verb/row-sep/ is \verb/0pt/. |
| 471 |
This will add a vertical space between the alternatives in the same production. |
| 472 |
|
| 473 |
\subsection{Layout}\label{subsec:layout} |
| 474 |
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/[]/. |
| 475 |
When provided, these should be provided after the configuration key-value list, which is wrapped in \verb/()/. |
| 476 |
|
| 477 |
The optional key-value list for the layout is passed to the backend \textsf{tabularray} package. |
| 478 |
|
| 479 |
An example of customizing the layout is shown below: |
| 480 |
\begin{example}[righthand width=.51\linewidth] |
| 481 |
\begin{center} |
| 482 |
\begin{bnf}( |
| 483 |
prod-delim = {--}, |
| 484 |
or-sym = {}, |
| 485 |
prod-sep = 5pt, |
| 486 |
)[ |
| 487 |
colspec = {|[2pt, gray5]llcll}, |
| 488 |
column{1} = {font = \sffamily}, |
| 489 |
column{2} = {mode = dmath}, |
| 490 |
column{4} = {font = \ttfamily}, |
| 491 |
column{5} = {font = \itshape}, |
| 492 |
cell{9}{4} = {cmd = \fbox}, |
| 493 |
row{-} = {bg = azure9}, |
| 494 |
] |
| 495 |
\tau : Type ::= |
| 496 |
| num : numbers |
| 497 |
| str : strings |
| 498 |
-- |
| 499 |
e : Expr ::= |
| 500 |
| $x$ : variable |
| 501 |
| $n$ : numeral |
| 502 |
| $e$ + $e$ : add |
| 503 |
| $e$ * $e$ : mult |
| 504 |
| $e$ \textasciicircum{} $e$ : concat |
| 505 |
| len($e$) : length |
| 506 |
| let $x$ = $e_1$ in $e_2$ : definition |
| 507 |
\end{bnf} |
| 508 |
\end{center} |
| 509 |
\end{example} |
| 510 |
|
| 511 |
\subsection{Full-line comments}\label{subsec:comment} |
| 512 |
To insert a full-line comment row inside a grammar, use \cmd{bnfcomment}: |
| 513 |
\begin{example} |
| 514 |
\begin{center} |
| 515 |
\begin{bnf} |
| 516 |
\bnfcomment{Types} |
| 517 |
$\tau$ : Type ::= \texttt{num} // \texttt{str} |
| 518 |
;; |
| 519 |
\bnfcomment{Expressions} |
| 520 |
$e$ : Expr ::= $x$ // $n$ // $e$ + $e$ |
| 521 |
\end{bnf} |
| 522 |
\end{center} |
| 523 |
\end{example} |
| 524 |
|
| 525 |
|
| 526 |
\section{Caveats} |
| 527 |
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{}. |
| 528 |
|
| 529 |
Another subtlety to consider while customizing delimiters, is that certain delimiters should not contain the others as a substring. |
| 530 |
For instance, as mentioned in \secref{sec:config}, the single-line alternative delimiter must not contain the new-line alternative delimiter as a substring. |
| 531 |
|
| 532 |
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$. |
| 533 |
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}.}. |
| 534 |
\begin{listing} |
| 535 |
\setmathfont{Garamond-Math.otf}[ |
| 536 |
Scale = MatchUppercase, |
| 537 |
range = {\Coloneq} |
| 538 |
] |
| 539 |
\end{listing} |
| 540 |
|
| 541 |
|
| 542 |
\section{The deprecated \texttt{bnfgrammar} environment} |
| 543 |
Prior to version 1.0.0, the package provided a sole environment \verb/bnfgrammar/ for typesetting grammars. |
| 544 |
While it is still available, it is now deprecated and will be removed in a future release. |
| 545 |
Using it will result in a deprecation warning. |
| 546 |
|
| 547 |
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|//|. |
| 548 |
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. |
| 549 |
While there will be some differences in the spacings and the font styles, it is an easy fix. |
| 550 |
See \secref{sec:tutorial} for more details. |
| 551 |
|
| 552 |
Below is an example of using the deprecated \verb/bnfgrammar/ environment, left here for historical reasons. |
| 553 |
\begin{example} |
| 554 |
\begin{bnfgrammar} |
| 555 |
a : Variables \in \textit{Vars} |
| 556 |
;; |
| 557 |
expr : Expressions ::= |
| 558 |
expr + term |
| 559 |
| term |
| 560 |
;; |
| 561 |
term ::= term * a || a |
| 562 |
\end{bnfgrammar} |
| 563 |
\end{example} |
| 564 |
|
| 565 |
|
| 566 |
\section*{Appendix}\label{sec:appendix} |
| 567 |
The following is the code used to typeset the metagrammar of the \textsf{simplebnf} DSL in the first page. |
| 568 |
\begin{listing}[breakable] |
| 569 |
\def\colonincolon{:in:} |
| 570 |
\def\doubleslash{//} |
| 571 |
\def\doublebslash{\char`\\\char`\\} |
| 572 |
\def\metabnfcomment{\char`\\bnfcomment} |
| 573 |
\begin{bnf}( |
| 574 |
prod-delim = ;;;, |
| 575 |
new-line-delim = !, |
| 576 |
single-line-delim = ?, |
| 577 |
comment = //, |
| 578 |
relation = {:::=|:in:}, |
| 579 |
relation-sym-map = |
| 580 |
{ |
| 581 |
{:::=} = $\Coloneqq$, |
| 582 |
{:in:} = $\in$, |
| 583 |
}, |
| 584 |
)[ |
| 585 |
colspec = lrcll, |
| 586 |
column{2} = {mode=dmath}, |
| 587 |
column{4} = {mode=text, font=\ttfamily}, |
| 588 |
] |
| 589 |
G // Grammar :::= |
| 590 |
! $P$ // production |
| 591 |
! $P \fatsemi {}$ // production w/ a trailing delimiter |
| 592 |
! $P \fatsemi G$ // production sequence |
| 593 |
! \texttt{\metabnfcomment\char`{$c$\char`}} // full-line comment |
| 594 |
! \texttt{\metabnfcomment\char`{$c$\char`}\,$P$} // commented production |
| 595 |
;;; |
| 596 |
P // Production :::= $L \rightarrowtriangle R$ |
| 597 |
;;; |
| 598 |
L // LHS :::= |
| 599 |
! $v$ // metavariable |
| 600 |
! $v\!\fatslash\,c$ // commented metavariable |
| 601 |
;;; |
| 602 |
R // RHS :::= |
| 603 |
! $A$ // alternative |
| 604 |
! $A \talloblong R$ // alternative sequence |
| 605 |
;;; |
| 606 |
A // Alternative :::= |
| 607 |
! $f$ // syntactic form |
| 608 |
! $f\fatbslash A$ // line-broken syntactic form |
| 609 |
! $f\!\fatslash\,c$ // commented syntactic form |
| 610 |
;;; |
| 611 |
\fatsemi // Prod. delimiter :::= |
| 612 |
! ;; // default symbol |
| 613 |
! $\cdots$ // user-defined |
| 614 |
;;; |
| 615 |
\rightarrowtriangle // Rule relation :::= |
| 616 |
! ::= // $\Coloneqq$ |
| 617 |
! -> // $\to$ |
| 618 |
! \colonincolon // $\in$ |
| 619 |
! $\cdots$ // user-defined |
| 620 |
;;; |
| 621 |
\fatslash // Annot. symbol :::= |
| 622 |
! : // default symbol |
| 623 |
! $\cdots$ // user-defined |
| 624 |
;;; |
| 625 |
\talloblong // Alt. delimiter :::= |
| 626 |
! | // new-line delimiter |
| 627 |
! \doubleslash // single-line delimiter |
| 628 |
! $\cdots$ // user-defined |
| 629 |
;;; |
| 630 |
\fatbslash // Line-break delim :::= |
| 631 |
! \doublebslash // default symbol |
| 632 |
! $\cdots$ // user-defined |
| 633 |
;;; |
| 634 |
v, f, c :in: \textsf{\TeX{} tl} // valid \TeX{} token lists |
| 635 |
;;; |
| 636 |
\end{bnf} |
| 637 |
\end{listing} |
| 638 |
|
| 639 |
\end{document} |