void Grammar::kill_chains() {
	std::vector<SymbolList> temp;
	for( auto &nT : non_terminals ) {
		SymbolList set;
		set.push_back(nT);
		for( auto rule : production_rules[get_nt_index(nT)] ) {
			if( rule.size() == 1 ) {
				if( contains_char(set, rule[0]) ) {
					continue;
				} else {
					set.push_back(rule[0]);
				}
			}
		}
		for( auto &ch : set ) {
			for( auto rule : production_rules[get_nt_index(ch)] ) {
				if( rule.size() == 1 && subset_of(rule, non_terminals) ) {
					continue;
				} else {
					temp.push_back(rule);
				}
			}
		}
		production_rules[get_nt_index(nT)] = temp;
		temp.clear();
	}
}
Ejemplo n.º 2
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 );
        }
      }
    }
  }
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;
}
SymbolList Grammar::get_reachables() {
	SymbolList reachables;
	reachables.push_back(starting_symbol);
	SymbolList prev_reachables;
	do {
		prev_reachables = reachables;
		for( auto &ch : prev_reachables ) {
			for( auto rule : production_rules[get_nt_index(ch)] ) {
				for( auto &symbol : rule ) {
					if( contains_char(reachables, symbol) || contains_char(terminals, symbol) ) { continue; }
					reachables.push_back(symbol);
				}
			}
		}
	} while( prev_reachables != reachables );
	return reachables;
}
SymbolList intersection(const SymbolList& str1, const SymbolList& str2) {
	SymbolList ret;
	for( auto &ch : str1 ) {
		if( contains_char(str2, ch) ) {
			ret.push_back(ch);
		}
	}
	return ret;
}
Ejemplo n.º 6
0
void BookPublisher::createBook (const char* symbol, const char* partId) 
{ 
    mBook = new MamdaOrderBook();
    // This turns on the generation of deltas at the order book 
    mBook->generateDeltaMsgs (true);
    if (symbol) mBook->setSymbol (symbol); 
    if (partId) mBook->setPartId (partId); 
    mBookTime.setToNow();
    mBook->setBookTime (mBookTime);
    mSymbolList.push_back (symbol);
}
SymbolList string_to_symbols(const std::string& str) {
	SymbolList ret;
	//std::cout << str << '\n';
	std::istringstream iss(str);
	std::string tmp;
	while( std::getline(iss,tmp, ',') ) { 
		//std::cout << tmp << '\n';
		ret.push_back(Symbol(tmp));
	}
	return ret;
}
SymbolList Grammar::find_as_list(const SymbolList& sl) {
	SymbolList ret;
	for( auto nT : non_terminals ) {
		for( auto rule : production_rules[get_nt_index(nT)] ) {
			if( rule.size() != 2 ) continue;
			if( rule[0] == sl[0] && rule[1] == sl[1] ) {
				ret.push_back(nT);
			}
		}
	}
	return ret;
}
Type* BaseSemanticWalker::evalInitStruct(const InitStructList& stc) {
  SymbolList flist;
  InitStructList::const_iterator it;
  for (it = stc.begin(); it != stc.end(); ++it) {
    flist.push_back(Symbol(it->first->getText(), 
                    it->second, 
                    _symtable->globalScope(), 
                    _symtable->unit(),
                    it->first->getLine(),
                    it->first->getColumn()));
  }
  return _typeBuilder->structType(flist);
}
void Grammar::pseudo_non_terminals() {
	auto current = get_first(unite(non_terminals, terminals), 'Q');
	SymbolList temp_non_terms = non_terminals;
	SymbolList pseudo_non_terms;
	unsigned int c = 0;
	for( auto T : terminals ) {
		SymbolList temp;
		temp.push_back(T);
		non_terminals.push_back(current);
		pseudo_non_terms.push_back(current);
		production_rules[get_nt_index(current)].push_back(temp);
		//std::cout << current << " " << production_rules[get_nt_index(current)].back() << " " << production_rules[get_nt_index(current)].size() << '\n';
		get_next(current, ++c);
	}

	for( auto nT : temp_non_terms ) {
		std::vector<SymbolList> temp_rules;
		for( auto rule : production_rules[get_nt_index(nT)] ) {
			//std::cout << "rule: " << rule << " => ";
			if( rule.size() == 1 ) {
				//std::cout << "unchanged\n";
				temp_rules.push_back(rule);
				continue;
			}
			for( auto ch : rule ) {
				//std::cout << "ch: " << ch << '\n';
				if( contains_char(terminals, ch) ) {
					unsigned int i = pseudo_non_terms.size();
					while( i --> 0 ) {
						//std::cout << "while( "<<i<<" --> 0)\n";
						auto x = production_rules[get_nt_index(pseudo_non_terms[i])];
						//std::cout << production_rules[get_nt_index(pseudo_non_terms[i])][0];
						//std::cout << x[0][0] << " =?= " << ch << '\n';
						if( ch == x[0][0] ) {
							//std::cout << " :: TRUE\n";
							break;
						}
					}
					auto t = pseudo_non_terms[i];
					replace_first_of(rule, ch, t);
				}
			}
			//std::cout << rule << '\n';
			temp_rules.push_back(rule);
		}
		production_rules[get_nt_index(nT)] = temp_rules;
	}

}
Ejemplo n.º 11
0
SymbolSet allLookaheadsOf(const Item & item, const Grammar & grammar)
{
    SymbolList followingSymbols = item.rule.remainingSymbolsAfter(item.dottedSymbol);
    followingSymbols.push_back({ Symbol::Type::TERMINAL, "" });

    SymbolSet allLookaheads;
    for(auto & lookahead : item.lookaheads)
    {
        followingSymbols.back() = lookahead;
        auto first = grammar.first(followingSymbols);
        allLookaheads.insert(first.begin(), first.end());
    }

    return allLookaheads;
}
void MamaEntitle::readSymbolsFromFile (void) 
{
        /* get subjects from file or interactively */
        FILE* fp = NULL;
        char charbuf[1024];

        if (mFilename && mFilename[0] )
        {
            if ((fp = fopen (mFilename, "r")) == (FILE *)NULL)
            {
                perror (mFilename);
                exit (1);
            }
        }
        else
        {
            fp = stdin;
        }
        if (isatty(fileno (fp)))
        {
            printf ("Enter one symbol per line and terminate with a .\n");
            printf ("Symbol> ");
        }

        while (fgets (charbuf, 1023, fp))
        {
            /* replace newlines with NULLs */
            char *c = charbuf;

            /* Input terminate case */
            if (*c == '.')
                break;

            while ((*c != '\0') && (*c != '\n'))
            {
                c++;
            }
            *c = '\0';

            /* copy the string and subscribe */
            mSymbolList.push_back (strdup (charbuf));
            if (isatty(fileno (fp)))
            {
              printf ("Symbol> ");
            }
        }
}
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;
}
Ejemplo n.º 14
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;
}
void MamaEntitle::parseCommandLine (int argc, const char* argv[])
{
    int i = 0;
    for (i = 1; i < argc;  )
    {
        if ((strcmp (argv[i], "-S") == 0) ||
            (strcmp (argv[i], "-source") == 0))
        {
            mSource = argv[i + 1];
            i += 2;
        }
        else if (strcmp (argv[i], "-d") == 0)
        {
            mDictSourceName = argv[i + 1];
            i += 2;
        }
        else if (strcmp (argv[i], "-dict_tport") == 0)
        {
            mDictTport = argv[i+1];
            i += 2;
        }
        else if (strcmp (argv[i], "-I") == 0)
        {
            mRequireInitial = 0;
            i++;
        }
        else if ((strcmp (argv[i], "-h") == 0) ||
                 (strcmp (argv[i], "-?") == 0))
        {
            usage (0); 
            i++;
        }
        else if (strcmp (argv[i], "-s") == 0)
        {
            mSymbolList.push_back (argv[i + 1]);
            i += 2; 
        }
        else if (strcmp (argv[i], "-f") == 0)
        {
            mFilename = argv[i + 1];
            i += 2;
        }
        else if (strcmp (argv[i], "-1") == 0)
        {
            mSnapshot = 1;
            i++;
        }
        else if (strcmp (argv[i], "-g") == 0)
        {
            mGroupSubscription = 1;
            i++;
        }
        else if (strcmp (argv[i], "-q") == 0)
        {
            mQuietness++;
            i++;
        }
        else if (strcmp (argv[i], "-threads") == 0)
        {
            mThreads = atoi (argv[i + 1]);
            i += 2;
        }
        else if (strcmp (argv[i], "-tport") == 0)
        {
            mTport = argv[i+1];
            i += 2;
        }
        else if (strcmp (argv[i], "-v") == 0)
        {
            if (mMamaLogLevel == MAMA_LOG_LEVEL_WARN)
            {
                mMamaLogLevel = MAMA_LOG_LEVEL_NORMAL;
                mama_enableLogging (stderr, MAMA_LOG_LEVEL_NORMAL); 
            }
            else if (mMamaLogLevel == MAMA_LOG_LEVEL_NORMAL)
            {
                mMamaLogLevel = MAMA_LOG_LEVEL_FINE;
                mama_enableLogging (stderr, MAMA_LOG_LEVEL_FINE); 
            }
            else if (mMamaLogLevel == MAMA_LOG_LEVEL_FINE)
            {
                mMamaLogLevel = MAMA_LOG_LEVEL_FINER;
                mama_enableLogging (stderr, MAMA_LOG_LEVEL_FINER);
            }
            else
            {
                mMamaLogLevel = MAMA_LOG_LEVEL_FINEST;
                mama_enableLogging (stderr, MAMA_LOG_LEVEL_FINEST);
            }

            i++;
        }
        else if (strcmp ("-m", argv[i]) == 0)
        {
            mMiddleware = argv[i+1];
            i += 2;               
        }
        else if (strcmp (argv[i], "-V") == 0)
        {
            if (mSubscLogLevel == MAMA_LOG_LEVEL_NORMAL)
            {
                mSubscLogLevel = MAMA_LOG_LEVEL_FINE;
            }
            else if (mSubscLogLevel == MAMA_LOG_LEVEL_FINE)
            {
                mSubscLogLevel = MAMA_LOG_LEVEL_FINER;
            }
            else
            {
                mSubscLogLevel = MAMA_LOG_LEVEL_FINEST;
            }

            i++;
        }
        else if (strcmp (argv[i], "-version") == 0)
        {
            printf ("%s\n", Mama::getVersion (mBridgeImpl)); 
            exit (0);
        }
        else if (strcmp (argv[i], "-t") == 0)
        {
            mShutdownTimeout = strtol (argv[i+1], NULL, 10);
            i+=2;
        }
        else
        {
            mFieldList.push_back (argv[i]);
            i++;
        }
    }
}
Ejemplo n.º 16
0
bool Disassembler::ParseSymbolTable(const list<Token *>::iterator &startiter, const list<Token *>::iterator &EndIter, SymbolList &Symbols, unsigned char Addressability, CallBackFunction)
{
	list<Token *>::iterator TokenIter, StartIter;
	TokenIter = StartIter = startiter;

	//process one symbol at a time
	while(StartIter != EndIter)
	{
		//Get the symbol name
		if(StartIter == EndIter || (*StartIter)->TokenType != TIdentifier)
		{
			CallBack(Error, "Missing symbol name: first cell in symbol table entry.", (*TokenIter)->LocationStack);
			return false;
		}
		string sSymbol = ((IDToken *)(*StartIter))->sIdentifier;
		TokenIter = StartIter++;

		//Check for possible "." which could be for a struct member.
		if(StartIter != EndIter && (*StartIter)->TokenType == TOperator && ((OpToken *)(*StartIter))->Operator == OpPeriod)
		{
			TokenIter = StartIter++;
			//Get past the member label
			if(StartIter != EndIter && (*StartIter)->TokenType == TIdentifier)
			{
				TokenIter = StartIter++;
			}
			else
			{
				CallBack(Error, "Missing member symbol name after struct symbol name and period: first cell in symbol table entry.", (*TokenIter)->LocationStack);
				return false;
			}
		}


		//Get the comma
		if(StartIter == EndIter || (*StartIter)->TokenType != TOperator || ((OpToken *)(*StartIter))->Operator != OpComma)
		{
			CallBack(Error, "Missing comma after symbol name: first cell in symbol table entry.", (*TokenIter)->LocationStack);
			return false;
		}
		TokenIter = StartIter++;

		//Get the symbol type
		SymbolEnum SymbolType = SymVoid;
		if(StartIter == EndIter || (*StartIter)->TokenType != (TokenEnum)TDirective || ((DirToken *)(*StartIter))->Directive != DirMacro)
		{
			if(StartIter == EndIter || (*StartIter)->TokenType != (TokenEnum)TDirective || ((DirToken *)(*StartIter))->Directive != DirExtern)
			{
				if(StartIter == EndIter || (*StartIter)->TokenType != (TokenEnum)TDirective || ((DirToken *)(*StartIter))->Directive != DirDefine)
				{
					if(StartIter == EndIter || (*StartIter)->TokenType != (TokenEnum)TData || ((DataToken *)(*StartIter))->DataType != STRUCT)
					{
						if(StartIter == EndIter || (*StartIter)->TokenType != TIdentifier || ((IDToken *)(*StartIter))->sIdentifier != "member")
						{
							if(StartIter == EndIter || (*StartIter)->TokenType != TIdentifier || ((IDToken *)(*StartIter))->sIdentifier != "label")
							{
								CallBack(Error, "Missing symbol type after symbol name: second cell in symbol table entry.", (*TokenIter)->LocationStack);
								return false;
							}
							else
								SymbolType = SymLabel;
						}
						else
							SymbolType = SymMember;
					}
					else
						SymbolType = SymStruct;
				}
				else
					SymbolType = SymDefine;
			}
			else
				SymbolType = SymExtern;
		}
		else
			SymbolType = SymMacro;
		TokenIter = StartIter++;

		uint64 Address;
		switch(SymbolType)
		{
		case SymMacro:
		case SymDefine:
		case SymStruct:
			//There's nothing else for this entry.
			break;

		case SymMember:
			//Get the comma
			if(StartIter == EndIter || (*StartIter)->TokenType != TOperator || ((OpToken *)(*StartIter))->Operator != OpComma)
			{
				CallBack(Error, "Missing comma after symbol type: second cell in symbol table entry.", (*TokenIter)->LocationStack);
				return false;
			}
			TokenIter = StartIter++;

			//Get the number
			if(StartIter == EndIter || (*StartIter)->TokenType != TInteger)
			{
				CallBack(Error, "Missing struct member symbol offset value: third cell in symbol table entry.", (*TokenIter)->LocationStack);
				return false;
			}
			TokenIter = StartIter++;
			
			break;

		case SymExtern:
		case SymLabel:
			//Get the comma
			if(StartIter == EndIter || (*StartIter)->TokenType != TOperator || ((OpToken *)(*StartIter))->Operator != OpComma)
			{
				CallBack(Error, "Missing comma after symbol type: second cell in symbol table entry.", (*TokenIter)->LocationStack);
				return false;
			}
			TokenIter = StartIter++;

			//Get the number
			if(StartIter == EndIter || (*StartIter)->TokenType != TInteger)
			{
				CallBack(Error, "Missing symbol address: third cell in symbol table entry.", (*TokenIter)->LocationStack);
				return false;
			}
			Address = ((IntegerToken *)(*StartIter))->Integer;
			TokenIter = StartIter++;

			//Get the comma
			if(StartIter == EndIter || (*StartIter)->TokenType != TOperator || ((OpToken *)(*StartIter))->Operator != OpComma)
			{
				CallBack(Error, "Missing comma after symbol address: third cell in symbol table entry.", (*TokenIter)->LocationStack);
				return false;
			}
			TokenIter = StartIter++;

			//Get the number
			if(StartIter == EndIter || (*StartIter)->TokenType != TInteger)
			{
				CallBack(Error, "Missing symbol segment-relative adddress: fourth cell in symbol table entry.", (*TokenIter)->LocationStack);
				return false;
			}
			TokenIter = StartIter++;

			if(SymbolType == SymLabel)
				Symbols.push_back( make_triple((*TokenIter)->LocationStack, Address << Addressability, sSymbol) );

			break;
		}

		//depending on if they saved after viewing in excel, there could be a number of commas at the end.
		while(StartIter != EndIter && (*StartIter)->TokenType == TOperator && ((OpToken *)(*StartIter))->Operator == OpComma)
			TokenIter = StartIter++;
	}

	return true;
}