Examples of SymbolSet


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

   */
  public ItemSet closure()
  {
    ItemSet J = new ItemSet(grammar, firstsets, this)// J=I

    SymbolSet b = new SymbolSet();
    SymbolSet b2 = new SymbolSet();

    boolean changed = false;
    do
    {
      changed = false;

      // for every item in itemset I
      for (int i = 0; i<J.elementCount; i++)
      {
        SymbolList productiondefinition = grammar.getProduction(J.productions[i]).getDefinition();

        // and not A=XYZ^
        if (J.positions[i]<productiondefinition.getSymbolCount())
        {
          Symbol symbol = productiondefinition.getSymbol(J.positions[i])// A=X ^symbol Z

          // for every item [A=u^Bv,a] in J and production B=w in G
          if (symbol instanceof Nonterminal)
          {
            int pos = J.positions[i]+1// for the FIRST set from (va)

            b.clear();

            // if [A=u^Bv,a]
            if (pos<productiondefinition.getSymbolCount())
            {
              // then is b the list of all terminal symbols from FIRST(va)
              do
              {
                if (productiondefinition.getSymbol(pos) instanceof Terminal)
                {
                  b2.clear();
                  b2.addSymbol(productiondefinition.getSymbol(pos));
                }
                else
                {
                  b2.clear();
                  b2.addSymbol(firstsets.getFirstSet(productiondefinition.getSymbol(pos)));
                }

                b.addSymbol(b2);
                pos++;
              }
              while ((b2.contains(EMPTYLIST)) && (pos<productiondefinition.getSymbolCount()));

              if (b.contains(EMPTYLIST))
                b.addSymbol(J.lookaheads[i]);

              b.removeSymbol(EMPTYLIST);
View Full Code Here

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

   *
   * @return Set of symbols.
   */
  public SymbolSet getReduceSymbols()
  {
    SymbolSet reducesymbols = new SymbolSet();

    for (int i = 0; i<elementCount; i++)
    {
      if (getItemNext(i).equals(EMPTYLIST))  // for all A=u^ and all symbols in FOLLOW(A)

        reducesymbols.addSymbol(lookaheads[i]);
    }

    return reducesymbols;
  }
View Full Code Here

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

            break;
          }
      }
    }

    SymbolSet set = getShiftSymbols();

    for (int index = 0; index<set.getSymbolCount(); index++)
    {
      buffer.append("Transition for ");
      buffer.append(set.getSymbol(index));
      buffer.append(" -> State ");
      buffer.append(String.valueOf(getTransition(set.getSymbol(index))));
      buffer.append("\n");
    }

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

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

    Violations violations = grammar.validate();

    if ((violations!=null) && (violations.getViolationCount()>0))
      throw new IllegalArgumentException("Grammar is not valid: "+violations.getViolation(0));

    SymbolSet symbols = grammar.getSymbols();

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

    //long time = System.currentTimeMillis();
    // generate all first sets
    if ((log!=null) && (log.isDebugEnabled()))
      log.debug("Generating first sets");

    firstsets = new FirstSetCollection(grammar, log);
    if ((log!=null) && (log.isDebugEnabled()))
      log.debug(firstsets.toString());

    // calculation of alle states and transitions
    if ((log!=null) && (log.isDebugEnabled()))
      log.debug("Building states and transitions");

    itemsets = new ItemSetCollection(grammar, firstsets, log);
    if ((log!=null) && (log.isDebugEnabled()))
      log.debug(itemsets.toString());

    if ((log!=null) && (log.isDebugEnabled()))
      log.debug("Building parser automaton");

    automaton =
      new ParserAutomaton(tsymbols.getSymbolCount(), ntsymbols.getSymbolCount(),
                          grammar.getProductionCount(), 0, itemsets.getItemSetCount());

    // for alle terminal symbols
    for (int i = 0; i<tsymbols.getSymbolCount(); i++)
      automaton.setTerminal(i, tsymbols.getSymbol(i).getName());

    // for the non terminal symbols
    for (int i = 0; i<ntsymbols.getSymbolCount(); i++)
      automaton.setNonterminal(i, ntsymbols.getSymbol(i).getName());

    // for all productions
    for (int i = 0; i<grammar.getProductionCount(); i++)
    {
      automaton.setProductionSymbol(i, ntsymbols.indexOf(grammar.getProduction(i).getSymbol()));
      automaton.setProductionLength(i, grammar.getProduction(i).getLength());
    }

    // for all itemsets I in itemsets C
    for (int state = 0; state<itemsets.getItemSetCount(); state++)
    {
      ItemSet I = itemsets.getItemSet(state);

      SymbolSet shiftsymbols = I.getShiftSymbols()// Transition symbols for shift actions
      SymbolSet reducesymbols = I.getReduceSymbols()// Lookahead symbols for reduce actions

      for (int symbol = 0; symbol<tsymbols.getSymbolCount(); symbol++)
      {
        IntegerSet reduceproductions = I.getReduceProductions(tsymbols.getSymbol(symbol));
        int productionpriority = -1;
        int highestproduction = -1;
        for (int k = 0; k<reduceproductions.getCount(); k++)
        {
          ReduceReduceConflict reduceconflict = null;

          if (k>0)
          {
            reduceconflict =
              new ReduceReduceConflict(grammar, itemsets, state,
                                       (Terminal)tsymbols.getSymbol(symbol),
                                       reduceproductions.get(k-1), reduceproductions.get(k));

            conflicts.addConflict(reduceconflict);
          }

          if (grammar.getPriority(grammar.getProduction(reduceproductions.get(k)))>productionpriority)
          {
            highestproduction = reduceproductions.get(k);
            productionpriority = grammar.getPriority(grammar.getProduction(highestproduction));

            if ((log!=null) && (reduceconflict!=null))
              log.info(reduceconflict.toString());
          }
          else if (grammar.getPriority(grammar.getProduction(reduceproductions.get(k)))==productionpriority)
          {
            if (log!=null)
              log.warn(reduceconflict.toString());
          }
        }

        if (reduceproductions.getCount()>1)
          if ((log!=null) && (log.isInfoEnabled()))
            log.info("The parser will reduce the production "+
                     grammar.getProduction(highestproduction));

        boolean errorreduce = reducesymbols.contains(Error.instance);

        if (shiftsymbols.contains(tsymbols.getSymbol(symbol)))
        {
          if ((errorreduce) || (reducesymbols.contains(tsymbols.getSymbol(symbol))))
          {
            int tokenpriority = grammar.getPriority((Terminal)tsymbols.getSymbol(symbol));

            ShiftReduceConflict shiftconflict =
              new ShiftReduceConflict(grammar, itemsets, state,
                                      (Terminal)tsymbols.getSymbol(symbol), highestproduction);

            if (tokenpriority>productionpriority)
            {
              automaton.setShiftAction(state, symbol, I.getTransition(tsymbols.getSymbol(symbol)));

              if (log!=null)
              {
                log.info(shiftconflict.toString());
                log.info("The parser will shift");
              }
            }
            else if (tokenpriority<productionpriority)
            {
              automaton.setReduceAction(state, symbol, highestproduction);

              if (log!=null)
              {
                log.info(shiftconflict.toString());
                log.info("The parser will reduce");
              }
            }
            else
            {
              if (log!=null)
                log.warn(shiftconflict.toString());

              Associativity associativity =
                grammar.getAssociativity((Terminal)tsymbols.getSymbol(symbol));
              if (associativity.equals(Associativity.RIGHT))
              {
                // if the terminal has a right associativity
                automaton.setShiftAction(state, symbol, I.getTransition(tsymbols.getSymbol(symbol)));

                if (log!=null)
                  log.warn("The parser will shift");
              }
              else if (associativity.equals(Associativity.LEFT))
              {
                // if the terminal has a left associativity
                automaton.setReduceAction(state, symbol, highestproduction);

                if (log!=null)
                  log.warn("The parser will reduce");
              }
              else
              {
                // SHIFT should be always prefered
                automaton.setShiftAction(state, symbol, I.getTransition(tsymbols.getSymbol(symbol)));

                if (log!=null)
                  log.warn("The parser will shift");
              }
            }
          }
          else
            automaton.setShiftAction(state, symbol, I.getTransition(tsymbols.getSymbol(symbol)));
        }
        else if ((errorreduce) || (reducesymbols.contains(tsymbols.getSymbol(symbol))))
          automaton.setReduceAction(state, symbol, highestproduction);
        else
          for (int i = 0; i<shiftsymbols.getSymbolCount(); i++)
            if (shiftsymbols.getSymbol(i) instanceof Error)
              automaton.setErrorAction(state, symbol, I.getTransition(shiftsymbols.getSymbol(i)));
      }

      // create all actions for the end of file.
      if (reducesymbols.contains(EOF))
      {
        IntegerSet reduceproductions = I.getReduceProductions(EOF);
        int productionpriority = -1;
        int highestproduction = -1;
        for (int k = 0; k<reduceproductions.getCount(); k++)
View Full Code Here

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

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

    SymbolSet usedsymbols = grammar.getSymbols();

    symbols = new Symbol[usedsymbols.getSymbolCount()];
    firstsets = new SymbolSet[usedsymbols.getSymbolCount()];
    for (int i = 0; i<usedsymbols.getSymbolCount(); i++)
    {
      if (log!=null)
        log.debug("Generating first set for "+usedsymbols.getSymbol(i).getName());

      symbols[i] = usedsymbols.getSymbol(i);
      firstsets[i] = first(symbols[i]);
    }
  }
View Full Code Here

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

   *
   * @return Set of symbols.
   */
  public SymbolSet getFirstSet(SymbolList symbols)
  {
    SymbolSet firstset = new SymbolSet();

    if (symbols.getSymbolCount()==0)
    {
      firstset.addSymbol(EMPTYLIST);
      return firstset;
    }

    int position = 0;
    do
    {
      firstset.removeSymbol(EMPTYLIST);

      if (symbols.getSymbol(position) instanceof Terminal)
        firstset.addSymbol(symbols.getSymbol(position));
      else
        firstset.addSymbol(getFirstSet(symbols.getSymbol(position)));

      position++;
    }
    while ((firstset.contains(EMPTYLIST)) && (position<symbols.getSymbolCount()));

    return firstset;
  }
View Full Code Here

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

   *
   * @return Set of symbol.
   */
  private SymbolSet first(Symbol symbol)
  {
    return first(symbol, new SymbolSet());
  }
View Full Code Here

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

   *
   * @return Set of symbols.
   */
  private SymbolSet first(Symbol symbol, SymbolSet visited)
  {
    SymbolSet firstset = new SymbolSet();

    // if the symbol is a terminal symbol
    if (symbol instanceof Terminal)
    {
      firstset.addSymbol(symbol);
      return firstset;
    }

    if (visited.contains(symbol))
      return firstset;
    else
      visited.addSymbol(symbol);

    // if is a non terminal symbol
    IntegerList productions = grammar.getProductionList(symbol);
    SymbolSet examined = new SymbolSet()// List of all examined symbols

    for (int i = 0; i<productions.getCount(); i++)
    {
      SymbolList productiondefinition = grammar.getProduction(productions.get(i)).getDefinition();
      if (productiondefinition.getSymbolCount()==0)

        // Symbol for a empty firstset added
        firstset.addSymbol(EMPTYLIST);
      else
      {
        // for every symbol in the production
        int j = 0;
        Symbol newsymbol;
        boolean foundEmptyList;
        do
        {
          foundEmptyList = true;
          newsymbol = productiondefinition.getSymbol(j);
          if (newsymbol instanceof Terminal)

            // if a terminal symbol
            firstset.addSymbol(newsymbol);
          else if (!newsymbol.equals(symbol))
          {
            // and if a non terminal symbol
            if (!examined.contains(newsymbol))
            {
              SymbolSet newfirstset = first(newsymbol, visited);
              foundEmptyList = newfirstset.contains(EMPTYLIST);
              for (int k = 0; k<newfirstset.getSymbolCount(); k++)
                if (!newfirstset.getSymbol(k).equals(EMPTYLIST))
                  firstset.addSymbol(newfirstset.getSymbol(k));

              examined.addSymbol(newsymbol);
            }
          }

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("first(");
      buffer.append(symbols.getSymbol(symbol).toString());
      buffer.append(")=");
      buffer.append(getFirstSet(symbols.getSymbol(symbol)).toString());
      buffer.append("\n");
    }

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

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

  {
    this.grammar = grammar;
    this.firstsets = firstsets;
    this.log = log;

    SymbolSet usedsymbols = grammar.getSymbols();

    symbols = new Symbol[usedsymbols.getSymbolCount()];
    followsets = new SymbolSet[usedsymbols.getSymbolCount()];
    for (int i = 0; i<usedsymbols.getSymbolCount(); i++)
    {
      if (log!=null)
        log.debug("Generating follow set for "+usedsymbols.getSymbol(i).getName());

      symbols[i] = usedsymbols.getSymbol(i);
      followsets[i] = follow(symbols[i]);
    }
  }
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.