예제 #1
0
  public static String parse(
      Lexer lexer,
      HashMap<Nonterminal, HashMap<Terminal, Rule>> parsingTable,
      Nonterminal startSymbol) {

    Stack<Token> stack = new Stack<Token>();
    ArrayList<Terminal> inputs = lexer.getTokens();
    int count = 0;

    stack.push(startSymbol);
    while (!stack.isEmpty() && count < inputs.size()) {
      Token top = stack.peek();
      System.out.println(
          "Testing top: " + top.toString() + ", next input: " + inputs.get(count).toString());
      if (top instanceof Nonterminal) {

        Nonterminal nextNonTerm = (Nonterminal) top;
        Rule rules = parsingTable.get(nextNonTerm).get(inputs.get(count));
        if (rules == null)
          return "Invalid. No rule found for "
              + nextNonTerm.toString()
              + " and the input "
              + inputs.get(count);

        stack.pop();

        for (int i = rules.rule.length - 1; i > -1; i--) {
          if (!rules.rule[i].equals(new Terminal(Terminal.TerminalType.EPSILON)))
            stack.push(rules.rule[i]);
        }

      } else if (top instanceof Terminal) {

        Terminal nextTerm = (Terminal) top;
        Terminal nextInput = inputs.get(count);

        if (nextTerm.equals(nextInput)) {
          ++count;
          stack.pop();

        } else return nextTerm + " and " + nextInput + " do not match: invalid";
      }
      System.out.println("Current stack: " + stack.toString());
    }

    if (stack.isEmpty()) return "Valid";
    return "Invalid. Ran out of tokens early or ran over input count";
  }
예제 #2
0
 @Override
 public boolean equals(Object obj) {
   if (this == obj) return true;
   if (obj == null) return false;
   if (getClass() != obj.getClass()) return false;
   Vuelo other = (Vuelo) obj;
   if (avion == null) {
     if (other.avion != null) return false;
   } else if (!avion.equals(other.avion)) return false;
   if (id != other.id) return false;
   if (pista == null) {
     if (other.pista != null) return false;
   } else if (!pista.equals(other.pista)) return false;
   if (terminal == null) {
     if (other.terminal != null) return false;
   } else if (!terminal.equals(other.terminal)) return false;
   return true;
 }