|
View:
New views
6 Messages
—
Rating Filter:
Alert me
|
|
|
JAVACC parserHi 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. |
|
|
Re: JAVACC parserIn the first code you are creating an instance of the parser (and
calling it check), passing it a buffered reader wrapped around System.in - I don't see any such initialisation code in the 2nd version. In fact I see no initialisation for the variable check at all. You need to take your string and wrap it in a StringReader and hand it to the parser. On Thu, Apr 9, 2009 at 5:42 AM, netchandri <netchandri@...> wrote: > > 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. > > -- > View this message in context: http://www.nabble.com/JAVACC-parser-tp22955955p22955955.html > Sent from the java.net - javacc users mailing list archive at Nabble.com. > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: users-unsubscribe@... > For additional commands, e-mail: users-help@... > > -- - J.Chris Findlay (c: --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscribe@... For additional commands, e-mail: users-help@... |
|
|
Re: JAVACC parserHi Chris,
Thanks for helping me out on this. It did work for me after I created a String wrapper for my variable. But my parser is only reading the first XML TAG and fails to read the next one in the XML file. I receive the following error : ----------------------------------------------- ERROR: Second call to constructor of static parser. You must either use ReInit() or set the JavaCC option STATIC to false during parser generation. Exception in thread "main" java.lang.Error at test.check.<init>(check.java:952) at test.check.main(check.java:52) ----------------------------------------------- This error is probably because I use a FOR loop to read the XML TAGS And I am calling the constructor each time I try to read a XML TAG and it causes the constructor call to fail for the second time and so forth. Any thoughts on this, I tried to use STATIC = false in options, but it fails. Thanks. |
|
|
Re: JAVACC parserFor the parser to be nonStatic, you have to use it then throw it away and make a whole new one, and all the code in it needs to be aware of the fact it's accessing instance data not static data.
Otherwise, follow the instructions in the errorMessage and use a single instance of the static parser, and reset it's input via ReInit as covered in the docs, making sure you don't have leftover custom state un-reset. On Thu, Apr 9, 2009 at 11:29 AM, netchandri <netchandri@...> wrote:
-- - J.Chris Findlay (c: |
|
|
Re: JAVACC parserHi Chris,
I see that when I use STATIC = false; I get an error for method invocation: "Cannot make a static reference to the non-static method event() from the type check event cannot be resolved" I couldn't get to use the Reinit, You mentioned some docs to refer to reset input via ReInit. Can you please let me know what docs you are reffering to. Thanks.
|
|
|
Re: JAVACC parserI am referring to the javacc docs, which cover all this.
Once your parser is not static, you must create an instance of it to use it rather than calling the instance method event() from the static main(). Java 101. On Thu, Apr 9, 2009 at 3:54 PM, netchandri <netchandri@...> wrote: > > Hi Chris, > > I see that when I use STATIC = false; I get an error for method invocation: > "Cannot make a static reference to the non-static method event() from the > type check event cannot be resolved" > > I couldn't get to use the Reinit, You mentioned some docs to refer to reset > input via ReInit. Can you please let me know what docs you are reffering to. > > Thanks. > > J.Chris Findlay wrote: > > > > For the parser to be nonStatic, you have to use it then throw it away and > > make a whole new one, and all the code in it needs to be aware of the fact > > it's accessing instance data not static data. > > Otherwise, follow the instructions in the errorMessage and use a single > > instance of the static parser, and reset it's input via ReInit as covered > > in > > the docs, making sure you don't have leftover custom state un-reset. > > > > On Thu, Apr 9, 2009 at 11:29 AM, netchandri <netchandri@...> wrote: > > > >> > >> Hi Chris, > >> > >> Thanks for helping me out on this. It did work for me after I created a > >> String wrapper for my variable. > >> > >> But my parser is only reading the first XML TAG and fails to read the > >> next > >> one in the XML file. I receive the following error : > >> ----------------------------------------------- > >> ERROR: Second call to constructor of static parser. > >> You must either use ReInit() or set the JavaCC option STATIC to > >> false > >> during parser generation. > >> Exception in thread "main" java.lang.Error > >> at test.check.<init>(check.java:952) > >> at test.check.main(check.java:52) > >> ----------------------------------------------- > >> > >> This error is probably because I use a FOR loop to read the XML TAGS And > >> I > >> am calling the constructor each time I try to read a XML TAG and it > >> causes > >> the constructor call to fail for the second time and so forth. > >> > >> Any thoughts on this, I tried to use STATIC = false in options, but it > >> fails. > >> > >> Thanks. > >> -- > >> View this message in context: > >> http://www.nabble.com/JAVACC-parser-tp22955955p22961835.html > >> Sent from the java.net - javacc users mailing list archive at Nabble.com. > >> > >> > >> --------------------------------------------------------------------- > >> To unsubscribe, e-mail: users-unsubscribe@... > >> For additional commands, e-mail: users-help@... > >> > >> > > > > > > -- > > - J.Chris Findlay > > (c: > > > > > > -- > View this message in context: http://www.nabble.com/JAVACC-parser-tp22955955p22964029.html > Sent from the java.net - javacc users mailing list archive at Nabble.com. > > > --------------------------------------------------------------------- > To unsubscribe, e-mail: users-unsubscribe@... > For additional commands, e-mail: users-help@... > -- - J.Chris Findlay (c: --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscribe@... For additional commands, e-mail: users-help@... |
| Free embeddable forum powered by Nabble | Forum Help |