summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/simplebnf/simplebnf-doc.tex
blob: 62d6cba868251d8fd686c8ba51d4a42575dfddec (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
\documentclass[a4paper]{article}

\usepackage{simplebnf}
\usepackage{hyperref}

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

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

\NewDocumentCommand \cmd { m } {\texttt{\textbackslash#1}}

\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 \env { m m }
  {
    \texttt{%
      \textbackslash begin\{#1\} \textrm{#2}%
      \textbackslash end\{#1\}%
    }%
  }

\title{%
  \textsf{simplebnf} --- A simple package to format Backus-Naur form%
  \footnote{This file describes v0.2.0.}}
\author{Jay Lee\footnote{E-mail: %
  \href{mailto:jaeho.lee@snu.ac.kr}{\texttt{jaeho.lee@snu.ac.kr}}}}
\date{2020/09/01}

\begin{document}
\maketitle

This package provides a simple way to typeset grammars written in Backus-Naur form (BNF).

\begin{presentcommand}
  \cmd{bnfexpr} \cmd{bnfannot}
\end{presentcommand}
These commands are wrappers around \cmd{texttt} and \cmd{textit} respectively.

\begin{presentcommand}
  \env{bnfgrammar}{text}
\end{presentcommand}
can be used to typeset BNF grammars. The \textit{text} inside the environment should be formatted as:
\begin{verbatim}
  term1 ::= rhs1
  ;;
  term2 ::= rhs2
  ;;
  ...
  termk ::= rhsk
\end{verbatim}
where each of the \textit{rhs} represents alternative syntactic forms of the \textit{term}. An annotation may accompany each alternative in which case the alternative must be separated from its annotation with a colon (\verb/:/). If you don't need annotations, simply omit the colons and annotations altogether. The alternatives themselves are separated using the pipe symbol (\verb/|/).

A sample code and the result is shown below:
\begin{exampleside}
  \begin{bnfgrammar}
    expr ::=
      expr + term : sum
    | term        : term
    ;;
    term ::=
      term * a : product
    | a        : variable
  \end{bnfgrammar}
\end{exampleside}
\end{document}