?? test_compile.java
字號(hào):
package mypack.compile;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Test_Compile {
//保留字?jǐn)?shù)組
private String[] key = {"if", "else", "for", "while", "do", "return", "break", "contiune"};
private int i = 0; //用于控制下標(biāo)
//IO流讀取C源文件,并判斷第一個(gè)字符
public void read(){
BufferedInputStream bis = null;
StringBuilder sb = new StringBuilder();
int len;
byte[] b = new byte[128];
try {
bis = new BufferedInputStream(new FileInputStream("source.C"));
while((len = bis.read(b)) != -1){
sb.append(new String(b ,0, len)); //將讀出的字符串添加到一個(gè)StringBuilder中
}
System.out.println("C源程序?yàn)?");
System.out.println(sb);
System.out.println("#################################################");
System.out.println("此法分析后結(jié)果為:");
System.out.println("================");
char[] s = sb.toString().toCharArray();
for(i = 0; i < s.length; i++){
if(s[i] == ' ' || s[i] == '\n'){
//空格或換行直接跳過(guò),沒(méi)有處理
}else if(('A' <= s[i] && s[i] <= 'Z') || ('a' <= s[i] && s[i] <= 'z')){
zimu(s); //跳轉(zhuǎn)到處理字母的方法中
}else if('0' <= s[i] && s[i] <= '9'){
shuzi(s); //跳轉(zhuǎn)到處理數(shù)字的方法中
}else{
other(s); //跳轉(zhuǎn)到處理其他非數(shù)字和字母的方法中
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//處理為字母的字符方法
public void zimu(char[] c){
StringBuilder alphap = new StringBuilder();
while(isKey(c[i])){
alphap.append(c[i++]);
}
i--; //將下標(biāo)回退
int type = search(alphap.toString()); //判斷是否為保留字
System.out.println("(" + type + ", '" + alphap + "')");
}
//處理為數(shù)字的字符方法
public void shuzi(char[] c){
StringBuilder digit = new StringBuilder();
while(('0' <= c[i] && c[i] <= '9') || c[i] == '.'){
digit.append(c[i++]);
}
i--; //將下標(biāo)回退
System.out.println("(" + 3 + ", '" + digit + "')");
}
//處理非字母、數(shù)字的字符
public void other(char[] c){
StringBuilder other = new StringBuilder();
other.append(c[i]);
if(c[i] == '{' || c[i] == '}' || c[i] == '(' || c[i] == ')' || c[i] == ';' || //判斷分隔符
c[i] == ',' || c[i] == '#' || c[i] == '"' || c[i] == '.'){
System.out.println("(" + 5 + ", '" + c[i] + "')");
} else if(c[i] == '+' || c[i] == '*' || c[i] == '/' || c[i] == '%'){ //+ * / % 算數(shù)運(yùn)算符
System.out.println("(" + 4 + ", '" + c[i] + "')");
} else if(c[i] == '-'){ //判斷'-'是負(fù)號(hào)還是減號(hào)
char before = c[--i]; //取'-'的前一個(gè)字符
if('0' <= before && before <= '9'){ //如果前一個(gè)字符是數(shù)字則為減號(hào)
System.out.println("(" + 4 + ", '" + c[++i] + "')");
}else{
//前一個(gè)負(fù)號(hào)不是數(shù)字則為負(fù)號(hào),并且繼續(xù)判斷負(fù)號(hào)后面的數(shù)字
StringBuffer digit = new StringBuffer();
digit.append(c[++i]);
i++; //下標(biāo)加1
while(('0' <= c[i] && c[i] <= '9') || c[i] == '.'){ //判斷后繼字符是否為數(shù)字或小數(shù)點(diǎn)
digit.append(c[i++]);
}
i--; //下標(biāo)回退
System.out.println("(" + 3 + ", '" + digit + "')");
}
} else if(c[i] == '>' || c[i] == '=' || c[i] == '<' || c[i] == '!'){ //超前搜索法判斷> < = >= <= == !=
char next = c[++i]; //取后一個(gè)字符
if(next == '='){ //如果后一個(gè)字符為'=',則為>= <= == !=
other.append(next);
System.out.println("(" + 5 + ", '" + other + "')");
} else { //否則為< > =
System.out.println("(" + 5 + ", '" + c[--i] + "')");
}
}else if(c[i] == '&'){ //超前搜索法判斷'&'或者'&&'
char next = c[++i];
if(next == '&'){
other.append(next);
System.out.println("(" + 5 + ", '" + other + "')");
} else {
System.out.println("(" + 5 + ", '" + c[--i] + "')");
}
}else if(c[i] == '\\'){ //超前搜索法判斷'\'或者'\n'
char next = c[++i];
if(next == 'n'){
other.append(next);
System.out.println("(" + 5 + ", '" + other + "')");
} else {
System.out.println("(" + 5 + ", '" + c[--i] + "')");
}
}
}
//與保留字相匹配
public int search(String str){
for(int k = 0; k < key.length; k++){
if(str.equals(key[k])){
return 1; //如果是保留字,則返回種類1
}
}
return 2; //如果不是保留字,則為標(biāo)識(shí)符,返回種類2
}
//判斷該字符是否為字母或數(shù)字或下劃線
public boolean isKey(char c){
if(c == '-' || ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') || ('0' <= c && c <= '9')){
return true;
}else{
return false;
}
}
public static void main(String[] args) {
new Test_Compile().read();
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -