title: Unambiguous grammar for arithmetic expression with Unary + and -
body: <p>I have just started self-studying the Dragon book of Compiler Design. I am working on a problem that says to design grammar for an expression containing binary +,-,*,/ and unary +,-</p>
  
  <p>I came up with following</p>
  
  <pre><code>E -&gt; E+T | E-T | T
  T -&gt; T*P | T/P | P
  P -&gt; +S | -S | S
  S -&gt; id | constant | (E)
  </code></pre>
  
  <p>However, there is an obvious flaw in it. According to this grammar, expressions like</p>
  
  <pre><code>1--3
  </code></pre>
  
  <p>are valid, which is an error in all programming languages I know. Though, expressions like</p>
  
  <pre><code>1+-+3
  and
  1- -3
  </code></pre>
  
  <p>must be valid. How can such a grammar be designed? </p>