From: Lukas Jiriste Date: Thu, 4 Jul 2024 15:03:13 +0000 (+0200) Subject: Fix some aspects of fill_closure function X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=0372b7a7fd93cd3de5f736634aa7b4bc5951f1d1;p=Libft.git Fix some aspects of fill_closure function --- diff --git a/ft_parse/ft_parsing_table_generate.c b/ft_parse/ft_parsing_table_generate.c index e61b67e..2bdcf28 100644 --- a/ft_parse/ft_parsing_table_generate.c +++ b/ft_parse/ft_parsing_table_generate.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/27 11:16:53 by ljiriste #+# #+# */ -/* Updated: 2024/07/04 14:36:08 by ljiriste ### ########.fr */ +/* Updated: 2024/07/04 16:33:02 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -65,7 +65,7 @@ int void_cmp_token_type(const void *v_token1, const void *v_token2) int cmp_rules(const t_grammar_rule *rule1, const t_grammar_rule *rule2) { return (cmp_token_type(&rule1->result, &rule2->result) - || ft_vec_is_equal(&rule1->constituents, + || !ft_vec_is_equal(&rule1->constituents, &rule2->constituents, void_cmp_token_type)); } @@ -78,7 +78,7 @@ int cmp_items(const t_lr1_item *item1, const t_lr1_item *item2) { return (cmp_rules(item1->core.rule, item2->core.rule) || item1->core.position != item2->core.position - || ft_vec_is_setequal(&item1->lookahead, &item2->lookahead, + || !ft_vec_is_setequal(&item1->lookahead, &item2->lookahead, void_cmp_token_type)); } @@ -134,6 +134,34 @@ t_lr1_item *duplicate_item(const t_lr1_item *item) return (res); } +t_ft_stat append_token(t_vec *tokens, const t_token *token) +{ + t_ft_stat res; + t_token token_clone; + + token_clone = ft_token_dup(token); + if (!token_clone.type) + return (alloc_fail); + res = ft_vec_append(tokens, &token_clone); + if (res != success) + ft_free_token(&token_clone); + return (res); +} + +t_ft_stat prepend_token(t_vec *tokens, const t_token *token) +{ + t_ft_stat res; + t_token token_clone; + + token_clone = ft_token_dup(token); + if (!token_clone.type) + return (alloc_fail); + res = ft_vec_insert(tokens, &token_clone, 0); + if (res != success) + ft_free_token(&token_clone); + return (res); +} + int is_viable_item(const t_lr1_item *item, const t_token *token) { const t_token *wanted_token; @@ -255,15 +283,39 @@ t_ft_stat add_first(t_vec *lookahead, const t_token *token, const t_vec *rules, return (success); } +void remove_empty_token(t_vec *lookahead) +{ + size_t i; + const t_token *token; + + i = lookahead->size; + while (i > 0) + { + --i; + token = ft_vec_caccess(lookahead, i); + if (!cmp_token_type(token, &empty_token)) + ft_vec_erase(lookahead, i, ft_free_token); + } +} + t_ft_stat expand_lookahead(t_vec *lookahead, const t_marked_grammar_rule *rule, const t_vec *rules, const t_vec *tokens) { - size_t i; - t_ft_stat res; + size_t i; + t_ft_stat res; + const t_token *token; i = rule->position + 1; - while ((i == rule->position + 1 || ft_vec_contains(lookahead, &empty_token, void_cmp_token_type)) && i < rule->rule->constituents.size) + if (lookahead->size == 0) + { + res = append_token(lookahead, &empty_token); + if (res != success) + return (res); + } + while (ft_vec_contains(lookahead, &empty_token, void_cmp_token_type) && i < rule->rule->constituents.size) { - res = add_first(lookahead, ft_vec_caccess(&rule->rule->constituents, i), rules, tokens); + remove_empty_token(lookahead); + token = ft_vec_caccess(&rule->rule->constituents, i); + res = add_first(lookahead, token, rules, tokens); if (res != success) return (res); ++i; @@ -302,7 +354,10 @@ t_ft_stat add_lookahead(t_lr1_item *new, const t_lr1_item *item, const t_vec *ru if (res != success) return (res); if (ft_vec_contains(&new->lookahead, &empty_token, void_cmp_token_type)) + { + remove_empty_token(&new->lookahead); res = add_to_lookahead(&item->lookahead, &new->lookahead); + } return (res); } @@ -322,12 +377,11 @@ t_ft_stat add_predictions(t_vec *closure, const t_lr1_item *item, const t_vec *r res = add_lookahead(&new_item, item, rules, tokens); if (res != success) return (res); - res = ft_vec_append(closure, &new_item); + res = ft_vec_setinsert(closure, &new_item, void_cmp_items); if (res != success) - { free_item(&new_item); + if (res != success && res != already_inside) return (res); - } } ++i; } @@ -467,34 +521,6 @@ t_ft_stat construct_first_kernel(__attribute__((unused))t_vec *kernel, __attribu return (res); } -t_ft_stat append_token(t_vec *tokens, const t_token *token) -{ - t_ft_stat res; - t_token token_clone; - - token_clone = ft_token_dup(token); - if (!token_clone.type) - return (alloc_fail); - res = ft_vec_append(tokens, &token_clone); - if (res != success) - ft_free_token(&token_clone); - return (res); -} - -t_ft_stat prepend_token(t_vec *tokens, const t_token *token) -{ - t_ft_stat res; - t_token token_clone; - - token_clone = ft_token_dup(token); - if (!token_clone.type) - return (alloc_fail); - res = ft_vec_insert(tokens, &token_clone, 0); - if (res != success) - ft_free_token(&token_clone); - return (res); -} - int token_in_results(const t_token *token, const t_vec *rules) { size_t i;