1 Reply Latest reply: Sep 14, 2005 8:27 AM by 807575 RSS

    help for JavaCC with grammar file

    807575
      Dear all,

      I am newbie here , and I am new to JavaCC. I develop one little programme as a lexical analyser and parser of sql grammar by using JavaCC technology.

      I have class : MyParser.jj which define the grammar and parse the query entry.

      After compilation I will get class " MyParser.java " which I use it in another application which will call the method parseQuery() of MyParser.java class.

      Then for the class MyParser.jj , I have to create methode : parseQuery() . but as process it doen't parse the query

      This is my code in MyParser.jj file:
      options {
             //JAVA_UNICODE_ESCAPE = true;
             LOOKAHEAD = 1;
      }
      
           /* Parser begin */
           PARSER_BEGIN(QueryParser)
      
           package myParser;
           
           public class QueryParser {
      
                /* Attribute */
                String _query;
           
                /* Constructor */     
                public QueryParser(String query){
                     _query = query;
                }
           
                /* Methode to set the query */
                
                public void setQuery(String query){
                     _query = query;     
                }
                
                // methode main
                public static void main(String args[]) throws ParseException
                {
                     QueryParser parser = new QueryParser(System.in);
                     //System.out.println("Query : "+);
                     System.out.println("Res : " + parser.parseQuery());
                } 
           }
      
           /* End the parser */
           PARSER_END(QueryParser)
      
           SKIP : /* WHITE SPACE */
           {
             " "
           | "\t"
           | "\n"
           | "\r"
           | "\f"
           }
      
           TOKEN : /* KEY WORDS */
           {
             < INSERT: "insert | INSERT" >
           | < CREATE: "create | CREATE" >
           | < SELECT: "select | SELECT" >
           | < DELETE: "delete | DELETE" >
           | < COMMIT: "commit | COMMIT" >
           | < INTO: "into | INTO" >
           | < WHERE: "where | WHERE" >
           | < VALUES: "values | VALUES" >
           | < TABLE: "table | TABLE" >
           | < EXIT: "exit | EXIT" >
           | < FROM: "from | FROM" >
           | < PRIMARY: "PRIMARY" >
      
           }
      
           TOKEN : /* SEPERATOR */
           {
             < COMMA: "," >
           | < SEMICOLOM: ";" >
           | < OPENPAREN: "(" > 
           | < CLOSEPAREN: ")" >
           | < DOT: "." >
      /*     | < EOL: "\n" > */
           
           }
      
           TOKEN : /* TYPE */
           {
             < TYPE: (<STRING> | <INT>) >
           | < STRING: "String" >
           | < BOOLEAN: "boolean" >
           | < INT: "int" >
           | < CHAR: "char" >
      
           }
           
           TOKEN : /* OPERATORS , LOGICAL OPERATORS */
           {
             < EQUAL: "=" >
           | < CLAUSE: "and | AND" >
           }
           
           TOKEN : /* NUMBER, STRING, VALUES */
           {
            < NUMBER: (<DIGIT>)+ ( "." (<DIGIT>)+ )? >
           | < DIGIT: ["0"-"9"] > 
           | < ANYSTRING: <LETTER> (<LETTER>|<DIGIT>)* >
           | < LETTER: ["a"-"z","A"-"Z"]>
           }
      
           /* The methods for pasing  query */
           
           boolean parseQuery():
           {
           }
           {
                <CREATE> { parserCreateQuery() ;}
                | {return false;}
           }
           /* The methods for pasing create query */
           boolean parseCreateQuery():
           {
           }
           {
                     <CREATE><TABLE><ANYSTRING><OPENPAREN>(<ANYSTRING><TYPE>)(<COMMA><ANYSTRING><TYPE>)*<CLOSEPAREN><SEMICOLOM> { return true ; }
                |{return false;}
           }
      it always return false, whatever the query is, so it mean that it doent do the lexical analyser on the query entry.

      Any idear, pls help

      Regards,
      Moni