Java 实现词法分析器(编译原理)

不多说,直接贴代码

package edu.software.compile;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * 1 基本字
 * 2 标识符 
 * 3 常数  
 * 4 算符
 * 5 界符
 */

public class Compile2 {
	
	public int ch;
	public int code;//保留字状态码
	
	public StringBuffer strToken = new StringBuffer();//存放构成单词符号的字符串
	
	public String [] retainWord = new String[]{"int","if","else","return","main","void","while","break"};//保留字
	
	//判断是否是字母
	public boolean IsLetter(){
		if((ch>=65 && ch <= 90) || (ch >= 97 && ch <=122)){
			return true;
		}
		return false;
	}
	
	//判断是否是数字
	public boolean IsDigit(){
		if(ch>=48 && ch <= 57){
			return true;
		}
		return false;
	}
	
	//判断是否是空格
	public boolean IsBC(int ch){
		if(ch == 32){
			return true;
		}
		return false;
	}
	
	//连接字符
	public void Concat(char ch){
		strToken.append(ch);
	}
	
	//判断是否是保留字
	public int Reserve(){
		for(int i = 0;i < retainWord.length;i++){
			if(strToken.toString().equals(retainWord[i])){
				return 1;
			}
		}
		if(strToken.length() != 0){
			if(strToken.charAt(0)>='0' && strToken.charAt(0)<='9'){
				return 3;
			}
		}
		
		return 2;
	}
	
	//
	public void Retract(){
		code = Reserve();
		if(code == 1){
			System.out.println("('"+1+"','"+strToken+"')");
		}else if(code == 2){
			System.out.println("('"+2+"','"+strToken+"')");
		}
		else if(code == 3){
			System.out.println("('"+3+"','"+strToken+"')");
		}
		strToken.delete(0, strToken.length());
	}
	
	/**
	 * 读取文件
	 */
	public void scanner(){
		BufferedReader br;
		try {
			br = new BufferedReader(new FileReader("tests.txt"));
			while((ch = br.read()) != -1){
				//System.out.println("======="+(char)ch);
				if(IsBC(ch) == false){
					if(IsLetter()){
						if(IsLetter() == true || IsDigit() == true){
							Concat((char) ch);
						}
					}else if(IsDigit() == true){
						Concat((char)ch);
					}else if(IsDigit()){
						Concat((char) ch);
					}else if(ch == 61){
						if((strToken.length() != 0 )&& (strToken.charAt(0) == '=')){
							strToken.append((char)ch);
							System.out.println("('"+4+"','"+strToken+"')");
							strToken.delete(0, strToken.length());
						}else{
							strToken.append((char)ch);
						}
					}else if(ch == 43){
						Retract();
						System.out.println("('"+4+"','"+(char) ch+"')");
					}else if(ch == 45){
						Retract();
						System.out.println("('"+4+"','"+(char) ch+"')");
					}else if(ch == 42){
						Retract();
						System.out.println("('"+4+"','"+(char) ch+"')");
					}else if(ch == 47){
						Retract();
						System.out.println("('"+4+"','"+(char) ch+"')");
					}else if((char) ch == ';'){
						Retract();
						System.out.println("('"+5+"','"+(char) ch+"')");
					}else if((char) ch == '('){
						Retract();
						System.out.println("('"+5+"','"+(char) ch+"')");
					}else if((char) ch == ')'){
						Retract();
						System.out.println("('"+5+"','"+(char) ch+"')");
					}else if((char) ch == '{'){
						Retract();
						System.out.println("('"+5+"','"+(char) ch+"')");
					}else if((char) ch == '}'){
						Retract();
						System.out.println("('"+5+"','"+(char) ch+"')");
					}else if((char) ch == ','){
						Retract();
						System.out.println("('"+5+"','"+(char) ch+"')");
					}
					
				}else{
					Retract();
				}
				
			}
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		Compile2 compile2 = new Compile2();
		compile2.scanner();
	}

}

测试文件1

int main(int x){
int a = 10;
int b = 2;
int c = 0;
int d1 ;
if(c == 0){
c = a + b;
}
return c;
}

输出1

('1','int')
('1','main')
('5','(')
('1','int')
('2','x')
('5',')')
('2','')
('5','{')
('1','int')
('2','a')
('2','=')
('3','10')
('5',';')
('1','int')
('2','b')
('2','=')
('3','2')
('5',';')
('1','int')
('2','c')
('2','=')
('3','0')
('5',';')
('1','int')
('2','d1')
('2','')
('5',';')
('1','if')
('5','(')
('2','c')
('4','==')
('2','')
('3','0')
('5',')')
('2','')
('5','{')
('2','c')
('2','=')
('2','a')
('2','')
('4','+')
('2','')
('2','b')
('5',';')
('2','')
('5','}')
('1','return')
('2','c')
('5',';')
('2','')
('5','}')

测试文件2

void ss(){
while(1){
break;
}
}

输出2

('1','void')
('2','ss')
('5','(')
('2','')
('5',')')
('2','')
('5','{')
('1','while')
('5','(')
('3','1')
('5',')')
('2','')
('5','{')
('1','break')
('5',';')
('2','')
('5','}')
('2','')
('5','}')

  • 8
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值