Fundamentals 10 min read

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.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Understanding BNF Notation and Its Application to Python Syntax

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>&lt;symbol&gt; ::= expression</code>

Where &lt;symbol&gt; 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

&lt;&gt;

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>&lt;full_name&gt; ::= &lt;first_name&gt; " " (&lt;middle_name&gt; " ")? &lt;family_name&gt;</code>

Supporting rules define uppercase and lowercase letters:

<code>&lt;uppercase_letter&gt; ::= [A-Z]
&lt;lowercase_letter&gt; ::= [a-z]</code>

And the name components themselves:

<code>&lt;first_name&gt; ::= &lt;uppercase_letter&gt; &lt;lowercase_letter&gt;*</code>

Identifiers in Python

Identifiers name variables, functions, classes, etc. The BNF rule is:

<code>&lt;identifier&gt; ::= &lt;char&gt; (&lt;char&gt; | &lt;digit&gt;)*</code>

With supporting definitions:

<code>&lt;char&gt; ::= [A‑Z] | [a‑z] | "_"
&lt;digit&gt; ::= [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>&gt;&gt;&gt; (length := len([1, 2, 3]))
3
&gt;&gt;&gt; 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.

PythonParsingprogramming languageBNFSyntaxGrammar
Python Programming Learning Circle
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.