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

📄 jcomplier.java

📁 编译原理
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
		}
		int n=0;																//n:记标志符的个数(建符号表用)
		for(int j=0;j<dancixulie.length;j++)
		{
			if(p.isguanjianzi(dancixulie[j]))
				neirong[j][1]="关键字!";
			else if(p.isbiaozhifu(dancixulie[j]))
			{
				neirong[j][1]="标志符!";
				n++;
			}
			else if(p.isyunsuanfu(dancixulie[j]))
				neirong[j][1]="运算符!";
			else if(p.iszifuchuan(dancixulie[j]))
				neirong[j][1]="字符串!";
			else if(p.iszifu(dancixulie[j]))
				neirong[j][1]="字符!";	
			else if(p.isshuzi(dancixulie[j]))
				neirong[j][1]="数字!";	
			else if(p.isfengefu(dancixulie[j]))
				neirong[j][1]="分隔符!";
			else
				neirong[j][1]="其他";
		}
		JTable table=new JTable(neirong,biaoti);								//主表
		table.setEnabled(false);
		
		String[] biaoti1={"标志符","属性"};										
		String[][] neirong1=new String[n][2];
		ArrayList al=new ArrayList();											//标志符存在al中
		for(int j=0;j<dancixulie.length;j++)
		{
			 if(p.isbiaozhifu(dancixulie[j])&&!p.isguanjianzi(dancixulie[j]))
				 al.add(dancixulie[j]);
			 else
				 continue;
		}
		for(int i=0;i<n;i++)
		{
			neirong1[i][0]=al.get(i).toString();
			neirong1[i][1]="";
		}
		JTable table1=new JTable(neirong1,biaoti1);								//标志符表
		table1.setEnabled(false);
		
		JPanel pa=new JPanel();
			pa.setLayout(new GridLayout(1,2));
			pa.add(new JScrollPane(table));
			pa.add(new JScrollPane(table1));
		this.remove(area2);
		this.remove(s2);
		this.remove(l2);
		this.remove(p2);
		//this.remove(table);
		this.add(pa,BorderLayout.SOUTH);
		validate();
	}
	
	public void actionPerformed(ActionEvent e) 								//事件处理
	{
		
		if(e.getSource()==menuitem[0])											//新建
			area1.setText(null);
		if(e.getSource()==menuitem[1])											//打开
		{
			area1.setText(null);
			int n=fileChooser.showOpenDialog(getContentPane());
			if(n==JFileChooser.APPROVE_OPTION)
			{
				File file=fileChooser.getSelectedFile();
				try{
					FileReader readfile=new FileReader(file);
					BufferedReader in=new BufferedReader(readfile);
					String s=null;
					while((s=in.readLine())!=null)
					{
						area1.append(s+"\n");
					}
				}
				catch(IOException ee)
				{
					area2.setText("错误:你没有选择文件!");
				}
				/*try{
					FileWriter outputFile=new FileWriter(file,true);
					PrintWriter output=new PrintWriter(outputFile);
				}
				catch(IOException ee)
				{
					area2.setText("不能打开文件!");
				}*/
				menuitem[2].setEnabled(true);
			}
		}
		if(e.getSource()==menuitem[2])
		{
			
		}
		if(e.getSource()==menuitem[3])
		{
			
		}
		if(e.getSource()==menuitem[4])
			System.exit(0);
		if(e.getSource()==menuitem[5])
			area1.cut();
		if(e.getSource()==menuitem[6])
			area1.copy();
		if(e.getSource()==menuitem[7])
			area1.paste();
		if(e.getSource()==menuitem[8])
		{
			
		}
		if(e.getSource()==menuitem[9])
			area1.selectAll();
		if(e.getSource()==menuitem[10])
			area1.setLineWrap(true);
		if(e.getSource()==menuitem[11])
		{
			cifafenxi();
		}
		if(e.getSource()==menuitem[12])
		{
			
		}
		if(e.getSource()==menuitem[13])
		{
			
		}
		if(e.getSource()==menuitem[14])
		{
			
		}
		if(e.getSource()==menuitem[15])
		{
			JOptionPane.showMessageDialog(this,"作者:\n黑龙江大学软件工程专业\n王利航(20054478)\n制作日期:2007.4","作者信息~",JOptionPane.INFORMATION_MESSAGE);
		}
		if(size[0].isSelected())													//处理关于字号的事件
		{
			area1Font=new Font(area1.getName(),area1Font.getStyle(),12);
			area1.setFont(area1Font);
		}
		if(size[1].isSelected())
		{
			area1Font=new Font(area1.getName(),area1Font.getStyle(),16);
			area1.setFont(area1Font);
		}
		if(size[2].isSelected())
		{
			area1Font=new Font(area1.getName(),area1Font.getStyle(),20);
			area1.setFont(area1Font);
		}
	}
	
	public void itemStateChanged(ItemEvent e)
	{
		int sty=Font.PLAIN;
		if(style[0].isSelected())
			sty+=Font.BOLD;															//粗体
		if(style[1].isSelected())
			sty+=Font.ITALIC;														//斜体
		
		area1Font=new Font(area1Font.getName(),sty,area1Font.getSize());
		area1.setFont(area1Font);
		
	}
}

class panduan																	//判断类
{
	public boolean isguanjianzi(String s)
	{
		String[] guanjianzi={"abstract","continue","for","new","switch","assert","default","goto","package","syschronized","boolean","do","if","private","this","break","double","implements","protected","throw","byte","else","import","public","throws","case","enum","instanceof","return","transient","catch","extends","int","short","void","class","finally","long","strictfp","volatile","const","float","native","super","while"};
		for(int i=0;i<guanjianzi.length;i++)
		{
			Pattern pat=Pattern.compile(guanjianzi[i]);
			Matcher mat=pat.matcher(s);
			if(mat.matches())
				return true;
			else
				continue;
		}
		return false;
	}
	public boolean isyunsuanfu(String s)										//判断运算符
	{
		Pattern pat=Pattern.compile("\\+?[\\+=]");								//+,+=,++,=
		Matcher mat=pat.matcher(s);
		
		Pattern pat1=Pattern.compile("-?[\\-=]");								//-,-=,--
		Matcher mat1=pat1.matcher(s);
	
		Pattern pat2=Pattern.compile("\\*\\=?");								//*,*=
		Matcher mat2=pat2.matcher(s);
		
		Pattern pat3=Pattern.compile("\\/\\=?");								//  /,/=
		Matcher mat3=pat3.matcher(s);
		
		Pattern pat4=Pattern.compile("\\!?\\=");								//=,!=
		Matcher mat4=pat4.matcher(s);
		
		Pattern pat5=Pattern.compile("<\\=?");									//<,<=
		Matcher mat5=pat5.matcher(s);
		Pattern pat6=Pattern.compile(">\\=?");									//>,>=
		Matcher mat6=pat6.matcher(s);
		if(mat.matches())
			return true;
		else if(mat1.matches())
			return true;
		else if(mat2.matches())
			return true;
		else if(mat3.matches())
			return true;
		else if(mat4.matches())
			return true;
		else if(mat5.matches())
			return true;
		else if(mat6.matches())
			return true;
		else 
			return false;
	}
	public boolean isbiaozhifu(String s)										//判断标志符
	{
		Pattern pat=Pattern.compile("[a-zA-Z_$]+[a-zA-Z_$0-9]*");
		Matcher mat=pat.matcher(s);
		return mat.matches();
	}
	public boolean isfengefu(String s)											//判断分隔符
	{
		String[] fengefu={",",";",".","[","]","{","}","(",")"};
		for(int i=0;i<fengefu.length;i++)
		{
			Pattern pat=Pattern.compile(fengefu[i]);
			Matcher mat=pat.matcher(s);
			if(mat.matches())
				return true;
			else
				continue;
		}
		return false;
	}
	public boolean isshuzi(String s)											//判断数字
	{
		Pattern pat=Pattern.compile("[0-9].*");
		Matcher mat=pat.matcher(s);
		return mat.matches();
	}	
	public boolean iszifu(String s)											//判断字符
	{
		Pattern pat=Pattern.compile("\\'.*\\'");
		Matcher mat=pat.matcher(s);
		return mat.matches();
	}
	public boolean iszifuchuan(String s)										//判断字符串
	{
		Pattern pat=Pattern.compile("\".*\"");
		Matcher mat=pat.matcher(s);
		return mat.matches();
	}
}

public class JComplier 
{
	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
			new frame();
	}

}

⌨️ 快捷键说明

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