Examples of SymbolSet


Examples of net.sourceforge.chaperon.model.symbol.SymbolSet

  private SymbolSet follow(Symbol symbol)
  {
    System.out.println();
    System.out.println("Calculate FOLLOW set for "+symbol);

    SymbolSet followset = new SymbolSet();

    // if symbol is start symbol, then add symbol for end of file
    if (symbol.equals(grammar.getStartSymbol()))
      followset.addSymbol(new EndOfFile());

    // if production A -> a B b exists, then add every symbol of
    // FIRST(b) except the symbol for an empty list to FOLLOW(B)
    SymbolList definition;
    for (int production = 0; production<grammar.getProductionCount(); production++)
    {
      definition = grammar.getProduction(production).getDefinition();
      for (int position = 0; position<definition.getSymbolCount(); position++)
        if (definition.getSymbol(position).equals(symbol))
        {
          System.out.println("Found "+symbol+" at position "+position+" in production "+production);

          SymbolSet firstset = null;
          if ((position+1)<definition.getSymbolCount())
          {
            SymbolList rest = new SymbolList();
            for (int restposition = position+1; restposition<definition.getSymbolCount();
                 restposition++)
              rest.addSymbol(definition.getSymbol(restposition));

            firstset = firstsets.getFirstSet(rest);

            System.out.println("first("+rest+")="+firstset);

            followset.addSymbol(firstset);
            followset.removeSymbol(EMPTYLIST);
          }

          if ((position+1)==definition.getSymbolCount())
            System.out.println(symbol+" is last symbol of production "+production);

          // if a production A -> a B or A -> a B b exist and FIRST(b)
          // contains the symbol for an empty list, then every symbol
          // of FOLLOW(A) belongs to FOLLOW(B)
          if ((((position+1)==definition.getSymbolCount()) || (firstset.contains(EMPTYLIST))) &&
              (!grammar.getProduction(production).getSymbol().equals(symbol)))
            followset.addSymbol(follow(grammar.getProduction(production).getSymbol()));
        }
    }
View Full Code Here

Examples of net.sourceforge.chaperon.model.symbol.SymbolSet

   */
  public String toString()
  {
    StringBuffer buffer = new StringBuffer();

    SymbolSet symbols = grammar.getSymbols();

    for (int symbol = 0; symbol<symbols.getSymbolCount(); symbol++)
    {
      buffer.append("follow(");
      buffer.append(symbols.getSymbol(symbol).toString());
      buffer.append(")=");
      buffer.append(getFollowSet(symbols.getSymbol(symbol)).toString());
      buffer.append("\n");
    }

    return buffer.toString();
  }
View Full Code Here

Examples of net.sourceforge.chaperon.model.symbol.SymbolSet

    return EMPTYLIST;
  }

  public SymbolSet getNextTerminals()
  {
    SymbolSet set = new SymbolSet();

    SymbolList productiondefinition;
    for (int item = 0; item<productions.length; item++)
      if ((positions[item]<((productiondefinition = productions[item].getDefinition()).getSymbolCount())) &&
          (productiondefinition.getSymbol(positions[item]) instanceof Terminal))
        set.addSymbol(productiondefinition.getSymbol(positions[item]));

    return set;
  }
View Full Code Here

Examples of net.sourceforge.chaperon.model.symbol.SymbolSet

    return set;
  }

  public SymbolSet getNextNonterminals()
  {
    SymbolSet set = new SymbolSet();

    SymbolList productiondefinition;
    for (int item = 0; item<productions.length; item++)
      if ((positions[item]<((productiondefinition = productions[item].getDefinition()).getSymbolCount())) &&
          (productiondefinition.getSymbol(positions[item]) instanceof Nonterminal))
        set.addSymbol(productiondefinition.getSymbol(positions[item]));

    return set;
  }
View Full Code Here

Examples of net.sourceforge.chaperon.model.symbol.SymbolSet

  public Automaton(Grammar grammar, Log log)
  {
    this.grammar = grammar;
    this.log = log;

    SymbolSet symbols = grammar.getSymbols();

    tsymbols = symbols.getTerminals();
    ntsymbols = symbols.getNonterminals();

    // C = closure( [S'=^S] )
    addState(getStartState());

    State I;
    State J;
    int index;
    SymbolSet nextterminals;
    SymbolSet nextnonterminals;

    for (int i = 0; i<getStateCount(); i++)
    {
      I = getState(i);

      // J = goto(I,X) add to C, for all nonterminal and terminal symbols X
      // for the non terminal symbols
      nextnonterminals = I.getNextNonterminals();
      for (int j = 0; j<nextnonterminals.getSymbolCount(); j++)
      {
        J = I.jump(nextnonterminals.getSymbol(j));

        if (!J.isEmpty())
        {
          index = indexOf(J);
          if (index<0// if C doesn't contain J
          {
            index = addState(J);

            // stores the transition for this symbol
            I.addShiftAction(nextnonterminals.getSymbol(j), J);
          }
          else
            I.addShiftAction(nextnonterminals.getSymbol(j), getState(index));
        }
      }

      // and for the terminal symbls
      nextterminals = I.getNextTerminals();
View Full Code Here

Examples of net.sourceforge.chaperon.model.symbol.SymbolSet

  public void testFollowSet()
  {
    FirstSetCollection firstsets = new FirstSetCollection(grammar)/*, new ConsoleLogger());*/
    FollowSetCollection followsets = new FollowSetCollection(grammar, firstsets);

    SymbolSet result = new SymbolSet();
    result.addSymbol(bclose);
    result.addSymbol(eof);
    assertEquals("Test if sets are equal", result, followsets.getFollowSet(E));
    assertEquals("Test if sets are equal", result, followsets.getFollowSet(Eprime));

    result = new SymbolSet();
    result.addSymbol(plus);
    result.addSymbol(bclose);
    result.addSymbol(eof);
    assertEquals("Test if sets are equal", result, followsets.getFollowSet(T));
    assertEquals("Test if sets are equal", result, followsets.getFollowSet(Tprime));

    result = new SymbolSet();
    result.addSymbol(mult);
    result.addSymbol(plus);
    result.addSymbol(bclose);
    result.addSymbol(eof);
    assertEquals("Test if sets are equal", result, followsets.getFollowSet(F));
  }
View Full Code Here

Examples of net.sourceforge.chaperon.model.symbol.SymbolSet

    }
  }

  public void testAddSymbol()
  {
    SymbolSet set = new SymbolSet();

    assertEquals("Test if set is empty", 0, set.getSymbolCount());
    assertEquals("Test if set is empty", true, set.isEmpty());

    set.addSymbol(a);

    assertEquals("Test if set is not empty", 1, set.getSymbolCount());
    assertEquals("Test if set is not empty", false, set.isEmpty());

    assertEquals("Test if symbols are equal", a, set.getSymbol(0));

    set.addSymbol(a);
    set.addSymbol(A);
    set.addSymbol(b);
    set.addSymbol(B);
    set.addSymbol(c);
    set.addSymbol(C);

    assertEquals("Test if set is not empty", 6, set.getSymbolCount());
    assertEquals("Test if set is not empty", false, set.isEmpty());

    assertEquals("Test if symbols are equal", a, set.getSymbol(0));
    assertEquals("Test if symbols are equal", B, set.getSymbol(3));
    assertEquals("Test if symbols are equal", C, set.getSymbol(5));

    assertEquals("Test if indices are equal", 0, set.indexOf(a));
    assertEquals("Test if indices are equal", 4, set.indexOf(c));

    SymbolSet set2 = new SymbolSet();

    set2.addSymbol(a);
    set2.addSymbol(a);
    set2.addSymbol(A);
    set2.addSymbol(b);

    assertNotEquals("Test if sets are not equal", set, set2);

    set2.addSymbol(B);
    set2.addSymbol(c);
    set2.addSymbol(C);

    assertEquals("Test if sets are equal", set, set2);

    SymbolSet set3 = new SymbolSet();

    set3.addSymbol(a);
    set3.addSymbol(A);
    set3.addSymbol(b);
    set3.addSymbol(B);
    set3.addSymbol(c);
    set3.addSymbol(C);
    set3.addSymbol(a);

    assertEquals("Test if sets are equal", set, set3);
  }
View Full Code Here

Examples of net.sourceforge.chaperon.model.symbol.SymbolSet

   *
   * @return Set of symbols
   */
  public SymbolSet getSymbols()
  {
    SymbolSet set = new SymbolSet();

    for (int i = 0; i < getProductionCount(); i++)
      set.addSymbol(getProduction(i).getSymbols());
    return set;
  }
View Full Code Here

Examples of net.sourceforge.chaperon.model.symbol.SymbolSet

      violations.addViolation("No productions are defined", location);

    for (Enumeration e = productions.elements(); e.hasMoreElements() ;)
      violations.addViolations(((Production)e.nextElement()).validate());

    SymbolSet ntsymbols = getSymbols().getNonterminals();
    for (int i = 0; i < ntsymbols.getSymbolCount(); i++)
      if (!contains(ntsymbols.getSymbol(i)))
        violations.addViolation("Nonterminal symbol \"" + ntsymbols.getSymbol(i) + "\"" +
                                "is not defined through a production", location);

    return violations;
  }
View Full Code Here

Examples of net.sourceforge.chaperon.model.symbol.SymbolSet

    assertEquals("Test if sets are equal", set, set3);
  }

  public void testEquals()
  {
    SymbolSet set = new SymbolSet();
    set.addSymbol(A);
    set.addSymbol(B);
    set.addSymbol(C);

    SymbolSet set2 = new SymbolSet();
    set2.addSymbol(A);
    set2.addSymbol(C);
    set2.addSymbol(B);

    assertEquals("Test if symbols are equal", set, set2);

    set2 = new SymbolSet();
    set2.addSymbol(A);
    set2.addSymbol(B);

    assertNotEquals("Test if sets are not equal", set, set2);
  }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.