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

📄 cminus.java

📁 词法和语法分析器
💻 JAVA
字号:
/**
 * 
 */
package cminusCompiler;

/**
 * @author tanfei E-mail:tanfei158876110@163.com
 * @version Create Time:2007-12-6 下午01:20:05 description:
 */

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Stack;
import java.util.StringTokenizer;

public class CMinus {

	private static LinkedList<Nonterminal> nonterminal = new LinkedList<Nonterminal>();

	private static HashMap<Integer, String> terminal = new HashMap<Integer, String>();

	private static HashMap<String, Integer> ter = new HashMap<String, Integer>();

	private static LinkedList[][] table;

	private static HashMap<String, Integer> nont = new HashMap<String, Integer>();

	private static HashMap<Integer, String> nont1 = new HashMap<Integer, String>();

	private static String sourceName;

	private static Stack<InputLine> input = new Stack<InputLine>();

	private static Stack<String> parse = new Stack<String>();

	private static BufferedReader stdIn = new BufferedReader(
			new InputStreamReader(System.in));

	private static PrintWriter stdOut = new PrintWriter(System.out, true);

	private static String commandLine;

	private static int chNo;

	public static void main(String[] args) {
		sourceName = "source/test";
		ReadParameter rp = new ReadParameter(nonterminal, terminal, ter);
		CalculateSet cs = new CalculateSet(nonterminal, terminal);
		TestTheProcess ttr = new TestTheProcess(nonterminal, terminal);
		rp.readNonterminal();
		rp.readTerminal();
		cs.calculateFirstSet();
		cs.CalculateFollowSet();
		initTable();
		ConstructLL1Table cllt = new ConstructLL1Table(nonterminal, ter, table,
				nont, nont1);		
		cllt.calculateLTable();
		while (true) {
			showMenu();
			try {
				commandLine = stdIn.readLine();
				StringTokenizer tokenizer = new StringTokenizer(commandLine);
				if (tokenizer.hasMoreTokens()) {
					chNo = Integer.parseInt(tokenizer.nextToken());
				} else {
					stdOut.println("You must show a choice number!");
					continue;
				}
				if (chNo > 8 || chNo < 0) {
					stdOut.println("Please input valid choice number!");
					continue;
				}
				if (chNo <= 7 && chNo >= 5) {
					if (tokenizer.hasMoreTokens()) {
						sourceName = tokenizer.nextToken();
					} else {
						stdOut.println("You must indicate a file name!");
						continue;
					}
				}

				CalculateStack cst = new CalculateStack(input, parse, nont1.get(0),
						sourceName);
				LL1Parse ll1parse = new LL1Parse(ter, nont, input, parse, table);
				
				switch (chNo) {
				case 1:
					ttr.testForRP();
					break;
				case 2:
					ttr.testForFirstSet();
					break;
				case 3:
					ttr.testForFollowSet();
					break;
				case 4:
					ttr.testForLL1Table(nont1, terminal, table);
					break;
				case 5:
					runCScan(sourceName);
					break;
				case 6:
					runCScan(sourceName);
					cst.constructInputStack();
					cst.constructParseStack();
					ll1parse.parseTheInput(false);
					break;
				case 7:
					runCScan(sourceName);
					cst.constructInputStack();
					cst.constructParseStack();
					ll1parse.parseTheInput(true);
					break;
				case 8:
					System.exit(0);
					break;
				default:
					stdOut.println("Unknown error!");
					System.exit(1);
					break;
				}
			} catch (Throwable t) {
				t.printStackTrace();
				stdOut.println("Read command line error!");
				System.exit(1);
			}
			sourceName = "source/test";
		}
	}

	private static void initTable() {
		int m = ter.size();
		int n = nonterminal.size();
		table = new LinkedList[n][m];
		for (int i = 0; i < n; i++)
			for (int j = 0; j < m; j++)
				table[i][j] = new LinkedList<String>();
	}

	private static void runCScan(String name) {
		try {
			Runtime rt = Runtime.getRuntime();
			Process proc = rt.exec("cScan/CScan.exe " + name);
			int exitVal = proc.waitFor();
			if (exitVal != 0) {
				System.out
						.println("Run the CScan.exe error, the program must exit!");
				System.exit(1);
			}
		} catch (Throwable t) {
			t.printStackTrace();
			System.out
					.println("Run the CScan.exe error, the program must exit!");
			System.exit(1);
		}
	}

	private static void showMenu() {
		System.out.println();
		System.out.println("*************************************************************");
		System.out.println("1. Print the load process.");
		System.out.println("2. Print the first set.");
		System.out.println("3. Print the follow set.");
		System.out.println("4. Print the LL1 table.");
		System.out.println("5. Run the C-Scan to Scan the sorce file.");
		System.out
				.println("   (Attention this command must follow a source file name)");
		System.out.println("6. Parse the input file.");
		System.out
				.println("   (Attention this command must follow a source file name)");
		System.out
				.println("7. Parse the input file with printing the parse tree.");
		System.out
				.println("   (Attention this command must follow a source file name)");
		System.out.println("8. Exit.");
		System.out.println("*************************************************************");
	}

}

⌨️ 快捷键说明

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