首页 > 代码库 > 词法分析实验报告

词法分析实验报告

实验一、词法分析程序实验

专业:商业软件   姓名:杨链新  学号:201506110113

一、        实验目的

 

通过设计调试词法分析程序,实现从源程序中分出各种单词的方法;加深对课堂教学的理解;提高词法分析方法的实践能力。掌握从源程序文件中读取有效字符的方法和产生源程序的内部表示文件的方法;掌握词法分析的实现方法;上机调试编出的词法分析程序。

二、        实验内容和要求

(1)   掌握从源程序文件中读取有效字符的方法和产生源程序的内部表示文件的方法。

(2)   掌握词法分析的实现方法。

(3)   上机调试编出的词法分析程序。

 

三、        实验方法、步骤及结果测试

 import java.awt.*;

import java.util.Scanner;

 

 

/**

 * Created by s2002 on 2016/9/30.

 */

public class com {

    public static void main(String[] args) {

 

        //限制字符串最大值

        final int MAX_LEN = 100;

 

        //关键字

        String[] key_word = new String[]{"begin","end","if","then","while","do"};

 

        //录入用户输入

        Scanner input = new Scanner(System.in);

        System.out.print("Please input a string <end with ‘#‘>:");

        String uString = input.nextLine();

 

        char[] analyseData = http://www.mamicode.com/new char[MAX_LEN];

 

        int index = 0, key = 0;

        List list = new List();

 

        do {

            String compareStr = null;

            char temp = uString.charAt(index);

            list = extactCharacters(temp, analyseData, key_word, uString, index, compareStr);

            if (list.getItemCount() == 0) {

                index++;

                continue;

            }

            // 规定List的第一个元素为index,第二个元素为key

            index = Integer.parseInt(list.getItem(0));

            key = Integer.parseInt(list.getItem(1));

            String words = list.getItem(2);

            System.out.println("< " + key + " ," + words + " >");

        } while (key != 0);

 

    }

 

 

    public static List extactCharacters(char temp, char[] analyseDate, String[] keywords, String uString, int index,

                                        String compareStr) {

 

        int keyID = -1, m = 0;

 

        List list = new List();

        //判断下一个读入的字符是否为空格,若读取到空格则跳过,提取下一个字符进行判断

        while (temp != ‘ ‘) {

            //判断当前字符是字母或者数字和字母的组合

            if (temp >= ‘a‘ && temp <= ‘z‘) {

                m = 0;

 

                // 当读取到不是大小写字母或者数字时候判断为一个单词读取完成

                while (temp >= ‘a‘ && temp <= ‘z‘ || temp >= ‘A‘ && temp <= ‘Z‘ || temp >= ‘0‘ && temp <= ‘9‘) {

                    analyseDate[m++] = temp;

                    compareStr += temp + "";

                    temp = uString.charAt(++index);

                }

 

                // 与读取出来的字符判断是否为关键字

                compareStr = compareStr.substring(4);

                for (int i = 0; i < 6; i++) {

                    if (compareStr.equals(keywords[i])) {

                        keyID = i + 1;

                        list.add(index + "");

                        list.add(keyID + "");

                        list.add(compareStr);

                        return list;

                    }

                }

 

                //若为非关键字就当作为标识符

                keyID = 10;

                list.add(index + "");

                list.add(keyID + "");

                list.add(compareStr);

                return list;

            }

 

            //判断当前字符是否为数字

            else if (temp >= ‘0‘ && temp <= ‘9‘) {

                m = 0;

                String tempTokens = null;

                // 对后面的字符进行判断是否为数字

                while (temp >= ‘0‘ && temp <= ‘9‘) {

                    analyseDate[m++] = temp;

                    tempTokens += temp;

                    temp = uString.charAt(++index);

                }

                // 不是数字则返回种别码,结束当前方法

                keyID = 11;

                tempTokens = tempTokens.substring(4);

                list.add(index + "");

                list.add(keyID + "");

                list.add(tempTokens + "");

                return list;

            }

            m = 0;

            //判断当前字符是否为其他关系运算符

            String token = null;

            switch (temp) {

                case ‘<‘:

                    // String token = null;

                    analyseDate[m++] = temp;

                    token += temp;

                    if (uString.charAt(++index) == ‘=‘) {

                        analyseDate[m++] = temp;

                        keyID = 22;

                        token += uString.charAt(index++);

                    } else if (uString.charAt(++index) == ‘>‘) {

                        analyseDate[m++] = temp;

                        keyID = 21;

                        token += uString.charAt(index++);

                    } else {

                        keyID = 23;

                    }

                    list.add(index + "");

                    list.add(keyID + "");

                    token = token.substring(4);

                    list.add(token);

                    return list;

                case ‘>‘:

                    analyseDate[m++] = temp;

                    token += temp;

                    if (uString.charAt(++index) == ‘=‘) {

                        keyID = 24;

                        analyseDate[m++] = temp;

                        token += uString.charAt(index++);

                    } else {

                        keyID = 20;

                    }

                    list.add(index + "");

                    list.add(keyID + "");

                    token = token.substring(4);

                    list.add(token);

                    return list;

                case ‘:‘:

 

                    analyseDate[m++] = temp;

                    token += temp;

                    if (uString.charAt(++index) == ‘=‘) {

                        keyID = 18;

                        // analyseDate[m++] = temp;

                        analyseDate[m++] = uString.charAt(index);

                        token += uString.charAt(index++);

                    } else {

                        keyID = 17;

                    }

                    list.add(index + "");

                    list.add(keyID + "");

                    token = token.substring(4);

                    list.add(token);

                    return list;

                case ‘*‘:

                    keyID = 13;

                    break;

                case ‘/‘:

                    keyID = 14;

                    break;

                case ‘+‘:

                    keyID = 15;

                    break;

                case ‘-‘:

                    keyID = 16;

                    break;

                case ‘=‘:

                    keyID = 25;

                    break;

                case ‘;‘:

                    keyID = 26;

                    break;

                case ‘(‘:

                    keyID = 27;

                    break;

                case ‘)‘:

                    keyID = 28;

                    break;

                case ‘#‘:

                    keyID = 0;

                    break;

                default:

                    keyID = -1;

                    break;

            }

            analyseDate[m++] = temp;

            list.add(++index + "");

            list.add(keyID + "");

            list.add(temp + "");

            return list;

        }

        return list;

    }

}

运行结果为:技术分享

 

 

 

 

.这次的实验很难,不过我不气馁,我通过查看书籍,通过网上找资料,通过问同学,终于悟出了其中的一些规律,因此得以完成了这个程序,虽然其中夹杂了很多的问题,但是,我努力过,因此很有成就感的,我会更加努力学习,把编译原理学得更好。

词法分析实验报告