JaVaCC Grammar modification required.
Topic: JavaLanguage
Vijendra Singh, Mar 14, 2006
Hi, If someone has good command over writing .jj or.jjt files. I need some help in writing code. I need to incorporate in my sqlParser.jj and ultimately sqlParser.java files; so that they can read and parse :- "create view as select * from tablename" statement. This sqlParser.jj files are generating ASCII_char_stream instead Simple_char_stream, but i don't think it will create any problem. So just like it parsers create table...statement,delete,alster tables etc. It sholud also parse view without giving any error. I am giving this file with some modification i tried to do for views but grammar is mistaken please give me some useful way to solve this Thanks Vijendra
/**
* Creation date: July 19th, 2000
* This is the JavaCC grammar definition file for the DDL dialect used by
* ZeroCode.
*/
// options {
// DEBUG_PARSER = true;
// }
PARSER_BEGIN(SqlParser)
package zerocode.sqlParser;
import com.sun.java.util.collections.*;
import java.io.*;
import java.util.Enumeration;
import zerocode.dbSupport.*;
import zerocode.core.*;
/**
* The SqlParser class encapsulates parsing of SQL DDL statements to enable
* zeroCode to construct the schema-related data structures. Most of its
* implementation is generated using the
* JavaCC
* application.
*/
public class SqlParser {
public interface ConstraintType {
public static final int UNIQUE = 1;
public static final int PRIMARY_KEY = 2;
}
public static void main (String args[]) throws Exception {
Schema dbSchema = buildSchema (new InputStreamReader (System.in));
System.out.println (dbSchema.toString());
}
public static synchronized Schema buildSchema (Reader stream)
throws ZcException {
_fkConstraints = new ArrayList();
_errorList = new Vector();
_dbSchema = new Schema ("ZeroCodeSchema");
if (_parser == null)
_parser = new SqlParser (stream);
else
_parser.ReInit (stream);
try {
_parser.ddlSequence ();
} catch (ParseException e) {
throw new ZcException
(ErrorCode.INVALID_SCHEMA, "SQL syntax error: " + e.getMessage());
}
_dbSchema.setConstraints (_fkConstraints);
if (_errorList.size() > 0)
throw new ZcException
(ErrorCode.INVALID_SCHEMA,
"Schema errors:\n" + StringUtils.join (_errorList, "\n"));
return _dbSchema;
}
private static void addTable (DbTable table) {
_dbSchema.addTable (table);
}
private static void addSequence (String sequenceName) {
_dbSchema.addSequence (sequenceName);
}
private static int convertToken (String message)
throws ZcException {
try {
int val = Integer.parseInt (token.toString());
return val;
} catch (NumberFormatException e) {
throw new ZcException
(ErrorCode.INVALID_SCHEMA,
message + "token '" + token.toString() + "' line " +
token.beginLine + " column " + token.beginColumn);
}
}
private static void setConstraint (DbTable table, String constraintName,
int constraintType,
StringSet columnNameSet) {
if (constraintType == ConstraintType.PRIMARY_KEY) {
int nCols = columnNameSet.size();
if (nCols > 1) {
// _errorList.add ("Table " + table.name() + ": must have " +
// "exactly one column as primary key.");
// We set the pri key anyway, to inhibit the subsequent
// "No primary key defined" error message.
table.setPrimaryKey (columnNameSet);
} else if (nCols <= 0) {
// _errorList.add ("Table " + table.name() + ": must have " +
// "a primary key.");
} else {
String colName = (String) columnNameSet.asArray()[0];
DbColumn col = table.columnWithName (colName);
if (col == null)
_errorList.add ("Table " + table.name() + ": Column '" +
colName + "', specified in primary key " +
"constraint, is not a declared column.");
else
table.setPrimaryKey (col.name());
}
} else {
// It should be a UNIQUE constraint
table.addUniqueConstraint
(new UniqueConstraint (constraintName, table, columnNameSet));
}
}
private static void addForeignKeyConstraint (String constraintName,
DbTable table,
StringSet columnNameSet,
String toTableName,
String toColName) {
if (columnNameSet.size() != 1) {
// We now silently ignore this problem -- MAS 1/7/2003
// _errorList.add ("Table " + table.name() + ": must have " +
// "exactly one column in foreign key.");
return;
}
String fkColName = columnNameSet.asArray()[0];
DbColumn fkCol = table.columnWithName (fkColName);
if (fkCol == null) {
_errorList.add ("Table " + table.name() +
": Cannot add foreign key constraint: no " +
"foreign key column named '" + fkColName + "'");
return;
}
DbTable toTable = _dbSchema.tableWithName (toTableName);
if (toTable == null) {
_errorList.add ("Table '" + toTableName + "' not found in schema.");
return;
}
DbColumn toColumn = toTable.columnWithName (toColName);
if (toColumn == null) {
_errorList.add ("Attempting to add foreign key from " +
table.name() + "(" +
columnNameSet.joinString(",") + ") to table '" +
toTableName + "': the latter has no column " +
"named '" + toColName + "'");
} else {
String constraintId = constraintName != null
? constraintName
: ("zcFkConstraint" + (++zcFkCount));
ForeignKeyConstraint constraint = new ForeignKeyConstraint
(constraintId, fkCol, toColumn, _fkConstraints.size());
_fkConstraints.add (constraint);
}
}
private static Schema _dbSchema;
private static Vector _errorList;
private static List _fkConstraints;
private static SqlParser _parser;
private static int zcFkCount = 0;
}
PARSER_END(SqlParser)
SKIP :
{
" "
|
"\t"
|
"\n"
|
"\r"
|
"--" : IN_LINE_COMMENT
|
"/*" : IN_COMMENT
}
{
"\n" : DEFAULT
}
{
< ~[] >
}
{
"*/" : DEFAULT
}
{
< ~[] >
}
TOKEN [IGNORE_CASE] :
{
< ADD: "add" >
| < ALL: "all" >
| < ALTER: "alter" >
| < AND: "and" >
| < ANY: "any" >
| < AS : "as" >
| < ASC: "asc" >
| < BY: "by" >
| < CACHE: "cache" >
| < CASCADE: "cascade" >
| < CHECK: "check" >
| < COLUMN: "column" >
| < COMMENT: "comment" >
| < CONSTRAINT: "constraint" >
| < CONSTRAINTS: "constraints" >
| < CREATE: "create" >
| < CYCLE: "cycle" >
| < DEFAULT_TOK: "default" >
| < DELETE: "delete" >
| < DESC: "desc" >
| < DROP: "drop" >
| < FOREIGN: "foreign" >
| < IDENTITY: "identity" >
| < IN: "in" >
| < INCREMENT: "increment" >
| < INDEX: "index" >
| < IS: "is" >
| < KEY: "key" >
| < MAXVALUE: "maxvalue" >
| < MINVALUE: "minvalue" >
| < NOCACHE: "nocache" >
| < NOCYCLE: "nocycle" >
| < NOMAXVALUE: "nomaxvalue" >
| < NOMINVALUE: "nominvalue" >
| < NOORDER: "noorder" >
| < NOT: "not" >
| < NULL: "null" >
| < ON: "on" >
| < OR: "or" >
| < ORDER: "order" >
| < PRIMARY: "primary" >
| < REFERENCES: "references" >
| < SELECT: "select" >
| < SEQUENCE: "sequence" >
| < SOME: "some" >
| < START: "start" >
| < TABLE: "table" >
| < UNIQUE : "unique" >
| < UPDATE : "update" >
| < VIEW : "view" >
| < WITH: "with" >
| < COMMA: "," >
| < DOT: "." >
| < IDENTIFIER: ["a"-"z","A"-"Z", "_"] (["a"-"z", "_", "0"-"9", "$", "#"])* >
| < LPAREN: "(" >
| < NUMBER: (["0"-"9"])+ >
| < RPAREN: ")" >
| < SEMICOLON: ";" >
| < SLASH: "/" >
| < STRING_LITERAL:
(
"'"
(~["'","\\","\n","\r"])*
"'"
)+
>
}
String allowedColumnName () :
{}
{
(
LOOKAHEAD(2)
< IDENTIFIER >
| < ANY >
| < AS >
| < ASC >
| < BY >
| < CACHE >
| < CASCADE >
| < CHECK >
| < COLUMN >
| < COMMENT >
| < CONSTRAINTS >
| < CREATE >
| < CYCLE >
| < DEFAULT_TOK >
| < DELETE >
| < DESC >
| < DROP >
| < FOREIGN >
| < IDENTITY >
| < IN >
| < INCREMENT >
| < INDEX >
| < IS >
| < KEY >
| < MAXVALUE >
| < MINVALUE >
| < NOCACHE >
| < NOCYCLE >
| < NOMAXVALUE >
| < NOMINVALUE >
| < NOORDER >
| < NOT >
| < NULL >
| < ON >
| < OR >
| < ORDER >
| < PRIMARY >
| < REFERENCES >
| < SELECT >
| < SEQUENCE >
| < SOME >
| < START >
| < TABLE >
| < UNIQUE >
| < UPDATE >
| < VIEW >
| < WITH >
) { return token.toString(); }
}
void ddlSequence () throws ZcException :
{
DbTable table;
String sequenceName;
}
{
(
(
comment()
|
(
(
table = tableDefinition () {
addTable (table);
}
|
indexDefinition ()
|
sequenceName = sequenceDefinition ()
{
addSequence (sequenceName);
}
)
)
|
(
(
view = viewDefinition ()
)
)
|
alterTable ()
|
dropTable()
)
(
|
)+
)+
}
void comment () :
{ }
{
(
(
No comments:
Post a Comment