void Grammar::generateStates() {
  verbose(2, _T("Computing FIRST1 sets\n"));
  findFirst1Sets();
  m_stateMap.clear();
  m_unfinishedSet.clear();

  BitSet eoiset(getTerminalCount());
  eoiset.add(0); // EOI
  LR1Item initialItem(true, 0, 0, eoiset);
  LR1State initialState(0);
  initialState.addItem(initialItem);

  computeClosure(initialState, true);
  addState(initialState);
//  dump(initialState);
  verbose(2, _T("Computing LALR(1) states\n"));

  for(int i = 0; i < getStateCount(); i++) {
    verbose(2, _T("state %d\r"), i);
    computeSuccessors(m_states[i]);
  }

/*
  Array<int> length = m_stateHash.length();
  FILE *ff = fopen("c:\\temp\\hlength.dat", "w");
  for(i = 0; i < length.size(); i++) {
    fprintf(ff, "%d %d\n", i, length[i]);
  }
  fclose(ff);
*/
  while(!m_unfinishedSet.isEmpty()) {
    const int unfinishedState = m_unfinishedSet.select();
    m_unfinishedSet.remove(unfinishedState);
    verbose(2, _T("Closing state %d.\r"), unfinishedState);
    computeClosure(m_states[unfinishedState], false);
    computeSuccessors(m_states[unfinishedState]);
  }

  verbose(2, _T("Checking consistensy\n"));

  m_warningCount = m_SRconflicts = m_RRconflicts = 0;
  for(int i = 0; i < getStateCount(); i++) {
    LR1State &state = m_states[i];
    m_result.add(StateResult());
    checkStateIsConsistent(state, m_result.last());
  }
}
int Grammar::getItemCount() const {
  const int n = getStateCount();
  int count = 0;
  for(int i = 0; i < n; i++) {
    count += (int)m_states[i].m_items.size();
  }
  return count;
}
void Grammar::computeSuccessors(LR1State &state) {
//dump(state); pause();
  BitSet itemsDone(state.m_items.size()); // this is correct!
  for(UINT i = 0; i < state.m_items.size(); i++) {
    if(itemsDone.contains(i)) {
      continue;
    }
    const LR1Item &origItem = state.m_items[i];
    if(isAcceptItem(origItem)) {
      continue; // No successor, but accept
    }
    const Production &origProduction = getProduction(origItem);
    if(origItem.m_dot < origProduction.getLength()) { // origItem is A -> alfa . X beta [la]
      CompactIntArray successorItems;
      successorItems.add(i);
      int symbolNr = origProduction.m_rightSide[origItem.m_dot];
      LR1State newState(getStateCount());
      const LR1Item newItem(true, origItem.m_prod, origItem.m_dot+1, origItem.m_la); // newItem is A -> alfa X . beta [la] (kernelItem)
      newState.addItem(newItem);
      itemsDone += i;
      for(UINT k = i+1; k < state.m_items.size(); k++) {
        if(itemsDone.contains(k)) {
          continue;
        }
        const LR1Item    &sameSymbolItem = state.m_items[k];
        const Production &sameSymbolProd = getProduction(sameSymbolItem);
        if(sameSymbolItem.m_dot < sameSymbolProd.getLength()
          && sameSymbolProd.m_rightSide[sameSymbolItem.m_dot] == symbolNr) { // sameSymbolItem is C -> gamma . X zeta [la1]
          const LR1Item newItem1(true, sameSymbolItem.m_prod, sameSymbolItem.m_dot+1, sameSymbolItem.m_la); // newItem1 is C -> gamma X . zeta [la1] (kernelItem)
          newState.addItem(newItem1);
          itemsDone += k;
          successorItems.add(k);
        }
      }
      newState.sortItems();
      int succStateIndex = findState(newState);
      if(succStateIndex < 0) {
        computeClosure(newState, true);
        succStateIndex = addState(newState);
      } else {
        if(mergeLookahead(m_states[succStateIndex], newState)) {
          m_unfinishedSet.add(succStateIndex);
//          printf(_T(")%d ", succStateIndex);
        }
      }

      assert(succStateIndex >= 0);

      for(size_t j = 0; j < successorItems.size(); j++) {
        state.m_items[successorItems[j]].m_succ = succStateIndex;
      }
    }
  }
}
Exemple #4
0
void StateMachineApp::setup() {
    int numStates = getStateCount();
    for(int i = 0; i < numStates; i++) {
        State* newState = createState(i);
        newState->setup();

        states.push_back(newState);
    }

    if(numStates > 0) {
        currentState = states[0];
        currentState->in();
    }

    setupInternal();
}
nfa* createNFA(char *regex){
	//printf("Creating NFA for |%s|\n",regex);
	int len=strlen(regex),i=0,j=0,temp;
	nfa* automata=initNFA(getStateCount(regex));
	while(i<automata->nStates && j<len){
		//printf("Innitialising state #%d\n",i);
		automata->states[i]=initQ();
		
		//printf("Reading regex at #%d\n",j);
		if(regex[j]!='['){
			//printf("Enqeueing terminal %c to state %d(line 51)\n",regex[j],i);
			enqueue(automata->states[i],regex[j]-0);
		}else{
			//printf("Found segment start\n");
			j++;
			if(regex[j]!='('){
				//printf("Enqeueing terminal %c to state %d(line 57)\n",regex[j],i);
				enqueue(automata->states[i],regex[j]-0);
			}
			j++;
			while(regex[j]!=']'){
				if(regex[j]!='(' && regex[j]!=')' && regex[j]!='-'){
					//printf("Enqeueing terminal %c to state %d(line63)\n",regex[j],i);
					enqueue(automata->states[i],regex[j]-0);
				}
				if(regex[j]=='-'){
					//printf("Found range marker\n");
					temp=getBack(automata->states[i])+1;
					while(temp!=regex[j+1]+1){
						//printf("Enqeueing terminal %c to state %d(line 70)\n",temp,i);
						enqueue(automata->states[i],temp);
						temp++;
					}
					j++;
				}
				j++;
			}
		}
		if(j<len-1 && regex[j+1]=='*' && i<automata->nStates){
			//printf("Found kleene star\n");
			automata->quants[i]=STAR;
			j++;
		}
		else if(j<len-1 && regex[j+1]=='+' && i<automata->nStates){
			//printf("Found plus\n");
			automata->quants[i]=PLUS;
			j++;
		}else if(j<len-1 && regex[j+1]=='|' && i<automata->nStates){
			//printf("Found or\n");
			automata->quants[i]=OR;
			j++;
		}
		else if(i<automata->nStates){
			//printf("No quantifiers found\n");
			automata->quants[i]=NONE;
		}
		j++;
		if(j<len){
			i++;
			//printf("Creating state #%d\n",i); 
		}
	}
	automata->nStates=i+1;
	//printf("\n\n");
	return automata;
}
Exemple #6
0
//private
void MenuMainState::buildMenu()
{
    const auto& font = m_fontResource.get("assets/fonts/VeraMono.ttf");
    
    auto button = xy::UI::create<xy::UI::Button>(font, m_textureResource.get("assets/images/ui/button.png"));
    button->setText("Settings");
    button->setPosition({ buttonMargin, buttonMargin });
    button->addCallback([this]()
    {
        showVideoOptions = !showVideoOptions;
        const auto& activeMode = getContext().appInstance.getVideoSettings().VideoMode;
        for (auto i = 0u; i < modes.size(); ++i)
        {
            if (modes[i] == activeMode)
            {
                currentResolution = i;
                break;
            }
        }
    });
    m_uiContainer.addControl(button);

    float currentSpacing = buttonSpacing + buttonMargin;
    button = xy::UI::create<xy::UI::Button>(font, m_textureResource.get("assets/images/ui/button.png"));
    button->setText("Sprite Editor");
    button->setPosition({ buttonMargin, currentSpacing });
    button->addCallback([this]()
    {
        if (getStateCount() > 1)
        {
            requestStackPop();
        }
        requestStackPush(States::ID::SpriteEditor);
    });
    m_uiContainer.addControl(button);
    currentSpacing += buttonSpacing;

    button = xy::UI::create<xy::UI::Button>(font, m_textureResource.get("assets/images/ui/button.png"));
    button->setText("Particle Editor");
    button->setPosition({ buttonMargin, currentSpacing });
    button->addCallback([this]()
    {
        if (getStateCount() > 1)
        {
            requestStackPop();
        }
        requestStackPush(States::ID::ParticleEditor);
    });
    m_uiContainer.addControl(button);
    currentSpacing += buttonSpacing;

    button = xy::UI::create<xy::UI::Button>(font, m_textureResource.get("assets/images/ui/button.png"));
    button->setText("Model Viewer");
    button->setPosition({ buttonMargin, currentSpacing });
    button->addCallback([this]()
    {
        if (getStateCount() > 1)
        {
            requestStackPop();
        }
        requestStackPush(States::ID::MaterialEditor);
    });
    m_uiContainer.addControl(button);
    currentSpacing += buttonSpacing;

    button = xy::UI::create<xy::UI::Button>(font, m_textureResource.get("assets/images/ui/button.png"));
    button->setText("Quit");
    button->setPosition({ buttonMargin, currentSpacing });
    button->addCallback([this]() 
    {
        xy::App::quit();
    });
    m_uiContainer.addControl(button);
    currentSpacing += buttonSpacing;
}
emb_size_type getOptModelStateCount(emb_optimizer optim)
{
	return getStateCount(myModel);
}
void Grammar::dumpStates(int flags, MarginFile *f) const {
  for(int i = 0; i < getStateCount(); i++) {
    dump(m_states[i], flags, f);
  }
}
int Grammar::addState(const LR1State &state) { // return index of state
  const int stateIndex = getStateCount();
  m_states.add(state);
  m_stateMap.put(&m_states[stateIndex], stateIndex);
  return stateIndex;
}