title: Improve BNF for mathematic expressions
body: <p>A good exercise while learning programming, is to write a calculator. To do this, I created some kind of <a href="http://en.wikipedia.org/wiki/Domain-specific_language" rel="nofollow">DSL</a> in <a href="http://en.wikipedia.org/wiki/Backus%E2%80%93Naur_Form" rel="nofollow">BNF</a> and want to ask for your help to improve it. With this minilanguage you should be able to <code>add</code>, <code>multiply</code> and <code>assign</code> values and expressions to names (a.k.a. create variables and functions).</p>
  
  <p>Hava a look at the BNF first:</p>
  
  <pre><code>&lt;Program&gt;     ::= &lt;Line&gt;(&lt;NewLine&gt;&lt;Line&gt;)*
  &lt;Line&gt;        ::= {"("}&lt;Expression&gt;{")"}|&lt;Assignment&gt;
  &lt;Assignment&gt;  ::= &lt;Identifier&gt;"="&lt;Expression&gt;
  &lt;Identifier&gt;  ::= &lt;Name&gt;{"("&lt;Name&gt;(","&lt;Name&gt;)*")"}
  &lt;Expression&gt;  ::= &lt;Summand&gt;(("+"|"-")&lt;Summand&gt;)*
  &lt;Summand&gt;     ::= &lt;Factor&gt;(("*"|"/")&lt;Factor&gt;)*
  &lt;Factor&gt;      ::= &lt;Number&gt;|&lt;Call&gt;
  &lt;Call&gt;        ::= &lt;Name&gt; {"("&lt;Expression&gt;(","&lt;Expression&gt;)*")"}
  &lt;Name&gt;        ::= &lt;Letter&gt;(&lt;Letter&gt;|&lt;Digit&gt;)*
  &lt;Number&gt;      ::= {"+"|"-"}(&lt;Digit&gt;|&lt;DigitNoZero&gt;&lt;Digit&gt;+)
  &lt;Digit&gt;       ::= "0"|&lt;DigitNoZero&gt;
  &lt;DigitNoZero&gt; ::= "1"|"2"|"3"|"4"|"5"|"6"|"7"|"8"|"9"
  &lt;Letter&gt;      ::= [a-zA-Z]
  &lt;NewLine&gt;     ::= "\n"|"\r"|"\r\n"
  </code></pre>
  
  <p>As you can see, this BNF treats no Whitespace beside <code>NewLine</code>. Before the parsing begins I plan to remove all whitespace (beside <code>NewLine</code> of course) from the string to parse. It is not nessesary for the parser anyway.</p>
  
  <p>There are 4 things, that might lead to <strong>problems</strong> when using this language as defined right now and I hope you can help me figure out appropriate solutions:</p>
  
  <ol>
  <li>I tried to follow the top-down approach, while generating this gramar, but there is a circle between <code>&lt;Expression&gt;</code>, <code>&lt;Summand&gt;</code>, <code>&lt;Factor&gt;</code> and <code>&lt;Call&gt;</code>.</li>
  <li>The gramar treats variables and functions exactly the same way. Most programming languages make a difference. <strong>Is it nessesary to differentiate here?</strong></li>
  <li>There are maybe some things that I don't know about programming, BNF, whatever, that will kill me later, while trying to implement the BNF. But you might be able to spot that before I start.</li>
  <li>There might be simple and stupid mistakes that I could not find myself. Sorry in that case. I hope there are none of these mistakes anymore.</li>
  </ol>
  
  <p>Using hand and brain I could successfully parse the following test cases:</p>
  
  <pre><code>"3"
  "-3"
  "3-3"
  "a=3"
  "a=3+b"
  "a=3+b\nc=a+3"
  "a(b,c)=b*c\ra(1+2,2*3)"
  </code></pre>
  
  <p>Please help to improve the BNF, that it can be used to successfully write a calculator.</p>
  
  <p><strong>edit:</strong>
  This BNF is really not finished. It does not treat the cases "2+-3" (should fail, but doesn't) and "2+(-3)" (should not fail, but does) correctly.</p>