0

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();
                }

2 Answers 2

2

You have a chain of if blocks, and not a chain of if, else if, else if blocks. So, when { is read, those two conditions are true, and the char is written twice:

if (c == '{') {
    ...
}
...
if (c != '}') {
    ...
}

Also, you shouldn't use String.concat(), but you should use a StringBuilder instead. And you should respect Java naming conventions: variables start with a lower-case letter. Also consider improving your naming: dfa1 and acp aren't descriptive names. temptok and ewriter should be named temporaryToken and errorWriter.

Final note: this is the kind of problems that are diagnosed immediately when you step through the code using a debugger. Learn using the debugger: it will save you hours of debugging time.

Sign up to request clarification or add additional context in comments.

2 Comments

i was just posting the same as you :)
@JB Nizet Thank you very much, your answer was really helpful for me.
0

Your third if:

if (c != '}')

seems like that should be:

if (c != '}' && c != '{')

Also, this doesn't seem like the most optimal way to process the file.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.