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

📄 wordanalyze.java

📁 一个小的编译器,可以模仿类似c语言的语法。很适合初级java学习者
💻 JAVA
字号:
class WordAnalyze
{
	//////program information////////
	public static char programBuffer[];
	public static int programLength;
	public static int programPointer;
	public static int currentPointer;
	
	//////string buffer///////////////
	public static String wordString[];
	public static int wordStringLength;
	public static int wordPointer;
	public static int lineOfWord[];
	public static int currentLine;
	
	
	public WordAnalyze()
	{
		//////give the room for program char////////
		programBuffer = new char[1000000];
		
		
		/////give the room for word string/////////
		wordString = new String[100000];
		lineOfWord = new int[100000];
		
	
	}
	
	public void init()
	{
		
		///////////get the content of editor
		programBuffer = CompileFrame.textArea.getText().toCharArray();
		programLength = CompileFrame.textArea.getText().length();
		programPointer = 0;
		currentPointer = 0;
		
		//////////////////
		wordStringLength = 0;
		wordPointer = 0;
		currentLine = 1;
		
		/////////parsing method////////////
		ParsingMethod.symbolTableLength=0;
		ParsingMethod.fileNum=0;
		ParsingMethod.successFlag = true;
		
		ParsingMethod.errorNum = 0;
		ParsingMethod.logicFlag = false;
		ParsingMethod.feiFlag = false;
		
		ParsingMethod.matchStackPointer=0;
		MiddleCodeFrame.textArea.setText("                           MIDDLE CODE"+String.valueOf('\n'));
	
	}
	
	
	///////////////////////////////////////////////////////////////////////	
	//////////////delete useless char////////////
	
	public void deleteUselessChar()
	{
		
		while((programPointer<programLength)
			&&(programBuffer[programPointer]==' '
			||programBuffer[programPointer]=='\t'
			||programBuffer[programPointer]=='\r'))
		{
			if(programBuffer[programPointer]=='\r')
			{
				currentLine++;
				programPointer++;	
			}
			programPointer++;
		}
	}
	
	
	////////////judge the stop sign/////////////////
	public boolean judgeStopSign()
	{
		if(currentPointer>=programLength
		||programBuffer[currentPointer]==' '
		||programBuffer[currentPointer]=='\t'
		||programBuffer[currentPointer]=='\r'
		||programBuffer[currentPointer]=='>'
		||programBuffer[currentPointer]=='<'
		||programBuffer[currentPointer]==';'
		||programBuffer[currentPointer]=='{'
		||programBuffer[currentPointer]=='}'
		||programBuffer[currentPointer]=='('
		||programBuffer[currentPointer]==')'
		||programBuffer[currentPointer]=='['
		||programBuffer[currentPointer]==']'
		||programBuffer[currentPointer]=='+'
		||programBuffer[currentPointer]=='-'
		||programBuffer[currentPointer]=='*'
		||programBuffer[currentPointer]=='/'
		||programBuffer[currentPointer]=='%'
		||programBuffer[currentPointer]==','
		||programBuffer[currentPointer]=='"'
		||programBuffer[currentPointer]=='!'
		||programBuffer[currentPointer]=='&'
		||programBuffer[currentPointer]=='|'
		||programBuffer[currentPointer]=='='
		||programBuffer[currentPointer]=='.'
		||programBuffer[currentPointer]=='#'
		||programBuffer[currentPointer]=='\''
		)
		{
			return true;
		}
		else
		{
			return false;
		}
		
	}
	
	
	
	//////////turn the textArea's content to strings///////////
	public void identifyWord()
	{
		currentPointer = programPointer;
		while(!judgeStopSign())
		{
			currentPointer++;
		}
		if(programPointer<programLength)
		{
			if(programPointer==currentPointer)
			{
				if((programPointer+1)<programLength
					&&programBuffer[programPointer]=='='
					&&programBuffer[programPointer+1]=='=')
					{
						programPointer+=2;
						lineOfWord[wordStringLength]=currentLine;
						wordString[wordStringLength++]="==";
						
					}
				else
				if((programPointer+1)<programLength
					&&programBuffer[programPointer]=='>'
					&&programBuffer[programPointer+1]=='=')
					{
						programPointer+=2;
						lineOfWord[wordStringLength]=currentLine;
						wordString[wordStringLength++]=">=";
					}
				else	
				if((programPointer+1)<programLength
					&&programBuffer[programPointer]=='<'
					&&programBuffer[programPointer+1]=='=')
					{
						programPointer+=2;
						lineOfWord[wordStringLength]=currentLine;
						wordString[wordStringLength++]="<=";
					}
				else 
				if((programPointer+1)<programLength
					&&programBuffer[programPointer]=='+'
					&&programBuffer[programPointer+1]=='+')
					{
						programPointer+=2;
						lineOfWord[wordStringLength]=currentLine;
						wordString[wordStringLength++]="++";
					}
				else 
				if((programPointer+1)<programLength
					&&programBuffer[programPointer]=='-'
					&&programBuffer[programPointer+1]=='-')
					{
						programPointer+=2;
						lineOfWord[wordStringLength]=currentLine;
						wordString[wordStringLength++]="--";
					}
				else 
				if((programPointer+1)<programLength
					&&programBuffer[programPointer]=='+'
					&&programBuffer[programPointer+1]=='=')
					{
						programPointer+=2;
						lineOfWord[wordStringLength]=currentLine;
						wordString[wordStringLength++]="+=";
					}
				else 
				if((programPointer+1)<programLength
					&&programBuffer[programPointer]=='-'
					&&programBuffer[programPointer+1]=='=')
					{
						programPointer+=2;
						lineOfWord[wordStringLength]=currentLine;
						wordString[wordStringLength++]="-=";
					}
				else 
				if((programPointer+1)<programLength
					&&programBuffer[programPointer]=='*'
					&&programBuffer[programPointer+1]=='=')
					{
						programPointer+=2;
						lineOfWord[wordStringLength]=currentLine;
						wordString[wordStringLength++]="*=";
					}
				else 
				if((programPointer+1)<programLength
					&&programBuffer[programPointer]=='/'
					&&programBuffer[programPointer+1]=='=')
					{
						programPointer+=2;
						lineOfWord[wordStringLength]=currentLine;
						wordString[wordStringLength++]="/=";
					}
				else
				if((programPointer+1)<programLength
					&&programBuffer[programPointer]=='&'
					&&programBuffer[programPointer+1]=='&')
					{
						programPointer+=2;
						lineOfWord[wordStringLength]=currentLine;
						wordString[wordStringLength++]="&&";
					}
				else
				if((programPointer+1)<programLength
					&&programBuffer[programPointer]=='|'
					&&programBuffer[programPointer]=='|')
					{
						programPointer+=2;
						lineOfWord[wordStringLength]=currentLine;
						wordString[wordStringLength++]="||";
					}
				else
				if((programPointer+1)<programLength
					&&programBuffer[programPointer]=='!'
					&&programBuffer[programPointer+1]=='=')
					{
						programPointer+=2;
						lineOfWord[wordStringLength]=currentLine;
						wordString[wordStringLength++]="!=";
					}
				else
				{
					lineOfWord[wordStringLength]=currentLine;
					wordString[wordStringLength++] = String.valueOf(programBuffer,programPointer,1); 
					programPointer++;
				}
			}
			else
			{
				lineOfWord[wordStringLength]=currentLine;
				wordString[wordStringLength++]=String.valueOf(programBuffer,programPointer,currentPointer-programPointer);
				programPointer = currentPointer;
			}
		}
	}
	
	//////////////////////////////////////////
	public void showWordArray()
	{
		for(int i=0;i<wordStringLength;i++)
		{
			CompileFrame.errorArea.append(wordString[i]+"   "+String.valueOf(lineOfWord[i])+String.valueOf('\n'));
		}
	}
	
	
	public void wordAnalyzeMethod()
	{
		init();
		
		while(programPointer<programLength)
		{
			deleteUselessChar();
			identifyWord();
		}
//		showWordArray();
		

	}		
}

⌨️ 快捷键说明

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