📄 word.java
字号:
package cifafenxi;
import java.io.*;
public class word {
private char ch = ' ';
private int act = 0;
private String fileName;
private char[] context = new char[1000];
private int current = 0;
private int strNO = 0;
private char[] strToken = new char[20];
private String[] symbol = new String[1000];
private int symbolNO = 0;
private int code = 0;
private int value = 0;
public String[][] result = new String[1000][2];
private int k = 0;
public void analysis(String inputFile) {
try {
fileName = inputFile;
int row = 0;
File file = new File(fileName);
FileInputStream input = new FileInputStream(file);
BufferedReader console = new BufferedReader(new InputStreamReader(input));
act = console.read(context);
console.close();
File aFile = new File("./output.txt");
aFile.createNewFile();
PrintWriter out = new PrintWriter(new FileOutputStream(aFile));
while (current < act) {
analyse();
strNO = 0;
}
for (int i = 0; i < k; i++) {
row++;
out.println("(" + row + ") (" + result[i][0] + ","
+ result[i][1] + ")");
}
out.close();
} catch (FileNotFoundException e) {
System.out.println("Sorry,file not found");
} catch (IOException e) {
System.out.println("IOException");
}
}
/* 词法分析的流程 */
public void analyse() {
GetChar();
GetBC();
if (IsLetter()) {
while (IsLetter() || IsDigit()) {
Concat();
GetChar();
}
Retract();
code = Reserve();
if (code != 0) {
result[k][0] = String.valueOf(code);
// result[k][1]=keyWords[code-1];
result[k][1] = "-";
k++;
} else {
value = InsertId();
result[k][0] = "11";
result[k][1] = symbol[value];
k++;
}
} // 判断标识符
else if (IsDigit()) {
while (IsDigit()) {
Concat();
GetChar();
}
Retract();
value = InsertConst();
result[k][0] = "12";
result[k][1] = String.valueOf(value);
k++;
} // 判断常量
else
switch (ch) {
case '+': // 判断+
result[k][0] = "13";
result[k][1] = "-";
k++;
break;
case '-': // 判断-
result[k][0] = "14";
result[k][1] = "-";
k++;
break;
case '(': // 判断 (
result[k][0] = "15";
result[k][1] = "-";
k++;
break;
case ')': // 判断 )
result[k][0] = "16";
result[k][1] = "-";
k++;
break;
case '=': // 判断=
result[k][0] = "17";
result[k][1] = "-";
k++;
break;
case '>': // 判断 >
result[k][0] = "18";
result[k][1] = "-";
k++;
break;
case '<': // 判断 <
result[k][0] = "19";
result[k][1] = "-";
k++;
break;
case ',': // 判断 ,
result[k][0] = "20";
result[k][1] = "-";
k++;
break;
case ';': // 判断 ;
result[k][0] = "21";
result[k][1] = "-";
k++;
break;
case ':':
GetChar(); // 判断 :
if (ch != '=') {
Retract();
result[k][0] = "22";
result[k][1] = "-";
k++;
} else { // 判断 :=
result[k][0] = "23";
result[k][1] = "-";
k++;
}
break;
case '*': // 判断 *
result[k][0] = "24";
result[k][1] = "-";
k++;
break;
case '/': // 判断 /
result[k][0] = "25";
result[k][1] = "-";
default: // System.out.println("over");
}
}
void GetBC() {
if (ch == ' ' || ch == '\r' || ch == '\n') {
GetChar();
GetBC();
} else
;
}
/* 检查ch中是否为空白 */
void GetChar() {
ch = context[current++];
}
/* 将下一输入字符读到ch中 */
void Concat() {
strToken[strNO++] = ch;
}
/* 将ch中的字符连接到strToken之后 */
boolean IsLetter() {
if ((ch <= 'Z' && ch >= 'A') || (ch >= 'a' && ch <= 'z'))
return true;
else
return false;
}
/* 判断字符 */
boolean IsDigit() {
if (ch <= '9' && ch >= '0')
return true;
else
return false;
}
/* 判断数字 */
int Reserve() {
String word = String.copyValueOf(strToken, 0, strNO);
if (word.toLowerCase().equals("program"))
return 1;
else if (word.toLowerCase().equals("begin"))
return 2;
else if (word.toLowerCase().equals("end"))
return 3;
else if (word.toLowerCase().equals("var"))
return 4;
else if (word.toLowerCase().equals("integer"))
return 5;
else if (word.toLowerCase().equals("if"))
return 6;
else if (word.toLowerCase().equals("then"))
return 7;
else if (word.toLowerCase().equals("else"))
return 8;
else if (word.toLowerCase().equals("do"))
return 9;
else if (word.toLowerCase().equals("while"))
return 10;
else
return 0;
}
/* 判断strToken中的字符串是否为保留字,并返回它的编码 */
void Retract() {
--current;
ch = ' ';
}
/* 搜索指示器毁掉一个位置,ch中置空白 */
int InsertId() {
symbol[symbolNO] = String.copyValueOf(strToken, 0, strNO);
return symbolNO++;
}
/* 将strToken中的标识符插入符号表,返回符号表指针 */
int InsertConst() {
String digit = String.copyValueOf(strToken, 0, strNO);
return Integer.parseInt(digit);
}
/*将strToken中的常量插入常量表,返回符号表指针*/
public int getK() {
return k;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -