Issue
I am studying JavaCC and thus started working through an example from a Brazilian university.
Coding the file and compiling it using the JavaCC gradle plugin worked fine. However, when invoking the generated class with input, the program seems idle as if it's waiting for input.
java Adder 123 + 1
Indeed, the Parser defines System.in as an argument to the Adder class (see source code further down). Therefore I tried just running the Adder file and typing the input afterwards. This had the same effect, though.
java Adder
123 + 1
I also tried different combinations using String literal "" and appending \n at the end, since the BNF expects EOF after the expression.
PRE-EDIT: I was able to solve the problem partially but decided to post the question anyway as in the future it might concern someone else, too. The tutorial provides the input to the program with a < operator. I ran java on my Ubuntu WSL installation and got it working with the same command. Interestingly enough that doesn't work on Windows (even though the tutorial obviously uses the same OS family - but probably not the same edition as they mention Windows 2000 at some point...go figure).
java Adder < input.txt
Content of input.txt: 123 + 1
For full transparency I added the .jj and gradle config
build.gradle:
plugins {
id 'java'
id "ca.coglinc.javacc" version "2.4.0"
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
compileJavacc {
inputDirectory = file('src/main/java/javacc')
outputDirectory = file(project.buildDir.absolutePath + '/generated/javacc')
}
Parser.jj
// Parser specification
options {
STATIC = false ;
}
PARSER_BEGIN(Adder)
public class Adder {
public static void main( String[] args ) throws ParseException, TokenMgrError {
// System.out.println("Initializing adder...");
Adder parser = new Adder( System.in ) ;
// System.out.println("Parsing input...");
parser.Start() ;
// System.out.println("done");
}
}
PARSER_END(Adder)
// Lexical analyzer specification
SKIP : { " " }
SKIP : { "\n" | "\r" | "\r\n" }
TOKEN : { < PLUS : "+" > }
TOKEN : { < NUMBER : (["0"-"9"])+ > }
void Start() : {}
{
<NUMBER>
(
<PLUS>
<NUMBER>
)*
<EOF>
}
I've been at it for a while so it might as well come down to a detail. I'll give it another try with a fresh mind tomorrow and check here for input as well.
Godspeed.
Solution
Works when running the command java Adder <input.txt from the WSL. Seems to be an operating system problem.
Answered By - Omnibyte Answer Checked By - Gilberto Lyons (WPSolving Admin)