Send Close Add comments: (status displays here)
Got it!  This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
EBNF to BNF


1. EBNF to BNF

2. EBNF to BNF
BNF (Backus Naur Form) is a simplified grammar that is easier to work with when automatically processing grammars using various algorithms. Note that BNF grammars were developed first, then, for human simplicity and use, EBNF grammars were created.

3. BNF grammar form
A BNF grammar has productions, variables, and terminal symbols but no meta-character notation.

An EBNF grammar is easily converted (manually or automatically) to BNF form. This is now covered.

A bottom up analysis (more difficult manually or automatically) can convert a BNF grammar to an EBNF grammar. In doing so, some human intervention can help in obtaining meaningful names for certain productions.

4. Production introduction
In each case, a EBNF part is given a new and unique name for the BNF production. The new production name X is used. The three dots "..." indicate other parts of a grammar.

The transformation is done recursively for each part of a EBNF production.

The process is continued until all EBNF constructs have been converted.

5. Conditional
The following is EBNF for conditional.
P = ... [ E ] ...

Convert this to the following BNF.
P = ... X ... X = . X = E .


6. Alternatives
The following is EBNF for alternatives
P = E1 | E2 | ... | En

Convert this to the following BNF.
P = X . X = E1 . X = E2 . ... X = En .


7. Sequence
The following is EBNF for sequence.
P = E1 E2 ... En .

Convert this to the following BNF.
P = E1 E2 ... En .


8. Grouping
The following is EBNF for grouping.
P = ... ( E ) ...

Convert this to the following BNF.
P = ... X ... X = E .

Note that the parentheses disappear.

9. Repetition
The following is EBNF for repetition.
P = ... { E } ...

Convert this to the following BNF (right recursive).
P = ... X ... X = . X = E X .


10. Recursive forms
The following recursive BNF forms are equivalent.

Right recursive: (LL and LR parsers):
X = . X = E X .

Left recursive: (LL parsers, not LR parsers):
X = . X = X E .


11. Numbers
Consider a grammar for a number.

Here is the associated EBNF (Extended Backus Naur Form) grammar.
Number = "digit" { "digit" } .

Here is the associated EBNF syntax diagram(s).

Syntax diagram for EBNF
Note: The generated diagram shows an improvement from the straight-forward conversion. Here is the associated EBNF grammar.
Number = "digit" { "digit" } .

Here is an equivalent BNF grammar.
Number = "digit" NumberMore . NumberMore = "digit" NumberMore . NumberMore = .

Here is the above syntax diagram and EBNF converted to BNF using "X".

Here is the associated EBNF syntax diagram(s).

Syntax diagram for EBNF
Here is the associated EBNF grammar.
Number = "digit" { "digit" } .

Here is the associated BNF grammar (without production compression) as follows.
Number = "digit" X1 .    X1 = .    X1 = "digit" X1 .


12. Identifiers
Consider a grammar for an identifier.

Here is the associated EBNF grammar.
Identifier = "letter" { LettorOrDigit } . LetterOrDigit = "letter" | "digit" .

Here is the associated EBNF syntax diagram(s).

Syntax diagram for EBNFHere is the associated EBNF grammar.
Identifier = "letter" { LettorOrDigit } . LetterOrDigit = "letter" | "digit" .

Here is an equivalent BNF grammar.
Identifier = "letter" IdentifierMore . IdentifierMore = LetterOrDigit IdentifierMore . IdentifierMore = . LettorOrDigit = "letter" . LettorOrDigit = "digit" .

Here is the above syntax diagram and EBNF converted to BNF using "X".

Here is the associated EBNF syntax diagram(s).

Syntax diagram for EBNF
Here is the associated EBNF grammar.
Identifier = "letter" { LettorOrDigit } . LetterOrDigit = "letter" | "digit" .

Here is the associated BNF grammar (without production compression) as follows.
Identifier = "letter" X1 .    X1 = .    X1 = LettorOrDigit X1 . LetterOrDigit = X2 .    X2 = "letter" .    X2 = "digit" .


13. EBNF in EBNF to BNF
Here is EBNF in EBNF

Here is the associated EBNF syntax diagram(s).

Syntax diagram for EBNFHere is the associated EBNF grammar.
Syntax = { Production } . Production = "Variable" "=" Expression "." . Expression = Term { "|" Term } . Term = Factor { Factor } . Factor = "Terminal" | "Variable" | "[" Expression "]" | "{" Expression "}" | "(" Expression ")" .


14. EBNF to BNF
Let us take the conversion of EBNF in EBNF to BNF in parts.

15. Syntax
Here is the associated EBNF syntax diagram(s).

Syntax diagram for EBNF
Here is the associated EBNF grammar.
Syntax = { Production } .

Here is the associated BNF grammar (without production compression) as follows.
Syntax = X1 .    X1 = .    X1 = Production X1 .

With production compression (and renaming), the following is an equivalent BNF grammar.
Syntax = . Syntax = Production Syntax .


16. Production
Here is the associated EBNF syntax diagram(s).

Syntax diagram for EBNF
Here is the associated EBNF grammar.
Production = "Variable" "=" Expression "." .

Here is the associated BNF grammar (without production compression) as follows.
Production = "Variable" "=" Expression "." .

Notice that with a sequence (without meta-characters), there is no change

17. Expression
Here is the associated EBNF syntax diagram(s).

Syntax diagram for EBNF
Here is the associated EBNF grammar.
Expression = Term { "|" Term } .

Here is the associated BNF grammar (without production compression) as follows.
Expression = Term X1 .    X1 = .    X1 = "|" Term X1 .

With production compression (and renaming), the following is an equivalent BNF grammar.
Expression = Term TermLoop . TermLoop = . TermLoop = "|" Term TermLoop


18. Term
Here is the associated EBNF syntax diagram(s).

Syntax diagram for EBNF
Here is the associated EBNF grammar.
Term = Factor { Factor } .

Here is the associated BNF grammar (without production compression) as follows.
Term = Factor X1 .    X1 = .    X1 = Factor X1 .

With production compression (and renaming), the following is an equivalent BNF grammar.
Term = Factor FactorLoop . FactorLoop = . FactorLoop = Factor FactorLoop .

The following improvement can be made (in line with the above syntax diagram), for LR parsers or LL parsers (recursive descent) that take into consideration the common prefix.
Term = Factor . Term = Factor Term .


19. Factor
Here is the associated EBNF syntax diagram(s).

Syntax diagram for EBNF
Here is the associated EBNF grammar.
Factor = "Terminal" | "Variable" | "[" Expression "]" | "{" Expression "}" | "(" Expression ")" .

Here is the associated BNF grammar (without production compression) as follows.
Factor = X1 .    X1 = "Terminal" .    X1 = "Variable" .    X1 = "[" Expression "]" .    X1 = "{" Expression "}" .    X1 = "(" Expression ")" .

With production compression (and renaming), the following is an equivalent BNF grammar.
Factor = "Terminal" . Factor = "Variable" . Factor = "[" Expression "]" . Factor = "{" Expression "}" Factor = "(" Expression ")" .


20. Chomsky Normal Form
Some parsing algorithms are easier to specify and show correct if the grammar is in CNF (Chomsky Normal Form) , developed by and named after the famous linguist Noam Chomsky. In a CNF grammar, each production needs to contain at most two parts of the following form.
A = B C . A = "c" .

A simple form results in a lot of simple productions - difficult for humans but easy for algorithm analysis and implementation involving parsing and grammars.

21. End of page

22. Acronyms and/or initialisms for this page