Question

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

--

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top