Exemplo n.º 1
0
void SymbolTable::addRelation(const RelationNode *relationNode) {
	const std::vector<Token> arguments = relationNode->getArguments();
	
	for (const Token &token : arguments) {
		if (token.getType() != TOKENIZER_TOKEN_TYPE_TERM) {
			throw Exception("Unexpected type in relation node");
		}
	}
	
	std::string name = relationNode->getName();
	STable::iterator it = table.find(name);
	Symbol *symbol;
	if (it == table.end()) {
		symbol = new Symbol(name, SYMBOL_TYPE_RELATION, relationNode->getArguments().size());
		std::pair<std::string, Symbol*> entry(name, symbol);
		table.insert(entry);
	} else {
		symbol = it->second;
		if (symbol->getNumOfArgument() != relationNode->getArguments().size()) {
			throw Exception("Different number of argument in same relation: " + name);
		}
	}
	for (size_t i = 0; i < relationNode->getArguments().size(); ++i) {
		symbol->addArgument(i, relationNode->getArguments()[i].getTokenStr());
	}
}
Exemplo n.º 2
0
void SymbolTable::addProposition(const PropositionNode *propositionNode, bool isRule) {
	std::string name = propositionNode->getName();
	STable::iterator it = table.find(name);
	
	Symbol *symbol;
	if (it != table.end()) {
		symbol = it->second;
		if (symbol->getType() == SYMBOL_TYPE_RELATION) {
			if (isRule) {
				throw Exception("Relation cannot be in the head of if node: " + name);
			} else {
				return;
			}
		} else if (symbol->getType() == SYMBOL_TYPE_PROPOSITION && isRule) {
			symbol->setType(SYMBOL_TYPE_RULE);
		}
		if (symbol->getNumOfArgument() != propositionNode->getArguments().size()) {
			throw Exception(std::string("Different number of arguments in same") + std::string(((isRule) ? "rule" : "proposition")) + std::string(": ") + name);
		}
	} else {
		symbol = new Symbol(name, ((isRule) ? SYMBOL_TYPE_RULE : SYMBOL_TYPE_PROPOSITION), propositionNode->getArguments().size());
		std::pair<std::string, Symbol*> entry(name, symbol);
		table.insert(entry);
	}
	for (size_t i = 0; i < propositionNode->getArguments().size(); ++i) {
		if (propositionNode->getArguments()[i].getType() == TOKENIZER_TOKEN_TYPE_TERM) {
			std::string argument = propositionNode->getArguments()[i].getTokenStr();
			if (!argumentExists(symbol->getArgumentDomain(i), argument)) {
				symbol->addArgument(i, argument);
			}
		}
	}
}
Exemplo n.º 3
0
void SymbolTable::addAction(const PropositionNode *action) {
	std::string name = action->getName();
	STable::iterator it = table.find(name);
	
	Symbol *symbol;
	if (it != table.end()) {
		symbol = it->second;
		if (symbol->getType() != SYMBOL_TYPE_ACTION) {
			throw Exception("Action is already in the table with differnet type: " + name);
		}
		if (symbol->getNumOfArgument() != action->getArguments().size()) {
			throw Exception("Different number of arguments in same action: " + name);
		}
	} else {
		symbol = new Symbol(name, SYMBOL_TYPE_ACTION, action->getArguments().size());
		std::pair<std::string, Symbol*> entry(name, symbol);
		table.insert(entry);
	}
	for (size_t i = 0; i < action->getArguments().size(); ++i) {
		if (action->getArguments()[i].getType() == TOKENIZER_TOKEN_TYPE_TERM) {
			std::string argument = action->getArguments()[i].getTokenStr();
			if (!argumentExists(symbol->getArgumentDomain(i), argument)) {
				symbol->addArgument(i, argument);
			}
		}
	}
}