编译原理中所用到的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在同目录中才能正确运行,亲测可以运行。