From 7f69ce954c71f077c2db33edb986c13519a88dad Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Thu, 28 Nov 2024 11:23:25 +0100 Subject: [PATCH] Make this branch fully Norm compliant I needed to split the main file a little yet. The grouping may not make much sense, I did not want to spend any brain power on this anymore. --- Makefile | 2 + .../ft_parsing_table_generate.c | 53 ++-------------- ft_parse/parsing_table_constructor/helpers.c | 61 +++++++++++++++++++ .../parsing_table_constructor/helpers_cmp.c | 18 +----- .../helpers_void_cmp.c | 28 +++++++++ .../parsing_table_constructor/lookahead.c | 5 +- .../pt_constructor.h | 4 +- 7 files changed, 102 insertions(+), 69 deletions(-) create mode 100644 ft_parse/parsing_table_constructor/helpers.c create mode 100644 ft_parse/parsing_table_constructor/helpers_void_cmp.c diff --git a/Makefile b/Makefile index ddf223d..4cd10a9 100644 --- a/Makefile +++ b/Makefile @@ -34,7 +34,9 @@ SRCparse:= ft_parse.c \ parsing_table_constructor/conversion_helpers.c \ parsing_table_constructor/conversion_subhelpers.c \ parsing_table_constructor/fill_closure.c \ + parsing_table_constructor/helpers.c \ parsing_table_constructor/helpers_cmp.c \ + parsing_table_constructor/helpers_void_cmp.c \ parsing_table_constructor/helpers_free.c \ parsing_table_constructor/init_new_row.c \ parsing_table_constructor/lookahead.c \ diff --git a/ft_parse/parsing_table_constructor/ft_parsing_table_generate.c b/ft_parse/parsing_table_constructor/ft_parsing_table_generate.c index cee4b80..3deec33 100644 --- a/ft_parse/parsing_table_constructor/ft_parsing_table_generate.c +++ b/ft_parse/parsing_table_constructor/ft_parsing_table_generate.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/27 11:16:53 by ljiriste #+# #+# */ -/* Updated: 2024/11/27 11:32:46 by ljiriste ### ########.fr */ +/* Updated: 2024/11/28 11:19:27 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,46 +16,7 @@ #include "libft.h" #include -t_ft_stat v_token_dup(void *dest, const void *src) -{ - if (dest == NULL) - return (alloc_fail); - *(t_token *)dest = ft_token_dup((t_token *)src); - return (success); -} - -t_lr1_item *duplicate_item(const t_lr1_item *item) -{ - t_lr1_item *res; - - res = malloc(sizeof(*res)); - if (!res) - return (res); - if (ft_vec_copy(&res->lookahead, &item->lookahead, v_token_dup, - ft_free_token) != success) - { - free(res); - return (NULL); - } - res->core = item->core; - return (res); -} - -int is_viable_item(const t_lr1_item *item, const t_token *token) -{ - const t_token *wanted_token; - - wanted_token - = ft_vec_caccess(&item->core.rule->constituents, item->core.position); - return (cmp_token_type(wanted_token, token) == 0); -} - -const t_token *get_next_token(const t_marked_grammar_rule *rule) -{ - return (ft_vec_caccess(&rule->rule->constituents, rule->position)); -} - -t_ft_stat init_state(t_generator_state **state) +static t_ft_stat init_state(t_generator_state **state) { t_ft_stat res; @@ -97,7 +58,7 @@ t_ft_stat construct_state(t_vec *kernel, t_vec *states, return (solve_gotos(state, states, rules, tokens)); } -t_ft_stat construct_first_kernel(t_vec *kernel, const t_vec *rules) +static t_ft_stat construct_first_kernel(t_vec *kernel, const t_vec *rules) { t_ft_stat res; t_lr1_item item; @@ -123,7 +84,7 @@ t_ft_stat construct_first_kernel(t_vec *kernel, const t_vec *rules) return (res); } -t_ft_stat construct_states( +static t_ft_stat construct_states( t_vec *states, const t_vec *rules, const t_vec *tokens) { t_vec kernel; @@ -141,12 +102,6 @@ t_ft_stat construct_states( return (success); } -static void remove_zeroth_rule(t_vec *rules) -{ - ft_vec_erase(rules, 0, ft_free_rule); - return ; -} - t_ft_stat ft_parsing_table_generate( t_parsing_table *table, const char *rules_filename) { diff --git a/ft_parse/parsing_table_constructor/helpers.c b/ft_parse/parsing_table_constructor/helpers.c new file mode 100644 index 0000000..cde2405 --- /dev/null +++ b/ft_parse/parsing_table_constructor/helpers.c @@ -0,0 +1,61 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* helpers.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/28 11:14:16 by ljiriste #+# #+# */ +/* Updated: 2024/11/28 11:17:22 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "pt_constructor.h" +#include "../ft_parse_inner.h" +#include "libft.h" +#include + +t_ft_stat v_token_dup(void *dest, const void *src) +{ + if (dest == NULL) + return (alloc_fail); + *(t_token *)dest = ft_token_dup((t_token *)src); + return (success); +} + +t_lr1_item *duplicate_item(const t_lr1_item *item) +{ + t_lr1_item *res; + + res = malloc(sizeof(*res)); + if (!res) + return (res); + if (ft_vec_copy(&res->lookahead, &item->lookahead, v_token_dup, + ft_free_token) != success) + { + free(res); + return (NULL); + } + res->core = item->core; + return (res); +} + +int is_viable_item(const t_lr1_item *item, const t_token *token) +{ + const t_token *wanted_token; + + wanted_token + = ft_vec_caccess(&item->core.rule->constituents, item->core.position); + return (cmp_token_type(wanted_token, token) == 0); +} + +const t_token *get_next_token(const t_marked_grammar_rule *rule) +{ + return (ft_vec_caccess(&rule->rule->constituents, rule->position)); +} + +void remove_zeroth_rule(t_vec *rules) +{ + ft_vec_erase(rules, 0, ft_free_rule); + return ; +} diff --git a/ft_parse/parsing_table_constructor/helpers_cmp.c b/ft_parse/parsing_table_constructor/helpers_cmp.c index f4be20f..fb2f67b 100644 --- a/ft_parse/parsing_table_constructor/helpers_cmp.c +++ b/ft_parse/parsing_table_constructor/helpers_cmp.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/26 16:22:19 by ljiriste #+# #+# */ -/* Updated: 2024/11/27 11:17:27 by ljiriste ### ########.fr */ +/* Updated: 2024/11/28 11:12:17 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,11 +26,6 @@ int cmp_token_type(const t_token *token1, const t_token *token2) return (ft_strcmp(token1->type, token2->type)); } -int void_cmp_token_type(const void *v_token1, const void *v_token2) -{ - return (cmp_token_type(v_token1, v_token2)); -} - int cmp_rules(const t_grammar_rule *rule1, const t_grammar_rule *rule2) { return (cmp_token_type(&rule1->result, &rule2->result) @@ -38,11 +33,6 @@ int cmp_rules(const t_grammar_rule *rule1, const t_grammar_rule *rule2) &rule2->constituents, void_cmp_token_type)); } -int void_cmp_rules(const void *v_rule1, const void *v_rule2) -{ - return (cmp_rules(v_rule1, v_rule2)); -} - int cmp_items(const t_lr1_item *item1, const t_lr1_item *item2) { return (cmp_rules(item1->core.rule, item2->core.rule) @@ -50,9 +40,3 @@ int cmp_items(const t_lr1_item *item1, const t_lr1_item *item2) || !ft_vec_is_setequal(&item1->lookahead, &item2->lookahead, void_cmp_token_type)); } - -int void_cmp_items(const void *v_item1, const void *v_item2) -{ - return (cmp_items(v_item1, v_item2)); -} - diff --git a/ft_parse/parsing_table_constructor/helpers_void_cmp.c b/ft_parse/parsing_table_constructor/helpers_void_cmp.c new file mode 100644 index 0000000..2c8e2e3 --- /dev/null +++ b/ft_parse/parsing_table_constructor/helpers_void_cmp.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* helpers_void_cmp.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/28 11:11:32 by ljiriste #+# #+# */ +/* Updated: 2024/11/28 11:12:54 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "pt_constructor.h" + +int void_cmp_token_type(const void *v_token1, const void *v_token2) +{ + return (cmp_token_type(v_token1, v_token2)); +} + +int void_cmp_rules(const void *v_rule1, const void *v_rule2) +{ + return (cmp_rules(v_rule1, v_rule2)); +} + +int void_cmp_items(const void *v_item1, const void *v_item2) +{ + return (cmp_items(v_item1, v_item2)); +} diff --git a/ft_parse/parsing_table_constructor/lookahead.c b/ft_parse/parsing_table_constructor/lookahead.c index 808903c..4770014 100644 --- a/ft_parse/parsing_table_constructor/lookahead.c +++ b/ft_parse/parsing_table_constructor/lookahead.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/26 16:48:32 by ljiriste #+# #+# */ -/* Updated: 2024/11/27 11:21:00 by ljiriste ### ########.fr */ +/* Updated: 2024/11/28 11:13:14 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,8 @@ #include "../ft_parse_inner.h" #include "libft.h" -static t_ft_stat add_to_lookahead(const t_vec *lookahead, t_vec *new_lookahead) +static t_ft_stat add_to_lookahead( + const t_vec *lookahead, t_vec *new_lookahead) { t_ft_stat res; size_t i; diff --git a/ft_parse/parsing_table_constructor/pt_constructor.h b/ft_parse/parsing_table_constructor/pt_constructor.h index 59efd5e..3205910 100644 --- a/ft_parse/parsing_table_constructor/pt_constructor.h +++ b/ft_parse/parsing_table_constructor/pt_constructor.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/26 16:57:15 by ljiriste #+# #+# */ -/* Updated: 2024/11/27 11:32:22 by ljiriste ### ########.fr */ +/* Updated: 2024/11/28 11:19:11 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -96,4 +96,6 @@ t_ft_stat prepare_table( t_ft_stat convert_to_table(t_parsing_table *table, const t_vec *states); +void remove_zeroth_rule(t_vec *rules); + #endif // PT_CONSTRUCTOR_H -- 2.30.2