// Returns survival during germination. double Strategy::germination(double m, const double Env[], double t) { double dmdt = ((*p).Y * (Production(Env, m) - Respiration(m)) - Turnover(m)) / LfAr(m); if (dmdt > 0.0) return 1.0 / (pow((*p).c_s0 / dmdt, 2.0) + 1.0); else return 0.0; }
void LL1Parsing::LL1() { OpenFile(); Production(); FisrtInit(); //FIRST集合初始化 //l.PrintVariFst(); GetVariFst(); //求FIRST集 //l.PrintVariFst(); GetVariFol(); //求FOLLOW集 //l.PrintVariFol(); GetProFst(); //求产生式的FIRST集合 //l.PrintProFst(); GetFAATable(); //构造预测分析表 //l.PrintFAATable(); stack<int> s; enum tokenType token; s.push(99); s.push(50); int top; token = GetNewToken(); do{ top = s.top(); if (top < t_len || top == 99){ if (top != 99 && top == GetCode(token)){ cout << "pop " << top << endl; s.pop(); token = GetNewToken(); } else { cerr << "error" << endl; } } else{ if (faaTable[top - 50][GetCode(token)].size()>0){ long long int code = faaTable[top - 50][GetCode(token)][0]; cout << top << "-->" << code << endl; s.pop(); if (code > 0){ for (size_t j = (int)((GetLength(code) + 1) / 2); j > 0; --j){ s.push(Get2Code(code, j)); } } } else { cerr << "error" << endl; } } } while (s.top() != 99 && !s.empty()); }
void CFG::augmentGrammar() { if(!augmented) { p.push_back(Production("_" + s, {s})); v.push_back("_" + s); s = "_" + s; augmented = true; } }
void ObservationTable::_buildBinaryRules(ContextFreeGrammar& G, map<NonTerminal, StringSet>& binaryRulesData) { for (pair<NonTerminal, StringSet> binaryRuleData : binaryRulesData) { for (string s : binaryRuleData.second) { NonTerminalNonTerminal* nTnT = G.getNonTerminalPairForString(s); G.P.insert(Production(binaryRuleData.first, nTnT)); G.getLexicalRules().insert({ s, binaryRuleData.first }); } } }
//========================================================================================================== // Create grammar from definitions in input file. // Production are of the form: // N -> A B C ... //========================================================================================================== void Grammar::read_grammar_file(string grammar_file) { ifstream file = ifstream(grammar_file); if(!file.is_open()) throw string("File " + grammar_file + " not found"); //------------------------------------------------------------------------------------------------------ // For each production line extract the symbols and add a production //------------------------------------------------------------------------------------------------------ for(string line; getline(file, line);) { if(trim(line, "//").empty()) continue; // Ignore comments and empty lines vector<Symbol> symbols; string action_name; // If an action is specified, a number sign will serve to separate between the production and it int split_pos = line.find_first_of('#'); extract_symbols(line.substr(0, split_pos), symbols); if(split_pos != string::npos) extract_action(line.substr(split_pos + 1), action_name); productions.push_back(Production(symbols, action_name)); } //------------------------------------------------------------------------------------------------------ // First production nonterminal considered the start symbol. If it is not START, add a production: // START -> FIRST-NONTERMINAL //------------------------------------------------------------------------------------------------------ if(productions[0][0] == START) { if(productions[0].size() != 2) throw string("Expected production for START to have just one symbol on right hand side"); if(not is_nonterminal(productions[0][1])) throw string("Expected production for START to have a nonterminal on the right hand side"); } else { productions.push_front(Production({START, productions[0][0]}, "")); } } // read_grammar_file()
// Calculates rates of growth, mortality and fecundity for individual with size m. Memory for GMR[5] must be allocated elsewhere // Env[] contains estimates of light environment at different depths in the canopy used in calculating production. void Strategy::Grow_Mort_Repro(vector<double>& GMR, double m, const double Env[], double t) { GMR[3] = Production(Env, m); // GPP GMR[6] = Respiration(m); // Maintenance respiration GMR[4] = (*p).Y * (GMR[3] - GMR[6]); // NPP GMR[5] = Turnover(m); // Tissue turnover double dmdt = GMR[4] - GMR[5]; // NET PRODUCTION GMR[0] = (1 - r_alloc(m)) / dTotalMass_dm(m) * max(0.0, dmdt); // GROWTH - only positive growth allowed GMR[1] = mortality(dmdt, m); // MORTALITY GMR[2] = (*p).Pi_0 * (max(0.0, dmdt) * r_alloc(m)) / ((*p).c_acc * total_mass_at_birth); // REPRODUCTION // Check for NaN in mortality if ((!(GMR[1] >= 0) && !(GMR[1] < 0))) cout << "Mort " << lma << "\t" << GMR[1] << "\t" << dmdt << "\t" << m << "\t" << LfAr(m) << "\t" << dmdt / LfAr(m) << "\t" << exp((*p).c_d1 * rho + (*p).c_d2 * dmdt / LfAr(m)) << endl; }
void CFG::eliminateUnitProductions() { findUnitPairs(); vector<Production> nonUnitProductions; for(size_t i = 0; i < unitPairs.size(); ++i) { for(size_t j = 0; j < p.size(); ++j) { if(p[j].left == unitPairs[i].second && !isUnitProduction(p[j])) { nonUnitProductions.push_back(Production(unitPairs[i].first, p[j].right)); } } } p.clear(); p = nonUnitProductions; }
void ObservationTable::_buildBinaryRulesFromK(ContextFreeGrammar& G, map<ContextSet, NonTerminal>& distributionNonTerminal) { // Compute binary rules from KK \ K for (string k1 : this->K) { for (string k2 : this->K) { string kk = k1 + k2; if (this->K.find(kk) != this->K.end()) { // Found in K continue; } // Get the non-terminal based on this distribution of this string ContextSet distribution = this->_getDistributionByK(kk); auto nonTerminalPair = distributionNonTerminal.find(distribution); if (nonTerminalPair == distributionNonTerminal.end()) { // Not found continue; } // Check if we have a lexical rule for these elements map<string, NonTerminal>::iterator elementK1 = G.getLexicalRules().find(k1); map<string, NonTerminal>::iterator elementK2 = G.getLexicalRules().find(k2); if (elementK1 == G.getLexicalRules().end() || elementK2 == G.getLexicalRules().end()) { string message = "Lexical rule data not found for strings '" + k1 + "' and/or '" + k2 + "'"; throw exception(message.c_str()); continue; } // Build the binary rule of the form N -> PQ NonTerminalNonTerminal* nTnT = new NonTerminalNonTerminal( make_pair(elementK1->second, elementK2->second)); G.P.insert(Production(nonTerminalPair->second, nTnT)); } } }
void CFG::putInCNF() { eliminateUselessSymbols(); eliminateEpsilonProductions(); eliminateUnitProductions(); vector<string> terminalsInBody; for(size_t i = 0; i < p.size(); ++i) { if(p[i].right.size() >= 2) { for(size_t j = 0; j < p[i].right.size(); ++j) { if(in(p[i].right[j], t) && !in(p[i].right[j], terminalsInBody)) { terminalsInBody.push_back(p[i].right[j]); } } } } for(size_t i = 0; i < p.size(); ++i) { if(p[i].right.size() >= 2) { for(size_t j = 0; j < p[i].right.size(); ++j) { if(in(p[i].right[j], terminalsInBody)) { p[i].right[j] = "_" + p[i].right[j]; } } } } for(size_t i = 0; i < terminalsInBody.size(); ++i) { p.push_back(Production("_" + terminalsInBody[i], {terminalsInBody[i]})); v.push_back("_" + terminalsInBody[i]); } int count = 0; for(size_t i = 0; i < p.size(); ) { if(p[i].right.size() >= 3) { for(size_t j = 0; j < p[i].right.size() - 1; ++j) { if(j == 0) { v.push_back("_" + to_string(count)); p.push_back(Production(p[i].left, {p[i].right[j], "_" + to_string(count)})); } else if(j == p[i].right.size() - 2) { p.push_back(Production("_" + to_string(count), {p[i].right[j], p[i].right[j + 1]})); ++count; } else { v.push_back("_" + to_string(count + 1)); p.push_back(Production("_" + to_string(count), {p[i].right[j], "_" + to_string(count + 1)})); ++count; } } p.erase(p.begin() + i); } else { ++i; } } }
void CFG::eliminateEpsilonProductions() { findNullableSymbols(); for(size_t i = 0; i < p.size(); ++i) { vector<string> ns; for(size_t j = 0; j < p[i].right.size(); ++j) { if(in(p[i].right[j], nullableSymbols)) { ns.push_back(p[i].right[j]); } } vector<bool> flag(ns.size()); size_t j = 0; if(ns.size() == p[i].right.size()) { j = 1; } for(; j < ns.size(); ++j) // j represents number of nullable symbols to be present { flag.clear(); fill(flag.begin(), flag.end() - ns.size() + j, true); do { vector<string> presentNullableSymbols; for(size_t k = 0; k < ns.size(); ++k) { if(flag[k]) { presentNullableSymbols.push_back(ns[i]); } } bool exist = false; for(size_t k = 0; k < p.size(); ++k) { if(p[k].left == p[i].left && p[k].right == presentNullableSymbols) { exist = true; break; } } if(!exist) { p.push_back(Production(p[i].left, presentNullableSymbols)); } }while(prev_permutation(flag.begin(), flag.end())); } } if(in(s, nullableSymbols)) { bool exist = false; for(size_t i = 0; i < p.size(); ++i) { if(p[i].left == s && p[i].right.size() == 1 && p[i].right[0] == "") { exist = true; break; } } if(!exist) { p.push_back(Production(s, {""})); } } }