summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/latex/simplebnf/simplebnf-doc.tex
blob: 30ea889f18b61e5b041b4b3ad359c2cf8e35a0f7 (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
\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=.55\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.3.0.}}
\author{Jay Lee\footnote{E-mail: %
  \href{mailto:jaeho.lee@snu.ac.kr}{\texttt{jaeho.lee@snu.ac.kr}}}}
\date{2022/05/07}

\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}
    a \in \textit{Vars}
    ;;
    expr ::=
      expr + term : sum
    | term        : term
    ;;
    term ::=
      term * a : product
    | a        : variable
  \end{bnfgrammar}
\end{exampleside}

Annotations can also be provided on left-hand sides, to label the nonterminal instead of a specific production.
\begin{exampleside}
  \begin{bnfgrammar}
    a : Variables \in \textit{Vars}
    ;;
    expr : Expressions ::=
      expr + term
    | term
    ;;
    term ::=
      term * a
    | a
  \end{bnfgrammar}
\end{exampleside}

You can also provide an optional specification to the grammar environment, to redefine alignment or spacing.
\begin{tcblisting}{text above listing}
  \begin{bnfgrammar}[lr@{\hspace{4pt}}c@{\hspace{2pt}}ll]
    a : Variables \in \textit{Vars}
    ;;
    expr ::=
      expr + term : sum
    | term        : term
    ;;
    term ::=
      term * a : product
    | a        : variable
  \end{bnfgrammar}
\end{tcblisting}

If you want to typeset multiple productions on a single line, you can use double vertical bars by default.
\begin{exampleside}
  \begin{bnfgrammar}
    a \in \textit{Vars}
    ;;
    expr ::= expr + term || term
    ;;
    term ::= term * a || a
  \end{bnfgrammar}
\end{exampleside}

The second and third optional arguments specify regular expressions for the line-breaking and non-breaking RHS seperators:
\begin{tcblisting}{text above listing}
  \begin{bnfgrammar}[llcll][\|\|][\|]
    a \in \textit{Vars}
    ;;
    expr ::= expr + term | term
    ;;
    term ::= term * a
    || a
  \end{bnfgrammar}
\end{tcblisting}

\end{document}