Context Free Grammar (CFG) for Chemistry

In this article, we have presented the Context Free Grammar for Chemical Formula like MnO4- in Chemistry. This will give us a strong idea of applying ideas of Context Free Grammar in Theory of Computation in other fields like Chemistry.

Prerequisite: Context Free Grammar

Table of contents:

  1. Context Free Grammar for Chemistry
  2. Example of using this CFG

Let us get started.

Context Free Grammar for Chemistry

The Context Free Grammar for Chemistry is as follows:

G = (V, Σ, R, S) where:

  • V is the set of variables.

V = {Form, Cmp, Ion, Elem, IonNum, Num}

You can make meaning of the variables as follows:

  • Form means Formulation and is the starting variable.

  • Cmp means Compound

  • Ion means Ions can it can be a single charge like +, - or multiple charge.

  • Num means a constant number that can be 1 or IonNum

  • IonNum means the Ionization Numbers. All integers from 2 and onwards should be added.

  • Elem means elements like Hydrogen (H), Helium (He) and others. All elements in the Periodic table needs to be added as a rule in this.

  • Term is a terminal variable and results in a Compound or Element

  • Σ is the set of terminals.

Σ = {All elements from Periodic table, All integers starting from 1, +, -}

Σ = {H, He, Li, Be, B, C, Mg, O, ..., 1, 2, 3, 4, ..., +, -}

  • S is the starting variable. S = Form.

  • R is the set of rules.

The Rules are:

Form -> Cmp | Cmp Ion
Cmp -> Term | Term Num | Cmp Cmp
Term -> Elem | (Cmp)
Elem -> H | He | Li | Be | B | C | Mg | ...
Ion -> + | - | IonNum + | IonNum -
IonNum -> 2 | 3 | 4 | ...
Num -> 1 | IonNum

Chemical Formulae that can be formed using the above rules are:

  • Cu3(CO3)2(OH)2
  • MnO4
  • H2O
  • S2-

Example of using this CFG

Let us use the above Rules of our Context Free Grammar G for Chemistry to form the formula MnO4-:

Form
=> Cmp Ion
=> Cmp Cmp Ion
=> Cmp Term Num Ion
=> Term Term Num Ion
=> Elem Term Num Ion
=> Mn Term Num Ion
=> Mn Elem Num Ion
=> MnO Num Ion
=> MnO Ion Num Ion
=> MnO4 Ion
=> MnO4-

With this article at OpenGenus, you must have the complete idea of forming a Context Free Grammar for Chemical Formula in Chemistry.