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

📄 themain.java

📁 不好意思
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import java.util.*;

public class TheMain {  //程序的主方法

	public static void main(String[] args) {

		MyFrame mf = new MyFrame();
	}

}

class MyFrame extends Frame implements ActionListener {  //创建界面的类
	
	String head1,head2,head3,head4,head5;

	FileDialog filedialog_save, filedialog_load;

	TextArea text1, text2;

	BufferedReader in;

	FileReader file_reader;

	BufferedWriter out;

	FileWriter tofile;

	Panel p1 = new Panel();

	Panel p2 = new Panel();

	Panel p3 = new Panel();

	Button b1 = new Button("打开");

	Button b2 = new Button("分析");

	Button b3 = new Button("保存");

	Button b4 = new Button("关闭");

	Button b5 = new Button("清空");

	MyFrame() {
		super("DO—WHILE循环编译器!");
		this.setBounds(0, 0, 750, 500);
		this.setLayout(new GridLayout(1, 2));
		b1.addActionListener(this);
		b2.addActionListener(this);
		b3.addActionListener(this);
		b4.addActionListener(this);
		b5.addActionListener(this);
		filedialog_save = new FileDialog(this, "保存文件对话框", FileDialog.SAVE);
		filedialog_load = new FileDialog(this, "打开文件对话框", FileDialog.LOAD);

		filedialog_save.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				filedialog_save.setVisible(false);
			}
		});

		filedialog_load.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				filedialog_load.setVisible(false);
			}
		});

		this.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});

		this.setLayout(new BorderLayout());
		text1 = new TextArea();
		text2 = new TextArea();
		text2.setEditable(false);
		text1.setFont(new Font("BinnerGothic", Font.BOLD, 14));
		text2.setFont(new Font("BinnerGothic", Font.BOLD, 14));

		p1.add(b1);
		p1.add(b2);
		p1.add(b3);
		p1.add(b5);
		p1.add(b4);

		p2.setLayout(new BorderLayout());
		p2.add(text2, BorderLayout.CENTER);
		p2.add(p1, BorderLayout.SOUTH);

		p3.setLayout(new BorderLayout());
		p3.add(text1, BorderLayout.WEST);
		p3.add(p2, BorderLayout.CENTER);
		this.add(p3);
		this.setVisible(true);
		this.setResizable(false);

	}

	public void actionPerformed(ActionEvent e) {  //事件监听的方法
		if (e.getSource() == b1) {   //如果按的是打开按钮,则打开文件
			filedialog_load.setVisible(true);
			text1.setText(null);
			String s;
			if (filedialog_load.getFile() != null) {  
				try {
					File file = new File(filedialog_load.getDirectory(),
							filedialog_load.getFile());
					file_reader = new FileReader(file);
					in = new BufferedReader(file_reader);
					while ((s = in.readLine()) != null)
						text1.append(s + '\n');
					in.close();
					file_reader.close();
				} catch (IOException e2) {
				}
			}
		} else if (e.getSource() == b3) { //如果按的是保存按钮,则保存文件
			filedialog_save.setVisible(true);
			if (filedialog_save.getFile() != null) {
				try {
					File file = new File(filedialog_save.getDirectory(),
							filedialog_save.getFile());
					tofile = new FileWriter(file);
					out = new BufferedWriter(tofile);
					out.write(text1.getText(), 0, (text1.getText().length()));
					out.flush();
					out.close();
					tofile.close();
				} catch (IOException e2) {
				}
			}
		} else if (e.getSource() == b4) { //如果按的是关闭按钮,则关闭界面
			this.dispose();

		} else if (e.getSource() == b5) { //如果按的是清空按钮,则清空两个文本区
			text1.setText("");
			text2.setText("");
		} else if (e.getSource() == b2) { //如果按的是分析按钮,则开始进行词法和语法分析
			CiFaFenXi cf = new CiFaFenXi();
			cf.CJudge();
			YuFaFenXi yf = new YuFaFenXi();
			yf.YJudge();
			text2.append("L1:" + "\n");
			Display ds = new Display();
			ds.EvaluateExpression();
			text2.append(head4+ " =" + head5 + "\n");
			text2.append("if "+head1+head2+head3+" goto L1"+ "\n");
			text2.append("else goto exit" + "\n");
		}

	}

	public class CiFaFenXi {  //词法分析的类,与以前编写的词法分析程序的方法类似
		KeyWord key = new KeyWord();

		int i = 0;

		int j = 0;

		int line = 1;

		char c = getChar();

		String input = new String();

		String output = new String();

		public boolean isLetter() { //判断是否是字母
			if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
				return true;
			else
				return false;
		}

		public boolean isNumber() {//判断是否是数字
			if (c >= '0' && c <= '9')
				return true;
			else
				return false;

		}

		public boolean isOperate() {//判断是否是操作符
			Operater oper = new Operater();
			for (int i = 0; i < oper.y; i++) {
				if (c == oper.op[i])
					return true;
			}
			return false;

		}

		public boolean isFenJie() {//判断是否是分解符
			if (c == ';') {

				return true;
			} else
				return false;

		}

		public char getChar() {  //取一个字符
			char[] ch = text1.getText().toCharArray();//通过把文本区的内容转换为一个字符数组,再一个个取出
			if (i < (text1.getText().length())) {
				c = ch[i];
				i++;
			}
			return c;
		}

		public void CJudge() { //开始词法分析,为每一行添加一个行号
			int a = text1.getText().length();
			text2.append("文件总的字符数为:" + a + "\n");
			text2.append("行号为:1 " + " \n");
			while (j <= text1.getText().length()) {//判别条件,只要没有取完则继续
				if (c == ' ' || c == 9) {//如果是空格后TAB,则跳过
					getChar();
					j++;
				} else if (c == '\n') {//如果是换行,则行号加1
					c = ' ';
					getChar();
					line++;
					j++;
					text2.append("行号为:" + line + "\n");

				} else if (isLetter()) {//如果是字母,则把接下来的所有字母和数字组合在一起
					boolean flag = false;
					while (isLetter() || isNumber() ) {
						output = output + c;
						getChar();
						j++;

					}
					for (int i = 0; i < key.x; i++) {//把该组合与关键字类中的关键字进行比较
						if (output.equals(key.k[i])) {
							flag = true;
							break;
						}
					}
					if (flag) {
						text2.append("	关键字为:" + output + "\n");//是关键字,则输出
					} else {
						text2.append("	标识符为:" + output + "\n");//否则,就是标识符
					}
					output = "";
				} else if (isNumber()) {//是数字,并且全是数字,则组合在一起
					while (isNumber()) {
						output = output + c;
						getChar();
						j++;
					}
					text2.append("	数字为:" + output + "\n");//输出数字
					output = "";
				} else if (isOperate()) {//是操作符
					text2.append("	操作符为:" + c + "\n");
					getChar();
					j++;
				} else if (isFenJie()) {//是分解符
					text2.append("	分解符为:" + c + "\n");
					getChar();
					j++;
				}

			}
		}

	}

	class YuFaFenXi {//语法分析的类,采用的是LL(1)方法
		char c = getChar(0);

		int count = 0;

		int k = 0;

		String output = new String();

		public boolean isLetter() {//以下的几个方法同词法分析
			if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
				return true;
			else
				return false;
		}

		public boolean isNumber() {//同上
			if (c >= '0' && c <= '9')
				return true;
			else
				return false;

		}

		public boolean isOperate() {//同上
			Operater oper = new Operater();
			for (int i = 0; i < oper.y; i++) {
				if (c == oper.op[i])
					return true;
			}
			return false;

		}

		public boolean isFenJie() {//同上
			if (c == ';') {

				return true;
			} else
				return false;

		}

		public char getChar(int i) {//取下一个字符,不同于词法分析的是,过滤掉$符
			char[] ch = text1.getText().toCharArray();
			if (i < (text1.getText().length())) {
				c = ch[i];
				while(c == '$') {
					++i;
					c = ch[i];
				}
				i++;
			}
			return c;
		}

		public String getString(int m) {//通过getChar()方法,来定义一个getStrig()方法,类似词法分析
			String output = new String();
			c = getChar(m);
			if (isLetter() || isNumber()) {//是关键字或标识符或数字,则将其返回
				while (isLetter() || isNumber()) {//count做为计数器,是一个全局的变量,将每次所取的位置
					output = output + c;//记录下来,以方便下次取字符
					c = getChar(++m);
					count = m;

				}
				return output;
			/*} else if (isNumber()) {
				while (isNumber()) {
					output = output + c;
					c = getChar(++m);
					count = m;

				}
				return output;*/
			} else if (isOperate()) {//是操作符,则将其返回
				output = "" + c;
				count = m + 1;
				// c = getChar();
				return output;
			} else if (isFenJie()) {//是分解符,则将其返回
				output = "" + c;
				count = m + 1;
				// c = getChar();
				return output;
			} else
				return "";  //否则返回一个空的字符
		}

		String[][] s = new String[8][15];//定义一个二维的字符串数组,作为预测分析表
		{
			for (int i = 0; i < 8; i++) {//先将所有的值置为@
				for (int j = 0; j < 15; j++) {
					s[i][j] = "@";
				}
			}
			s[0][0] = ")E(w};G{d";//再分别为对应位置赋值
			s[1][2] = "R=i";
			s[2][2] = "Bi";
			s[3][7] = "BRT";
			s[3][8] = "BRT";
			s[3][9] = "BRT";
			s[3][10] = "BRT";
			s[4][7] = "+";
			s[4][8] = "-";
			s[4][9] = "*";
			s[4][10] = "/";
			s[5][2] = "RFR";
			s[6][11] = ">";
			s[6][12] = "=";
			s[6][13] = "<";

		}

		Stack<String> cs = new Stack<String>();//创建一个栈

		String string = this.getString(count);//创建一个变量,用来每次取一个字符串

		int getLittle() {//为每一个字符串变量,进行定位操作,即为其编号,以方便后面的比较操作
			// char c1 = getChar();
			if (string.equals("do"))
				return 0;
			if (string.equals("while"))
				return 1;
			/*

⌨️ 快捷键说明

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