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

📄 syntaxanalyzer.java

📁 自己写的一个用java实现的一个语法分析器
💻 JAVA
字号:
package Syntax;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class SyntaxAnalyzer extends Frame implements ActionListener{
	//int check_point=0;
	String info="";
	String grammar_content="";
	String end_symbols_content="";
	String not_end_symbols_content="";
	String start_symbol_content="";
	String test_string_content="";
	Characters Symbols;//符号表 
	Sentences sentences;//产生式
	DFA dfa;
    MenuBar mb = new MenuBar();
    Menu fileMenu = new Menu("文件");
    Menu syntaxMenu = new Menu("语法分析");
    Menu help = new Menu("帮助");
    Menu about = new Menu("关于");
    MenuItem closeWindow = new MenuItem("退出");
    MenuItem openFile = new MenuItem("打开文件");
    MenuItem save = new MenuItem("保存");
    MenuItem syntax_analyzer = new MenuItem("开始语法分析");    
    TextArea grammar  = new TextArea(18,40);
    //TextArea input = new TextArea(20,40);
    TextArea table = new TextArea(18,60);
    TextArea analyzer_stack = new TextArea(20,60);
    TextArea end_symbols = new TextArea(1,40);
    TextArea not_end_symbols = new TextArea(1,40);
    TextArea start_symbol = new TextArea(1,40);
    TextArea test_string = new TextArea(1,40);

    FileDialog file_dialog_load = new FileDialog(this, "打开文件...", FileDialog.LOAD);
    
    SyntaxAnalyzer(){
            
            this.setLayout(new FlowLayout());
            this.setMenuBar(mb);
            mb.add(fileMenu);
            mb.add(syntaxMenu);
            mb.add(help);
            mb.add(about);
            fileMenu.add(openFile);
            fileMenu.add(save);
            fileMenu.add(closeWindow);
            syntaxMenu.add(syntax_analyzer);
            this.add(grammar);
            this.add(table);
            this.add(end_symbols);
            this.add(analyzer_stack);
            this.add(not_end_symbols);
            this.add(start_symbol);
            this.add(test_string);
            
            
            closeWindow.addActionListener(this);
            openFile.addActionListener(this);
            save.addActionListener(this);
            syntax_analyzer.addActionListener(this);
            help.addActionListener(this);
            about.addActionListener(this);
            this.setSize(800, 700);
            this.addWindowListener(new WindowAdapter()
            {public void windowClosing(WindowEvent e){System.exit(0);}});
            this.setVisible(true);
            
            /*grammar.setText("请输入一个文本或导入一个文件");
        	//end_symbols.setText("请输入终结符集");
        	not_end_symbols.setText("请输入非终结符集");
        	start_symbol.setText("请输入开始符号");
        	test_string.setText("请输入测试字串");*/
    }
    public static void main(String[] args)
    {
    	SyntaxAnalyzer syntax_analyzer = new SyntaxAnalyzer();
    }
    public void actionPerformed(ActionEvent e){
        if(e.getSource() == closeWindow)System.exit(0);
        else if(e.getSource() == openFile)
        {
            file_dialog_load.setVisible(true);
            File myfile = new File(file_dialog_load.getDirectory(), file_dialog_load.getFile());
            try
            {   
                BufferedReader bufReader = new BufferedReader(new FileReader(myfile));
                String str,content;
                content="";
                while((str = bufReader.readLine()) != null)
                {
                	content +=str + "\n";
                    grammar.setText(content);
                }
            }
            catch(IOException ie)
            {
                System.out.println("I/O异常...");
            }
        }
        else if(e.getSource() == syntax_analyzer)
        {
        	table.setText("");
        	if(initialize())
        	{
        		syntax();
        	}
        	
        }
       
    }
    public boolean initialize()
    {
    	if(!initialize_content())return false;
    	if(!initialize_characters())return false;
    	if(!initialize_sentences())return false;
    	if(!initialize_dfa())return false;
    	return true;
    }
    public boolean initialize_content()
    {
    	grammar_content=grammar.getText();
    	end_symbols_content=end_symbols.getText();
    	not_end_symbols_content=not_end_symbols.getText();
    	start_symbol_content=start_symbol.getText();
    	test_string_content=test_string.getText();
    	String error_info="";
    	if(grammar_content.equals(""))
        {
    		error_info = "内容为空,请输入一个文本或导入一个文件!\n";
            grammar.setText(error_info);
            return false;
         }
    	else if(end_symbols_content.equals(""))
    	{
    		error_info = "请输入终结符集!\n";
    		table.setText(error_info);
    		return false;
    	}
    	else if(not_end_symbols_content.equals(""))
    	{
    		error_info = "请输入非终结符集!\n";
    		table.setText(error_info);
    		return false;
    	}
    	else if(start_symbol_content.equals(""))
    	{
    		error_info = "请输入开始符号!\n";
    		table.setText(error_info);
    		return false;
    	}
    	else if(test_string_content.equals(""))
    	{
    		error_info = "请输入测试字串!\n";
    		table.setText(error_info);
    		return false;
    	}
    	return true;
    }
    public boolean initialize_characters()
    {
    	String error_info="";
    	Symbols=new Characters();
    	error_info=Symbols.initialize_end_symbols(end_symbols_content);
    	if(!error_info.equals(""))
    	{
    		table.setText(error_info);
    		return false;
    	}
    	error_info=Symbols.initialize_start_symbol(start_symbol_content);
    	if(!error_info.equals(""))
    	{
    		table.setText(error_info);
    		return false;
    	}
    	error_info=Symbols.initialize_not_end_symbols(not_end_symbols_content);//产生字符集
    	if(!error_info.equals(""))
    	{
    		table.setText(error_info);
    		return false;
    	}
    	error_info=Symbols.check_test_string(test_string_content);//产生字符集
    	if(!error_info.equals(""))
    	{
    		table.setText(error_info);
    		return false;
    	}
    	return true;
    }
    public boolean initialize_sentences()
    {
    	info="正在检查产生式...... ";
    	sentences=new Sentences();
    	String error_info=sentences.initialize(grammar_content,Symbols);//
    	String show_sentences="";
    	if(!error_info.equals(""))
    	{
    		//info+=error_info+grammar_content.length();
    		info+=error_info;
    		table.setText(info);
    		return false;
    	}
    	info+="正确\n";
    	info=info+sentences.showAll();
    	table.setText(info);
        //show_sentences=sentences.showAll();
        //grammar.setText(show_sentences);
    	return true;
    }
    public boolean initialize_dfa()
    {
    	info+="正在生成DFA...\n";
    	dfa=new DFA();
    	String error_info=dfa.initialize(sentences,Symbols);
    	if(error_info!="")
    	{
    		return false;
    	}
    	info+=dfa.show_dfa();
    	table.setText(info);
    	return true;
    }
    public void syntax()
    {
    	//check_point=0;
    	analyzer_stack.setText("语法分析结果: \n");
    }
}

⌨️ 快捷键说明

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