Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashMap;
- import java.util.Stack;
- public class Main {
- public static void main(String[] args) {
- Helper.printMembers();
- for(;;)
- labelTrans:{
- HashMap<String, String> hsTransicoes = new HashMap<String, String>();
- String[] automatoTeste = Helper.readArchive().split(";");
- String[][] strTransicoes = new String[automatoTeste.length][];
- for (int i = 0; i < automatoTeste.length; i++) {
- strTransicoes[i] = automatoTeste[i].replaceAll("[>{};]", "").split("[-]");
- hsTransicoes.put(strTransicoes[i][0], strTransicoes[i][1]);
- }
- for(;;)
- labelStr:{
- String estInicial = strTransicoes[0][1].substring(0, strTransicoes[0][1].indexOf(",") + 1);
- Stack<String> pilha = new Stack<String>();
- String raiz = null;
- String prox = null;
- String[] operations = {null, null};
- System.out.print("Insira a sentença: ");
- String sentenca = Helper.readKeyboard();
- System.out.println();
- int strLength = sentenca.length();
- for (int i = 0; i < sentenca.length() + 1; i++) {
- raiz = (estInicial + (strLength == 0 ? "E" : sentenca.charAt(i)) + "," + (pilha.isEmpty() ? "Z" : pilha.peek()) + ")");
- System.out.print(estInicial);
- if (strLength != 0)
- for (int j = i; j < sentenca.length(); j++)
- System.out.print(sentenca.charAt(j));
- else
- System.out.print("E");
- System.out.print(",");
- for (int j = pilha.size(); j > 0; j--)
- System.out.print(pilha.isEmpty() ? "Z" : pilha.get(j - 1));
- System.out.print("Z)");
- if (hsTransicoes.containsKey(raiz)) {
- prox = hsTransicoes.get(raiz);
- if (prox.length() > 0) {
- estInicial = prox.substring(0, prox.indexOf(",") + 1);
- operations = prox.replaceAll("[()]", "").split(",");
- if (operations[1].length() > 1)
- for (int j = 0; j < (operations[1].length() - 1); j++)
- pilha.push(Character.toString(operations[1].charAt(j)));
- else if (operations[1].equals("E") && !pilha.empty())
- pilha.pop();
- if (strLength != 0)
- strLength--;
- System.out.println("->" + prox);
- }
- } else {
- operations[0] = "stop";
- break;
- }
- }
- System.out.println();
- System.out.println("Sentenca vazia: " + ((strLength == 0) ? "SIM" : "NAO"));
- System.out.println("Estado final: " + (operations[0].endsWith("f") ? "SIM" : "NAO"));
- System.out.println(((strLength == 0) && operations[0].endsWith("f")) ? "ACEITA" : "REJEITA");
- System.out.println();
- switch (Helper.menu()) {
- case 1: break labelStr;
- case 2: break labelTrans;
- default: System.exit(0);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement