编译中的词法分析程序

import java.util.*;

/**
 * @author SXH
 * @说明 词法分析器
 * */
public class LexicalAnalysis {
	/**
	 * 存储源代码
	 * */
	static String sourceCode;

	/**
	 * 两个指针
	 * */
	static int pointA = 0, pointB = 0;

	/**
	 * 当前字符
	 * */
	static char presentChar;

	/**
	 * 存放每次识别的单词
	 * */
	static StringBuilder temp = new StringBuilder("");

	/**
	 * 是否处于单词中
	 */
	static boolean flagL = false;

	/**
	 * 是否处于数字中
	 * */
	static boolean flagN = false;

	/**
	 * 是否是双星号即“**”
	 * */
	static boolean isDoubleAsterisk = false;

	/**
	 * 词法分析
	 */
	public static void main(String[] args) {

		System.out.println("单词符号\t\t种别编码\t\t单词属性");
		System.out.println("begin\t\t  1\t\t ---");
		System.out.println("if\t\t  2\t\t ---");
		System.out.println("then\t\t  3\t\t ---");
		System.out.println("else\t\t  4\t\t ---");
		System.out.println("end\t\t  5\t\t ---");
		System.out.println("标识符\t\t  6\t\t在名字表中的地址");
		System.out.println("整型常数\t\t  7\t\t十进制整数");
		System.out.println("+\t\t  8\t\t ---");
		System.out.println("*\t\t  9\t\t ---");
		System.out.println("**\t\t  10\t\t ---");
		System.out.println("(\t\t  11\t\t ---");
		System.out.println(")\t\t  12\t\t ---");
		System.out.println("*************************************");
		System.out.println("   将以二元式形式输出:(种别编码,属性值)");

		Scanner s = new Scanner(System.in);
		System.out.print("请输入:");
		sourceCode = s.nextLine();

		lexicalAnalysis(sourceCode);
	}

	/**
	 * 词法分析
	 * 
	 * @param sourceCode
	 *            需要分析的字符串
	 * */
	static void lexicalAnalysis(String sourceCode) {

		for (int i = 0; i < sourceCode.length(); i++) {
			if (isDoubleAsterisk) {// 是连续的两个*,去检测下一个字符
				isDoubleAsterisk = false;
				continue;
			} else {
				presentChar = sourceCode.charAt(i);
				if (sourceCode.charAt(i) >= 'a' && sourceCode.charAt(i) <= 'z') {// 如果是字母
					if (flagL == false) {// 标识符的第一个字符
						pointA = i;
						flagL = true;

					} else {// 仍然处于标识符中

					}
					continue;

				} else if (sourceCode.charAt(i) >= '0'
						&& sourceCode.charAt(i) <= '9') {// 如果是数字
					if (flagN == false && flagL == false) {// 数字的第一个字符,他一定是数字
						pointA = i;
						flagN = true;

					} else if (flagL == false && flagN == true) {// 仍然处于数字中

					} else if (flagL == true && flagN == false) {// 虽是数字,但他处于标识符中

					} else {// 数字中出现了字符,错误!!!
						System.err.println("数字中出现了字符,错误!!!");
						return;
					}
					continue;

				} else if (sourceCode.charAt(i) == '+'
						|| sourceCode.charAt(i) == '*'
						|| sourceCode.charAt(i) == '('
						|| sourceCode.charAt(i) == ')') {// 是其它几个字符
					pointB = i;
					switch (sourceCode.charAt(i)) {
					case '+':
						display();
						System.out.println("(8,-)");
						break;
					case '*':
						if (sourceCode.charAt(i + 1) == '*') {
							System.out.println("(10,-)");
							isDoubleAsterisk = true;
							// i++;
						} else {
							display();
							System.out.println("(9,-)");
						}
						break;
					case '(':
						System.out.println("(11,-)");
						break;
					case ')':
						display();
						System.out.println("(12,-)");
						break;
					}
				} else if (sourceCode.charAt(i) == ' ') {
					pointB = i;
					display();
				} else {//输入了不能识别的字符
					System.err.println("输入了不能检测的字符:" + sourceCode.charAt(i));
					return;
				}
			}
		}
	}

	/**
	 * 显示标识符
	 * */
	static void letter() {
		// temp.append(sourceCode.charAt(presentChar));
		System.out.println("(6," + (pointA + 1) + ")");
		temp = new StringBuilder();

		return;
	}

	/**
	 * 显示数字
	 * */
	static void number() {
		// temp.append(sourceCode.charAt(presentChar));
		System.out.println("(7," + temp + ")");
		temp = new StringBuilder();

		return;
	}

	/**
	 * 显示其他字符,但是没有用上
	 * */
	static void other() {
		temp.append(sourceCode.charAt(presentChar));

		return;
	}

	/**
	 * 将数字、标识符、关键字输出
	 * */
	static void display() {
		flagL = false;
		flagN = false;

		temp.append(sourceCode, pointA, pointB);
		if (temp.charAt(0) >= '0' && temp.charAt(0) <= '9') {// 以数字开头一定是数字
			number();
		} else if (temp.charAt(0) >= 'a' && temp.charAt(0) <= 'z') {// 以字母开头是标识符或关键字
			if (temp.toString().equals("begin")) {
				System.out.println("(1,-)");
			} else if (temp.toString().equals("if")) {
				System.out.println("(2,-)");
			} else if (temp.toString().equals("then")) {
				System.out.println("(3,-)");
			} else if (temp.toString().equals("else")) {
				System.out.println("(4,-)");
			} else if (temp.toString().equals("end")) {
				System.out.println("(5,-)");
			} else {
				letter();
			}
		}
	}

	/**
	 * 本来打算写一个将字符串转化为16位字符数组的函数,结果还是不能匹配,没有使用
	 * */
	static char[] toCharArray16(String string) {
		char array[] = new char[16];
		for (int j = 0; j < array.length; j++) {
			if (j < string.length())
				array[j] = string.charAt(j);
			else {
				array[j] = ' ';
			}
		}

		return array;
	}
}

猜你喜欢

转载自blog.csdn.net/BaiFeng303/article/details/42748327