def flattened(self):
stack = [(self, <NUM_LIT:1>)]<EOL>result = Counter()<EOL>while len(stack) > <NUM_LIT:0>:<EOL><INDENT>var, value = stack.pop()<EOL>if isinstance(var, Formula):<EOL><INDENT>for sub_var, sub_value in iteritems(var._values):<EOL><INDENT>stack.append((sub_var, value*sub_value))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>result[var] += value<EOL><DEDENT><DEDENT>return Formula(result)<EOL>
Return formula where subformulas have been flattened

        >>> str(Formula.parse('(CH2)(CH2)2').flattened())
        'C3H6'