title: ANTLR correct grammar for if statement
body: <p>I have created a simple grammar for a language I have decided to call SWL:</p>
  
  <pre><code>grammar swl;
  
  program   : 'begin' statement+ 'end';
  
  statement : assign | add | sub | mul | div | print | whilecond | ifcond ;
  condition : expr ;
  expr      : '(' expr ')' | expr 'and' expr | expr 'or' expr | 'not' expr | expr '=' expr | expr '&gt;=' expr | expr '&lt;=' expr | expr '&gt;' expr | expr '&lt;' expr | ID | NUMBER;
  
  assign    : 'let' ID 'be' (NUMBER | ID) ;
  print     : 'print' (NUMBER | ID) ;
  
  add       : 'add' (NUMBER | ID) 'to' ID ;
  sub       : 'sub' (NUMBER | ID) 'to' ID ;
  mul       : 'mul' (NUMBER | ID) 'to' ID ;
  div       : 'div' (NUMBER | ID) 'to' ID ;
  
  whilecond : 'while (' condition ') do' statement+ 'stop' ;
  
  ifcond    : 'if (' condition ') then' statement+ 'stop' | 'if (' condition ') then' statement+ 'else' statement+ 'stop' ;
  
  ID        : [a-z]+ ;
  NUMBER    : [0-9]+ ;
  WS        : [ \n\t]+ -&gt; skip;
  ErrorChar : . ;
  </code></pre>
  
  <p>I am a bit in trouble with the <code>ifcond</code>. Given this SWL program:</p>
  
  <pre><code>begin
      if (not(a &gt; 1) or (c &lt;= b)) then
       mul 5 to k
      stop
  end
  </code></pre>
  
  <p>I am able to get this C++ output:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  int main() {
      if ( !(a&gt;1) || (c&lt;=b)) {
      k *= 5;
      }
  }
  </code></pre>
  
  <p>This is great! By the way with the following input:</p>
  
  <pre><code>begin
      if (not(a &gt; 1) or (c &lt;= b)) then
       mul 5 to k
      else
       mul 15 to k
      stop
  end
  </code></pre>
  
  <p>I have this output:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;iostream&gt;
  
  using namespace std;
  
  int main() {
      if ( !(a&gt;1) || (c&lt;=b)) {
      k *= 5;
      k *= 15;
      }
  }
  </code></pre>
  
  <p>As you can see the second example is missing the else <code>statement</code> part. What is wrong? Do I have to change the <code>ifcond</code> grammar and add another rule/variable? This is the <code>MyListner.cpp</code> file.</p>
  
  <pre class="lang-cpp prettyprint-override"><code>void fixString(string&amp; cond) {
  
      size_t pos = cond.find("and", 0);
      if (pos != string::npos) { cond.replace(pos, 3, " &amp;&amp; "); }
  
      pos = cond.find("or", 0);
      if (pos != string::npos) { cond.replace(pos, 2, " || "); }
  
      pos = cond.find("not", 0);
      if (pos != string::npos) { cond.replace(pos, 3, " !"); }
  
  }
  
  void MyListener::enterIfcond(swlParser::IfcondContext *ctx) {
      string cond = ctx-&gt;condition()-&gt;getText();
      fixString(cond);
  
      cout &lt;&lt; string(indent, ' ') &lt;&lt; "if (" &lt;&lt; cond &lt;&lt; ") {" &lt;&lt; endl;
  }
  
  void MyListener::exitIfcond(swlParser::IfcondContext *ctx) {
      cout &lt;&lt; string(indent, ' ') &lt;&lt; "}" &lt;&lt; endl;
  }
  </code></pre>
  
  <p>My suspect is that the grammar is not good enough and I'd need another variable to call the else part. I don't know how to fix this issue, any idea?</p>