Package net.sourceforge.chaperon.grammar.symbol

Examples of net.sourceforge.chaperon.grammar.symbol.SymbolList


   *
   * @return Symbol of the next position
   */
  private Symbol getItemNext(int index)
  {
    SymbolList productiondefinition;

    if (_positions[index] < ((productiondefinition =
        _grammar.getProductionList().getProduction(_productions[index]).getDefinition()).getSymbolCount()))
      return productiondefinition.getSymbol(_positions[index]);
    return EmptySymbol.EMPTY;
  }
View Full Code Here


  public ItemSet closure()
  {
    ItemSet J = new ItemSet(_grammar, _firstsets, this); // J=I

    int i, j, k;                                       // Index variables
    SymbolList productiondefinition;
    SymbolList b = new SymbolList();
    SymbolList b2 = new SymbolList(true);
    IntegerList productionlist;
    Symbol symbol;

    // for every item in itemset I
    for (i = 0; i < J._elementCount; i++)
    {
      productiondefinition = _grammar.getProductionList().getProduction(J._productions[i]).getDefinition();

      // and not A=XYZ^
      if (J._positions[i] < productiondefinition.getSymbolCount())
      {
        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.isTerminal())
        {
          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 alle terminal symbols from FIRST(va)
            do
            {
              if (productiondefinition.getSymbol(pos).isTerminal())
              {
                b2.clear();
                b2.addSymbol(productiondefinition.getSymbol(pos));
              }
              else
                b2 = _firstsets.getFirstSet(productiondefinition.getSymbol(pos));
              b.addSymbolList(b2);
              pos++;
            }
            while ((b2.contains(EmptySymbol.EMPTY)) && (pos < productiondefinition.getSymbolCount()));

            if (b.contains(EmptySymbol.EMPTY))
              b.addSymbol(J._lookaheads[i]);

            b.removeSymbol(EmptySymbol.EMPTY);
View Full Code Here

   *
   * @return List of symbols
   */
  public SymbolList getReduceSymbols()
  {
    SymbolList reducesymbols = new SymbolList(true);

    for (int i = 0; i < _elementCount; i++)
    {
      if (getItemNext(i).isEmpty()) // for all A=u^ and all symbols in FOLLOW(A)
        reducesymbols.addSymbol(_lookaheads[i]);
    }
    return reducesymbols;
  }
View Full Code Here

   * @return Content of the itemset
   */
  public String toString()
  {
    StringBuffer buffer = new StringBuffer();
    SymbolList list;

    for (int i = 0; i < _elementCount; i++)
    {
      buffer.append(_grammar.getProductionList().getProduction(_productions[i]).getSymbol());
      buffer.append("->");
      list = (SymbolList) _grammar.getProductionList().getProduction(_productions[i]).getDefinition();

      for (int j = 0; j < list.getSymbolCount(); j++)
      {
        if (j == _positions[i])
          buffer.append(".");

        buffer.append(list.getSymbol(j) + " ");
      }

      if (_positions[i] == list.getSymbolCount())
        buffer.append(".");

      if (_lookaheads[i] != null)
        buffer.append("," + _lookaheads[i]);

View Full Code Here

          }
        }
      }


    SymbolList symbols;
    do
    {
      mustrepeat = false;

      for (int i = 0; i < C.getSize(); i++)
        if (changedState.get(i) != 0)
        {
          changedState.set(i, 0);
          I = C.get(i);

          if (_logger!=null)
          {
            _logger.debug("Inspecting state "+i+"\n"+I);
          }

          symbols = I.getShiftSymbols();
         
          for (int j = 0; j < symbols.getSymbolCount(); j++)
          {
            J = I.gotoX(symbols.getSymbol(j));
            index = I.getTransition(symbols.getSymbol(j));
            changed = C.get(index).add(J); // if the found state change

            if (changed)
            {
              if (index < changedState.getSize())
                changedState.set(index, 1);
              else
                changedState.add(1);

              if (index <= i) // if J before I, and J
                // was changed then must the loop repeat
                mustrepeat = true;
            }

            if (_logger!=null)
              _logger.debug("State "+i+" + "+symbols.getSymbol(j)+" -> State "+index);
          }
        }

    }
    while (mustrepeat);                        // Repeat till no state changed
View Full Code Here

    // for all itemsets I in collection C
    ItemSet I;
    Token token;
    IntegerList reduceproductions;
    int highestproduction, priority;
    SymbolList shiftsymbols, reducesymbols;

    for (i = 0; i < _C.getSize(); i++)
    {
      I = _C.get(i);

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

      for (j = 0; j < _tsymbols.getSymbolCount(); j++)
      {
        reduceproductions = I.getReduceProductions(_tsymbols.getSymbol(j));
        priority = -1;
        highestproduction = -1;
        for (k = 0; k < reduceproductions.getSize(); k++)
        {
          /*if ((_grammar.getProductionList().getProduction(reduceproductions.get(k)).getSymbol().equals(
                      _grammar.getStartSymbol())) && (_tsymbols.getSymbol(j).equals(endsymbol)))
            _table.setAcceptAction(i, j, reduceproductions.get(k));*/

          if (_grammar.getProductionList().getProduction(reduceproductions.get(k)).getPriority()
                  > priority)
          {
            highestproduction = reduceproductions.get(k);
            priority = _grammar.getProductionList().getProduction(highestproduction).getPriority();
          }
        }

        //if (!_table.isAcceptAction(i, j))

        if (shiftsymbols.contains(_tsymbols.getSymbol(j)))
        {
          if (reducesymbols.contains(_tsymbols.getSymbol(j)))
          {
            token = _grammar.getTokenList().getToken(_tsymbols.getSymbol(j));

            if (token.getPriority() > priority)
            {
              _table.setShiftAction(i, j, I.getTransition(_tsymbols.getSymbol(j)));

              if (_logger!=null)
                _logger.warn("Shift/Reduce Conflict State "+i+" between"+
                             System.getProperty(("line.separator"))+
                             token+System.getProperty(("line.separator"))+
                             _grammar.getProductionList().getProduction(highestproduction)+
                             System.getProperty(("line.separator"))+
                             "The parser will shift");
            }
            else if (token.getPriority() < priority)
            {
              /*if ((_grammar.getProductionList().getProduction(highestproduction).getSymbol().equals(
                      _grammar.getStartSymbol()))
                   && (_tsymbols.getSymbol(j).equals(endsymbol)))
                _table.setAcceptAction(i, j, highestproduction);
              else*/
                _table.setReduceAction(i, j, highestproduction);

              if (_logger!=null)   
                _logger.warn("Shift/Reduce Conflict State "+i+" between"+
                             System.getProperty(("line.separator"))+
                             token+System.getProperty(("line.separator"))+
                             _grammar.getProductionList().getProduction(highestproduction)+
                             System.getProperty(("line.separator"))+
                             "The parser will reduce");
            }
            else
            {
              if (token.getAssociativity()==Associativity.RIGHT)
              {
                _table.setShiftAction(i, j, I.getTransition(_tsymbols.getSymbol(j)));

                if (_logger!=null)   
                  _logger.warn("Shift/Reduce Conflict State "+i+" between"+
                               System.getProperty(("line.separator"))+
                               token+System.getProperty(("line.separator"))+
                               _grammar.getProductionList().getProduction(highestproduction)+
                               System.getProperty(("line.separator"))+
                               "The parser will shift");
              }
              else if (token.getAssociativity()==Associativity.LEFT)
              {
                /*if ((_grammar.getProductionList().getProduction(highestproduction).getSymbol().equals(
                        _grammar.getStartSymbol()))
                    && (_tsymbols.getSymbol(j).equals(endsymbol)))
                  _table.setAcceptAction(i, j, highestproduction);
                else*/
                  _table.setReduceAction(i, j, highestproduction);

                if (_logger!=null)   
                  _logger.warn("Shift/Reduce Conflict State "+i+" between"+
                               System.getProperty(("line.separator"))+
                               token+System.getProperty(("line.separator"))+
                               _grammar.getProductionList().getProduction(highestproduction)+
                               System.getProperty(("line.separator"))+
                               "The parser will reduce");
              }
              else
              {
                _table.setShiftAction(i, j, I.getTransition(_tsymbols.getSymbol(j)));

                if (_logger!=null)   
                  _logger.info("Shift/Reduce Conflict State "+i+" between"+
                               System.getProperty(("line.separator"))+
                               token+System.getProperty(("line.separator"))+
                               _grammar.getProductionList().getProduction(highestproduction)+
                               System.getProperty(("line.separator"))+
                               "The parser will shift");
                _conflicts.add(new ShiftReduceConflict(I, _tsymbols.getSymbol(j)));
              }
            }
          }
          else
            _table.setShiftAction(i, j, I.getTransition(_tsymbols.getSymbol(j)));
        }
        else if (reducesymbols.contains(_tsymbols.getSymbol(j)))
        {
          /*if ((_grammar.getProductionList().getProduction(highestproduction).getSymbol().equals(
                  _grammar.getStartSymbol()))
               && (_tsymbols.getSymbol(j).equals(endsymbol)))
            _table.setAcceptAction(i, j, highestproduction);
          else*/
            _table.setReduceAction(i, j, highestproduction);
        }

        for (k = 0; k < reduceproductions.getSize(); k++)
          if ((_grammar.getProductionList().getProduction(reduceproductions.get(k)).getSymbol().equals(
                      _grammar.getStartSymbol())) && (_tsymbols.getSymbol(j).equals(endsymbol)))
            _table.setAcceptAction(i, j, reduceproductions.get(k));
      }

      for (j = 0; j < _ntsymbols.getSymbolCount(); j++)
        if (shiftsymbols.contains(_ntsymbols.getSymbol(j)))
          _table.setTransition(i, j, I.getTransition(_ntsymbols.getSymbol(j)));
    }

    /*_table.setAcceptAction(_C.get(0).getTransition(_grammar.getStartSymbol()),
                           _ntsymbols.indexOf(_grammar.getStartSymbol()), 0);*/
 
View Full Code Here

  public FirstSets(Grammar grammar, Logger logger)
  {
    _grammar = grammar;
    enableLogging(logger);

    SymbolList usedsymbols = grammar.getSymbols();

    _symbols = new Symbol[usedsymbols.getSymbolCount()];
    _firstsets = new SymbolList[usedsymbols.getSymbolCount()];
    for (int i = 0; i < usedsymbols.getSymbolCount(); i++)
    {
      if (_logger!=null)
        _logger.debug("Generating first set for "+usedsymbols.getSymbol(i).getName());
      _symbols[i] = usedsymbols.getSymbol(i);
      _firstsets[i] = first(_symbols[i]);
    }
  }
View Full Code Here

  public SymbolList getFirstSet(Symbol symbol)
  {
    for (int i = 0; i < _symbols.length; i++)
      if (_symbols[i].equals(symbol))
        return _firstsets[i];
    return new SymbolList();
  }
View Full Code Here

   *
   * @return List of symbol
   */
  private SymbolList first(Symbol symbol)
  {
    return first(symbol, new SymbolList(true));
  }
View Full Code Here

   *
   * @return List of symbol
   */
  private SymbolList first(Symbol symbol, SymbolList visited)
  {
    SymbolList firstset = new SymbolList(true);

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

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

    //System.out.println("Symbol="+symbol.toString());

    // if is a non terminal symbol
    IntegerList productions = _grammar.getProductionList().getProductionList(symbol);
    SymbolList examined = new SymbolList(true); // List of all examined symbols
    SymbolList productiondefinition, newfirstset;
    boolean foundEMPTY;
    int i, j, k; // index variables
    Symbol newsymbol;

    for (i = 0; i < productions.getSize(); i++)
    {
      productiondefinition = _grammar.getProductionList().getProduction(
        productions.get(i)).getDefinition();
      if (productiondefinition.getSymbolCount() == 0)
      {
        // Symbol for a empty firstset added
        firstset.addSymbol(EmptySymbol.EMPTY);
      }
      else
      {
        // for every symbol in the production
        j = 0;
        do
        {
          foundEMPTY = true;
          newsymbol = productiondefinition.getSymbol(j);
          if (newsymbol.isTerminal())
          {
            // if a terminal symbol
            firstset.addSymbol(newsymbol);
          }
          else if (!newsymbol.equals(symbol))
          {
            // and if a non terminal symbol
            if (!examined.contains(newsymbol))
            {
              newfirstset = first(newsymbol, visited);
              foundEMPTY = newfirstset.contains(EmptySymbol.EMPTY);
              for (k = 0; k < newfirstset.getSymbolCount(); k++)
                if (!newfirstset.getSymbol(k).isEmpty())
                  firstset.addSymbol(newfirstset.getSymbol(k));
              examined.addSymbol(newsymbol);
            }
          }

          j++;
        }
        while ((!newsymbol.isTerminal()) && (foundEMPTY) &&
               (j < productiondefinition.getSymbolCount()) &&
               (!productiondefinition.getSymbol(j - 1).equals(symbol)));
      }
    }
    //System.out.println("Symbol="+symbol.toString()+" beendet");
    return firstset;
  }
View Full Code Here

TOP

Related Classes of net.sourceforge.chaperon.grammar.symbol.SymbolList

Copyright © 2018 www.massapicom. 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.