There are parsers available in various programming languages for breaking chemical formulas into elements and their proportions. Here I do the job with AWK and calculate the formula’s mass per mole.
Source: LXer – Building a molar mass calculator