/**
  * Mark the specified production as token-level. This method sets the specified production's
  * {@link Properties#TOKEN} property and removes any {@link Properties#TEXT_ONLY} property. It
  * does, however, <em>not</em> adjust the production's type to <code>Token</code>.
  *
  * @param p The production.
  */
 public static void markToken(Production p, boolean verbose) {
   if (verbose) {
     System.err.println("[Recognizing " + p.qName + " as token-level]");
   }
   p.setProperty(Properties.TOKEN, Boolean.TRUE);
   p.removeProperty(Properties.TEXT_ONLY);
 }
    /** Visit the specified nonterminal. */
    public void visit(NonTerminal nt) {
      Production p;

      try {
        p = analyzer.lookup(nt);
      } catch (IllegalArgumentException x) {
        // Too many productions. We assume the worst.
        isLexical = false;
        return;
      }

      if (null == p) {
        // No such production. We assume the worst.
        isLexical = false;

      } else if (analyzer.isProcessed(p.qName)) {
        // If the corresponding production has already been processed,
        // make sure it is lexical.
        if (!p.getBooleanProperty(Properties.LEXICAL)) {
          isLexical = false;
        }

      } else if (!analyzer.isBeingWorkedOn(p.qName)) {
        // The production has not been processed and is not yet under
        // consideration.  If is text-only, accept it.  If it is void,
        // check it out.
        if (p.getBooleanProperty(Properties.TEXT_ONLY)) {
          // Nothing to do.
        } else if (AST.isVoid(p.type)) {
          dispatch(p);
        } else {
          isLexical = false;
        }
      }
    }
Exemplo n.º 3
0
 /**
  * Representación en forma de String de la gramática. En este caso, únicamente se representarán
  * las producciones, ya que el resto de elementos que definen la gramática, se pueden obtener de
  * forma inmediata viendo esta información y la nomenclatura seguida.
  */
 public String toString() {
   final StringBuffer grammar = new StringBuffer();
   for (Production production : productions) {
     grammar.append(production.toString()).append("\n");
   }
   return grammar.toString();
 }
Exemplo n.º 4
0
  /* Checks that the method storeLengthProduction works fine. */
  @Test(timeout = 100)
  public void storeLengthProductionTest() {

    Collection<Production> cp;
    int res;

    try {
      l.storeLengthProductions(g);
      cp = g.getProductions();
      System.out.println();
      System.out.println("*******************");
      System.out.println();
      System.out.println("TEST StoreLengthProduction");
      System.out.println();
      System.out.println("*******************");
      for (Production p : cp) {
        res = g.getLenghtProduction(p);

        System.out.println();
        System.out.println("The length of the Production " + p.getSymbol() + " is: " + res);
        System.out.println();
      }

    } catch (GrammarExceptionImpl e) {
      System.out.println(e.getMessage());
    }
  }
  /** Visit the specified grammar. */
  public Object visit(Module m) {
    // Recognize lexical syntax first.
    new Tester(runtime, analyzer).dispatch(m);

    // Initialize the per-grammar state.
    analyzer.register(this);
    analyzer.init(m);

    // Make sure that all lexical and void productions are tested for
    // whether they consume the input.
    for (Production p : m.productions) {
      if (p.getBooleanProperty(Properties.LEXICAL) || AST.isVoid(p.type)) {
        analyzer.notWorkingOnAny();
        analyzer.consumesInput(p.qName);
      }
    }

    // Determine which productions to process.
    List<Production> todo;
    if (m.hasProperty(Properties.ROOT)) {
      todo = new ArrayList<Production>(1);
      todo.add(analyzer.lookup((NonTerminal) m.getProperty(Properties.ROOT)));
    } else {
      todo = m.productions;
    }

    // Process the productions.
    for (Production p : todo) {
      // Skip processed or non-public productions.
      if (analyzer.isProcessed(p.qName) || (!p.hasAttribute(Constants.ATT_PUBLIC))) {
        continue;
      }

      // Mark production as processed to avoid recursive processing.
      analyzer.processed(p.qName);

      if (p.getBooleanProperty(Properties.LEXICAL)) {
        // We have reached a lexical production.  If it consumes the
        // input, we mark it as token-level.
        analyzer.notWorkingOnAny();
        if (analyzer.consumesInput(p.qName)) {
          markToken(p, runtime.test("optionVerbose"));
        }
      } else {
        // Recurse into the production.
        analyzer.process(p);
      }
    }

    // Done.
    return null;
  }
Exemplo n.º 6
0
  public void compileParser(Trace trace, Strictness strictness) {

    SGrammar sGrammar = new SGrammar(this);
    OldGrammar oldGrammar = null;
    for (Production production : sGrammar.getProductions()) {

      if (oldGrammar == null) {
        oldGrammar = new OldGrammar(this, production.getName(), production);
      }

      OldProduction oldProduction = oldGrammar.getProduction(production.getName(), production);

      for (Alternative alternative : production.getAlternatives()) {

        OldAlternative oldAlternative = oldProduction.addAlternative("", alternative);

        for (Element element : alternative.getElements()) {
          OldElement oldElement;
          if (element instanceof Element.TokenElement) {
            Element.TokenElement tokenElement = (Element.TokenElement) element;
            oldElement =
                oldAlternative.addTokenElement(
                    this,
                    element.getName(),
                    oldGrammar.getToken(tokenElement.getTypeName()),
                    tokenElement);
          } else {
            Element.ProductionElement productionElement = (Element.ProductionElement) element;
            oldElement =
                oldAlternative.addProductionElement(
                    element.getName(),
                    oldGrammar.getProduction(
                        element.getTypeName(), productionElement.getReference()),
                    productionElement);
          }
          sGrammar.addOldElement(element, oldElement);
        }
      }
    }

    oldGrammar.stabilize();
    oldGrammar.computeShortestLengthAndDetectUselessProductions();
    sGrammar.setLRAutomaton(new LRAutomaton(oldGrammar, trace));
    this.simplifiedGrammar = sGrammar;
  }
    /** Visit the specified grammar. */
    public void visit(Module m) {
      // Initialize the per-grammar state.
      analyzer.register(this);
      analyzer.init(m);

      // Process the productions.
      for (Production p : m.productions) {
        // Make sure that the production has not been processed
        // already and that it returns a string.
        if (analyzer.isProcessed(p.qName)) {
          continue;
        } else if (p.getBooleanProperty(Properties.TEXT_ONLY)) {
          mark(p);
          analyzer.processed(p.qName);
          continue;
        } else if (!AST.isVoid(p.type)) {
          analyzer.processed(p.qName);
          continue;
        }

        // Clear the per-production state.
        isLexical = true;

        // Process the production.
        analyzer.process(p);

        // Tabulate the results.
        if (isLexical) {
          // All visited productions are guaranteed to be lexical.
          for (NonTerminal nt : analyzer.working()) {
            // This lookup is guaranteed to work, as the production's
            // fully qualified name was added by visit(Production).
            Production p2 = analyzer.lookup(nt);
            mark(p2);
            analyzer.processed(p2.qName);
          }

        } else {
          // We only know that the current production is not lexical.
          analyzer.processed(p.qName);
        }
      }
    }
  /**
   * Constructs a nullable set for the given grammar.
   *
   * @param g
   */
  public NullableNonterminals(CFG g) {
    super();

    // Construct Nullable Nonterminals
    List<Production> productions = g.productions();
    boolean changing = true;
    while (changing) {
      changing = false;
      for (Production p : productions) {
        Token left = p.leftHandSide();
        if (!nullable.contains(left)) {
          if (p.goesToEpsilon()) {
            nullable.add(left);
            changing = true;
          } else {
            List<Token> rhs = p.rightHandSide();
            boolean all_nullable = true;
            for (Token right : rhs) {
              if (!nullable.contains(right)) {
                all_nullable = false;
                break;
              }
            }

            if (all_nullable) {
              nullable.add(left);
              changing = true;
            }
          }
        }
      }
    }

    // ---------------------------------------------------------------------
    /*
     * Stores an immutable version of the non-terminal set to avoid
     * accidentally mutating it later.
     */
    this.nullable = Collections.unmodifiableSortedSet(nullable);
  }
Exemplo n.º 9
0
 /** Visit the specified production. */
 public void visit(Production p) {
   if (!p.hasProperty(Properties.META_DATA)) {
     p.setProperty(Properties.META_DATA, new MetaData());
   }
 }
 /**
  * Mark the specified production as lexical.
  *
  * @param p The production.
  */
 protected void mark(Production p) {
   if (runtime.test("optionVerbose")) {
     System.err.println("[Recognizing " + p.qName + " as lexical syntax]");
   }
   p.setProperty(Properties.LEXICAL, Boolean.TRUE);
 }
Exemplo n.º 11
0
 private boolean productionGenerateNonTerminal(
     Production production, GrammarNonTerminalSymbol nonTerminal) {
   return production.getLeft().compareTo(nonTerminal) == 0;
 }
Exemplo n.º 12
0
 private boolean functionIsCompatible(GrammarTerminalSymbol terminal, Production production) {
   return terminal.getFunction().isCompatibleWith(production.getFunction().getFunction());
 }
Exemplo n.º 13
0
  /* Checks that the methods calculateLengths in the Grammar class works fine. */
  @Test(timeout = 100)
  public void calculateLengthsTest() {
    int len = -2;
    Collection<Production> cp;
    Collection<NonTerminal> cnt;

    /*Reloads again because need to clean the structures.*/
    try {
      cr = new CreateGrammarImpl("c:/Archivos de programa/GeLi/Test/grammar.gr");
      cr.loadGrammar();
      g = cr.getGrammar();
    } catch (GrammarExceptionImpl ex) {
      System.out.println(ex.getMessage());
    }

    System.out.println();
    System.out.println("*******************");
    System.out.println();
    System.out.println("TEST CalculateLengths");
    System.out.println();
    System.out.println("*******************");

    try {
      cp = g.getProductions();
      Assert.assertNotNull(cp);
      for (Production p4 : cp) {
        len = g.calculateLenghtProduction(p4);
        System.out.println();
        System.out.println("The length of the Production " + p4.getSymbol() + " is: " + len);
        System.out.println();
      }

      cnt = g.getNonTerminals();
      Assert.assertNotNull(cnt);
      for (NonTerminal nt4 : cnt) {
        len = g.calculateLenghtNonTerminal(nt4);
        System.out.println();
        System.out.println("The length of the NonTerminal " + nt4.getSymbol() + " is: " + len);
        System.out.println();
      }

      g.calculateLengthProductions();
      cp = g.getProductions();
      System.out.println();
      System.out.println("*******************");
      System.out.println();
      System.out.println("STORE PRODUCTIONS");
      System.out.println();
      System.out.println("*******************");
      for (Production p4 : cp) {
        len = g.getLenghtProduction(p4);

        System.out.println();
        System.out.println("The length of the Production " + p4.getSymbol() + " is: " + len);
        System.out.println();
      }

      g.calculateLengthNonTerminals();
      cnt = g.getNonTerminals();
      System.out.println();
      System.out.println("*******************");
      System.out.println();
      System.out.println("STORE NonTERMINALS");
      System.out.println();
      System.out.println("*******************");
      for (NonTerminal nt4 : cnt) {
        len = g.getLenghtNonTerminal(nt4);

        System.out.println();
        System.out.println("The length of the NonTerminal " + nt4.getSymbol() + " is: " + len);
        System.out.println();
      }

    } catch (GrammarExceptionImpl e) {
      System.out.println(e.getMessage());
    }
  }