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

📄 scaner.java

📁 一个简易的c++词法分析器
💻 JAVA
字号:
package cifa;

import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.util.regex.*;


public class Scaner{
	private Analyser analyser;
	private String fileName="C:\\";
	private final String[] KEY_WORD={"asm","auto","break","case","catch","char","class","const","continue","default","delete","do","double", 
            "else","enum","extern","float","friend","for","friend","goto","if","inline","int","long","new","register", 
            "opeeerator","private","protected","public","return","short","signed","sizeof","static","struct", 
           "switch","template","this","throw","try","typedef","union","unsigned","void","volatile","while","true",
           "false","bool","Include","iostream. h ","main"};
	//构造函数,用于指定控制
	public Scaner(Analyser analyser)
	{
		this.analyser=analyser;
	}

	public void openFile()	//打开文件
	{
		JFileChooser jfc=new JFileChooser(fileName);
		jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
		int result=jfc.showOpenDialog(analyser);
		if(result==JFileChooser.CANCEL_OPTION)
			return;
		File file=jfc.getSelectedFile();
		fileName=file.getAbsolutePath();
		try{
			StringBuffer buffer=new StringBuffer("");
			FileInputStream fis=new FileInputStream(fileName);
			int c;
			while((c=fis.read())!=-1){
				buffer.append((char)c);
			}
			analyser.outputArea.setText("");
			analyser.inputArea.setText(new String(buffer));
		}catch(IOException e){
			e.printStackTrace();
		}
	}


	public void scan()	//扫描文件
	{
		analyser.outputArea.setText("");
		//用正则表达式查找匹配字符
		Matcher m=
			Pattern.compile("/\\*.*\\*/|\'.+\'|(\\++|\\--|\\>=|\\<=|\\//|\\<<|\\>>|\\!=|\\==|\\->|\\::)|\\d+\\.\\d*|\\w+|\".+\"|\\S").matcher(analyser.inputArea.getText());
		while(m.find()){
				if(m.group()=="//")
					{
					while(m.group()!="\n")
					out("注释内容",m.group());
					}
				else	
				check(m.group());
			
		}
	}

	private void check(String s)	//检测匹配字符串
	{
		if(Character.isDigit(s.charAt(0)))
			checkDigit(s);
		else if(s.length()==1)
			checkChar(s.charAt(0));
		else 
			checkString(s);
			
		
	}

	private void checkDigit(String s)	//检测数字
	{
		if(s.indexOf(".")!=-1)
			out("浮点数",Double.valueOf(s).toString());
		else
			out("整数",s);
	}

	private void checkChar(char c)	//检测单个字符
	{	
		
		switch (c){
		   	case '<':
		    	out1("小于号", c);
		    	break;
		   	case '>':
		    	out1("大于号", c);
		    	break;
		   	case '=':
		    	out1("等于号", c);
		    	break;
		   	case '+':
		    	out1("加号", c);
		    	break;
		   	case '-':
		    	out1("减号", c);
		    	break;
		   	case '*':
		    	out1("乘号", c);
		    	break;
		   	case '/':
		   		out1("除号", c);
		    	break;
		   	case '(':
		    	out1("左小括号", c);
		    	break;
		   	case ')':
		    	out1("右小括号", c);
		    	break;
		   	case '[':
		    	out1("左中括号", c);
		    	break;
		   	case ']':
		    	out1("右中括号", c);
		    	break;
		   	case '{':
		    	out1("左大括号", c);
		    	break;
		   	case '}':
		    	out1("右大括号", c);
		    	break;
		   	case ',':
		    	out1("逗号", c);
		    	break;
		   	case ';':
		    	out1("分号", c);
		    	break;
		   	case '!':
		   		out1("取反", c);
		    	break;
		    case '.':
		   		out1("点号", c);
		    	break;
		    case ':':
		   		out1("冒号", c);
		    	break;
		   	default:
		    	out1("标识符", c);
		    	break;
		
		 }
	}

	private void checkString(String s)	//检测多个字符
	{
		if(s.charAt(0)=='\'')
			out("字符", s);
		else if(s.charAt(0)=='"')
			out("字符串", s);
		else if(s.equals("//"))
			out("注释",s);
		else if(s.equals("++"))
			out("自加符", s);
		else if(s.equals("--"))
			out("自减符", s);
		else if(s.equals(">="))
			out("大于等于", s);
		else if(s.equals("<="))
			out("小于等于", s);
		else if(s.equals("<<"))
			out("输入符", s);
		else if(s.equals(">>"))
			out("输出符", s);
		else if(s.equals("\n"))
			out("回车符", s);
		else if(s.equals("::"))
			out("类符号", s);
		else if(isKeyWord(s))
			out("关键字", s);
		else 
			out("标识符", s);
	
	}

	private boolean isKeyWord(String s)	//是否是关键字
	{
		boolean b=false;
		for(int i=0;i<KEY_WORD.length;i++)
			if(s.equals(KEY_WORD[i]))
				b=true;
		return b;
	}

	private void out(String s,Object o)	//向显示器输出结果
	{
		if(o instanceof String)
			analyser.outputArea.append("( "+s+" , "+"“" +(String)o+"”" +" )\n");
		else
			analyser.outputArea.append("( "+s+" , "+"“" +((Character)o).charValue()+"”" +" )\n");

	}
	private void out1(String s,char o)	//向显示器输出结果
	{

			analyser.outputArea.append("( " + s +" ," +"“" +o +"”" + " )\n" );

	}

	public void saveFile()	//将输出的结果保存为文件
	{
		JFileChooser fileChooser=new JFileChooser(fileName);
		fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		int result=fileChooser.showSaveDialog(analyser);
		if(result==JFileChooser.CANCEL_OPTION)
			return;
		File file=fileChooser.getSelectedFile();
		fileName=file.getAbsolutePath();

		if(fileName==null || file.getName().equals(""))
			JOptionPane.showMessageDialog(fileChooser,"Invalid File Name","Invalid File Name",JOptionPane.ERROR_MESSAGE);
		else{
			try{
				OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream(file));
				String saveString=analyser.outputArea.getText().replaceAll("\n","\r\n");
				osw.write(saveString);
				osw.flush();
				osw.close();
			}
			catch(IOException ioException){
				ioException.printStackTrace();
			}
		}
	}

	public void clear()	//清除输入区域与输出结果区域
	{
		analyser.inputArea.setText("");
		analyser.outputArea.setText("");
	}

	public void help()	//帮助信息
	{
		JOptionPane.showMessageDialog(analyser,
			"Tips1:可在\"输入区域\"手写或点击\"打开\"按钮打开要扫描的文件!\n"+
			"Tips2:点击\"扫描\"按钮可对\"输入区域\"的源程序进行词法扫描!\n"+
			"Tips3:扫描结果在\"输出结果\"下列出!\n","使用方法",
			JOptionPane.INFORMATION_MESSAGE);
	}

	public void about()	//“关于”信息
	{
		JOptionPane.showMessageDialog(analyser,
			"这是一个用Java编写的C++词法分析器!\n");
	}

}

⌨️ 快捷键说明

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