title: math syntax checker written in python
body: <p>All I need is to check, using python, if a string is a valid math expression or not.</p>
  
  <p>For simplicity let's say I just need <code>+ - * /</code> operators (<code>+ -</code> as unary too) with numbers and nested parenthesis. I add also simple variable names for completeness.</p>
  
  <p>So I can test this way:</p>
  
  <pre><code>test("-3 * (2 + 1)") #valid
  test("-3 * ")        #NOT valid
  
  test("v1 + v2")      #valid
  test("v2 - 2v")      #NOT valid ("2v" not a valid variable name)
  </code></pre>
  
  <p>I tried <a href="http://pyparsing.wikispaces.com/" rel="noreferrer">pyparsing</a> but just trying the example: <a href="http://pyparsing.wikispaces.com/file/view/fourFn.py" rel="noreferrer">"simple algebraic expression parser, that performs <code>+,-,*,/</code> and <code>^</code> arithmetic operations"</a> I get passed invalid code and also trying to fix it I always get wrong syntaxes being parsed without raising Exceptions</p>
  
  <p><strong>just try</strong>:</p>
  
  <pre><code>&gt;&gt;&gt;test('9', 9)
  9 qwerty = 9.0 ['9'] =&gt; ['9']
  &gt;&gt;&gt;test('9 qwerty', 9)
  9 qwerty = 9.0 ['9'] =&gt; ['9']
  </code></pre>
  
  <p>both test pass... o_O</p>
  
  <p>Any advice?</p>