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

📄 analyze.java

📁 语法语义分析 java简单实现 初学JAVA所作…………
💻 JAVA
字号:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;


public class analyze{

	/**
	 * @param args
	 */
	//public static StringBuffer cs=new StringBuffer();
	public static char [] cs=new char[1000];
	public static char ch;
	public static String token="";
	public static int leng=0,n=0;
	public static keyword [] key=new keyword[13];
	public static keyword [] analyzeword=new keyword[100];
	
	public static void main(String[] args) throws IOException{

		analyze test=new analyze();
		test.begine();
		int i=0;
		while(n<leng)
		{
			analyzeword[i]=scaner();
			System.out.println(analyzeword[i]);
			i++;
			token="";
		}
		meaning t=new meaning(analyzeword);
		
	}

	public void begine(){										//关键字初始化
		key[0]=new keyword("main",1);
		key[1]=new keyword("if",2);
		key[2]=new keyword("else",3);
		key[3]=new keyword("while",4);
		key[4]=new keyword("do",5);
		key[5]=new keyword("for",6);
		key[6]=new keyword("int",7);
		key[7]=new keyword("long",8);
		key[8]=new keyword("char",9);
		key[9]=new keyword("void",10);
		key[10]=new keyword("include",11);
		key[11]=new keyword("return",12);

	}
	
	public analyze() throws IOException{
		File input=new File("in.txt");	
		FileInputStream in=new FileInputStream(input);	
		int c;
		while((c=in.read())!=-1)
		{
			cs[leng]=(char)c;
			leng++;
		}

		in.close();
		
	}
	

	
	//取字符,每次使用均需检查n与leng的关系
	public  static char getch() 
{
		if(n<leng)
		{			
			ch=cs[n];
			n++;
			
		}

		else 	ch='$';
		return ch;
	
	}
	
	
	//去除空格回车
	public static char getbc() {
		getch();
		while((ch==' ')||(ch=='\n')||(ch=='\r'))
			getch();
			return ch;

	}
	
	
	//连接单词
	public static void concat() {
		token=token+ch;
	}
	
	//判断字符类型是字母
	public static boolean letter() {
	
	if((ch>='a'&&ch<='z') || (ch>='A'&&ch<='Z'))
		return true;
	else return false;
}

	
	//判断字符类型是数字
public static boolean digit(){
	if(ch>='0'&& ch<='9')
		return true;
	else return false;
}



//查找关键字
 public static int reserve() {
	 int i=0;
	 for(;i<12;i++)
	 {
		 if(token.equalsIgnoreCase(key[i].word))
		 {
			 return i;
		 }
		
	 }
	 return 13; 
	 
 }
 
	//字符串转换二进制字符串
 public static String dtb() {
	 int mul=1,total=0,i=token.length()-1;
	 String d="";
	do
	{
		total=total+(token.charAt(i)-'0')*mul;
		mul=mul*10;
		i--;
	}while(i>=0);
	while(total!=0) {
		d=(total%2)+d;
		total=total/2;		
	}
	return d;
 };
 
 //字符回退
 public static void retract() {
	 n--;
 }
 
 public static keyword scaner() {
	 
	 String c="";
	 int num=0;
	 getbc();
	 
	 if(ch=='$')//完结
	 {
		 //return new keyword("成功",0);
		 c="成功";
		 num=0;
	 }
	 
	 //字符开始
	 else if(letter()) {
		 concat();
		 getch();
		 while (letter()||digit()) {
			 concat();
			 getch();
		 }
		 
		 retract();//回退
		 if(reserve()==13) {c=token;num=27;}// return new keyword(token,27);//返回关键字
		 else {c=key[reserve()].word;num=key[reserve()].number;}//return key[reserve()];//返回标识符
		 
	 }
	 
	 //数字
	 else if(digit())
	 {
		 concat();
		 getch();
		 while(digit()) {
			 concat();
			 getch();
		 }
		 retract();
		 {c=dtb();num=26;}//返回常数
	 }
	 else
		 switch(ch)
		 {
		 case'+':getch();
		 if(ch=='+') {c="++";num=14;}
		 else 
		 {
			 retract(); 
			 c="+";
			 num=13;
			 }
		 break;
		 case'-':getch();if(ch=='-') {c="--";num=16;}else {retract(); c="-";num=15;} break;
		 case'*':c="*";num=17;break;
		 case'/':getch();
		 	if(ch=='/') 
		 	{
		 		while(ch!='$'&&ch!='\n'&&ch!='\r'&&ch!=' ') 
		 		{
		 			getch();

		 		}
		 		if(ch=='$') 
		 		{
		 			retract();
		 			c="注释";
		 			num=19;
		 		}
		 		else
		 		{c="注释";num=19;}
		 	}
		 	break;
		 case'=':getch();if(ch=='='){c="==";num=21;}else {retract();c="=";num=20;} break;
		 case'>':getch();if(ch=='='){c=">=";num=23;}else {retract();c=">";num=22;} break;
		 case'<':getch();if(ch=='=') {c="<=";num=25;}else {retract();c="<";num=24;} break;
		 case';':c=";";num=28;break;
		 case'(':c="(";num=29;break;
		 case')':c=")";num=30;break;
		 case'#':c="#";num=31;break;
		 case'{':c="{";num=32;break;
		 case'}':c="}";num=33;break;
		 default:c="出错";num=100;
		 }
	 return new keyword(c,num);
 }
 
}

⌨️ 快捷键说明

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