Package net.sourceforge.chaperon.model.symbol

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


    buffer.append(super.toString());
    buffer.append("\n");

    buffer.append("Terminal symbols:\n");
    SymbolSet tsymbols = getSymbols().getTerminals();
    for (int i = 0; i < tsymbols.getSymbolCount(); i++)
    {
      buffer.append(String.valueOf(i));
      buffer.append(".Terminal: ");
      buffer.append(tsymbols.getSymbol(i));
      buffer.append(" Priority=");
      buffer.append(String.valueOf(getPriority((Terminal)tsymbols.getSymbol(i))));
      buffer.append(" Associativity=");
      buffer.append(String.valueOf(getAssociativity((Terminal)tsymbols.getSymbol(i))));
      buffer.append("\n");
    }

    buffer.append("Produktions:\n");
    for (int i = 0; i < getProductionCount(); i++)
View Full Code Here


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

    SymbolSet symbols = grammar.getSymbols();

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

    // C = closure( [S'=^S,EOF] )
    IntegerList changedState = new IntegerList()// 0=not changed 1=changed

    addItemSet(getStartItemSet());
    changedState.add(CHANGED);

    boolean mustrepeat = false;
    for (int i = 0; i<getItemSetCount(); i++)
    {
      changedState.set(i, NOTCHANGED);

      ItemSet I = getItemSet(i);

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

        if (!J.isEmpty())
        {
          int index = indexOfCore(J);
          if (index<0// if C doesn't contain J
          {
            index = addItemSet(J);

            changedState.add(CHANGED);
          }
          else  // otherwise the found state extends through J
          {
            if (getItemSet(index).addItemSet(J))  // if the found state change
            {
              if (index<changedState.getCount())
                changedState.set(index, CHANGED);
              else
                changedState.add(CHANGED);

              if (index<=i// if J before I, and J


                // was changed then must the loop repeat
                mustrepeat = true;
            }
          }

          I.setTransition(nextnonterminals.getSymbol(j), index)// stores the transition for this symbol
        }
      }

      // and for the terminal symbols
      SymbolSet nextterminals = I.getNextTerminals();
      for (int j = 0; j<nextterminals.getSymbolCount(); j++)
      {
        ItemSet J = I.jump(nextterminals.getSymbol(j));

        if (!J.isEmpty())
        {
          int index = indexOfCore(J);
          if (index<0// if C doesn't contain J
          {
            index = addItemSet(J);

            changedState.add(CHANGED);
          }
          else  // otherwise the found state extends through J
          {
            if (getItemSet(index).addItemSet(J))  // if the found state change
            {
              if (index<changedState.getCount())
                changedState.set(index, CHANGED);
              else
                changedState.add(CHANGED);

              if (index<=i// if J before I, and J


                // was changed then must the loop repeat
                mustrepeat = true;
            }
          }

          I.setTransition(nextterminals.getSymbol(j), index)// stores the transition for this symbol
        }
      }
    }

    do
View Full Code Here

    for (int i = 0; i<elementCount; i++)
      if ((productions[i]==production) && (positions[i]==position))
      {
        if (lookaheads[i]==null)
          lookaheads[i] = new SymbolSet();

        return lookaheads[i].addSymbol(lookahead);
      }

    ensureCapacity(elementCount+1);
    productions[elementCount] = production;
    positions[elementCount] = position;
    lookaheads[elementCount] = new SymbolSet();
    lookaheads[elementCount++].addSymbol(lookahead);

    return true;
  }
View Full Code Here

    for (int i = 0; i<elementCount; i++)
      if ((productions[i]==production) && (positions[i]==position))
      {
        if (this.lookaheads[i]==null)
          this.lookaheads[i] = new SymbolSet();

        return this.lookaheads[i].addSymbol(lookaheads);
      }

    ensureCapacity(elementCount+1);
    productions[elementCount] = production;
    positions[elementCount] = position;
    this.lookaheads[elementCount] = new SymbolSet();
    this.lookaheads[elementCount++].addSymbol(lookaheads);

    return true;
  }
View Full Code Here

    return EMPTYLIST;
  }

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

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

    return set;
  }
View Full Code Here

    return set;
  }

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

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

    return set;
  }
View Full Code Here

   */
  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

   *
   * @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

            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

    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

TOP

Related Classes of net.sourceforge.chaperon.model.symbol.SymbolSet

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.