编译原理词法分析 java

 

输出结果

 

实验报告

  • 实验目的
      1. 学会针对DFA转换图实现相应的高级语言源程序。
      2. 深刻领会状态转换图的含义,逐步理解有限自动机。
      3. 掌握手工生成词法分析器的方法,了解词法分析器的内部工作原理。
  • 实验内容

(自己设计的某种计算机语言)的编译程序的词法分析部分实现。

从左到右扫描每行该语言源程序的符号,拼成单词,换成统一的内部表示(token)送给语法分析程序。

为了简化程序的编写,有具体的要求如下:

  1. 数包含整型和浮点型。
  2. 空白符仅仅是空格、回车符、制表符。
  3. 代码是自由格式。
  4. 注释应放在花括号之内,并且不允许嵌套

给出自己的记号定义。举例如下:

 

定义保留字以及特殊符号

 

static String keyWord[] = {"else", "end", "if", "read", "repeat", "then", "until", "write"};
static String symbol[] = {"%", "*", "+", "-", "/", ":", ";", "<", "=", ">"};

  • 实验要求

要求实现编译器的以下功能:

    1. 按规则拼单词,并转换成二元式形式
    2. 删除注释行

else  if (contents.get(i)=='{'){      //删除注释行
   
for(int i2 =i;i2<contents.size();i2++){
        if(contents.get(i2)=='}'){
            i=i2;
            break;
        }
    }

 

    1. 删除空白符 (空格、回车符、制表符)

 

    1. 列表打印源程序,按照源程序的行打印,在每行的前面加上行号,并且打印出每行包含的记号的二元形式

 

//读取文件

file = new File("D:\\data.txt");

StringBuffer sbf = new StringBuffer();



try {

    BufferedReader reader = new BufferedReader(new FileReader(file));

    String tempStr;

    int n = 1;

    while ((tempStr = reader.readLine()) != null) {

        System.out.print("" + n + "\t");

        System.out.println(tempStr);

        n++;

    }

} catch (FileNotFoundException e) {

    e.printStackTrace();

} catch (IOException e) {

    e.printStackTrace();

}

 

    1. 发现并定位错误

给出词法分析程序使用的状态图(DFA)

 

自己设计的词法分析进行具体的要求:

程序源码

public class WordAnalysis {

    static File file;

    static String keyWord[] = {"else", "end", "if", "read", "repeat", "then", "until", "write"};

    static String symbol[] = {"%", "*", "+", "-", "/", ":", ";", "<", "=", ">"};

    private static void getToken() {

        file = new File("D:\\data.txt");

        ArrayList<Character> contents = new ArrayList<>();

        int n = 1;

        try {

            Reader reader  = new InputStreamReader(new FileInputStream(file));

            int tempchar;

            while ((tempchar = reader.read()) != -1) {

                if (((char) tempchar) != '\r') {

                    contents.add((char) tempchar);

                }

            }

        } catch (FileNotFoundException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

        System.out.print(""+n+"\t");

        for (int i = 0; i < contents.size(); i++) {

            if(contents.get(i)=='\n'&&i!=contents.size()-1){          //判断第几行

                System.out.print("\n"+(++n)+"\t");

            }else if (isCharacter(contents.get(i))){   //判断是否为标识符

                String tok="";

                tok = tok+contents.get(i);

                while (isCharacter(contents.get(++i))){

                    tok = tok+contents.get(i);

                }

                i--;

                if(!iskeyword(tok)){

                    System.out.print("(标识符,"+tok+")\t");

                }

            }else if(isNumber(contents.get(i))){    //判断是否为数字

                String tok="";

                tok = tok+contents.get(i);

                while (isNumber(contents.get(++i))){

                    tok = tok+contents.get(i);

                }

                i--;

                System.out.print("(数字,"+tok+")\t");

            }else  if (contents.get(i)=='{'){      //删除注释行

                for(int i2 =i;i2<contents.size();i2++){

                    if(contents.get(i2)=='}'){

                        i=i2;

                        break;

                    }

                }

            }else {

                isSymbol(contents.get(i));

            }

        }



    }



    //是否为符号

    private static boolean isSymbol(Character character) {

        for (int i1 = 0; i1 < symbol.length; i1++) {

            if(symbol[i1].equals(character.toString())){

                System.out.print("(符号,"+character+")\t");

            }

        }

        return false;

    }



    //是否为关键字

    private static boolean iskeyword(String tok) {

        for (int i1 = 0; i1 < keyWord.length; i1++) {

            if(keyWord[i1].equals(tok)){

                System.out.print("(关键字,"+tok+")\t");

                return true;

            }

        }

        return false;

    }

    private static void printSource() {

        //读取文件

        file = new File("D:\\data.txt");

        StringBuffer sbf = new StringBuffer();

        try {

            BufferedReader reader = new BufferedReader(new FileReader(file));

            String tempStr;

            int n = 1;

            while ((tempStr = reader.readLine()) != null) {

                System.out.print("" + n + "\t");

                System.out.println(tempStr);

                n++;

            }

        } catch (FileNotFoundException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }



    }



    // 判断是否为数字

    private static boolean isNumber(char c) {

        if (c <= '9' && c >= '0') {

            return true;

        } else {

            return false;

        }

    }



    //判断是否为字符

    private static boolean isCharacter(char c) {

        if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z') {

            return true;

        } else {

            return false;

        }

    }

    public static void main(String[] args) {

        printSource();    //打印源程序

        getToken();       //源程序处理

    }



}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章