EBNF

Introduction

EBNF is an acronym for Extended Backus-Naur Form and can be used to describe a computer or other formal language. Backus-Naur Form (BNF) is a notation that describes the syntax of high-level languages. It is used to formally define the grammar of a language. A parser can be mechanically constructed for it. BNF is like a mathematical game: start with a symbol and rules are then given for what this symbol can be replaced with.

Grammar

A grammar has four important parts:

  • the set of tokens
  • the set of non-terminal symbols
  • the set of productions
  • a particular non-terminal symbol called the start symbol

The four parts in our English language grammar are:

start symbol

production

non-terminal symbols

<S> ::= <NP> <V> <NP> <NP> ::= <A> <N> <V> ::= loves | hates | eats <N> ::= dog | cat | rat <A> ::= a | the

Form

EBNF syntax is a sequence of EBNF rules.

Example

The following shows an example of EBNF syntax.

<RULES> ::= (<RULE>)* <RULE> ::= <SELECTOR> { (<DECLARATION>)+ } <SELECTOR> ::= <CLASS> | (<TAG>)+ <CLASS> ::= .<NAME> <ID> ::= #<NAME> <TAG> ::= <NAME> <DECLARATION> ::= <PROPERTY>: <VALUE>; <PROPERTY> ::= <NAME> <VALUE> ::= (<LETTER> | <DIGIT> | -)+ <NAME> ::= <LETTER> (<LETTER> | <DIGIT> | -)* <SINGLEQUOTEDLITERAL> ::= ' (<LETTER> | <DIGIT>)* ' <DOUBLEQUOTEDLITERAL> ::= " (<LETTER> | <DIGIT>)* "