Package net.sourceforge.chaperon.helpers

Examples of net.sourceforge.chaperon.helpers.IntegerList


    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);
          }
          else if (pos >= productiondefinition.getSymbolCount())
            // otherwise is b FIRST(a)
            b.addSymbol(J._lookaheads[i]);

          // list of all productions B
          productionlist = _grammar.getProductionList().getProductionList(symbol);

          // for alle productions B
          for (j = 0; j < productionlist.getSize(); j++)
          {
            // if J doesn't contain [B=^w,b] , should it added
            for (k = 0; k < b.getSymbolCount(); k++)
            {
              if (!J.containsItem(productionlist.get(j), 0, b.getSymbol(k)))
                J.addItem(productionlist.get(j), 0, b.getSymbol(k));
            }
          }
        }
      }
    }
View Full Code Here


   *
   * @return List of indicies from the productions
   */
  public IntegerList getReduceProductions()
  {
    IntegerList reduceproductions = new IntegerList(true);

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

   *
   * @return List of indicies from the productions
   */
  public IntegerList getReduceProductions(Symbol lookahead)
  {
    IntegerList reduceproductions = new IntegerList(true);

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

  private Collection items()
  {
    // C = closure( [S'=^S,EOF] )
    Collection C = new Collection(_grammar);

    IntegerList changedState = new IntegerList(); // 0=not changed 1=changed

    ItemSet I = new ItemSet(_grammar, _firstsets);
    IntegerList startlist = _grammar.getProductionList().getProductionList(_grammar.getStartSymbol());
    Symbol endsymbol = _tsymbols.getSymbol("EOF");

    for (int i = 0; i < startlist.getSize(); i++)
      I.addItem(startlist.get(i), 0, endsymbol);

    C.add(I.closure());
    changedState.add(1);

    ItemSet J;
View Full Code Here

        _table.setTransition(i, j, 0);

    // 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)));
View Full Code Here

      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);
      }
View Full Code Here

    long time = System.currentTimeMillis();

    int state = 0; // Current state
    int i, j, oldstate;
    IntegerList stack = new IntegerList();
    stack.push(0); // First state is zero
    Stack treestack = new Stack();

    int tokenindex = -1;   // Index of the token from the input

    while (true)
    {
      state = stack.peek();

      /* ============================= Reading token ================================ */
      if (tokenindex < 0)
      {
        do
        {
          scanner.readNextToken(state);
          tokenindex = scanner.getTokenIndex();

          //System.out.println("tokenindex="+tokenindex+" ignorable="+scanner.isIgnorable());

          if (scanner.isIgnorable())
          {
            TokenContainer tokencontainer = new TokenContainer();
            tokencontainer.tokenindex = tokenindex;
            tokencontainer.textstart = scanner.getTextStart();
            tokencontainer.textlength = scanner.getTextLength();
            tokencontainer.ignorable = true;
            treestack.push(tokencontainer);
          }
        } while ((tokenindex>=0) && (scanner.isIgnorable()));

        if (tokenindex < 0)
        {
          if (_logger!=null)
            _logger.debug("State " + state + " no token could get recognized");

          Vector acceptedsymbols = new Vector();

          for (tokenindex = 0; tokenindex < table.getTerminalSymbolCount(); tokenindex++)
            if (!table.isErrorAction(state, tokenindex))
              acceptedsymbols.addElement(table.getTerminalSymbol(tokenindex));

          String[] symbols = new String[acceptedsymbols.size()];

          for (i = 0; i < acceptedsymbols.size(); i++)
            symbols[i] = (String) acceptedsymbols.elementAt(i);

          throw new ParserException("Token was not recognized",
                                    scanner.getLineNumber(),
                                    scanner.getColumnNumber(), symbols);
        }
      }

      switch (table.getAction(state, tokenindex))
      {
      /* ================================== Error =================================== */
      case ParserTable.ERROR :
        if (_logger!=null)
          _logger.debug("State " + state + " error token \"" +
                        Decoder.decode(new String(text, scanner.getTextStart(), scanner.getTextLength())) +
                        "\"("+table.getTerminalSymbol(tokenindex)+")");

        Vector acceptedsymbols = new Vector();

        for (tokenindex = 0; tokenindex < table.getTerminalSymbolCount(); tokenindex++)
          if (!table.isErrorAction(state, tokenindex))
            acceptedsymbols.addElement(table.getTerminalSymbol(tokenindex));

        String[] symbols = new String[acceptedsymbols.size()];

        for (i = 0; i < acceptedsymbols.size(); i++)
          symbols[i] = (String) acceptedsymbols.elementAt(i);

        throw new ParserException("Token was not recognized",
                                  scanner.getLineNumber(),
                                  scanner.getColumnNumber(), symbols);

      /* ==================================== Shift  =================================== */
      case ParserTable.SHIFT :
        if (_logger!=null)
          _logger.debug("State " + state + " shift token \"" +
                        Decoder.decode(new String(text, scanner.getTextStart(), scanner.getTextLength())) +
                        "\"("+table.getTerminalSymbol(tokenindex)+")");
        TokenContainer tokencontainer = new TokenContainer();
        tokencontainer.tokenindex = tokenindex;
        tokencontainer.textstart = scanner.getTextStart();
        tokencontainer.textlength = scanner.getTextLength();
        treestack.push(tokencontainer);

        stack.push(table.getActionArgument(state, tokenindex));
        tokenindex = -1;
        break;
     
      /* ============================ Reduce & Accept =================================== */
      case ParserTable.REDUCE :
      case ParserTable.ACCEPT :
        if (_logger!=null)
          _logger.debug("State " + state + " reduce by " +
                        table.getNonTerminalSymbol(table.getProductionSymbol(
                          table.getActionArgument(state, tokenindex))) +
                        " (" + (table.getActionArgument(state, tokenindex)) + ")");
        ProductionContainer newcontainer = new ProductionContainer();
        newcontainer.productionindex = table.getActionArgument(state, tokenindex);

        for (i = 0; i < table.getProductionLength(table.getActionArgument(state, tokenindex));  i++)
        {
          if (treestack.peek() instanceof ProductionContainer)
          {
            stack.pop();
            ProductionContainer container = (ProductionContainer) treestack.pop();
            switch (table.getProductionReduceType(container.productionindex))
            {
              case ParserTable.APPEND : // APPEND
                if (table.getProductionSymbol(newcontainer.productionindex)==
                    table.getProductionSymbol(container.productionindex))
                  newcontainer.insertChilds(container);
                else
                  newcontainer.insert(container);
                break;
 
              case ParserTable.RESOLVE : // RESOLVE
                newcontainer.insertChilds(container);
                break;

              case ParserTable.NORMAL : // NORMAL
                newcontainer.insert(container);
                break;
 
              case ParserTable.NEGLECT : // NEGLECT
                break;
            }
          }
          else
          {
            TokenContainer container = (TokenContainer) treestack.pop();
            newcontainer.insert(container);

            if (container.ignorable)
              i--;
            else
              stack.pop();
          }
        }
        oldstate = stack.peek();
        treestack.push(newcontainer);
        //stack.push(table.getTransition(oldstate, table.getProductionSymbol(table.getActionArgument(state,
        //            tokenindex))));

      /* ================================== Accept =================================== */
        if ((table.isAcceptAction(state, tokenindex)) && (stack.getSize() == 1))
        {
          if (_logger!=null)
           _logger.debug("State " + state + " accept & reduce production " +
                         table.getActionArgument(state, tokenindex));

          stack.pop();
          DocumentContainer documentcontainer = new DocumentContainer(table, text,
            (ProductionContainer)treestack.pop());

          if (_logger!=null)
            _logger.debug("Parsing time " + (System.currentTimeMillis() - time) + " ms");
          return documentcontainer;
        }
        else
          stack.push(table.getTransition(oldstate, table.getProductionSymbol(
            table.getActionArgument(state, tokenindex))));
      }
    }
  }
View Full Code Here

          _stack.push(automate.getTransitions(state));
      }

      if (_stack.empty())
      {
        IntegerList temp = _newstack;

        _newstack = _stack;
        _stack = temp;
        position++; // next character
      }
View Full Code Here

        throw new IllegalArgumentException("Definition element not recognized");
    }

    if (element.getMaxOccurs() < Integer.MAX_VALUE)
    {
      IntegerList labels = new IntegerList();

      for (int i = element.getMinOccurs(); i < element.getMaxOccurs(); i++)
      {
        labels.add(automate.addState(new int[]{ automate.getSize() + 1 }));

        if (element instanceof Alternation)
          evalAlternation(automate, (Alternation) element);
        else if (element instanceof BeginOfLine)
          evalBeginOfLine(automate, (BeginOfLine) element);
        else if (element instanceof CharacterClass)
          evalCharacterClass(automate, (CharacterClass) element);
        else if (element instanceof CharacterSequence)
          evalCharacterSequence(automate, (CharacterSequence) element);
        else if (element instanceof Concatenation)
          evalConcatenation(automate, (Concatenation) element);
        else if (element instanceof EndOfFile)
          evalEndOfFile(automate, (EndOfFile) element);
        else if (element instanceof EndOfLine)
          evalEndOfLine(automate, (EndOfLine) element);
        else if (element instanceof UniversalCharacter)
          evalUniversalCharacter(automate, (UniversalCharacter) element);
        else
          throw new IllegalArgumentException("Definition element not recognized");
      }
      for (int i = element.getMinOccurs(); i < element.getMaxOccurs(); i++)
        automate.addTransition(labels.get(i - element.getMinOccurs()),
        // automate.getSize() + 1);
        automate.getSize()); // experimentell
    }
    else                     // for max = infinity
    {
View Full Code Here

   *
   * @return List of indices from found productions
   */
  public IntegerList getProductionList(Symbol ntsymbol)
  {
    IntegerList list = new IntegerList();
    int i;

    for (i = 0; i < getProductionCount(); i++)
    {
      if (getProduction(i).getSymbol().equals(ntsymbol))
        list.add(i);
    }
    return list;
  }
View Full Code Here

TOP

Related Classes of net.sourceforge.chaperon.helpers.IntegerList

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.