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

📄 cframe.java

📁 编译原理的课程设计,词法分析器,基于java jdk1.4做的,有源码和bat文件可直接运行
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JOptionPane;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.io.*;
import java.net.*;
import javax.swing.text.*;
import java.util.*;
public class Cframe implements ActionListener 
{   
   	public Frame frame;
	public Button sendtext;
	public Button sendfile;
	public TextArea text;
	public TextArea say;
    public String s1=null; 
    TextField address=null;
    boolean ed=false;//设定刚开始的源码没被重新编辑过
    Label lab=null;                      
    Button edit=null;
    Label labname=null;
    TextField name=null;
    JTable table=null;
    Hashtable hash=null;
    Hashtable var=null;
    Hashtable con=null;
    Hashtable fuhao=null;
    Hashtable hash1=null;
    Hashtable fh=null;
    String result[][];
    boolean bool;
    public	Cframe(String s)
	{   
	    bool=true;
		frame=new Frame(s);
		frame.setBounds(100,100,700,550);
		frame.setVisible(true);
		frame.setLayout(null);
		sendtext=new Button("分析");
		sendtext.setBounds(50,500,30,30);
		sendfile=new Button("打开文件");
		edit=new Button("编辑");
		edit.setBounds(220,500,50,30);
		frame.add(edit);
		edit.setVisible(true);
		sendfile.setBounds(150,500,50,30);
		text=new TextArea(10,10);
		text.setBounds(2,30,700,200);
		hash=new Hashtable();
		hash.put("abstract","abstract");
		hash.put("boolean","boolean");
		hash.put("break","break");
		hash.put("byte","byte");
		hash.put("case","case");
		hash.put("catch","catch");
		hash.put("char","char");
		hash.put("class","class");
		hash.put("continue","continue");
		hash.put("do","do");
		hash.put("double","double");
		hash.put("else","else");
		hash.put("extends","extends");
		hash.put("false","false");
		hash.put("find","find");
		hash.put("finally","finally");
		hash.put("final","final");
		hash.put("float","float");
		hash.put("for","for");
		hash.put("implements","implements");
		hash.put("import","import");
		hash.put("instanceof","instanceof");
		hash.put("int","int");
		hash.put("interface","interface");
		hash.put("long","long");
		hash.put("nativenew","nativenew");
		hash.put("null","null");
		hash.put("package","package");
		hash.put("private","private");
		hash.put("public","public");
		hash.put("return","return");
		hash.put("short","short");
		hash.put("static","static");
		hash.put("super","super");
		hash.put("switch","switch");
		hash.put("synchronized","synchronized");
		hash.put("this","this");
		hash.put("throw","throw");
		hash.put("true","true");
		hash.put("try","try");
		hash.put("void","void");
		hash.put("while","while");
		var=new Hashtable();
		con=new Hashtable();
		fuhao=new Hashtable();
		fuhao.put(";",";");
		fuhao.put(":",":");
		fuhao.put("'","'");
		fuhao.put("\"","\"");
		fuhao.put("[","[");
		fuhao.put("]","]");
		fuhao.put("(","(");
		fuhao.put(")",")");
		fuhao.put(".",".");
		fuhao.put("++","++");
		fuhao.put("--","--");
		fuhao.put("&","&");
		fuhao.put("+","+");
		fuhao.put("-","-");
		fuhao.put("~","~");
		fuhao.put("!","!");
		fuhao.put("|","|");
		fuhao.put("^","^");
		fuhao.put("","");
		fuhao.put("*","*");
		fuhao.put("/","/");
		fuhao.put("%","%");
		fuhao.put("<","<");
		fuhao.put(">",">");
		fuhao.put("<=","<=");
		fuhao.put(">=",">=");
		fuhao.put("=","=");
		fuhao.put("==","==");
		fuhao.put("!=","!=");
		fuhao.put("&&","&&");
		fuhao.put("||","||");
		fuhao.put("?","?");
		fuhao.put("*=","*=");
		fuhao.put("/=","/=");
		fuhao.put("%=","%=");
		fuhao.put("+=","+=");
		fuhao.put("-=","-=");
		fuhao.put("!=","!=");
		fuhao.put("&=","&=");
		fuhao.put("^=","^=");
		fuhao.put("|=","|=");
		fuhao.put(",",",");
		fuhao.put("?","?");
		fh=new Hashtable();
		hash1=new Hashtable();
		say=new TextArea(10,10);
		say.setBounds(1,271,700,200);
		frame.add(sendtext);
		sendtext.setVisible(true);
		frame.add(sendfile);
		
		sendfile.setVisible(true);
		
		text.setEditable(false);
	
		frame.add(text);
		text.setVisible(true);
		frame.add(say);
		say.setVisible(true);
		say.setEditable(false);
		
		
		frame.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
			});
			
			sendtext.addActionListener(this);
			sendfile.addActionListener(this);
			edit.addActionListener(this);
			
	}
	public void actionPerformed(ActionEvent e)
	{  
		if(e.getSource()==sendfile)
		{    
			 if(!bool){

		       frame.remove(table);
		        frame.add(say);
			   }
			   text.setText("");
			try{
				 JFileChooser filechooser=new JFileChooser();
			    int state=filechooser.showOpenDialog(null);
		         File file=filechooser.getSelectedFile();
		         RandomAccessFile in=new RandomAccessFile(file,"rw");
		         long filepoint=0;
		         long filelength=in.length();
		         String s="";
		         boolean bo=false;//当以/*为开头时为false;
	         	while(filepoint<filelength)
	         	{
	         		s=in.readLine();
	         		if(s.indexOf("/*")!=-1)
	         		{
	         			bo=true;
	         		}
	         		if(!bo)
	         		{
	         			if(s.indexOf("//")==-1)
	         			text.append(s+"\n");
	         		}
	         		if(s.indexOf("*/")!=-1)
	         		{
	         			bo=false;
	         		}
	         		
	         		say.append(s+"\n");
	         		filepoint=in.getFilePointer();
	         		
	         	}
	         	in.close();
	         	
		      }
		      catch(Exception e1)
		      {
		      	System.out.println(""+e1);
		      }
	     }
	     if(e.getSource()==sendtext)
	     {  
	        
	        bool=false;
	     	String s="";//say.getText();
	     	StringTokenizer fenxi=null;
	     	if(ed)
	     	{   text.setText("");
	     	  	fenxi=new StringTokenizer(say.getText(),"\n");
	     	  	boolean boo=false;//当以/*为开头时为false;
	     		while(fenxi.hasMoreTokens())
	     		{
	     			String s2=fenxi.nextToken();
	     			 
	         	  
	         	  
	         	  	if(s2.indexOf("/*")!=-1)
	         		{
	         			boo=true;
	         		}
	         		if(!boo)
	         		{
	         			if(s2.indexOf("//")==-1)
	         		   {	
	         		      text.append(s2+"\n");
	         			
	         			}
	         		}
	         		if(s2.indexOf("*/")!=-1)
	         		{
	         			boo=false;
	         		}
	         		
	         	  
	     		}
	     		//text.append("陈繁荣");
	     	}
	     	hash1.clear();
	     	fh.clear();
	     	con.clear();
	        frame.remove(say);
	        s=text.getText();
	        fenxi=new StringTokenizer(s,"\n");
	        String temp="";
	        int number=fenxi.countTokens();
	        while(fenxi.hasMoreTokens())
	        {
	        	temp=fenxi.nextToken();
	        	//System.out.println(temp);
	        	if(temp.indexOf("=")!=-1)
	        	{
	        		int i1=temp.indexOf("=");
	        		if(temp.charAt(i1+1)=='=')
	        		{
	        			if(fh.get("==")==null)
	        			fh.put("==","==");
	        		}
	        		else 
	        		{
	        			switch(temp.charAt(i1-1))
	        			{
	        				case '+':
	        				  if(fh.get("+=")==null)
	        				  {
	        				  	fh.put("+=","+=");
	        				  }
	        				  break;
	        				case '-':
	        				if(fh.get("-=")==null)
	        				fh.put("-=","-=");
	        				break;
	        				case '*':
	        				if(fh.get("*=")==null)
	        				{
	        					fh.put("-=","-=");
	        				}
	        				break;
	        				case '/':
	        				if(fh.get("/=")==null)
	        				fh.put("/=","/=");
	        				break;
	        				case '%':
	        				if(fh.get("%=")==null)
	        				fh.put("%=","%=");
	        				break;
	        				case '&':
	        				if(fh.get("&=")==null)
	        				fh.put("&=","&=");
	        				break;
	        				case '|':
	        				if(fh.get("|=")==null)
	        				fh.put("|=","|=");
	        				break;
	        				case '!':
	        				if(fh.get("!=")==null)
	        				fh.put("!=","!=");
	        				break;
	        				case '~':
	        				if(fh.get("~=")==null)
	        				fh.put("~=","~=");
	        				break;
	        				case '<':
	        				if(fh.get("<=")==null)
	        				fh.put("<=","<=");
	        				break;
	        				case '>':
	        				if(fh.get(">=")==null)
	        				fh.put(">=",">=");
	        				break;
	        				default :
	        				if(fh.get("=")==null)
	        				fh.put("=","=");
	        			}
	        		}
	        		
	        	}
	        	if(temp.indexOf("<")!=-1)
	        	{
	        		if(temp.charAt(temp.indexOf("<")+1)=='=')
	        		{
	        			if(fh.get("<=")==null)
	        			{
	        				fh.put("<=","<=");
	        			}
	        		}
	        		else if(fh.get("<")==null)
	        		fh.put("<","<");
	        		
	        	}
	        	if(temp.indexOf(">")!=-1)
	        	{
	        			if(temp.charAt(temp.indexOf(">")+1)=='=')
	        		{
	        			if(fh.get(">=")==null)
	        			{
	        				fh.put(">=",">=");
	        			}
	        		}
	        		else if(fh.get(">")==null)
	        		fh.put(">",">");
	        		
	        	}
	        	if(temp.indexOf("+")!=-1)
	        	{
	        		if(temp.charAt(temp.indexOf("+")+1)=='+')
	        		{
	        			if(fh.get("++")==null)
	        			{
	        				fh.put("++","++");
	        			}
	        		}
	        		else
	        		{
	        	    	if(temp.charAt(temp.indexOf("+")+1)=='=')
	        	     	{
	        		    	if(fh.get("+=")==null)
	        		    	{
	        			    	fh.put("+=","+=");
	        		    	}
	        	     	}
	        		else if(fh.get("+")==null)
	        			{
	        				fh.put("+","+");
	        			}
	        		}
	        		
	        	}
	        	if(temp.indexOf("-")!=-1)
	        	{
	        		if(temp.charAt(temp.indexOf("-")+1)=='-')
	        		{
	        			if(fh.get("--")==null)
	        			{
	        				fh.put("--","--");
	        			}
	        		}
	        		else if(temp.charAt(temp.indexOf("-")+1)=='=')
	        		{
	        			if(fh.get("-=")==null)
	        			{
	        				fh.put("-=","-=");
	        			}
	        		}
	        		else
	        		{
	        			if(fh.get("-")==null)
	        			{
	        				fh.put("-","-");
	        			}
	        		}
	        		
	        	}
	        	if(temp.indexOf("*")!=-1)
	        	{
	        		if(temp.charAt(temp.indexOf("*")+1)=='=')
	        		{
	        			if(fh.get("*=")==null)
	        			{
	        				fh.put("*=","*=");
	        			}
	        		}
	        		else if(fh.get("*")==null)
	        		fh.put("*","*");
	        		
	        	}
	        	if(temp.indexOf("/")!=-1)
	        	{
	        			if(temp.charAt(temp.indexOf("/")+1)=='=')
	        		{
	        			if(fh.get("/=")==null)
	        			{
	        				fh.put("/=","/=");
	        			}
	        		}
	        		else if(fh.get("/")==null)
	        		fh.put("/","/");
	        		
	        	}
	        	if(temp.indexOf("%")!=-1)
	        	{
	        			if(temp.charAt(temp.indexOf("%")+1)=='=')
	        		{
	        			if(fh.get("%=")==null)
	        			{
	        				fh.put("%=","%=");
	        			}
	        		}
	        		else if(fh.get("%")==null)
	        		fh.put("%","%");
	        		
	        	}
	        	if(temp.indexOf("(")!=-1)
	        	{
	        		if(fh.get("(")==null)
	        		fh.put("(","(");
	        		
	        	}
	        	if(temp.indexOf(")")!=-1)
	        	{
	        		if(fh.get(")")==null)
	        		fh.put(")",")");
	        		
	        	}
	        	if(temp.indexOf("{")!=-1)
	        	{
	        		if(fh.get("{")==null)
	        		fh.put("{","{");
	        		
	        	}
	        	if(temp.indexOf("}")!=-1)
	        	{
	        		if(fh.get("}")==null)
	        		fh.put("}","}");
	        		
	        	}
	        	if(temp.indexOf("&")!=-1)
	        	{
	        		if(temp.charAt(temp.indexOf("&")+1)=='&')
	        		{
	        			if(fh.get("&&")==null)
	        			{
	        				fh.put("&&","&&");
	        			}
	        		}
	        		else
	        		{
	        			if(temp.charAt(temp.indexOf("&")+1)=='=')
	        		     {
	        		    	if(fh.get("&=")==null)
	        		     	{
	        			    	fh.put("&=","&=");
	        		    	}
	        	        }
	        		else
	        			if(fh.get("&")==null)
	        			{
	        				fh.put("&","&");
	        			}
	        		}
	        	}
	        	if(temp.indexOf("|")!=-1)
	        	{   
	        	    if(temp.charAt(temp.indexOf("|")+1)=='|')
	        	    {
	        	    	if(fh.get("||")==null)
	        	    	fh.put("||","||");
	        	    	
	        	    }
	        	    else{
	        	    	if(temp.charAt(temp.indexOf("|")+1)=='=')
	        		     {
	        			     if(fh.get("|=")==null)
	        			     {
	        			    	fh.put("|=","|=");
	        			       }

⌨️ 快捷键说明

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