I am working with file reader and writer using text files. Basically implementing a dfa of pascal grammer that will handle comments in a program. For this I have used 3 text files 1 file to read data from, 1 to write token in it and 1 to write error token in it. The pascal compiler does not consider this {{ as a comment because the comment syntax is "{this is a comment}" . I used an array of [5][4] for this purpose and I used file reader to read from a text file name "code.txt" containing this text "{a}". But the problem is that when i write this into a new text file name "token.txt" (initially writing it in token.txt but it should be in error.tx), it prints this {{a} means that it prints the first character two times like {{ . I want to output same as I have written it in the code.txt file. Thanks in advance.
public class Lex {
public static char dfa1[][] = new char[5][4];
/*
* for(int i=0; i<5; i++) { for(int j=0; j<4; j++) {
* System.out.println("arr["+i+"]["+j+"]"+dfa1[i][j]); } }
*/
//FileReader fr = new FileReader(filename);
//BufferedReader br = new BufferedReader(fr);
File file = new File("code.txt");
BufferedReader reader = null;
int StartState = 1;
char CurrentState = '1';
int acp =0;
String temptok = new String();
try {
reader = new BufferedReader(new FileReader(file));
int i;
// repeat until EOF
while ((i = reader.read()) != -1) {
char c = (char) i;
if (c == '{') {
if (CurrentState == dfa1[1][0]) {
CurrentState = '2';
acp =0;
temptok = temptok.concat(String.valueOf(c));
} else if (CurrentState == dfa1[2][0]) {
temptok = temptok.concat(String.valueOf(c));
acp =0;
CurrentState = '4';
} else if (CurrentState == dfa1[3][0]) {
temptok = temptok.concat(String.valueOf(c));
CurrentState = '4';
acp =0;
} else if (CurrentState == dfa1[4][0]) {
temptok = temptok.concat(String.valueOf(c));
acp =0;
break;
}
}
if (c == '}') {
if (CurrentState == dfa1[1][0]) {
acp =0;
temptok = temptok.concat(String.valueOf(c));
break;
} else if (CurrentState == dfa1[2][0]) {
temptok = temptok.concat(String.valueOf(c));
CurrentState = '3';
acp = 1;
} else if (CurrentState == dfa1[3][0]) {
temptok = temptok.concat(String.valueOf(c));
CurrentState = '3';
acp = 1;
} else if (CurrentState == dfa1[4][0]) {
temptok = temptok.concat(String.valueOf(c));
acp = 0;
break;
}
}
if (c != '}') {
if (CurrentState == dfa1[1][0]) {
temptok = temptok.concat(String.valueOf(c));
acp =0;
break;
} else if (CurrentState == dfa1[2][0]) {
temptok = temptok.concat(String.valueOf(c));
CurrentState = '2';
acp =0;
} else if (CurrentState == dfa1[3][0]) {
temptok = temptok.concat(String.valueOf(c));
acp =0;
break;
} else if (CurrentState == dfa1[4][0]) {
temptok = temptok.concat(String.valueOf(c));
acp =0;
break;
}
}
//System.out.println(temptok);
PrintWriter writer = new PrintWriter("token.txt", "UTF-8");
PrintWriter ewriter = new PrintWriter("error.txt", "UTF-8");
if(acp == 1){
writer.println(temptok);
writer.close();
}
else if(acp == 0)
{
ewriter.println(temptok);
ewriter.close();
}