BNF

  • BNF (Backus-Naur Form)
  • A formal mathematical way to describe the syntax of a language

Syntax

Production rules

  • syntactic_category ::= definition   (production rule)
  • syntactic_category := definition   (alternate syntax for production rules)

Terminals

  • "word"  : Literal words (terminals) (i.e. "if")

Symbols

  • word  : Syntactic categories (nonterminals / symbols) (i.e. if_statement) (not in double quotes)

Number of occurrences

  • [expr]  : Optional
  • {expr}  : Zero or more times
  • expr?  : Optional
  • expr*  : Zero or more times
  • expr+  : One or more times

Logical operators

  • expr | expr  : Alternatives
  • (expr)  : One or more items logically treated as a single item

Misc

  • @  : Nothing (a placeholder that can be removed outright)
  • double_quote   : Used to represent a double-quote character

Examples

if_statement ::=
"if" condition "then"
sequence_of_statements
{"elsif" condition "then"
sequence_of_statements}
["else"
sequence_of_statements]
"end if;"

sequence_of_statements ::= statement { statement }

statement ::= { label } ( simple_statement | compound_statement )

compound_statement ::=
if_statement
| case_statement
| loop_statement


S := '-' FN | FN
FN := DL FP
FP := @ | '.' DL
DL := D DR
DR := D DR | @
D := '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'



Sources
  • http://www.dwheeler.com/lovelace/bnf.htm
  • http://www.garshol.priv.no/download/text/bnf.html

Parent URL: 
category/programming