简体   繁体   中英

JavaCC: parsing a string and not reading from stdin or from a file

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

--

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM