void GrammarParser::checkGrammar() {
  for(int i = 1; i < m_grammar.getSymbolCount(); i++) { // dont check EOI
    const GrammarSymbol &sym = m_grammar.getSymbol(i);
    if(sym.m_type == NONTERMINAL) {
      checkNonTerminal(i);
    } else {
      checkTerminal(i);
    }
  }
  if(m_grammar.getProductionCount() == 0) {
    m_lex.error(_T("No productions in grammar."));
  } else {
    checkDuplicateProd();
    checkReachability();
    checkTermination();
  }
}
示例#2
0
int main(void) {
    initialize();
    srand(time(NULL));

    int SHEEP_INTERVAL = 0;
    int COW_INTERVAL = 0;
    int THIEF_INTERVAL = 0;
    int HUNTER_INTERVAL = 0;

    long long sheep_time = 0;
    long long cow_time = 0;
    long long thief_time = 0;
    long long hunter_time = 0;

    int sheepSleepTime = 0;
    int cowSleepTime = 0;
    int thiefPathTime = 0;
    int hunterPathTime = 0;

    //========================================================
    printf("Input the Sheep Interval:");
    scanf("%d", &SHEEP_INTERVAL);
    printf("Input the maximum time that a sheep grazes:");
    scanf("%d", &sheepSleepTime);
    //=======================================================
    printf("Input the Cow Interval:");
    scanf("%d", &COW_INTERVAL);
    printf("Input the maximum time that a cow grazes:");
    scanf("%d", &cowSleepTime);
    //=======================================================
    printf("Input the Thief Interval:");
    scanf("%d", &THIEF_INTERVAL);
    printf("Input the maximum time that a thief looks for the path:");
    scanf("%d", &thiefPathTime);
    //========================================================
    printf("Input the Hunter Interval:");
    scanf("%d", &HUNTER_INTERVAL);
    printf("Input the maximum time that a hunter looks for the path:");
    scanf("%d", &hunterPathTime);
    //========================================================

    sheep_time += SHEEP_INTERVAL;
    cow_time += COW_INTERVAL;
    thief_time += THIEF_INTERVAL;
    hunter_time += HUNTER_INTERVAL;
    int genflag = 0; // the flag tells what to generate

    long long elapsetime = 0;
    long long lastelapsetime = 0;
    struct timeval lasttime;
    struct timeval curtime;
    gettimeofday(&lasttime, NULL);

    //produce smaug
    pid_t result = fork();
    if (result < 0) {
        printf("fork error\n");
        exit(1);
    }
    if (result == 0) {
        smaug();
    }
    else {
        pid_t r;
        while (1) {
            gettimeofday(&curtime, NULL);
            elapsetime += (curtime.tv_sec - lasttime.tv_sec) * 1000000 + (curtime.tv_usec - lasttime.tv_usec);
//	        pid_t localpid = getpid();
            //printf("In process: %d Elapsed time: %lld\n", localpid,elapsetime);
//            if(elapsetime - lastelapsetime >= 500000)
            lasttime = curtime;

            if (checkTermination()) {

                printf("****************************terminating in parent process**************************************************\n");

                terminateSimulation();
                int status;
                // block till all children exits
                waitpid(-1, &status, 0);
                printf("****************************In main: all children have exited\n");
                releaseResource();
                exit(0);
            }

            if (elapsetime > sheep_time) {
                genflag = 0;
                sheep_time += SHEEP_INTERVAL;
                r = fork();
                if (r == 0) break;
            }

            if (elapsetime > cow_time) {
                genflag = 1;
                cow_time += COW_INTERVAL;
                r = fork();
                if (r == 0) break;
            }

            if (elapsetime > thief_time) {
                genflag = 2;
                thief_time += THIEF_INTERVAL;
                r = fork();
                if (r == 0) break;
            }

            if (elapsetime > hunter_time) {
                genflag = 3;
                hunter_time += HUNTER_INTERVAL;
                r = fork();
                if (r == 0) break;
            }
        }
        //=============================================================
        if (genflag == 0) sheep(rand() % sheepSleepTime);
        else if (genflag == 1) cow(rand() % cowSleepTime);
        else if (genflag == 2) thief(rand() % thiefPathTime);
        else if (genflag == 3) hunter(rand() % hunterPathTime);
        exit(0);
    }
}