0

i wrote a parser with JavaCC, which i can successfully test in eclipse reading the stream from standard input (keyboard). now i want to call this class with the parser from another class. the data to parse are available in a string. currently i didn't find a way to pass the string as argument. it's only possible to parse from stdin or from a file. writing the string to a file and than parsing the file is in my opinion not a solution.

any idea how to solve this issue ? i can't believe that i am the only one who want's to analyze only a string and not a complete file.

kind regards hans

--

  • 1
    Doesn't it have an option to parse from an `InputStream`? – fge Mar 24 '14 at 23:33
  • I believe you'll need to wrap the string in a Reader e.g. `Reader reader = new BufferedReader(new StringReader('abc'));` then invoke your parser with the reader as a parameter. – deanosaur Mar 24 '14 at 23:46

1 Answers1

0

You can construct the parser instance with an InputStream. This page shows you how to create the InputStream, How does one create an InputStream from a String?

Then you can construct your parser with this as defined in the JavaCC API.

TheParser.TheParser(java.io.InputStream stream) 

Do notice though:

This constructor is available only when both the options USER_TOKEN_MANAGER and USER_CHAR_STREAM are false. If the option STATIC is true, this constructor (along with other constructors) can be called exactly once to create a single parser object.

https://javacc.java.net/doc/apiroutines.html

You set these options in the option_bindings. https://javacc.java.net/doc/javaccgrm.html

Community
  • 1
  • 1
capturesteve
  • 373
  • 2
  • 10
  • thanks for your answer. it solved my problem. but as you said, the constructor can only be called once. i tried ReInit() but this seems to have a bug. and with static = false i run into the problem: "Cannot make a static reference to the non-static method xxxxx() from the type YYYYYY". there one can also find a lot of questions but no solution. thanks a lot. // hans –  Mar 25 '14 at 21:07