I have defined a grammar language using Antlr3. ANTLRWorks generates the lexer and parser code.
Below is a sample grammar:
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
The above grammar is only a example. I am actually developing a big grammar for my company.
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:
- Does the Java code have to be written within the grammar?
- I would prefer to use the Lexer and Parser, and write the Java code within another class instead within the grammar.
According to the following post: ANTLR: Is there a simple example? 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:
For example, the above grammar generates this code:
public void insert() throws RecognitionException;
What is the best way to handle this? I have lots of rules, which are return type of void.
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:
String input = "INSERT INTO table VALUES (null);";
if(input.equals("INSERT INTO table VALUES (null);") {
insertData();
}
public void insertData() {
// insert data into database
}
It is a good idea to check against every syntax and call my code?