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

📄 priority.java

📁 算术表达式的词法分析和语法分析,编译原理的实验
💻 JAVA
字号:
package Sentence;
import java.util.ArrayList;
public class Priority{
	public Priority(PriorityTable tab){
		table = tab;
	}
	public Object analyzeSentence(String str){
		String sentence = str;
		int fence = 0; // the fence of sentence
		boolean succeed = true;
		String last = "" + sentence.charAt(fence); // a 为刚读入的输入符.
		ArrayList stackFin = new ArrayList();
		ArrayList stackUnF = new ArrayList();
		stackFin.add("#");
		stackFin.add("<");
		stackFin.add(last);
		fence++;
		String temp = "" + sentence.charAt(fence);
		while(true){
			//System.out.println("P-last " + last);
			if(last.compareTo("#")==0&&temp.compareTo("#")==0){
				break;
			}
			while(table.getValueAt(last,temp,"P").compareTo(">")!=0){
				stackFin.add(table.getValueAt(last,temp,"P"));
				stackFin.add(temp);
				last = temp;
				fence++;
				if(temp.compareTo("#")==0){
					for(int i=0;i<stackFin.size();i++){
						System.out.print("P last  " + stackFin.get(i));
					}
				}
				temp = "" + sentence.charAt(fence);
			}
			/*for(int i=0;i<stackFin.size();i++){
				System.out.print(stackFin.get(i));
			}
			System.out.println("");
			for(int i=0;i<stackUnF.size();i++){
				System.out.print(stackUnF.get(i));
			}*/
			//System.out.println("");
			//System.out.println("P before reduce ");
			succeed = table.reduce(stackFin,stackUnF);
			last = (String)stackFin.get(stackFin.size()-1);
			if(succeed==false){
				System.out.println("P " + "wrong");
				break;
			}
		}
		String result = "succeed";
		if(succeed==false/*检测非终结符*/){
			result = "false";
		}
		return result;
	}
	private PriorityTable table;
	
}

⌨️ 快捷键说明

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