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

📄 wordsanalysis.java

📁 编译原理课程中的单词词法分析程序
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

public class wordsAnalysis extends JFrame implements ActionListener {

	TextArea textResult, textSource; // 输入输出文本区

	JButton buttonAnalysis;

	String str; // 源程序字符串

	char[] buf; // 源程序缓冲数组

	// String strIden, strNum, strSym; //标识符
	int offset = 0;

	char ch;

	int length;

	// ArrayList identifier, symbol, number;
	int idenOffset = 0;

	int symOffset = 0;

	int numOffset = 0;

	public wordsAnalysis() {
		JPanel panelSource = new JPanel();
		JPanel panelResult = new JPanel();
		JPanel panel = new JPanel();
		panelSource.setLayout(new BorderLayout());
		panelResult.setLayout(new BorderLayout());
		panel.setLayout(new GridLayout(2, 1));
		textSource = new TextArea();
		textResult = new TextArea();
		textResult.setEditable(false);
		buttonAnalysis = new JButton("分析");
		JLabel labelSource = new JLabel("请在下面文本区输入源码:", JLabel.LEFT);
		JLabel labelResult = new JLabel("分析结果如下所示:", JLabel.LEFT);
		panelSource.add(labelSource, "North");
		panelSource.add(textSource, "Center");
		panelResult.add(labelResult, "North");
		panelResult.add(textResult, "Center");
		panel.add(panelSource);
		panel.add(panelResult);
		Container c = this.getContentPane();
		c.add(panel, "Center");
		c.add(buttonAnalysis, "South");
		buttonAnalysis.addActionListener(this);

		setBounds(200, 100, 450, 450);
		setVisible(true);

		textResult.append("1->标识符,2->数字,3->界符" + '\n' + '\n');
	}

	public void analyse(String str) {

		buf = str.toCharArray();
		length = str.length();
		if (offset < length) {

			ch = buf[offset];

			if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')
					|| ch == '$' || ch == '_') {

				scanIden();

			} else if (ch >= '0' && ch <= '9') {

				scanNumber();

			} else {

				switch (ch) {
				case '\n':
				case '\t':
				case 13:
				case ' ':
					scanNext();
					break;

				case '{':
				case '}':
				case '[':
				case ']':
				case '(':
				case ')':
				case '>':
				case '<':
				case '=':
				case '+':
				case '-':
				case '*':
				case '|':
				case '&':
				case '%':
				case ',':
				case ':':
				case ';':
				case 39: // '
				case 34: // "
				case 92: // \
					scanSymbol();
					break;

				case '.':
					scanPoint();
					break;

				case '/':
					scanLine();
					break;
				}
			}
		}

	}

	public void scanNext() {

		offset++;

		analyse(str);
	}

	public void scanIden() {

		int begin = offset;
		do {
			offset++;
			if (offset >= length)
				break;

			ch = buf[offset];

		} while ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z')
				|| ch == '$' || ch == '_' || (ch >= '0' && ch <= '9'));

		int count = offset - begin;
		String strIden = new String(buf, begin, count);
		textResult.append("( 1, " + strIden + " )" + '\n');

		analyse(str);
	}

	public void scanSymbol() {

		String strSym = new String(buf, offset, 1);
		textResult.append("( 3, " + strSym + " )" + '\n');
		offset++;

		analyse(str);
	}

	public void scanNumber() {
		int begin = offset;
		if (buf[offset] == '0' && offset < length - 1) {
			if (buf[offset + 1] == '.' || buf[offset + 1] == 'x'
					|| buf[offset + 1] == 'X')
				offset++;
		} else if (offset < length - 1) {
			if (buf[offset + 1] == '.')
				offset++;
		}

		do {
			offset++;
			if (offset >= length)
				break;

			ch = buf[offset];
		} while (ch >= '0' && ch <= '9');

		int count = offset - begin;
		String strNum = new String(buf, begin, count);
		textResult.append("( 2, " + strNum + " )" + '\n');
		analyse(str);
	}

	public void scanPoint() {
		if (buf[offset + 1] > '0' && buf[offset + 1] < '9') {

			scanNumber();
		} else {
			scanSymbol();
		}
	}

	public void scanLine() {

		if (buf[offset + 1] == '/') {
			do {
				offset++;

			} while (buf[offset] != '\n');
			offset++;

			analyse(str);

		} else if (buf[offset + 1] == '*') {

			do {
				offset++;

			} while (buf[offset] == '/');
			offset++;

			analyse(str);

		} else {

			scanSymbol();
		}
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == buttonAnalysis) {
			str = textSource.getText();
			// System.out.println(str.length());
			analyse(str);

		}
	}

	public static void main(String[] args) {
		wordsAnalysis Analyser = new wordsAnalysis();

	}

}

⌨️ 快捷键说明

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