I have a file with a specific format that I would like to parse. In this file I have a number on a line which specifies the number of lines to follow it.
example excerpt from the file:
3 // number of lines to follow = 3
1 3 // 1st line
3 5 // 2nd line
2 7 // 3rd line
I want to read the number (3 in this example) and then read the following 3 lines only. If there are less or more lines I want to generate an error. How can I do this in javacc?
Thank you
Here’s a way:
The
Lines.parse() : intmethod returns the difference between the number of lines it is supposed to read, and the actual number of lines that are read.Generate the parser like this:
compile all classes:
And feed it your test data (
test.txt):like this:
which produces the output: