Java模拟Micro的词法语法分析

编译原理中所用到的micro的词法语法分析用Java模拟实现

import java.io.*;
import java.util.*;
public class B{
	public static int length = 0;
	static WordsType sym = WordsType.SYM_IDENTIFIER;
	static String id = ""; 
public enum WordsType{
	SYM_Baoliu,//保留字1
	SYM_IDENTIFIER, // 标识符2
	SYM_NUMBER, // 常数3
	SYM_MATH,//运算符4
	SYM_LPARENl;//分隔符5
}
public static WordsType analysis(char[] temp, int len) {
	String sys = "";  
	length = len; 
	while (temp[length] == ' '||temp[length]=='\t') {
		length++;
	}
	if (length<temp.length&&isLetter(temp[length])&&!isDigit(temp[length+1])) {
		while(length<temp.length&&temp[length]!=' '&&isLetter(temp[length])) {
			sys+=temp[length];
			length++;
		}
		id = sys;
		WordsType wt = reserve(id);
		return wt;
	}
	if (length<temp.length&&isLetter(temp[length])&&isDigit(temp[length+1])) {
		while(length<=temp.length&&temp[length]!=' '&&(isLetter(temp[length])||isDigit(temp[length]))) {
			sys+=temp[length];
			length++;
		}
		id = sys;
		WordsType wt = reserve(id);
		return wt;
	}
	if (length < temp.length && isDigit(temp[length])&&!isLetter(temp[length-1])) {
		while (length < temp.length && temp[length] != ' '&& isDigit(temp[length])) {
			sys += temp[length];
			length++;
		}
		if (length < temp.length && !isDigit(temp[length])) {
			id = sys;
			sym = WordsType.SYM_NUMBER;
			return sym;
		}
	}
	if (length < temp.length)
		return caculator(temp);
	return null;
}
public static WordsType caculator(char[] temp) {
	sym = null;
	int len = length;
	length++;
	id = temp[len] + "";
	switch (temp[len]) {
	case '+':sym = WordsType.SYM_MATH;break;
	case '-':sym = WordsType.SYM_MATH;break;
	case '*':sym = WordsType.SYM_MATH;break;
	case '/':sym = WordsType.SYM_MATH;break;
	case '=':sym = WordsType.SYM_MATH;break;
	case '(':sym = WordsType.SYM_LPARENl;break;
	case ')':sym = WordsType.SYM_LPARENl;break;
	case ',':sym = WordsType.SYM_LPARENl;break;
	case ';':sym = WordsType.SYM_LPARENl;break;
	case '.':sym = WordsType.SYM_LPARENl;break;
	case ':':sym = WordsType.SYM_LPARENl;break;
	case '{':sym = WordsType.SYM_LPARENl;break;
	case '}':sym = WordsType.SYM_LPARENl;break;
	case '[':sym = WordsType.SYM_LPARENl;break;
	case ']':sym = WordsType.SYM_LPARENl;break;
	}
	if (temp[len] == '>' && (len + 1 < temp.length) && temp[len + 1] == '=') {
		length++;
		id += temp[len + 1];
		sym = WordsType.SYM_MATH;
	}else if (temp[len] == '>' && (len + 1 < temp.length)&& temp[len + 1] != '=')
		sym = WordsType.SYM_MATH;
	else if (temp[len] == '<' && (len + 1 < temp.length)&& temp[len + 1] != '=')
		sym = WordsType.SYM_MATH;
	else if (temp[len] == '<' && (len + 1 < temp.length)&& temp[len + 1] == '=') {
		length++;
		id += temp[len + 1];
		sym = WordsType.SYM_MATH;
	}
	return sym;
}
public static boolean isLetter(char a) {
	if ((a >= 'a' && a <= 'z') || (a >= 'A' && a <= 'Z'))
		return true;
	return false;
}
public static boolean isDigit(char a) {
	if (a >= '0' && a <= '9')
		return true;
	return false;
}
public static WordsType reserve(String str) {//如果是字母则运行此方法。
	if ("begin".equals(str))
		return WordsType.SYM_Baoliu;
	else if ("end".equals(str))
		return WordsType.SYM_Baoliu;
	else if ("const".equals(str))
		return WordsType.SYM_Baoliu;
	else if ("if".equals(str))
		return WordsType.SYM_Baoliu;
	else if ("then".equals(str))
		return WordsType.SYM_Baoliu;
	else if ("while".equals(str))
		return WordsType.SYM_Baoliu;
	else if ("do".equals(str))
		return WordsType.SYM_Baoliu;
	else if ("var".equals(str))
		return WordsType.SYM_Baoliu;  
	else if ("procedure".equals(str))
		return WordsType.SYM_Baoliu;
	else 
		return WordsType.SYM_IDENTIFIER;
}
public static List<String> getInputStream(String filename) throws IOException{//将文件内容一行一行地压入list数组中
	List<String> list = new ArrayList<String>();
	FileInputStream fis = new FileInputStream(filename);
	BufferedReader br = new BufferedReader(new InputStreamReader(fis));
	String buffer = "";
	while ((buffer = br.readLine()) != null) {
		list.add(buffer);
	}
	return list;
}
public static void output(String str) {
	length=0;
	char[] chs = str.toCharArray();
	while (length < str.length()) {
	WordsType wt = analysis(chs, length);
	if (wt == WordsType.SYM_NUMBER)
		System.out.println('3' + "," + id);
	else if (wt == WordsType.SYM_Baoliu)
		System.out.println('1' + "," + id);
	else if(wt==WordsType.SYM_MATH)
		System.out.println('4'+"," + id);
	else if (wt==WordsType.SYM_LPARENl) 
	    System.out.println('5'+","+id);
	else 
		System.out.println('2' + "," + id);
}
}
public static void main(String[] args) throws IOException { 
	List<String> list = getInputStream("1.txt"); 
	for (int i = 0; i < list.size(); i++) { 
		System.out.println(list.get(i)); 
	    output(list.get(i)); 
		System.out.println("-----------"); 
} 
}
}

 

需要配置一个1.txt在同目录中才能正确运行,亲测可以运行。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值