title: Antlr Grammar Java Code
body: <p>I have defined a grammar language using Antlr3. ANTLRWorks generates the lexer and parser code.</p>
  
  <p>Below is a sample grammar:</p>
  
  <pre><code>grammar i;
  
  @header {
  package com.data;
  }
  
  elements    : (value (',' value)*)?;
  insert      : 'INSERT INTO table' 'VALUES' '('elements')'';';
  WS      : (' '|'\t'|'\f'|'\n'|'\r')+ {skip();}; // handle white space between keywords
  </code></pre>
  
  <p>The above grammar is only a example. I am actually developing a big grammar for my company.</p>
  
  <p>I have imported the grammar into com.data package of my project and can use the Lexer and Parser generated. What I would like to find out:</p>
  
  <ul>
  <li>Does the Java code have to be written within the grammar?</li>
  <li>I would prefer to use the Lexer and Parser, and write the Java code within another class instead within the grammar.</li>
  </ul>
  
  <p>According to the following post: <a href="https://stackoverflow.com/questions/1931307/antlr-is-there-a-simple-example">ANTLR: Is there a simple example?</a> it shows a simple example. However, in my case the grammar is big and would prefer the grammar to be imported and used. I have many rules:</p>
  
  <p>For example, the above grammar generates this code:</p>
  
  <pre><code>public void insert() throws RecognitionException;
  </code></pre>
  
  <p>What is the best way to handle this? I have lots of rules, which are return type of void.</p>
  
  <p>Each rule has a specific meaning in Java terms. I don't think the best way would be to get the syntax and then check call my code. For example:</p>
  
  <pre><code>String input = "INSERT INTO table VALUES (null);";
  if(input.equals("INSERT INTO table VALUES (null);") {
       insertData();
  }
  
  public void insertData() {
     // insert data into database
  }
  </code></pre>
  
  <p>It is a good idea to check against every syntax and call my code?</p>