Ejemplo n.º 1
0
  public boolean add(StateSet set) {
    
    if (DEBUG) Out.dump("StateSet.add("+set+") start");

    if (set == null) return false;

    if (bits.length < set.bits.length) {
      long newbits[] = new long[set.bits.length];
      System.arraycopy(bits, 0, newbits, 0, bits.length);
      
      bits = newbits;
    }
    
    boolean changed = false;

    for (int i = 0; i < set.bits.length; i++) {
      long n = bits[i] | set.bits[i];
      if ( n != bits[i] ) changed = true;
      bits[i] = n;
    }
    
    if (DEBUG) {
      Out.dump("StateSet.add("+set+") end");
      Out.dump("Set is : "+this);
    }

    return changed;
  }
Ejemplo n.º 2
0
 public void addState(int state) {
   if (DEBUG) {
     Out.dump("StateSet.addState("+state+") start");
     Out.dump("Set is : "+this);
   }
   
   resize(state);
   bits[state >> BITS] |= (1L << (state & MASK));
   
   if (DEBUG) {
     Out.dump("StateSet.addState("+state+") end");
     Out.dump("Set is : "+this);
   }
 }
Ejemplo n.º 3
0
  /**
   * @throws ClassCastException if b is not a StateSet
   * @throws NullPointerException if b is null
   */
  public boolean equals(Object b) {

    int i = 0;
    int l1,l2;
    StateSet set = (StateSet) b;

    if (DEBUG) Out.dump("StateSet.equals("+set+"), this="+this);

    l1 = bits.length;
    l2 = set.bits.length;

    if (l1 <= l2) {      
      while (i < l1) {
        if (bits[i] != set.bits[i]) return false;
        i++;
      }
      
      while (i < l2) 
        if (set.bits[i++] != 0) return false;
    }
    else {
      while (i < l2) {
        if (bits[i] != set.bits[i]) return false;
        i++;
      }
      
      while (i < l1) 
        if (bits[i++] != 0) return false;
    }

    return true;
  }
Ejemplo n.º 4
0
 public int nextElement() {
   if (DEBUG)
     Out.dump(
         "nextElement, index = " + index + ", offset = " + offset); // $NON-NLS-1$ //$NON-NLS-2$
   int x = (index << StateSet.BITS) + offset;
   advance();
   return x;
 }
Ejemplo n.º 5
0
 public boolean hasMoreElements() {
   if (DEBUG)
     Out.dump(
         "hasMoreElements, index = "
             + index
             + ", offset = "
             + offset); //$NON-NLS-1$ //$NON-NLS-2$
   return index < bits.length;
 }
Ejemplo n.º 6
0
  private void advance() {

    if (DEBUG)
      Out.dump(
          "Advancing, at start, index = "
              + index
              + ", offset = "
              + offset); //$NON-NLS-1$ //$NON-NLS-2$

    // cache fields in local variable for faster access
    int _index = this.index;
    int _offset = this.offset;
    long _mask = this.mask;
    long[] _bits = this.bits;

    long bi = _bits[_index];

    do {
      _offset++;
      _mask <<= 1;
    } while (_offset <= StateSet.MASK && ((bi & _mask) == 0));

    if (_offset > StateSet.MASK) {
      int length = _bits.length;

      do _index++;
      while (_index < length && _bits[_index] == 0);

      if (_index >= length) {
        this.index = length; // indicates "no more elements"
        return;
      }

      _offset = 0;
      _mask = 1;
      bi = _bits[_index];

      // terminates, because bi != 0
      while ((bi & _mask) == 0) {
        _mask <<= 1;
        _offset++;
      }
    }

    // write back cached values
    this.index = _index;
    this.mask = _mask;
    this.offset = _offset;
  }
Ejemplo n.º 7
0
  public boolean containsSet(StateSet set) {

    if (DEBUG)
      Out.dump("StateSet.containsSet("+set+"), this="+this);

    int i;
    int min = Math.min(bits.length, set.bits.length);
    
    for (i = 0; i < min; i++) 
      if ( (bits[i] & set.bits[i]) != set.bits[i] ) return false;
    
    for (i = min; i < set.bits.length; i++)
      if ( set.bits[i] != 0 ) return false;
    
    return true;
  }
Ejemplo n.º 8
0
 /**
  * Returns the set of elements that contained are in the specified set
  * but are not contained in this set.
  */
 public StateSet complement(StateSet set) {
   
   if (set == null) return null;
   
   StateSet result = new StateSet();
   
   result.bits = new long[set.bits.length];
   
   int i;
   int m = Math.min(bits.length, set.bits.length);
   
   for (i = 0; i < m; i++) {
     result.bits[i] = ~bits[i] & set.bits[i];
   }
   
   if (bits.length < set.bits.length) 
     System.arraycopy(set.bits, m, result.bits, m, result.bits.length-m);
   
   if (DEBUG) 
     Out.dump("Complement of "+this+Out.NL+"and "+set+Out.NL+" is :"+result);
   
   return result;
 }
Ejemplo n.º 9
0
  /**
   * Generates a scanner for the specified input file.
   *
   * @param inputFile a file containing a lexical specification to generate a scanner for.
   */
  public static void generate(File inputFile) {

    Out.resetCounters();

    Timer totalTime = new Timer();
    Timer time = new Timer();

    LexScan scanner = null;
    LexParse parser = null;
    FileReader inputReader = null;

    totalTime.start();

    try {
      Out.println(ErrorMessages.READING, inputFile.toString());
      inputReader = new FileReader(inputFile);
      scanner = new LexScan(inputReader);
      scanner.setFile(inputFile);
      parser = new LexParse(scanner);
    } catch (FileNotFoundException e) {
      Out.error(ErrorMessages.CANNOT_OPEN, inputFile.toString());
      throw new GeneratorException();
    }

    try {
      NFA nfa = (NFA) parser.parse().value;

      Out.checkErrors();

      if (Options.dump) Out.dump(ErrorMessages.get(ErrorMessages.NFA_IS) + Out.NL + nfa + Out.NL);

      if (Options.dot) nfa.writeDot(Emitter.normalize("nfa.dot", null)); // $NON-NLS-1$

      Out.println(ErrorMessages.NFA_STATES, nfa.numStates);

      time.start();
      DFA dfa = nfa.getDFA();
      time.stop();
      Out.time(ErrorMessages.DFA_TOOK, time);

      dfa.checkActions(scanner, parser);

      nfa = null;

      if (Options.dump) Out.dump(ErrorMessages.get(ErrorMessages.DFA_IS) + Out.NL + dfa + Out.NL);

      if (Options.dot) dfa.writeDot(Emitter.normalize("dfa-big.dot", null)); // $NON-NLS-1$

      Out.checkErrors();

      time.start();
      dfa.minimize();
      time.stop();

      Out.time(ErrorMessages.MIN_TOOK, time);

      if (Options.dump) Out.dump(ErrorMessages.get(ErrorMessages.MIN_DFA_IS) + Out.NL + dfa);

      if (Options.dot) dfa.writeDot(Emitter.normalize("dfa-min.dot", null)); // $NON-NLS-1$

      time.start();

      Emitter e = new Emitter(inputFile, parser, dfa);
      e.emit();

      time.stop();

      Out.time(ErrorMessages.WRITE_TOOK, time);

      totalTime.stop();

      Out.time(ErrorMessages.TOTAL_TIME, totalTime);
    } catch (ScannerException e) {
      Out.error(e.file, e.message, e.line, e.column);
      throw new GeneratorException();
    } catch (MacroException e) {
      Out.error(e.getMessage());
      throw new GeneratorException();
    } catch (IOException e) {
      Out.error(ErrorMessages.IO_ERROR, e.toString());
      throw new GeneratorException();
    } catch (OutOfMemoryError e) {
      Out.error(ErrorMessages.OUT_OF_MEMORY);
      throw new GeneratorException();
    } catch (GeneratorException e) {
      throw new GeneratorException();
    } catch (Exception e) {
      e.printStackTrace();
      throw new GeneratorException();
    }
  }