title: Writing a recursive descent parser with nested if-conditions
body: <p>I am writing a recursive descent parser for parsing a specific grammar, and I have to do the nested if/else in that grammar.</p>
  
  <p>The grammar is as follows:  </p>
  
  <pre><code>&lt;statement&gt; ::= if &lt;cond&gt; &lt;statement&gt; 
  </code></pre>
  
  <p>In the <code>parseStmt</code> we have to do the following: basically the <code>getToken()</code> method will remove blanks and grab the token.</p>
  
  <pre class="lang-java prettyprint-override"><code>// if &lt;cond&gt; &lt;statement&gt;
  else if (token.equals("if")) {
      token = getToken();
      cond1 = (parseCond(token));
      cond = (cond1) &amp;&amp; (cond);
  
      token = getToken();
      execute = (cond) &amp;&amp; (execute);
      parseStmt(cond, token);
      token = getToken();
  
      if (token.equals("else")) {
          if (cond) {
              execute = false;
              token = getToken();
              parseStmt(cond, token);
          } else {
              execute = (cond) &amp;&amp; (execute);
              token = getToken();
              parseStmt(cond, token);
              cond = true;
              execute = true;
              token = getToken();
          }
      } else {
          line = token + line;
      }
  }
  </code></pre>