« Return to Thread: JAVACC parser

JAVACC parser

by netchandri :: Rate this Message:

Reply to Author | View in Thread

Hi there,

I am new to JAVA world and I have a small problem with JavaCC parsing. Please find the brief explanation of my problem below:

I use a check.jj file that parses a string entered in the command line. The code for that is as below:

--CODE--

options {
  JDK_VERSION = "1.5";
}
PARSER_BEGIN(check)
package test;

public class check {
  public static void main(String args[]) throws Exception, XmlException, FileNotFoundException {
  System.out.println("Enter a Rule");
     BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    check parser = new check(bf);  
    String event = check.event();
    System.out.println("IN Main CLass: " +event);
   
     }    
}
PARSER_END(check)

-- CODE --

The above code runs fine and method EVENT parses the string from command line.

Now, instead of me providing the string from the command line, I need to pass that string from a XML file. The string I used to write in the command line in one of the XML TAGS in the XML file. The code for that is as below:

-- CODE --
options {
  JDK_VERSION = "1.5";
}
PARSER_BEGIN(check)
package test;

public class check {
  public static void main(String args[]) throws Exception, XmlException, FileNotFoundException {
  System.out.println("Rule from XML file");


    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    Document doc = docBuilder.parse (new File("C:/bdb/Frule.xml"));
    doc.getDocumentElement ().normalize ();
   
    NodeList numberofrules = doc.getElementsByTagName("rule");
    int totalrules = numberofrules.getLength();
    System.out.println("Total no of rules : " + totalrules);
   
    for(int s=0; s<numberofrules.getLength() ; s++)
    {
        Node fstNode = numberofrules.item(s);
        if(fstNode.getNodeType() == Node.ELEMENT_NODE)
        {
   
    Element firstPersonElement = (Element)fstNode;
    NodeList firstNameList = firstPersonElement.getElementsByTagName("condaction");
    Element firstNameElement = (Element)firstNameList.item(0);
    //System.out.println("2");
    NodeList textFNList = firstNameElement.getChildNodes();
    String Rule = textFNList.item(0).getNodeValue().trim();
    System.out.println("Frule " +s+ ": " +Rule );
   
    String event = check.event(Rule);
    System.out.println("IN Main CLass: " +event);
   
        }
    }
    }    
}
PARSER_END(check)


Now when I run the above code I see that I am able to read the string from the XML file (variable Rule) but when I pass that string to the method EVENT, I get the following error:

--------------------------------------------------

Total no of rules : 5

Frule 0: let #xyz := collection('data.dbxml')/Bookstore/Book let $x := collection('data.dbxml')/Bookstore/Sales_Record/
book_name/text() return if( $y[book_name = $x]/%quantity_in_stock/text() >= LOW) then insert nodes <Pending_Order>
<t> 1</t>  </Pending_Order > after collection("data.dbxml")/Bookstore/Pending_Order[book_name =
'maths'] else replace value of node collection('data.dbxml')/Bookstore/Pending_Order/copies_ordered with "1000"

Exception in thread "main" java.lang.NullPointerException
        at test.check.jj_consume_token(check.java:999)
        at test.check.ValidCharacters(check.java:805)
        at test.check.event(check.java:71)
        at test.check.main(check.java:52)
--------------------------------------------------

Please help to identify the mistake for the above error. I think it is someting to do with the way I am passing the varialbe rule to method EVENT.

Thanks.

 « Return to Thread: JAVACC parser