Exemplo n.º 1
0
void grammar_free(struct grammar_t *g)
{
	list_clear_func(&(g->connectors), (void (*)(void *)) grammar_connector_free);
	list_clear_func(&(g->variables), (void (*)(void *)) grammar_symbol_free);
	list_clear_func(&(g->terminals), (void (*)(void *)) grammar_symbol_free);
	free(g);
}
Exemplo n.º 2
0
void afd_free(struct afd *afd)
{
	list_clear(&afd->st);
	list_clear(&afd->stf);
	list_clear(&afd->alfa);
	list_clear_func(&afd->trans, afd_free_tran);
	
	free(afd);
}
Exemplo n.º 3
0
int afd_reduce(struct afd *afd)
{
	struct queue_t estados;
	struct list_t visitados;
	struct list_t nuevas_trans;
	struct list_node_t *node;
	struct afd_tran *tran, *tran2;
	void *st;

	debug("AFD ptr = %p", afd);

	queue_empty(&estados);
	list_empty(&visitados);
	list_empty(&nuevas_trans);

	if(queue_push(&estados, afd->sti))
	{
		debug("queue_push");
		return -1;
	}
	
	if(list_add(&visitados, afd->sti))
	{
		debug("list_add");
		return -1;
	}

	while(!queue_pop(&estados, &st))
	{
		debug("Estado %s", (char *) st);
		for(node = afd->trans.start; node != NULL; node = node->next)
		{
			debug("Transición %p", node->ptr);
#ifndef NDEBUG
			afd_print_tran(node->ptr);
#endif
			tran = (struct afd_tran *) node->ptr;
			if(strcmp(tran->ini, st) == 0)
			{
				if((tran2 = malloc(sizeof(struct afd_tran))) == NULL)
				{
					perror("malloc");
					return -1;
				}
				memcpy(tran2, tran, sizeof(struct afd_tran));
				if(list_add(&nuevas_trans, tran2))
				{
					debug("list_add");
					return -1;
				}
				if(list_find(&visitados, tran2->fin, afd_cmp) == NULL)
				{
					debug("Añadiendo %s a visitados", tran2->fin);
					if(list_add(&visitados, tran2->fin))
					{
						debug("list_add");
						return -1;
					}
					if(queue_push(&estados, tran2->fin))
					{
						debug("queue_push");
						return -1;
					}
				}
			}
		}
	}

#ifndef NDEBUG
	debug("Antiguas transiciones");
	list_map(&afd->trans, afd_print_tran);
	debug("Nuevas transiciones");
	list_map(&nuevas_trans, afd_print_tran);
#endif
	list_clear_func(&afd->trans, afd_free_tran);
	list_clear(&visitados);
	memcpy(&afd->trans, &nuevas_trans, sizeof(struct list_t));
	
	return 0;
}