title: Creating a markdown parser with ANTLR
body: <p>I am an ANTLR newbie. I am trying to implement a markdown to HTML translator.</p>
  
  <p>These are the grammar and the associated files that i coded.</p>
  
  <p><strong>Grammar</strong> </p>
  
  <pre><code>grammar markdown;
  
  prog    :   stat+;
  
  stat    :   main_heading
          |   sub_heading
          |   custom
          |   italic
          |   bold
          |   bolditalic
          |   text
          |   WS
          ;
  
  text    :   TEXT|SPACE;
  
  main_heading: text* WS '='+ WS+;
  
  sub_heading : WS? text* WS '-'+ WS+;
  
  custom      : '#'+ text* WS; //to support custom headings
  
  italic  :   ('*'stat*'*')|('_'stat*'_');
  
  bold    :   ('**'stat*'**')|('__'stat*'__');
  
  bolditalic: ('***' stat* '***');
  
  TEXT    :   [a-zA-Z0-9]+;
  
  SPACE   :   ' ';
  
  WS      :   [\t\r\n]+;
  </code></pre>
  
  <p><strong>Visitor Method</strong></p>
  
  <pre><code>import org.antlr.v4.runtime.*;
  import org.antlr.v4.runtime.tree.*;
  
  import java.util.Objects;
  
  public class markdownEvaluator extends markdownBaseVisitor {
  
  
      @Override
      public Object visitItalic(markdownParser.ItalicContext ctx) {
          int i=1;
          System.out.print("&lt;i&gt;");
          while(ctx.getChild(i)!=null)
          {
              visit(ctx.getChild(i));
              i++;
          }
          System.out.print("&lt;/i&gt;");
          return null;
      }
  
      @Override
      public Object visitBold(markdownParser.BoldContext ctx) {
          int i=1;
          System.out.print("&lt;strong&gt;");
          while(ctx.getChild(i)!=null)
          {
              visit(ctx.getChild(i));
              i++;
          }
          System.out.print("&lt;/strong&gt;");
          return null;
      }
  
      @Override
      public Object visitBolditalic(markdownParser.BolditalicContext ctx) {
          int i=1;
          System.out.print("&lt;strong&gt;&lt;i&gt;");
          while(ctx.getChild(i)!=null)
          {
              visit(ctx.getChild(i));
              i++;
          }
          System.out.print("&lt;/i&gt;&lt;/strong&gt;");
          return null;
      }
  
      @Override
      public Object visitSub_heading(markdownParser.Sub_headingContext ctx) {
          int i=0;
          System.out.print("&lt;h2&gt;");
          while(ctx.getChild(i)!=null)
          {
              visit(ctx.getChild(i));
              i++;
          }
          System.out.print("&lt;/h2&gt;");
          System.out.println();
          return null;
      }
  
      @Override
      public Object visitCustom(markdownParser.CustomContext ctx) {
          int headernumber = 0;
          int i=0;
          while(Objects.equals(ctx.getChild(i).getText(), "#"))
          {
              headernumber++;
              i++;
          }
          System.out.print("&lt;h"+String.valueOf(headernumber)+"&gt;");
          while (ctx.getChild(i)!=null)
          {
              visit(ctx.getChild(i));
              i++;
          }
          System.out.print("&lt;/h"+String.valueOf(headernumber)+"&gt;");
          return null;
      }
  
      @Override
      public Object visitMain_heading(markdownParser.Main_headingContext ctx) {
          int i=0;
          System.out.print("&lt;h1&gt;");
          while(ctx.getChild(i)!=null)
          {
              visit(ctx.getChild(i));
              i++;
          }
          System.out.print("&lt;/h1&gt;");
          System.out.println();
          return null;
      }
  
      @Override
      public Object visitText(markdownParser.TextContext ctx) {
          System.out.print(ctx.getText());
          return null;
      }
  }
  </code></pre>
  
  <ol>
  <li><p>How correct is my markdown grammar? Are there better methods of specifying it?</p></li>
  <li><p>How can I improve the way in which the code is generated? Presently I am using a while loop to check if the nodes have a child and then respond accordingly. Are there better methods to do this? Are there other methods that ANTLR supports that i can use to create the same effect?</p></li>
  <li><p>We want the HTML code to be written on to a file. So Is there a way to automatically create a file and write &lt;html&gt; when the program starts parsing and append &lt;/html&gt; and close the file when the parser finishes the parsing? </p></li>
  </ol>
  
  <p>Thanks.</p>