Demo entry 6747530

Java

   

Submitted by l on Jun 04, 2018 at 10:33
Language: Java. Code size: 4.2 kB.

package im.aladdin.analyzer;

import java.util.Formatter;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.Stack;

public class Console {

	private static final Character END_SYMBOL = '#';
	private static final Character N = 'N';
	private static final String[][] TABLE = new String[5][6];
	private static final Map<Character, Integer> INDEX_DICT = new HashMap<Character, Integer>();
	
	// 构建分析表
	static {
		TABLE[0][0] = "TM";  //使用M替换E撇
		TABLE[0][3] = "TM";
		TABLE[1][1] = "+TM";
		TABLE[1][4] = "N";  // 使用N替换一个西龙
		TABLE[1][5] = "N";
		TABLE[2][0] = "FP"; // 使用P替换T撇
		TABLE[2][3] = "FP";
		TABLE[3][1] = "N";
		TABLE[3][2] = "*FP";
		TABLE[3][4] = "N";
		TABLE[3][5] = "N";
		TABLE[4][0] = "i";
		TABLE[4][3] = "(E)";
		INDEX_DICT.put('E', 0);
		INDEX_DICT.put('M', 1);
		INDEX_DICT.put('T', 2);
		INDEX_DICT.put('P', 3);
		INDEX_DICT.put('F', 4);
		INDEX_DICT.put('i', 0);
		INDEX_DICT.put('+', 1);
		INDEX_DICT.put('*', 2);
		INDEX_DICT.put('(', 3);
		INDEX_DICT.put(')', 4);
		INDEX_DICT.put('#', 5);
	}
			
	public Console() {}
	
	public void analyze(String expression) {
		// 分解字符串
		char[] words = parseExpression(expression);
		Queue<Character> inputQueue = generateInputQueue(words);
		Stack<Character> symbolStack = generateSymbolStack();
		analyzeExpression(inputQueue, symbolStack);
	}
	
	private void analyzeExpression(
			Queue<Character> inputQueue, Stack<Character> symbolStack) {
		
		int step = 0;
		printTitle();
		// 打印起始信息
		String production = "";
		printState(step++, symbolStack, inputQueue, production);
		Character symbol;
		while(!(symbol = symbolStack.pop()).equals(END_SYMBOL)) {
			
			Character word = inputQueue.peek();
			// 判断是否满足抛出条件
			if(symbol.equals(word)) {
				inputQueue.poll();
				printState(step++, symbolStack, inputQueue, "");
				continue;
			}
			production = findProduction(symbol, word);
			if(production == null) {
				break;
			}
			addProduction2Stack(symbolStack, production);
			printState(step++, symbolStack, inputQueue, join(symbol, production));
		}
		
	}
	
	private String join(Character c, String s) {
		return c + " -> " + s;
	}
	
	private void addProduction2Stack(Stack<Character> stack, String production) {
		
		char[] words = parseExpression(production);
		for(int i = words.length - 1; i >= 0; i--) {
			if(words[i] != ' ' && words[i] != N) {
				stack.push(words[i]);
			}
		}
	}
	
	private String findProduction(Character symbol, Character word) {
		int si = getIndex(symbol);
		int wi = getIndex(word);
		return TABLE[si][wi];
	}
	
	private int getIndex(Character c) {
		return INDEX_DICT.get(c);
	}
	
	private Formatter format = new Formatter(System.out);
	private void printTitle() {
		format.format("%-5s %-15s %15s    %-10s\n", "Steps", "Stack", "Input", "Exception");
	}
	
	private void printState(
			int step, Stack<Character> symbolStack,
			Queue<Character> inputQueue, String production) {
		
		String symbol = getSymbolState(symbolStack);
		String input = getInputState(inputQueue);
		// 格式化输出
		format.format("%-5d %-15s %15s    %-10s\n", step, symbol, input, production);
	}
	
	private String getInputState(Queue<Character> queue) {
		
		StringBuilder input = new StringBuilder();
		Character[] words = queue.toArray(new Character[0]);
		for(Character word : words) {
			input.append(word.charValue());
		}
		return input.toString();
	}
	
	private String getSymbolState(Stack<Character> stack) {
		
		StringBuilder symbol = new StringBuilder();
		for(int i = 0; i < stack.size(); i++) {
			symbol.append(stack.get(i));
		}
		return symbol.toString();
	}
	
	private Stack<Character> generateSymbolStack(){
		
		Stack<Character> symbolStack = new Stack<Character>();
		symbolStack.push(END_SYMBOL);
		symbolStack.push('E');
		return symbolStack;
	}
	
	// 生成输出串队列
	private Queue<Character> generateInputQueue(char[] words){
		
		Queue<Character> inputQueue = new LinkedList<Character>();
		for(char word : words) {
			// 去除空格干扰
			if(word != ' ') {
				inputQueue.add(Character.valueOf(word));
			}
		}
		inputQueue.add(END_SYMBOL);
		return inputQueue;
	}
	
	// 分解输入
	private char[] parseExpression(String line) {
		return line.toCharArray();
	}
	
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).