title: Tokenizing a parenthesized text
body: <p>I'm trying to tokenize a parenthesized input so I can make a tree. This is what I've written so far:</p>
  
  <pre><code>void parse(){
      int ch, i = 0;
      char temp[10*MAX] = {0};
      while ((ch = getchar()) != EOF){
      if(isspace(ch)){
          continue;
      }else if(ch == ')'){
          /* go one level up in the tree*/
      }else if(ch != '(' &amp;&amp; ch != ']'){
          temp[i++] = ch;
      }
      else{
          /*add child or sibling*/
          printf("%s\n", temp);
          parse();
          }
      }
  }
  </code></pre>
  
  <p>And this is the result of the code above</p>
  
  <pre><code>////////INPUT/////////////////////
  BIKE(2*WHEEL(RIM[60.0 ],
  2*AXLE,  ------------&gt;PROBLEMATIC
  SPOKE[120.],
  HUB(2*GEAR[25.],AXLE(5*BOLT[0.1], 7 * NUT[.15]))),
  FRAME(REARFRAME [175.00],
  1*FRONTFRAME (FORK[22.5] ,AXLE, 2 *HANDLE[10.])))--------&gt;PROBLEMATIC
  //////////////OUTPUT/////////////
  BIKE
  2*WHEEL
  RIM[60.0
  ,2*AXLE,SPOKE[120.---&gt;PROBLEMATIC
  ,HUB
  2*GEAR[25.
  ,AXLE
  5*BOLT[0.1
  ,7*NUT[.15
  ,FRAME
  REARFRAME[175.00
  ,1*FRONTFRAME
  FORK[22.5
  ,AXLE,2*HANDLE[10.---&gt;PROBLEMATIC
  </code></pre>
  
  <p>Comma at the zero index is important for me. It signs whether the current token is a child or a sibling for the token before that, so I need it. But there is one little problem since the input is not fully strict. I've marked the troublesome lines in the I/O.</p>
  
  <p>I should be getting one name for one token. In the output, there are two lines having two names, because of the the absence of parentheses in the input. To handle this, I thought I could write a function to check if <code>temp</code> has two commas, and then another function to parse if so. But of course, a better way of doing this would be in the <code>parse()</code> function. Is it possible to modify the code for these purposes?</p>
  
  <p>BNF notation for the input is the following.</p>
  
  <pre><code>&lt;part&gt;           ::=  &lt;composite part&gt; | &lt;basic part&gt;
  &lt;composite part&gt; ::=  &lt;name&gt; | &lt;name&gt; ( &lt;list of items&gt; )
  &lt;basic part&gt;     ::=  &lt;name&gt; | &lt;name&gt; [ &lt;price&gt; ]
  &lt;item&gt;           ::=  &lt;part&gt; | &lt;quantity&gt; * &lt;part&gt;
  &lt;list of items&gt;  ::=  &lt;item&gt; | &lt;item&gt;, &lt;list of items&gt;
  &lt;name&gt;           ::=  &lt;uppercase letter sequence of maximal length 20&gt;
  &lt;quantity&gt;       ::=  &lt;positive integer&gt;
  &lt;price&gt;          ::=  &lt;floating point&gt;
  </code></pre>
  
  <p>Also, are there any systematical steps to follow when the BNF notation is provided? I always want to come up with the most general solution but have been encountering these kind of problems a lot.</p>