Beispiel #1
0
  void trial( SymbolList & testSet,
	      SymbolList & notCirc,
	      SymbolMap  & symbolMap
	    )
  {
    bool bDone = false;

    while (!bDone)
    {
      bDone = true;

      SymbolList::iterator item = testSet.begin();
      SymbolList::iterator last = testSet.end();
      
      while (item != last)
      {
        SymbolList::iterator thisItem = item++;
        const Symbol sym = *thisItem;

        bool bIsReferedTo = symbolMap.isReferredTo( sym );
        bool bRefersTo    = symbolMap.refersTo    ( sym );

        if (!( bIsReferedTo && bRefersTo) )
        {
          bDone = false;

          symbolMap.remove( sym );

          testSet.remove( sym );

          notCirc.push_back( sym );
        }
      }
    }
  }
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 SymbolTable::insertSymbols(const SymbolList& symbols) {
    for (SymbolList::const_iterator it = symbols.begin();
            it != symbols.end();
            ++it) {
        insertSymbol(*it);
    }
}
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);
}
void  MamaEntitle::subscribeToSymbols ()
{
    mQueueGroup = new MamaQueueGroup (mThreads, mBridgeImpl);

    SymbolList::iterator i;
    for (i=mSymbolList.begin(); i != mSymbolList.end(); i++)
    {
        subscribeToSymbol (*i);
    }
}
bool BookPublisher::publishingSymbol (const char * symbol) 
{
    SymbolList::const_iterator i;
    
    for (i = mSymbolList.begin (); i != mSymbolList.end (); i++)
    {
        if  (strcmp(*i,symbol)==0)
        {
            return true;
        }
    }
    return false;
}
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;
}
Beispiel #8
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;
}
Beispiel #9
0
/**
Function to generate ASM File.
@param afileName - ASM File name
@return 0 on success, otherwise throw error
@internalComponent
@released
*/
int FileDump::GenerateAsmFile(const char* afileName)//DumpAsm
{
    DefFile *iDefFile = new DefFile();
    SymbolList *aSymList;
    aSymList = iDefFile->ReadDefFile(iParameterListInterface->DefInput());

    FILE *fptr;

    if((fptr=fopen(afileName,"w"))==NULL)
    {
        throw FileError(FILEOPENERROR,(char*)afileName);
    }
    else
    {
        SymbolList::iterator aItr = aSymList->begin();
        SymbolList::iterator last = aSymList->end();
        Symbol *aSym;

        while( aItr != last)
        {
            aSym = *aItr;

            if(aSym->Absent())
            {
                aItr++;
                continue;
            }

            fputs("\tIMPORT ",fptr);
            fputs(aSym->SymbolName(),fptr);
            //Set the visibility of the symbols as default."DYNAMIC" option is
            //added to remove STV_HIDDEN visibility warnings generated by every
            //export during kernel build
            fputs(" [DYNAMIC]", fptr);
            fputs("\n",fptr);
            aItr++;
        }

        // Create a directive section that instructs the linker to make all listed
        // symbols visible.

        fputs("\n AREA |.directive|, READONLY, NOALLOC\n\n",fptr);

        fputs("\tDCB \"#<SYMEDIT>#\\n\"\n", fptr);

        aItr = aSymList->begin();
        while (aItr != last)
        {
            aSym = *aItr;

            if ( aSym->Absent() )
            {
                aItr++;
                continue;
            }

            // Example:
            //  DCB "EXPORT __ARM_ll_mlass\n"
            fputs("\tDCB \"EXPORT ",fptr);
            fputs(aSym->SymbolName(),fptr);
            fputs("\\n\"\n", fptr);

            aItr++;
        }

        fputs("\n END\n",fptr);
        fclose(fptr);
    }
    return 0;
}
Beispiel #10
0
bool Disassembler::Disassemble(LocationVector &LocationStack, istream &InputStream, SymbolList &Symbols)
{
	bool fRetVal = true;
	unsigned int i, TempChar;
	JMT::ByteData ByteData;

	if(fRunOnce)
		throw "Disassembler called twice on the same program!";
	fRunOnce = true;

	//First read starting address.
	for(i = 0; i < sizeof(uint64); i++)
	{
		TempChar = InputStream.get();
		if(TempChar == -1)	//EOF
			break;
		//The address in the file is always little endian
#ifdef BIG_ENDIAN_BUILD
		ByteData.Bytes[sizeof(uint64) - i - 1] = TempChar;
#else
		ByteData.Bytes[i] = TempChar;
#endif
		LocationStack.rbegin()->second++;
	}
	if(i < sizeof(uint64))
	{
		CallBack(Fatal, "Unexpected end of file.", LocationStack);
		return false;
	}
	StartAddress = ByteData.UI64;

	//Then read ending address
	for(i = 0; i < sizeof(uint64); i++)
	{
		TempChar = InputStream.get();
		if(TempChar == -1)	//EOF
			break;
		//The address in the file is always little endian
#ifdef BIG_ENDIAN_BUILD
		ByteData.Bytes[sizeof(uint64) - i - 1] = TempChar;
#else
		ByteData.Bytes[i] = TempChar;
#endif
		LocationStack.rbegin()->second++;
	}
	if(i < sizeof(uint64))
	{
		CallBack(Fatal, "Unexpected end of file.", LocationStack);
		return false;
	}
	EndAddress = StartAddress + ByteData.UI64;

	//Check addresses
	if(EndAddress < StartAddress)
	{
		CallBack(Fatal, "Program end address is less than start address.", LocationStack);
		return false;
	}

	//Create program origin
	TheProg.fDynamicAddress = false;
	TheProg.Address = StartAddress;

	//Create program segment
	TheProg.Segments.push_back(new Segment(LocationStack, Addressability, 0));

	//Add the pre-existing symbols
	for(SymbolList::iterator SymbolIter = Symbols.begin(); SymbolIter != Symbols.end(); SymbolIter++)
		DisassembleSymbol(SymbolIter->first, SymbolIter->second, SymbolIter->third);

	//Try to disassemble instructions. Instructions have highest priority.
	uint64 Address = StartAddress;
	if(ToLower(TheProg.sFileName.Ext) == "obj")
	{
		if(!DisassembleInstructions(LocationStack, InputStream, Address))
			return false;
	}
	else	//"bin"
	{
		if(!DisassembleData(LocationStack, InputStream, Address))
			return false;
	}

	if(Address < EndAddress)
	{
		CallBack(Error, "Unexpected end of file.", LocationStack);
		return false;
	}

	//Create labels
	list<Element *>::iterator ElemIter = (*TheProg.Segments.begin())->Sequence.begin();
	for(LabelMap::iterator LabelIter = Labels.begin(); LabelIter != Labels.end(); LabelIter++)
	{
		//Find the element this label points to
		while(ElemIter != (*TheProg.Segments.begin())->Sequence.end() && (*ElemIter)->Address < LabelIter->first)
			ElemIter++;

		if(ElemIter == (*TheProg.Segments.begin())->Sequence.end())
		{
			ElemIter--;
			if((*ElemIter)->Address + (*ElemIter)->Size != LabelIter->first)
				throw "Disassembler: Ran out of elements while adding labels!";
			//Label points to end of program
			ElemIter++;
		}
		else if((*ElemIter)->Address != LabelIter->first)
			throw "Disassembler: Label points to inside an instruction!";

		//Add the label to the sequence
		Label *pLabel = new Label(LocationStack, LabelIter->second->sSymbol, *TheProg.Segments.begin());
		(*TheProg.Segments.begin())->Sequence.insert(ElemIter, pLabel);
		LabelIter->second->pLabel = pLabel;
		if(ElemIter != (*TheProg.Segments.begin())->Sequence.end())
		{
			if(!TheProg.TheSymbolTable.ResolveSymbol((*ElemIter)->LocationStack, LabelIter->second->sSymbol, CallBack, SymLabel))
				throw "ResolveSymbol in Disassembler failed!";
			pLabel->pElement = *ElemIter;
		}
		else
			if(!TheProg.TheSymbolTable.ResolveSymbol((*(*TheProg.Segments.begin())->Sequence.rbegin())->LocationStack, LabelIter->second->sSymbol, CallBack, SymLabel))
				throw "ResolveSymbol in Disassembler failed!";
	}

	return fRetVal;
}