I am trying to validate the input string: (10 30i) or should I say (10 30i)\n by using a parser, but am unable to implement StreamTokenizer.TT_EOL so that my program is done when it sees ); checks if it maches ')' (it does) and sees that next token is end of line EOL ( or should it be EOF?) ; and prints out "Reached the end!". Here is my code, I have tried to implement EOL but was unsuccesful:
import java.io.*;
import static java.util.Objects.requireNonNull;
public class Test2 {
private int lookahead;
private StreamTokenizer tokenizer;
private final Reader input;
private final Writer output;
public Test2(Reader input, Writer output) throws IOException {
this.input = requireNonNull(input);
this.output = requireNonNull(output);
this.lookahead = this.input.read();
}
private void next() throws IOException {
lookahead = tokenizer.nextToken();
if (lookahead == StreamTokenizer.TT_EOL) {
System.out.println("Reached the end!");
}
if (tokenizer.sval != null) {
char ch = tokenizer.sval.charAt(0);
lookahead = (int) ch;
}
}
private void match(int expected) throws IOException {
if (lookahead != expected ) {
switch (expected) {
case '(':
System.out.println("Error, missing opening round bracket.");
break;
case ')':
System.out.println("Error, missing closing round bracket.");
break;
case 'i':
System.out.println("Error, missing i.");
break;
case ' ':
System.out.println("Error, missing ");
}
} else {
next();
}
}
private void parseExpr() throws IOException {
if (lookahead == '(') {
match('(');
if (lookahead == StreamTokenizer.TT_NUMBER) {
next();
} else {
System.out.println("Error, missing number.");
return;
}
match(' ');
if (lookahead == StreamTokenizer.TT_NUMBER) {
next();
} else {
System.out.println("Error, missing number.");
return;
}
match('i');
match(')');
System.out.println("We have a complex number.");
next();
parseExpr();
if (lookahead == ' ') {
next();
parseExpr();
}
}
}
public static void main(String[] args) throws IOException {
InputStreamReader reader = new InputStreamReader(System.in);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(outputStream);
Test2 parse = new Test2(reader, writer);
parse.tokenizer = new StreamTokenizer(reader);
parse.parseExpr();
writer.close(); // Close and flush output stream writer.
String output = outputStream.toString();
System.out.println("Output generated by parser: " output);
}
}
I tried debugging it , it works fine until ) is recognized as a token.
This is the last step of the debugger, then it stops working:
Please help me :( even ideas on what changes I can make to whole code would help me a lot.
CodePudding user response:
The issue is that the tokenizer will not recognize an EOL
by default. You will need to enable this functionality with tokenizer.eolIsSignificant(true);
.
After parse.tokenizer = new StreamTokenizer(reader);
add this line parse.tokenizer.eolIsSignificant(true);
and the tokenizer should recognize EOL
s