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

📄 parser.java

📁 CMM编译器词法分析及语法分析代码
💻 JAVA
字号:
package try_sw;import java.util.regex.*;import java.io.*;import java.util.*;import   java.awt.Dimension;import  java.awt.Color;import  javax.swing.JFrame;import  javax.swing.JPanel;import  javax.swing.JScrollPane;import  javax.swing.JTree;import  javax.swing.BoxLayout;import  javax.swing.tree.TreePath;import  javax.swing.tree.DefaultMutableTreeNode;import  javax.swing.tree.DefaultTreeModel;class ParserError extends Exception{	private String msg;	public ParserError(String msg){		this.msg=msg;	}	public String toString(){		return "ParserError"+msg;	}}public class Parser {    	private Stack<String> mys = new Stack<String>(); //mys中存放分析栈		private boolean cut(String ana,String[] inp){//检查两栈的头是否相等[void,1]---value-void		//ana--analyse stack inp--inputstack		Pattern pn1= Pattern.compile("value-");		Pattern pn2 = Pattern.compile("style-");		Matcher m1= pn1.matcher(ana);		Matcher m2= pn2.matcher(ana);				if(m1.lookingAt()){//先查value:pass			ana = m1.replaceFirst("");											if(ana.equals(inp[0]))return true;													}		else if(m2.lookingAt()){			ana=m2.replaceFirst("");							if(ana.equals(inp[1]))return true;									}		else{						return false;		}				return false;					}		private String encode_val(String str){				return "value-"+str;			}		private String encode_typ(String str){		return "style-"+str;	}		private boolean ifnt(String str){//if it is an nt		Pattern pn1= Pattern.compile("value-");		Pattern pn2 = Pattern.compile("style-");		Matcher m1= pn1.matcher(str);		Matcher m2= pn2.matcher(str);		if(m1.lookingAt()||m2.lookingAt())return false;		else return true;	}		private boolean isfirstSibling(DefaultMutableTreeNode a){				DefaultMutableTreeNode tmp = a.getPreviousSibling();		if(tmp==null)return true;		else return false;			}		private DefaultMutableTreeNode getnow(DefaultMutableTreeNode nownode){				Pattern pn = Pattern.compile("value-");		Pattern pn1 = Pattern.compile("style-");				while(true){		if(nownode.getParent()==null)return nownode;		String test=(String)nownode.getUserObject();		//getUserObject有没错误?		if(pn.matcher(test).lookingAt()||pn1.matcher(test).lookingAt()||nownode.getChildCount()!=0				||test.equals("@NULL")){				if(isfirstSibling(nownode)){					nownode=(DefaultMutableTreeNode) nownode.getParent();				}else{		             nownode=nownode.getPreviousSibling();				}					             //若是总结符或不是首结点,则要去他前一个兄弟	             //若是非总结符,则去else return之		}		else{			//if(nownode.getUserObject().toString().equals("c"))System.out.println()			return nownode;		}//end else		}//end while							}				public DefaultMutableTreeNode Parse(LLhash llh,Mystack mysta){//树的建立		//llh里存放llh的规则,mysta里存放输入栈 mys存放分析站		//mysta.makei();		Mystack mystb=new Mystack();		while(!mysta.empty()){			String[] tmp = mysta.top();			mysta.pop();			mystb.push(tmp[0], Integer.parseInt(tmp[1]));		}//got a normal mystb		mys.push("value-#");		mys.push("Start");		DefaultMutableTreeNode nownode = new DefaultMutableTreeNode("Start",true);				DefaultMutableTreeNode remember = new DefaultMutableTreeNode("Start",true);		while(true){			try{			if(mys.isEmpty())break;						if(ifnt(mys.peek())){									   String[] top = mystb.top(); 									    String[] change={};				   if(llh.containsKey(mys.peek(), encode_val(top[0]))){					change=llh.get(mys.peek(), encode_val(top[0]));//1。查找				   }else if(llh.containsKey(mys.peek(), encode_typ(top[1]))){					change=llh.get(mys.peek(), encode_typ(top[1]));									   }				   //goal:parameter:ok,				    //       return:ok,															//DefaultMutableTreeNode r=new DefaultMutableTreeNode(mys.peek(),true);										mys.pop();					///////////////test					remember=nownode;					///////////////test				   DefaultMutableTreeNode tmp = new DefaultMutableTreeNode("c",true);					for(String c:change){//2.栈的转变						if(!c.equals("EPSILON")){mys.push(c);//空集						tmp= new DefaultMutableTreeNode(c,true);}						else{							tmp= new DefaultMutableTreeNode("@NULL",true);						}						nownode.add(tmp);					}//end for					nownode = getnow(tmp);//incorrect-->Start																				}else{//end if 									//nownode 的问题在cut之前就决定好了,所以这里不用处理nownode						if(cut(mys.peek(),mystb.top())){//3.两个部分的消去							mys.pop();							mystb.pop();							//mysta.pop();是错的因为有顺序问题						}else{							throw new ParserError(mys.peek());						}//end else											}//end else		}catch(ParserError pe){//end try			System.out.println(pe);		}		}//end while				//System.out.println(nownode.getChildCount());		return (DefaultMutableTreeNode)nownode.getRoot();	}//end Parser		//done	/**	 * @param args	 * @throws IOException 	 */	public static void main(String[] args) throws IOException {		// TODO Auto-generated method stub		String a ="abc";		for(String c:a.split("@"))		System.out.println(c);		//DefaultMutableTreeNode dn = new Parser().Parse(, mysta)			}}

⌨️ 快捷键说明

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