0

テキストファイルを使用してファイルリーダーとライターを使用しています。基本的に、プログラム内のコメントを処理するパスカル文法の dfa を実装します。このために、3 つのテキスト ファイルを使用しました。1 つのファイルからデータを読み取り、1 つのファイルにトークンを書き込み、1 つのファイルにエラー トークンを書き込みます。パスカル コンパイラは、コメント構文が "{this is a comment}" であるため、この {{ をコメントとは見なしません。この目的のために [5][4] の配列を使用し、ファイル リーダーを使用して、このテキスト「{a}」を含むテキスト ファイル名「code.txt」から読み取りました。しかし問題は、これを新しいテキストファイル名「token.txt」に書き込むと(最初はtoken.txtに書き込んでいますが、error.txにあるはずです)、これを出力することです{{a}は、 {{ のように最初の文字を 2 回。code.txtファイルに書いた通りに出力したいです。

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

2 に答える 2