I have a program that reads in a specific grammar for a CFL and outputs the separate productions for the start symbol, but for some reason passing in command line arguments doesn't work.
I've tried using a String[]
in the program, named input
and using that seems to provide output, but when I try and run the program with specific commands from the command line, I get errors.
Here's the code:
char[][] prod; int prodCount = 0, numProds = 0;
String[] input = new String[] {"a","a","S","b","X",
"a","a","S","a","X",
"a","S","a","X"};
for(int i = 0; i < input.length; i++) {
if(input[i] == "X")
prodCount++;
}
if(input[input.length - 1] == "X") {
prod = new char[prodCount + 1][1];
prod[prod.length - 1][0] = '\0';
} else
prod = new char[prodCount][];
int current = 0;
for(int i = 0; i < input.length; i++) {
if(input[i] == "X") {
prod[current] = new char[numProds];
current++; numProds = 0;
} else
numProds++;
}
int currentTerminal = 0; current = 0;
for(int i = 0; i < input.length; i++) {
if(input[i] == "X") {
currentTerminal = 0;
current++;
}
else {
prod[current][currentTerminal] = input[i].charAt(0);
currentTerminal++;
}
}
for(int i = 0; i < prod.length; i++) {
for(int j = 0; j < prod[i].length; j++) {
if(prod[i][j] == '\0')
System.out.print("E");
else
System.out.print(prod[i][j]);
}
System.out.println();
}
Using this code with the String[] input
gives me
aaSb
aaSa
aSa
E
However, when I try using input from the command line I get...
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
Line 36
Here's the line that the above error points to
prod[current][currentTerminal] = args[i].charAt(0);
I'm not sure what's different between String[] input
and String[] args
. Can someone let me know where I'm making a mistake?