Quidest?

Backus-Naur Syntax

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.

In formal languages, terminal and nonterminal symbols are parts of the vocabulary under a formal grammar. Vocabulary is a finite, nonempty set of symbols. Terminal symbols are symbols that cannot be replaced by other symbols of the vocabulary. Nonterminal symbols are symbols that can be replaced by other symbols of the vocabulary by the production rules under the same formal grammar.

Core Syntax

Basic Structure

1rule_name = definition ;

Every rule ends with semicolon. Rule names are typically lowercase or capitalized.

Terminal vs Non-terminal

1non_terminal = "terminal" ;
2digit = "0" | "1" | "2" ;

EBNF Operators

Concatenation (sequence)

1full_name = first_name, " ", last_name ;

Comma means “followed by”. Can also use space in some dialects.

Alternation (choice)

1operator = "+" | "-" | "*" | "/" ;

Pipe means “or”. Pick one option.

Optional (zero or one)

1sign = ["+"] | ["-"] ;
2number = [sign], digit, {digit} ;

Square brackets [] mean optional. May appear once or not at all.

Repetition (zero or more)

1identifier = letter, {letter | digit} ;

Curly braces {} mean repeat zero or more times.

Grouping

1expression = term, {("+" | "-"), term} ;

Parentheses () group things together.

One or More

Some EBNF variants add:

1digits = digit, {digit} ;

Or explicit “one or more”:

1digits = digit+ ;  (* In some notations *)

Range

1digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ;
2(* Or in some dialects: *)
3digit = "0".."9" ;

Comments

1(* This is a comment *)

Complete Example: JSON Grammar

 1ebnf(* JSON Grammar in EBNF *)
 2
 3json = value ;
 4
 5value = object
 6      | array
 7      | string
 8      | number
 9      | "true"
10      | "false"
11      | "null" ;
12
13object = "{", [members], "}" ;
14members = pair, {",", pair} ;
15pair = string, ":", value ;
16
17array = "[", [elements], "]" ;
18elements = value, {",", value} ;
19
20string = '"', {character}, '"' ;
21character = letter | digit | " " | "!" (* simplified *) ;
22
23number = ["-"], digits, [".", digits] ;
24digits = digit, {digit} ;
25digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ;
26letter = "a" | "b" (* ... simplified ... *) | "z" ;

Breakdown:

Complete Example: Arithmetic Grammar

1factor = number | "(", expr, ")" ;
2term = factor, { ( "*" | "/" ), factor } ;
3expr = term, { ( "+" | "-" ), term } ;
4
5number = digit, { digit }, [ ".", digit, { digit } ] ;
6digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ;

Table of Symbols

UsageNotationAlternativeMeaning
definition=
concatenation,
termination;.
alternation/ or !
optional[ … ](/ … /)none or once
repetition{ … }(: … :)none or more
grouping( … )
terminal string" … "’ … '
comment(* … *)
special sequence? … ?
exception-

#computer-science #coding #parsing #bnf