Programming languages have strict grammar rules โ just like English. BNF is how we write those rules down, and syntax diagrams (railroad diagrams) show them visually.
BNF (Backus-Naur Form) is a way of writing the grammar rules of a programming language. Just like English has rules ("a sentence needs a subject and a verb"), programming languages have rules about what code looks like.
<name> ::= option1 | option2 ยท Terminals in "quotes" ยท Non-terminals in <angle brackets>
| Symbol | Meaning | Example |
|---|---|---|
| ::= | "is defined as" | <digit> ::= "0" | "1" |
| | | "or" โ choose one option | "a" | "b" | "c" |
| < > | Non-terminal โ a rule name (gets expanded) | <expression> |
| " " | Terminal โ an actual character/word (stays as-is) | "hello" |
| <empty> | Nothing โ the rule can produce an empty string | <list> ::= <item> <list> | <empty> |