title: Make outer tokens change inner tokens in a chemical formula using pyparsing
body: <p>I'm trying to use pyparsing to parse a chemical formula that may be nested and with non-integer stoichiometries using pyparsing. What I want is a list of each element present in the formula and its correponding total stoichiometry.</p>
  
  <p>I have used the example on the pyparsing wiki as a start, and looked at fourFn.py for more ideas. I'm having trouble understanding how to use all the features in the package.</p>
  
  <p>I came up with the following grammar:</p>
  
  <pre><code>from pyparsing import Word, Group, ZeroOrMore, Combine,\
       Optional, OneOrMore, ParseException, Literal, nums,\
       Suppress, Dict, Forward
  
  caps = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  lowers = caps.lower()
  digits = "0123456789"
  integer = Word( digits )
  parl = Literal("(").suppress()
  parr = Literal(")").suppress()
  
  element = Word( caps, lowers )
  separator = Literal( "," ).setParseAction(lambda s,l,t: t[0].replace(',','.')) | Literal( "." )
  
  nreal = (Combine( integer + Optional( separator +\
      Optional( integer ) ))\
      | Combine( separator + integer )).setParseAction( lambda s,l,t: [ float(t[0]) ] )
  
  block = Forward()
  groupElem = Group( element + Optional( nreal, default=1)) ^ \
       Group( parl + block + parr + Optional( nreal,default=1 ) )
  block &lt;&lt; groupElem + ZeroOrMore( groupElem )
  formula = OneOrMore( block )
  </code></pre>
  
  <p>Non-nested formulas work as expected:</p>
  
  <pre><code>&gt;&gt;&gt; formula.parseString('H2O')
  ([(['H', 2.0], {}), (['O', 1], {})], {})
  </code></pre>
  
  <p>Despite having those empty fields (which I could not find the use), I can extract the information I want.</p>
  
  <p>But when I try something like:</p>
  
  <pre><code>&gt;&gt;&gt; formula.parseString('C6H8(OH)4')
  ([(['C', 6.0], {}), (['H', 8.0], {}), ([(['O', 1], {}), (['H', 1], {}), 4.0], {})], {})
  </code></pre>
  
  <p>I can see the formula is correctly parsed, but I would like the outher '4' in (OH)4 to multiply the inner numbers. But I can't see how to do it.</p>
  
  <p>How can one token change the value of another one?</p>
  
  <p>Or how can I walk these results and make a function that, if a block has an outer number attached to it, I can compute the total number of each element inside the block? </p>
  
  <p>Thanks in advance.</p>
  
  <p>edit1: I believe I need something like: suppress the outer nreal on occurrences of "( block )nreal", and multiply all occurrences of nreal by the outer value...</p>