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

📄 meaning.java

📁 语法语义分析 java简单实现 初学JAVA所作…………
💻 JAVA
字号:


public class meaning {

	public keyword[] word=new keyword[100];
	int index=0;
	int err=1;
	
	
	public meaning(keyword[] word)
	{
		this.word=word;
		begin();
	}
	
	/**   
	 *   程序开始   
	 *   @param   void    
	 *   @return   void   
	 *   @throws   void
	 */
	private void begin(){
		match("main");
		match("(");
		match(")");
		Statement_Block();
		if(word[index].number==0&&err==1) System.out.println("语法分析成功");
		else System.out.println("语法分析失败");
	}
	
	/**   
	 *   处理语句块  
	 *   @return   void   
	 *   @throws   void
	 */
	private void Statement_Block() {
		match("{");
		Statement_Sequence();
		match("}");
		
	}

	/**   
	 *   处理语句串  
	 *   @return   void   
	 *   @throws   void
	 */
	private void Statement_Sequence() {
		Statement();
		while(word[index].number==27||word[index].number==2||word[index].number==4) {
		Statement();
		}
	}
		
	/**   
	 *   处理语句   
	 *   @return   void   
	 *   @throws   void
	 */
	private void Statement() {
		switch(word[index].number) {
		case 27:
			index++;
			match("=");
			Expression();
			match(";");
			break;
		case 2:
			index++;
			match("(");
			Condition();
			match(")");
			Statement_Block();
			break;
		case 4:
			index++;
			match("(");
			Condition();
			match(")");
			Statement_Block();
			break;
		
	}

	}

	/**   
	 *   处理判断式   
	 *   @return   void   
	 *   @throws   void
	 */
	private void Condition() {
		Expression();
		if(word[index].number<=25&&word[index].number>=21) {
			index++;
			Expression();
		}
		else error("关系运算符");
		
	}

	/**   
	 *   处理表达式   
	 *   @return   void   
	 *   @throws   void
	 */
	private void Expression() {
		Term();
		while(word[index].word.equals("+")||word[index].word.equals("-")) {
			index++;
			Term();
			
		}
		
	}

	/**   
	 *   处理条件   
	 *   @return   void   
	 *   @throws   void
	 */
	private void Term() {
		Factor();
		while(word[index].word.equals("*")||word[index].word.equals("/")) {
			index++;
			Factor();			
		}
		
	}

	/**   
	 *   返回单词类型  
	 *   @return   void   
	 *   @throws   void
	 */
	private void Factor() {
		if(word[index].number==27||word[index].number==26) index++;
		else {
			match("(");
			Expression();
			match(")");
			
		}
		
	}

	/**   
	 *   匹配单词  
	 *   @param   单词    
	 *   @return   void   
	 *   @throws   void
	 */
	private void match(String w) {
		if(word[index].word.equals(w))
		index++;
		else
			error(w);
	}

	/**   
	 *   出错处理  
	 *   @param   单词    
	 *   @return   void   
	 *   @throws   void
	 */
	private void error(String w) {
		System.out.println("第"+  index+"单词出错   "+w);
		err=0;//设置出错位
		
	}
	
	
}

⌨️ 快捷键说明

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