⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 test_compile.java

📁 编译技术词法分析算法。对一个C语言源程序进行词法分析
💻 JAVA
字号:
package mypack.compile;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class Test_Compile {
	
	//保留字数组
	private String[] key = {"if", "else", "for", "while", "do", "return", "break", "contiune"};
	
	private int i = 0;  //用于控制下标
	
	//IO流读取C源文件,并判断第一个字符
	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));     //将读出的字符串添加到一个StringBuilder中
			}
			System.out.println("C源程序为:");
			System.out.println(sb);
			System.out.println("#################################################");
			System.out.println("此法分析后结果为:");
			System.out.println("================");
			char[] s = sb.toString().toCharArray();
			for(i = 0; i < s.length; i++){
				if(s[i] == ' ' || s[i] == '\n'){
					//空格或换行直接跳过,没有处理
				}else if(('A' <= s[i] && s[i] <= 'Z') || ('a' <= s[i] && s[i] <= 'z')){
					 zimu(s);          //跳转到处理字母的方法中
				}else if('0' <= s[i] && s[i] <= '9'){
					shuzi(s);          //跳转到处理数字的方法中
				}else{
					other(s);          //跳转到处理其他非数字和字母的方法中
				}
			}
		} 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--;   //将下标回退
		int type = search(alphap.toString()); //判断是否为保留字
		System.out.println("(" + type + ", '" + alphap + "')");
	}
	
	//处理为数字的字符方法
	public void shuzi(char[] c){
		StringBuilder digit = new StringBuilder();
		while(('0' <= c[i] && c[i] <= '9') || c[i] == '.'){
			digit.append(c[i++]);
		}
		i--;  //将下标回退
		System.out.println("(" + 3 + ", '" + digit + "')");
	}

	//处理非字母、数字的字符
	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] == '%'){   //+ * / % 算数运算符
			System.out.println("(" + 4 + ", '" + c[i] + "')");
		} else if(c[i] == '-'){                 //判断'-'是负号还是减号
			char before = c[--i];         //取'-'的前一个字符
			if('0' <= before && before <= '9'){   //如果前一个字符是数字则为减号
				System.out.println("(" + 4 + ", '" + c[++i] + "')");
			}else{          
				//前一个负号不是数字则为负号,并且继续判断负号后面的数字
				StringBuffer digit = new StringBuffer();
				digit.append(c[++i]);
				i++;  //下标加1
				while(('0' <= c[i] && c[i] <= '9') || c[i] == '.'){  //判断后继字符是否为数字或小数点
					digit.append(c[i++]);
				}
				i--;  //下标回退
				System.out.println("(" + 3 + ", '" + digit + "')");
			}
		} else if(c[i] == '>' || c[i] == '=' || c[i] == '<' || 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] == '&'){     //超前搜索法判断'&'或者'&&'
			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; //如果不是保留字,则为标识符,返回种类2
	}
	
	//判断该字符是否为字母或数字或下划线
	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();
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -