Understanding BNF Notation and Its Application to Python Syntax
This article explains the Backus–Naur Form (BNF) notation, its core components, and how to use it to describe Python grammar, providing concrete examples such as full‑name definitions, identifiers, and control‑flow statements, along with best‑practice tips for reading Python BNF rules.
Understanding BNF Notation and Its Application to Python Syntax
When reading Python documentation you may encounter the Backus–Naur Form (BNF) notation, a meta‑syntax for describing context‑free grammars. This article introduces BNF, its three core parts—terminals, nonterminals, and rules—and shows how to combine them to define language syntax.
Core Components of BNF
Terminals : literal strings that must match the input exactly, e.g. "def" , ":" .
Nonterminals : syntactic variables that are replaced by other rules, e.g. <letter> , <digit> .
Rules : definitions that relate terminals and nonterminals, e.g. <letter> ::= "a" .
By combining terminals and nonterminals you can construct BNF rules that form a complete grammar.
BNF Rule Format
<code><symbol> ::= expression</code>Where <symbol> is a nonterminal, ::= means “is defined as”, and expression consists of terminals, nonterminals, and other symbols.
Common Symbol Table
Symbol
Meaning
""Encloses a terminal string
<>Denotes a nonterminal
()Groups a set of alternatives
+One or more repetitions of the preceding element
*Zero or more repetitions
?Zero or one occurrence (optional)
|Choice between alternatives
[x‑z]Character or digit range
Example: Full‑Name Grammar
A full name consists of a first name, an optional middle name, and a family name, separated by spaces.
<code><full_name> ::= <first_name> " " (<middle_name> " ")? <family_name></code>Supporting rules define uppercase and lowercase letters:
<code><uppercase_letter> ::= [A-Z]
<lowercase_letter> ::= [a-z]</code>And the name components themselves:
<code><first_name> ::= <uppercase_letter> <lowercase_letter>*</code>Identifiers in Python
Identifiers name variables, functions, classes, etc. The BNF rule is:
<code><identifier> ::= <char> (<char> | <digit>)*</code>With supporting definitions:
<code><char> ::= [A‑Z] | [a‑z] | "_"
<digit> ::= [0‑9]</code>Python‑Specific BNF Variants
Python’s grammar uses a slightly different notation: nonterminals are written without angle brackets, optional parts use [] , and repetitions use * or + . For example, the pass statement:
<code>pass_stmt ::= "pass"</code>And the return statement with an optional expression list:
<code>return_stmt ::= "return" [expression_list]</code>Assignment Expressions (Walrus Operator)
<code>assignment_expression ::= [identifier ":="] expression</code>Example usage:
<code>>>> (length := len([1, 2, 3]))
3
>>> length
3</code>Control‑Flow Statements
BNF for if statements (including elif and else ):
<code>if_stmt ::= "if" assignment_expression ":" suite ("elif" assignment_expression ":" suite)* ["else" ":" suite]</code>BNF for for loops:
<code>for_stmt ::= "for" target_list "in" starred_list ":" suite ["else" ":" suite]</code>BNF for while loops:
<code>while_stmt ::= "while" assignment_expression ":" suite ["else" ":" suite]</code>Best Practices for Reading Python BNF
Familiarize yourself with BNF notation and test rules on the BNF Playground website.
Understand Python’s specific BNF variant.
Break complex rules into smaller parts.
Identify and explore nonterminals that require further definition.
Recognize terminals (keywords, operators, literals) and note they are quoted.
Compare BNF rules with actual Python code examples to see how they apply.
Following these steps helps you decode Python’s grammar and write correct code that conforms to the language specification.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.