Lably
Skip to content

Extended Backus–Naur form

Family of metasyntax notations

  • 16 min read
  • 5 chapters

Related on Lably

Through Computer science, Context-free grammar and Code

This article uses EBNF as specified by the ISO for examples applying to all EBNFs. Other EBNF variants use somewhat different syntactic conventions.

Basics

EBNF is a code that expresses the syntax of a formal language. An EBNF consists of terminal symbols and non-terminal production rules which are the restrictions governing how terminal symbols can be combined into a valid sequence. Examples of terminal symbols include alphanumeric characters, punctuation marks, and whitespace characters.

The EBNF defines production rules where sequences of symbols are respectively assigned to a nonterminal:

<span id="mwLw"></span><span class="k" id="mwMA">digit excluding zero </span><span class="o" id="mwMQ">=</span> <span class="s2" id="mwMg">"1"</span> <span class="p" id="mwMw">|</span> <span class="s2" id="mwNA">"2"</span> <span class="p" id="mwNQ">|</span> <span class="s2" id="mwNg">"3"</span> <span class="p" id="mwNw">|</span> <span class="s2" id="mwOA">"4"</span> <span class="p" id="mwOQ">|</span> <span class="s2" id="mwOg">"5"</span> <span class="p" id="mwOw">|</span> <span class="s2" id="mwPA">"6"</span> <span class="p" id="mwPQ">|</span> <span class="s2" id="mwPg">"7"</span> <span class="p" id="mwPw">|</span> <span class="s2" id="mwQA">"8"</span> <span class="p" id="mwQQ">|</span> <span class="s2" id="mwQg">"9"</span> <span class="p" id="mwQw">;</span> <span class="k" id="mwRA">digit </span><span class="o" id="mwRQ">=</span> <span class="s2" id="mwRg">"0"</span> <span class="p" id="mwRw">|</span> <span class="k" id="mwSA">digit excluding zero </span><span class="p" id="mwSQ">;</span>

This production rule defines the nonterminal digit which is on the left side of the assignment. The vertical bar represents an alternative and the terminal symbols are enclosed with quotation marks followed by a semicolon as terminating character. Hence a digit is a 0 or a digit excluding zero that can be 1 or 2 or 3 and so forth until 9.

A production rule can also include a sequence of terminals or nonterminals, each separated by a comma:

<span id="mwVg"></span><span class="k" id="mwVw">twelve </span><span class="o" id="mwWA">=</span> <span class="s2" id="mwWQ">"1"</span><span class="p" id="mwWg">,</span> <span class="s2" id="mwWw">"2"</span> <span class="p" id="mwXA">;</span> <span class="k" id="mwXQ">two hundred one </span><span class="o" id="mwXg">=</span> <span class="s2" id="mwXw">"2"</span><span class="p" id="mwYA">,</span> <span class="s2" id="mwYQ">"0"</span><span class="p" id="mwYg">,</span> <span class="s2" id="mwYw">"1"</span> <span class="p" id="mwZA">;</span> <span class="k" id="mwZQ">three hundred twelve </span><span class="o" id="mwZg">=</span> <span class="s2" id="mwZw">"3"</span><span class="p" id="mwaA">,</span> <span class="k" id="mwaQ">twelve </span><span class="p" id="mwag">;</span> <span class="k" id="mwaw">twelve thousand two hundred one </span><span class="o" id="mwbA">=</span> <span class="k" id="mwbQ">twelve</span><span class="p" id="mwbg">,</span> <span class="k" id="mwbw">two hundred one </span><span class="p" id="mwcA">;</span>

Expressions that may be omitted or repeated can be represented through curly braces { ... }:

<span id="mwdA"></span><span class="k" id="mwdQ">positive integer </span><span class="o" id="mwdg">=</span> <span class="k" id="mwdw">digit excluding zero</span><span class="p" id="mweA">,</span> <span class="p" id="mweQ">{</span> <span class="k" id="mweg">digit </span><span class="p" id="mwew">}</span> <span class="p" id="mwfA">;</span>

In this case, the strings 1, 2, ..., 10, ..., 10000, ... are correct expressions. To represent this, everything that is set within the curly braces may be repeated arbitrarily often, including not at all.

An option can be represented through squared brackets [ ... ]. That is, everything that is set within the square brackets may be present just once, or not at all:

<span id="mwhQ"></span><span class="k" id="mwhg">integer </span><span class="o" id="mwhw">=</span> <span class="s2" id="mwiA">"0"</span> <span class="p" id="mwiQ">|</span> <span class="p" id="mwig">[</span> <span class="s2" id="mwiw">"-"</span> <span class="p" id="mwjA">],</span> <span class="k" id="mwjQ">positive integer </span><span class="p" id="mwjg">;</span>

Therefore, an integer is a zero (0) or a positive integer that may be preceded by an optional minus sign.

EBNF also provides, among other things, the syntax to describe repetitions (of a specified number of times), to exclude some part of a production, and to insert comments in an EBNF grammar.

Table of symbols

The following represents a proposed ISO/IEC 14977 standard, by R. S. Scowen, page 7, tables 1 and 2.

Sign in to keep reading

4 more chapters are free with a Lably account.

More in Computing and communication