Page cover

πŸŒ‚Formal Grammar Description (BNF)

Backus-Naur Form (BNF) is a formal notation used to describe the syntax of programming languages

Grammar Notation

Example of BNF

Below is an example of a simple BNF grammar defining a basic arithmetic expression:

<expression> ::= <term> | <term> "+" <expression>
<term> ::= <factor> | <factor> "*" <term>
<factor> ::= <number> | "(" <expression> ")"
<number> ::= "0" | "1" | "2" | ... | "9"

This grammar describes expressions involving addition and multiplication of single-digit numbers, including the use of parentheses for grouping.

  • <...>: Non-terminal symbols to be expanded

  • ::=: Defined as

  • |: Or (alternative choices)

  • *: Zero or more occurrences

  • +: One or more occurrences

  • ?: Optional (zero or one occurrence)

  • Literals: Enclosed in quotes (e.g., "+" or "(")

  • Grouping: Square brackets [...]

  • Comments: Indicated by

Maryl's grammar description (BNF)

Last updated