BNF Notation: Dive Deeper Into Python's Grammar :

BNF Notation: Dive Deeper Into Python's Grammar
by:
blow post content copied from  Real Python
click here to view original post


While reading the Python documentation, you may have found fragments of BNF notation (Backus–Naur form) that look something like the following:

BNF Grammar
name      ::= lc_letter (lc_letter | "_")*
lc_letter ::= "a"..."z"

What’s the meaning of all this strange code? How can this help you in understanding Python concepts? How can you read and interpret this notation?

In this tutorial, you’ll get to know the basics of Python’s BNF notation and learn how to take advantage of it to get a deep understanding of the language’s syntax and grammar.

In this tutorial, you’ll:

  • Learn what BNF notation is and what it’s used for
  • Explore the characteristics of Python’s BNF variation
  • Learn how to read the BNF notation in the Python documentation
  • Explore some best practices for reading Python’s BNF notation

To get the most out of this tutorial, you should be familiar with Python syntax, including keywords, operators, and some common constructs like expressions, conditional statements, and loops.

Getting to Know Backus-Naur Form Notation (BNF)

The Backus–Naur form or Backus normal form (BNF) is a metasyntax notation for context-free grammars. Computer scientists often use this notation to describe the syntax of programming languages because it allows them to write a detailed description of a language’s grammar.

The BNF notation consists of three core pieces:

Component Description Examples
Terminals Strings that must exactly match specific items in the input. "def", "return", ":"
Nonterminals Symbols that will be replaced by a concrete value. They may also be called simply syntactic variables. <letter>, <digit>
Rules Conventions of terminals and nonterminals that define how these elements relate. <letter> ::= "a"

By combining terminals and nonterminals, you can create BNF rules, which can get as detailed as you need. Nonterminals must have their own defining rules. In a piece of grammar, you’ll have a root rule and potentially many secondary rules that define the required nonterminals. This way, you may end up with a hierarchy of rules.

BNF rules are the core components of a BNF grammar. So, a grammar is a set of BNF rules that are also called production rules.

In practice, you can build a set of BNF rules to specify the grammar of a language. Here, language refers to a set of strings that are valid according to the rules defined in the corresponding grammar. BNF is mainly used for programming languages.

For example, the Python syntax has a grammar that’s defined as a set of BNF rules, and these rules are used to validate the syntax of any piece of Python code. If the code doesn’t fulfill the rules, then you’ll get a SyntaxError.

You’ll find many variations of the original BNF notation out there. Some of the most relevant include the extended Backus–Naur form (EBNF) and augmented Backus–Naur form (ABNF).

In the following sections, you’ll learn the basics of creating BNF rules. Note that you’ll use a variation of BNF that matches the requirements of the BNF Playground site, which you’ll use for testing your rules.

BNF Rules and Their Components

As you already learned, by combining terminals and nonterminals, you can create BNF rules. These rules typically follow the syntax below:

BNF Grammar
<symbol> ::= expression

In the BNF rule syntax, you have the following parts:

  • <symbol> is a nonterminal variable, which is often enclosed in angle brackets (<>).
  • ::= means that the nonterminal on the left will be replaced with the expression on the right.
  • expression consists of a series of terminals, nonterminals, and other symbols that define a specific piece of grammar.

When building BNF rules, you can use a variety of symbols with specific meanings. For example, if you’re going to use the BNF Playground site to compile and test your rules, then you’ll find yourself using some of the following symbols:

Symbol Meaning
"" Encloses a terminal symbol
<> Indicates a nonterminal symbol
() Indicates a group of valid options
+ Specifies one or more of the previous element
* Specifies zero or more of the previous element
? Specifies zero or one occurrence of the previous element
| Indicates that you can select one of the options
[x-z] Indicates letter or digit intervals

Read the full article at https://realpython.com/python-bnf-notation/ »


[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]


February 14, 2024 at 07:30PM
Click here for more details...

=============================
The original post is available in Real Python by
this post has been published as it is through automation. Automation script brings all the top bloggers post under a single umbrella.
The purpose of this blog, Follow the top Salesforce bloggers and collect all blogs in a single place through automation.
============================

Salesforce