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

📄 prioritytable.java

📁 算术表达式的词法分析和语法分析,编译原理的实验
💻 JAVA
字号:
package Sentence;
import java.util.ArrayList;
public class PriorityTable extends GrammarTable{
	public PriorityTable(int row,int col){
		super(row,col);
		init();
		setValueAt("<","#","+");
		System.out.println("PT" + this.getValueAt("#","+"));
	}
	public void init(){
		finals = new String[7];
		finals[0] = "+";
		finals[1] = "*";
		finals[2] = "^";
		finals[3] = "i";
		finals[4] = "(";
		finals[5] = ")";
		finals[6] = "#";
		for(int i=0;i<finals.length;i++){
			setRowName((""+finals[i]),i+1);
			setColumnName((""+finals[i]),i+1);
		}
		setValueAt(">","+","+");
		setValueAt("<","+","*");
		setValueAt("<","+","^");
		setValueAt("<","+","i");
		setValueAt("<","+","(");
		setValueAt(">","+",")");
		setValueAt(">","+","#");
		
		setValueAt(">","*","+");
		setValueAt(">","*","*");
		setValueAt("<","*","^");
		setValueAt("<","*","i");
		setValueAt("<","*","(");
		setValueAt(">","*",")");
		setValueAt(">","*","#");
		
		setValueAt(">","^","+");
		setValueAt(">","^","*");
		setValueAt("<","^","^");
		setValueAt("<","^","i");
		setValueAt("<","^","(");
		setValueAt(">","^",")");
		setValueAt(">","^","#");
		
		setValueAt(">","i","+");
		setValueAt(">","i","*");
		setValueAt(">","i","^");
		setValueAt(">","i",")");
		setValueAt(">","i","#");
		
		setValueAt("<","(","+");
		setValueAt("<","(","*");
		setValueAt("<","(","^");
		setValueAt("<","(","i");
		setValueAt("<","(","(");
		setValueAt("=","(",")");
		
		setValueAt(">",")","+");
		setValueAt(">",")","*");
		setValueAt(">",")","^");
		setValueAt(">",")",")");
		setValueAt(">",")","#");
		
		setValueAt("<","#","+");
		setValueAt("<","#","*");
		setValueAt("<","#","^");
		setValueAt("<","#","i");
		setValueAt("<","#","(");
		setValueAt("=","#","#");
		
		rules = new Rule[5];
		Rule rule;
		rule = new Rule("E","E+E");
		String[] fin = {"+"};
		String[] unF = {"E","E"};
		rule.setFinals(fin);
		rule.setUnFinals(unF);
		rules[0] = rule;
		
		rule = new Rule("E","E*E");
		String[] fin1 = {"*"};
		String[] unF1 = {"E","E"};
		rule.setFinals(fin1);
		rule.setUnFinals(unF1);
		rules[1] = rule;
		
		rule = new Rule("E","E^E");
		String[] fin2 = {"^"};
		String[] unF2 = {"E","E"};
		rule.setFinals(fin2);
		rule.setUnFinals(unF2);
		rules[2] = rule;
		
		rule = new Rule("E","(E)");
		String[] fin3 = {"(",")"};
		String[] unF3 = {"E"};
		rule.setFinals(fin3);
		rule.setUnFinals(unF3);
		rules[3] = rule;
		
		rule = new Rule("E","i");
		String[] fin4 = {"i"};
		String[] unF4 = {};
		rule.setFinals(fin4);
		rule.setUnFinals(unF4);
		rules[4] = rule;
	}
	public String getValueAt(String rowName,String colName,String type){
		String item = (String)super.getValueAt(rowName,colName);
		return item;
	}
	public boolean reduce(ArrayList fin,ArrayList unF){
		//System.out.println("PT   " + "reduce");
		boolean succeed = false;
		String temp;
		int fence = fin.size()-1;
		temp = (String)fin.get(fence);
		String tempFin = "";
		while(temp.compareTo("<")!=0){
			if(temp.compareTo("=")!=0){
				tempFin = temp + tempFin;
			}
			fence--;
			temp = (String)fin.get(fence);
		}
		//System.out.println("PT + What is tempFin "+tempFin);
		for(int i=0;i<rules.length;i++){
			Rule rule;
			rule = rules[i];
			if(tempFin.length()==rule.getFinals().length){
				String tempUnF = "";
				String tempRuleFin = "";
				String tempRuleUnF = "";
				String[] ruleFin = rule.getFinals();
				String[] ruleUnF = rule.getUnFinals();
				for(int j=0;j<ruleFin.length;j++){
					tempRuleFin += ruleFin[j];
				}
				if(tempFin.compareTo(tempRuleFin)==0){
					for(int j=0;j<ruleUnF.length;j++){
						tempRuleUnF += ruleUnF[j];
					}
					for(int j=1;j<=ruleUnF.length;j++){
							tempUnF += unF.get(unF.size()-j);
						
					}
					if(tempUnF.compareTo(tempRuleUnF)==0){
						System.out.println("reduce   " + rule);
						//System.out.println(unF.size()+ " ? " + ruleUnF.length);
						int tempSize = unF.size();
						for(int j=1;j<=ruleUnF.length;j++){
							unF.remove(tempSize-j);
						}
						unF.add(rule.getAim());
						//System.out.println("PT" + fence);
						while(fin.size()>fence){
							fin.remove(fin.size()-1);
							//System.out.println("remove  "+(String)fin.remove(fin.size()-1));
						}
						succeed = true;
						break;
					}
				}
			}	
		}
		return succeed;
	}
}

⌨️ 快捷键说明

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