package sy1
import java.io.BufferedReader
import java.io.BufferedWriter
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.IOException
import java.io.InputStreamReader
import java.io.OutputStreamWriter
import java.nio.charset.StandardCharsets
public class Bin {
static char op[] = {'+','-','*','/','%'}
//用于区分符号类别
static String[] mp= {"id","key","relop","界符","num","op"}
//c是否是分界符
static int is_delim(char c) {
if (c == ',' || c == ';' || c == '(' || c == ')' || c == '{' || c == '}' || c == '[' || c == ']') {
return 1
}
return 0
}
//查找word是否在保留字表key中
static int search_key(String word){
//定义key表
String[] key = {"void","main","short","long","int","double","float","while","if","else","for","break","return","char" }
for (int i = 0
if ( word.equals(key[i]) )
//是保留字,返回1
return 1
}
//不是保留字,返回0
return 0
}
//c是否在op数组中
static int is_op(char c) {
int p = -1
for(int i = 0
if(c == op[i]) p = i
}
return p
}
public static void main(String []arg) {
String file="input.txt"
String word
String jz
int op_pos = -1
char ch
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(
new FileInputStream("input.txt"),
StandardCharsets.UTF_8
)
)
BufferedWriter outputFile = new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream("output.txt"),
StandardCharsets.UTF_8
),
1024 * 8 * 8
)
){
StringBuffer sb = new StringBuffer()
int chs=0
while ((chs = reader.read()) != -1) {
sb.append((char) chs)
}
reader.close()
String content= sb.toString()
// System.out.print(content)
//第一个单词
int i=0
ch=content.charAt(i)
while( i<content.length() ) {
//识别分界符
if (is_delim(ch)==1) {
outputFile.write("<"+ch+','+mp[3]+'>'+"\n")
i++
if(i<content.length()) ch = content.charAt(i)
else {break
}
//识别数字
else if (Character.isDigit(ch)) {
word = ""
//连续的数字
while (Character.isDigit(ch)) {
word += ch
i++
ch = content.charAt(i)
if( Character.isDigit(ch) ) continue
//如果出现小数点
if (ch == '.') {
word += ch
i++
ch = content.charAt(i)
//连续的数字
if (Character.isDigit(ch)){
while(Character.isDigit(ch)) {
word += ch
i++
ch = content.charAt(i)
}
//num(实数)
outputFile.write('<'+word+','+mp[4]+'>'+"\n")
break
}
//如果小数点后没有数字,则报错
else
{
//一直读取到这个错误的单词结束
while(!Character.isDigit(ch)&&ch!=','&&ch!='
word += ch
i++
ch = content.charAt(i)
if(!(i<content.length())) break
}
outputFile.write(word + " is error!\n")
break
}
}
//如果数字后面是分界符或操作符,即为整数,作为常数存储
else if(ch==','||ch==';'||ch==')'||ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='='||ch=='>'||ch=='<'||ch=='?'||ch=='!'||ch=='&'){
outputFile.write('<'+word+','+mp[4]+'>'+"\n")
break
}
//数字后面跟着字符,如8ws,则出错
else{
while(ch!=','&&ch!='
word += ch
i++
ch = content.charAt(i)
if(!(i<content.length())) break
}
outputFile.write(word + " is error!\n")
break
}
}
}
//标志符可以以下划线/字母开始
//识别标识符/保留字
else if (Character.isLetter(ch) || ch == '_') {
word = ""
while (Character.isLetter(ch) || Character.isDigit(ch) || ch =='_') {
word += ch
i++
ch = content.charAt(i)
}
//在key表中查找word是否是保留字
if (search_key(word)==1) {
//保留字
outputFile.write('<'+word+','+ mp[1]+'>'+"\n")
}
//标识符
else
outputFile.write('<'+word+','+mp[0]+'>'+"\n")
}
//复合运算符单独列出来处理
else if (ch == '>') {
word = ""
word += ch
i++
ch = content.charAt(i)
if (ch == '=') {//">="
word += ch
//读下一个单词,进入下一个循环
i++
ch = content.charAt(i)
}
outputFile.write('<'+word+','+mp[2]+'>'+"\n")
}
else if (ch == '<') {
word = ""
word += ch
i++
ch = content.charAt(i)
if (ch == '=') {//"<="
word += ch
i++
ch = content.charAt(i)
}
outputFile.write('<'+word+','+mp[2]+'>'+"\n")
}
else if (ch == '&') {
word = ""
word += ch
i++
ch = content.charAt(i)
if (ch == '&') {
word += ch
i++
ch = content.charAt(i)
}
outputFile.write('<'+word+','+mp[5]+'>'+"\n")
}
else if (ch == '|') {
word = ""
word += ch
i++
ch = content.charAt(i)
if (ch == '|') {
word += ch
i++
ch = content.charAt(i)
}
outputFile.write('<'+word+mp[5]+'>'+"\n")
}
else if (ch == '!') {
word = ""
word += ch
i++
ch = content.charAt(i)
if (ch != '=') {//"<="
outputFile.write('<'+word+mp[5]+'>'+"\n");
}
else if( ch == '=' ){//"!="
word += ch;
outputFile.write('<'+word+mp[2]+'>'+"\n");
i++;
ch = content.charAt(i);
}
}
else if (ch == '=') {
word = "";
word += ch;
i++;
ch = content.charAt(i);
if (ch == '=') {//"=="
word += ch;
i++;
ch = content.charAt(i);
}
outputFile.write('<'+word+','+mp[2]+'>'+"\n");
}
//其余的单个的运算符
else if((op_pos = is_op(ch)) != -1) {
outputFile.write("<"+ch+','+mp[5]+'>'+"\n");
i++;
ch = content.charAt(i);
}
//跳过空格
else {
i++;
ch = content.charAt(i);
}
}
}catch (IOException e) {
System.err.println("文件读取异常,请进行检查");
}
}
}