예제 #1
0
파일: Alignment.cpp 프로젝트: kgori/bpp
size_t getNumberOfDistinctPositionsWithoutGap(const SymbolList& l1, const SymbolList& l2) {
      if (l1.getAlphabet()->getAlphabetType() != l2.getAlphabet()->getAlphabetType()) throw AlphabetMismatchException("SymbolListTools::getNumberOfDistinctPositions.", l1.getAlphabet(), l2.getAlphabet());
      const Alphabet* alpha = l1.getAlphabet();
      int gapCode = alpha->getGapCharacterCode();
      size_t n = min(l1.size(), l2.size());
      size_t count = 0;
      for (size_t i = 0; i < n; i++) {
          int x = l1[i];
          int y = l2[i];
          if (alpha->isUnresolved(x)) x = gapCode;
          if (alpha->isUnresolved(x)) y = gapCode;
          if (x != gapCode && y != gapCode && x != y) count++;
      }
      return count;
}
bool Grammar::cyk(const std::string& input) {
	unsigned int n = input.size();
	SymbolList in_sl;
	for( auto ch : input ) {
		in_sl.push_back(ch);
	}
	in_sl = to_pseudo_non_terminals(in_sl);

	std::vector<SymbolList> pairs;
	for( auto it = in_sl.begin(); it != in_sl.end() - 1; ++it ) {
		SymbolList temp_sl;
		temp_sl.push_back(*it);
		temp_sl.push_back(*( it + 1 ));
		pairs.push_back(temp_sl);
	}

	std::vector<SymbolList> sl_vect;
	for( auto t : pairs ) {
		SymbolList temp = find_as_list(t);
		if( temp.size() == 0 ) {
			Symbol temp_sym("empty");
			temp.push_back(temp_sym);
		}
		sl_vect.push_back(temp);
	}

	for( auto x : sl_vect ) {
		for( auto t : x ) {
			std::cout << t << '|';
		}
		std::cout << '\n';
	}

	return true;
}
예제 #3
0
void SymbolTable::checkAmbiguity(const Symbol& s) {
//  f(a : inteiro)
//  f(b : inteiro, ... resto)
//  -> declaracao ambigua

    if (!s.type()->isSubprogram()) {
        return;
    }

    SymbolList list =
        _table[globalScope()].findAllByLexeme(s.lexeme());

    if (list.size() == 0) {
        return;
    }

    for (SymbolList::iterator it = list.begin(); it != list.end(); ++it) {
        if (!(*it).type()->isSubprogram()) {
            continue;
        }

        int size = (*it).type()->paramTypes().size();
        if ((size + 1) == s.type()->paramTypes().size()) {
            Type *sss = s.type()->paramTypes().back();
            bool bn = sss->isReticences();
            if (s.type()->paramTypes().back()->isReticences()) {
                throw AmbiguousDeclarationException(s, *it);
            }
        } else if ((size - 1) == s.type()->paramTypes().size()) {
            if ((*it).type()->paramTypes().back()->isReticences()) {
                throw AmbiguousDeclarationException(s, *it);
            }
        }
    }
}
void replace_first_of(SymbolList& sl, const Symbol& of, const Symbol& with) {
	//std::cout << "replace_first_with\n";
	for( unsigned int i = 0; i < sl.size(); ++i ) {
		if( sl[i] == of ) {
			sl[i] = with;
			return;
		}
	}
}
std::vector<SymbolList> permutations(const SymbolList& str, const SymbolList& of) {
	std::vector<SymbolList> ret;
	unsigned int size = of.size();
	unsigned int n = factorial(size);
	SymbolList temp = str;
	//std::cout << temp << '\n';
	while( !empty_intersection(temp, of) && temp.size() > 1 ) {
		SymbolList copy = temp;
		for( unsigned int i = 0; i < copy.size(); ++i ) {
			if( contains_char(of, copy[i]) ) {
				copy.erase(copy.begin() + i);
				ret.push_back(copy);
				//std::cout << copy << std::endl;
				break;
			}
		}
		temp = ret.back();
	}
	return ret;
}
예제 #6
0
  virtual
  void reportResults (CReport & report)
  {
    trial( testSet, notCirc, symbolMap);

    if (testSet.size() == 0) return;

    report.addAtLine(1, "Circularities found");

    stringstream str;

    str << testSet << ends;

    report.addAtLine(2, str.str() );
  }
예제 #7
0
void Preprocessor::setSymbols(const SymbolList& symbols)
{
    uint nb_symbols = symbols.size();
    this->symbols.resize(nb_symbols);

#ifdef PREPROC_KEEP_ORIGINAL_SYMBOLS
    this->original_symbols.resize(nb_symbols);
#endif

    for(uint i=0 ; i < nb_symbols ; i++)
    {
        this->symbols[i] = symbols[i];
#ifdef PREPROC_KEEP_ORIGINAL_SYMBOLS
        this->original_symbols[i] = symbols[i];
#endif
    }
}
SymbolList Grammar::those_that_product(SymbolList& sl) {
	SymbolList ret;
	for( auto CV : sl ) {
		for( auto nT : non_terminals ) {
			for( auto rule : production_rules[get_nt_index(nT)] ) {
				if( sl.size() != rule.size() ) { continue; }
				for( unsigned int i = 0; i < rule.size(); ++i ) {
					if( !(sl[i] == rule[i]) ) { break; }
					if( i == rule.size() - 1 ) {
						ret.push_back(nT);
					}
				}
			}
		}
	}
	return ret;
}
예제 #9
0
const Symbol&
SymbolTable::getSymbol(const std::string& lexeme, const TypeList& params) {
    //deve considerar promocao de tipos
    //    função f(a:real) ...
    //    f(1);                //resolve para a função "f_real"

//checar:
//  f(a : inteiro)
//  f(b : real)
//  f(2); //primeira versão, sempre!


    SymbolList list = _table[globalScope()].findAllByLexeme(lexeme);

    if (list.size() == 0) {
        throw UndeclaredSymbolException(lexeme);
    }

    //try exact version
    for (SymbolList::iterator it = list.begin(); it != list.end(); ++it) {
        if ((*it).type()->isSubprogram() &&
                (*it).type()->paramTypes().equals(params)) {
            return (*it);
        }
    }

    //tentando promocoes...
    for (SymbolList::iterator it = list.begin(); it != list.end(); ++it) {
        if ((*it).type()->isSubprogram() &&
                (*it).type()->paramTypes().isLValueFor(params)) {
            return (*it);
        }
    }

    throw UnmatchedException(lexeme);

//   std::string id = Symbol::buildIdentifier(lexeme, params);
//   SymbolList::const_iterator it = _table[globalScope()].findByIdentifier(id);
//   if (it == _table[globalScope()].end()) {
//     throw UndeclaredSymbolException(id);
//   }
//   return (*it);
}
예제 #10
0
void LexerTests::nominal_test()
{
    cout << "-------------------------------------- Lexer ------------------------------------" << endl;

    ifstream ifs("../../tests/files/correct.lt");
    //Vérification du fichier
    if (!ifs.good())
    {
        ifs.close();
        FAILED(0);
        PRINT("Failed to open file...");
        return;
    }
    Lexer lexer(ifs);
    SymbolList symbols;
    Symbol symbol;
    lexer.MoveForward(); // commence à lire en placant la tete de lecture au debut du flux
    symbol = lexer.GetNext();
    while(symbol.code != S_EOF)
    {   if(symbol.code == S_LEXER_ERROR)
        {   PRINT("Lexer error encountered...");
            break;
        }
        // on récupère les valeurs
        symbols.push_back(symbol);
        // on déplace la tête de lecture
        lexer.MoveForward();
        // on lit le prochain symbole
        symbol = lexer.GetNext();
    }
    symbols.push_back(symbol);
    /*
     * Programme testé : correct.lt
     *
     * 1. var a,b;
     * 2. const c = 4;
     * 3. const d = 6;
     * 4. var e;
     * 5. a := (c+d)*3-5;
     * 6. lire b;
     * 7. ecrire a*b;
     * 8. e := b+d;
     * 9. ecrire e;
     */
    SymbolList expectedSymbols;
#define PUSH_SYM(symbolcode, value) expectedSymbols.push_back(Symbol(symbolcode, value));
    // ligne 1
    PUSH_SYM(S_VAR,"var");PUSH_SYM(S_ID,"a");PUSH_SYM(S_V,",");PUSH_SYM(S_ID,"b");PUSH_SYM(S_PV,";");
    // ligne 2
    PUSH_SYM(S_CONST,"const");PUSH_SYM(S_ID,"c");PUSH_SYM(S_EQ,"=");PUSH_SYM(S_NUM,"4");PUSH_SYM(S_PV,";");
    // ligne 3
    PUSH_SYM(S_CONST,"const");PUSH_SYM(S_ID,"d");PUSH_SYM(S_EQ,"=");PUSH_SYM(S_NUM,"6");PUSH_SYM(S_PV,";");
    // ligne 4
    PUSH_SYM(S_VAR,"var");PUSH_SYM(S_ID,"e");PUSH_SYM(S_PV,";");
    // ligne 5
    PUSH_SYM(S_ID,"a");PUSH_SYM(S_AFFECT,":=");PUSH_SYM(S_PO,"(");PUSH_SYM(S_ID,"c");PUSH_SYM(S_PLUS,"+");PUSH_SYM(S_ID,"d");PUSH_SYM(S_PF,")");PUSH_SYM(S_MULT,"*");PUSH_SYM(S_NUM,"3");PUSH_SYM(S_MINUS,"-");PUSH_SYM(S_NUM,"5");PUSH_SYM(S_PV,";");
    // ligne 6
    PUSH_SYM(S_READ,"lire");PUSH_SYM(S_ID,"b");PUSH_SYM(S_PV,";");
    // ligne 7
    PUSH_SYM(S_WRITE,"ecrire");PUSH_SYM(S_ID,"a");PUSH_SYM(S_MULT,"*");PUSH_SYM(S_ID,"b");PUSH_SYM(S_PV,";");
    // ligne 8
    PUSH_SYM(S_ID,"e");PUSH_SYM(S_AFFECT,":=");PUSH_SYM(S_ID,"b");PUSH_SYM(S_PLUS,"+");PUSH_SYM(S_ID,"d");PUSH_SYM(S_PV,";");
    // ligne 9
    PUSH_SYM(S_WRITE,"ecrire");PUSH_SYM(S_ID,"e");PUSH_SYM(S_PV,";");
    // eof
    PUSH_SYM(S_EOF,"");

    // verification de l'adequation des quatres tableaux
    // -- on vérifie d'abord la taille
    if(symbols.size() != expectedSymbols.size())
    {   FAILED(0);
        PRINT("Les tailles des tableaux ne correspondent pas !");
        PRINT("symbols.size() = " << symbols.size());
        PRINT("expectedSymbols.size() = " << expectedSymbols.size());
    }
    else
    {   list<Symbol>::iterator realSymbol = symbols.begin(), expectedSymbol = expectedSymbols.begin();
        // ici on peut tester que sur un itérateur car on sait que tous les tableaux font la meme taille
        int iter(1);
        bool failed(false);
        while(realSymbol != symbols.end())
        {   // tests de correspondance
            if(realSymbol->code != expectedSymbol->code)
            {   FAILED(iter);
                PRINT("real code = '" << realSymbol->code << "'");
                PRINT("expected code = '" << expectedSymbol->code << "'");
                failed = true;
                break; // sortie du while
            }
            else if(realSymbol->buf != expectedSymbol->buf)
            {   FAILED(iter);
                PRINT("real buf = '" << realSymbol->buf << "'");
                PRINT("expected buf = '" << expectedSymbol->buf << "'");
                failed = true;
                break; // sortie du while
            }
            // incrément des itérateurs
            realSymbol++; expectedSymbol++;
            iter++;
        }
        if(!failed)
        {   SUCCESS(0);
        }
    }


    cout << "-------------------------------------- done -------------------------------------" << endl;
}
bool subset_of(const SymbolList& in, const SymbolList& of) {
	return intersection(in, of).size() == in.size();
}