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

📄 codefile.java

📁 Java语言写的词法分析
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package compilation;

import java.io.*;

public class CodeFile {

	public char c;
	public StringBuffer strBuffer;
	public char[] strChar;
	/**
	 * @param args
	 */
		
	public static void main(String[] arge)
	{
		CodeFile file;
		file = new CodeFile("code.txt");
	}
	
	public CodeFile(String strTxt)       
	{
		//define the variable
		int num;
		String str;
		
		int lengthChar;
		
		try
		{
			//input the Data into the String
			File file = new File(strTxt);
			int fLength =(int) file.length();
			FileReader fReader = new FileReader(file);
			char[] data = new char[fLength];
			num = 0;
			while(fReader.ready())
		    {
		    	num += fReader.read(data, num, fLength - num);
		    }
		    fReader.close();
		    str = new String(data, 0,num);
		    
		    //input the data into char[]
		    strChar = str.toCharArray();
		    lengthChar = str.length();
		    //for (int i = 0; i < lengthChar; i ++)
		    //{
		    //	System.out.print(strChar[i]);
		    //}
		    
		    //define the new variable
		    char A, Z, a, z, underline, zero, nine, newline, dot;
		    //give the value
			A =         'A';
			Z =         'Z';
			a =         'a';
			z =         'z';
			underline = '_';
			zero =      '0';
			nine =      '9';
			newline =   '\n';
			dot =       '.';
			//define the Buffer
			StringBuffer strBuffer = new StringBuffer();
			
			for (int i = 0; i < lengthChar; i ++)
			{
				c = strChar[i];
			     //key and identifier will start with charactor
			     if (((c >= A) && (c <= Z)) || ((c >= a) && (c <= z))
			                                     || (c == underline)) 
			     {
			    	 strBuffer.delete(0, strBuffer.length());
			    	 while (((c >= A) && (c <= Z)) || ((c >= a) && (c <= z))
			    			 || (c == underline) || (c >= zero && c <= nine))
			    	 {
			    		 strBuffer.append(c);
			             c = strChar[++ i];
			    	 }
			      System.out.print(strBuffer.toString() + "\t\t"
			    		  + checkWord(strBuffer.toString()) + "\t\n");
			      i--;
			     } 
			     else 
			     {
			    	 // if current char is a number, it will be numbers
			    	 if ((c >= zero) && (c <= nine)) 
			    	 { 
			    		 strBuffer.delete(0, strBuffer.length());
			    		 while ((c >= zero) && (c <= nine)) 
			    		 {
			    			 strBuffer.append(c);
			                 c = strChar[++ i];
			    		 }
			    		 if (c == dot)             //decial
			    		 {
			    			 // the current char is dot,it will be float/double
			    			 strBuffer.append(c);
			                 c = strChar[++ i];
			                 while ((c >= zero) && (c <= nine)) 
			                 {
			                	 strBuffer.append(c);
			                	 c = strChar[++ i];
			                 }
			                 System.out.print(strBuffer.toString() + "\t\t"
			                		 + "float" + "\t\n");
			    		 }
			    		 else
			    		 {
			    			 System.out.print(strBuffer.toString() + "\t\t"
			                		 + "integer" + "\t\n");
			    		 }//end numbe
			    		 i--;
			    	 }
			    	 else
			    	 {
			    		 //special character '/'
			    		 if (c == '/') 
			    		 {
			    		      strBuffer.delete(0, strBuffer.length());
			    		      strBuffer.append(c);
			    		      c = strChar[++ i];
			    		      //this is a note mark
			    		      //the sentence behind does't appear
			    		      if (c == '/') 
			    		      {		    		          
			    		          c = strChar[++ i];
			    		          while (c != newline) 
			    		          {		    		           
			    		        	  c = strChar[++ i];
			    		          }
			    		          c = strChar[++ i];
			    		      }
			    		      else
			    		      {
			    		    	  if (c == '*')         // next *
				    		      {
			    		    		  c = strChar[++ i];
					    		      while (c != '/')
					    		      {
					    		         c = strChar[++ i];
					    		      }    
					    		      c = strChar[++ i];  
				    		      }
			    		    	  else
			    		    	  {
			    		    		  //this is a operator '/='
			    		    		  if (c == '=') 
			    		    		  { 
			    		    			  strBuffer.append(c);
				    		    	      System.out.print(strBuffer.toString()
				    		    	            + "\t\t" + "operator" + "\t\n");
				    		    	      c = strChar[++ i];
				    		    	   } 
				    		    	   else 
				    		    	  {
				    		    		  System.out.print(strBuffer.toString()
						    		    	    + "\t\t" + "operator" + "\t\n");
				    		    	  }					    		    		    		    	         
			    		    	  }
			    		      }			    		      
			    		 }
			    		 else
		    		     {
		    		    	  strBuffer.delete(0, strBuffer.length());
		    		    	  //the other operator
		    		    	  switch (c) 
		    		      	  {
		    		      	  case '+': strBuffer.append(c);
		    		             			c = strChar[++ i];
		    		             			if (c == '=') 
		    		             			{
		    		             				strBuffer.append(c);
		    		             				System.out.print(strBuffer.toString()
		    		                                      + "\t\t" + "operator" + "\t\n");
		    		             				c = strChar[++ i];
		    		             			} 
		    		             			else
		    		             			{
		    		             				if (c == '+')
		    		             				{
		    		             					strBuffer.append(c);
		    		             					System.out.print(strBuffer.toString()
		    		             							+ "\t\t" + "operator" + "\t\n");
		    		             					c = strChar[++ i];
		    		             				} 
		    		             				else 
		    		             				{
		    		             					System.out.print(strBuffer.toString() 
		    		             							+ "\t\t" + "operator" + "\t\n");
		    		             				}
		    		             			}
		    		             			i--;
		    		             			break;
		    		            case '-':  strBuffer.append(c);
		    		             			c = strChar[++ i];
		    		             			if (c == '=') 
		    		             			{
		    		             				strBuffer.append(c);
		    		             				System.out.print(strBuffer.toString()
		    		             						+ "\t\t" + "operator" + "\t\n");
		    		             				c = strChar[++ i];
		    		             			}
		    		             			else 
		    		             			{
		    		             				if (c == '-') {
		    		             					strBuffer.append(c);
		    		             					System.out.print(strBuffer.toString() 
		    		             							+ "\t\t" + "operator" + "\t\n");
		    		             					c = strChar[++ i];
		    		             				}
		    		             				else
		    		             				{
		    		             					System.out.print(strBuffer.toString() 
		    		             							+ "\t\t" + "operator" + "\t\n");
		    		             				}
		    		             			}
		    		             			i--;
		    		             			break;
		    		            case '*':
		    		            			strBuffer.append(c);
		    		            			c = strChar[++ i];
		    		            			if (c == '=')
		    		            			{
		    		            				strBuffer.append(c);
		    		            				System.out.print(strBuffer.toString()
		    		            						+ "\t\t" + "operator" + "\t\n");
		    		            				c = strChar[++ i];
		    		            			}
		    		            			else 
		    		            			{
		    		            				System.out.print(strBuffer.toString()
		    		            						+ "\t\t" + "operator" + "\t\n");
		    		            			}
		    		            			i--;
		    		            			break;
		    		            case '%':
		    		            			strBuffer.append(c);
		    		            			c = strChar[++ i];
		    		            			if (c == '=') 
		    		            			{
		    		            				strBuffer.append(c);
		    		            				System.out.print(strBuffer.toString()
		    		            						+ "\t\t" + "operator" + "\t\n");
		    		            				c = strChar[++ i];
		    		            			}
		    		            			else
		    		            			{
		    		            				System.out.print(strBuffer.toString()
		    		            						+ "\t\t" + "operator" + "\t\n");
		    		            			}
		    		            			i--;
		    		            			break;
		    		            case '>':
		    		            			strBuffer.append(c);
		    		            			c = strChar[++ i];
		    		            			if (c == '=')
		    		            			{
		    		            				strBuffer.append(c);
		    		            				System.out.print(strBuffer.toString()
		    		            						+ "\t\t" + "relation operator" + "\t\n");
		    		            				c = strChar[++ i];
		    		            			}
		    		            			else 
		    		            			{
		    		            				if (c == '>') 
		    		            				{
		    		            					strBuffer.append(c);
		    		            					System.out.print(strBuffer.toString()
		    		            							+ "\t\t" + "position operator" + "\t\n");
		    		            					c = strChar[++ i];
		    		            				}
		    		            				else 
		    		            				{
		    		            					System.out.print(strBuffer.toString()
		    		            							+ "\t\t" + "relation operator" + "\t\n");
		    		            				}
		    		            			}
		    		            			i--;
		    		            			break;
		    		            case '<':
		    		            			strBuffer.append(c);
		    		            			c = strChar[++ i];
		    		            			if (c == '=')

⌨️ 快捷键说明

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