From: Lukas Jiriste Date: Thu, 28 Nov 2024 10:58:17 +0000 (+0100) Subject: Merge parsing_table_constructor into no_crash X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=ee133592aaa226cf3afcaa2904ad802e12c1ace5;p=Libft.git Merge parsing_table_constructor into no_crash --- ee133592aaa226cf3afcaa2904ad802e12c1ace5 diff --cc Makefile index 537e92d,83286a7..502f614 --- a/Makefile +++ b/Makefile @@@ -1,10 -1,5 +1,10 @@@ CC := cc - CFLAGS := -std=c99 -Wall -Wextra -Werror -Wpedantic + CFLAGS := -std=c99 -Wall -Wextra -Werror -Wpedantic -fPIE + +ifneq ("$(wildcard .debug)","") + CFLAGS += -g +endif + AR := ar RM := rm -f @@@ -14,138 -9,155 +14,156 @@@ INCLUDE := $(addprefix -I, $(INCDIR) SRCDIR := ft_gen ft_math ft_str ft_mem ft_io ft_check ft_conv ft_lst ft_arr ft_parse ft_struct - SRCstruct:= ft_stack_free.c \ - ft_stack_init.c \ - ft_stack_pop.c \ - ft_stack_pop_forget.c \ - ft_stack_push.c \ - ft_stack_top.c \ - - SRCparse:= ft_parse.c \ - ft_parsing_table_init.c \ - ft_parsing_table_load.c \ - ft_parsing_table_load_helpers.c \ - ft_parsing_table_print.c \ - ft_parsing_table_free.c \ - ft_parse_tree_print.c \ - ft_parse_tree_free.c \ - load_rules.c \ - add_line.c \ - actions.c \ - helpers.c \ - - SRCgen := ft_swap.c \ - - SRCmath := ft_abs.c \ - ft_sgn.c \ - ft_max.c \ - ft_min.c \ - - SRCstr := ft_strcat_alloc.c \ - ft_strncat_alloc.c \ - ft_strcmp.c \ - ft_strncmp.c \ - ft_strndup.c \ - ft_strchr.c \ - ft_strtrim.c \ - ft_strlen.c \ - ft_strdup.c \ - ft_strlcpy.c \ - ft_strnstr.c \ - ft_striteri.c \ - ft_strmapi.c \ - ft_strjoin.c \ - ft_remove_space.c \ - ft_split.c \ - ft_substr.c \ - ft_strlcat.c \ - ft_strrchr.c \ - - SRCmem := ft_calloc.c \ - ft_memchr.c \ - ft_memcmp.c \ - ft_memset.c \ - ft_memmove.c \ - ft_memcpy.c \ - ft_bzero.c \ - - SRCio := ft_putendl_fd.c \ - ft_putnbr_fd.c \ - ft_printf/conversion.c \ - ft_printf/ft_printf.c \ - ft_printf/padding.c \ - ft_printf/parsing.c \ - ft_printf/formatting.c \ - ft_putchar_fd.c \ - get_next_line.c \ - ft_putstr_fd.c \ - - SRCcheck:= ft_isalnum.c \ - ft_isspace.c \ - ft_isdigit.c \ - ft_isascii.c \ - ft_isalpha.c \ - ft_islower.c \ - ft_isupper.c \ - ft_isprint.c \ - ft_isint.c \ - - SRCconv := ft_itoa_base.c \ - ft_tolower.c \ - ft_toupper.c \ - ft_atoi.c \ - ft_ctoa.c \ - ft_itoa.c \ - ft_uitoa_base.c \ - - SRClst := ft_lst_reverse.c \ - ft_lstdelone.c \ - ft_lst_foreach_if.c \ - ft_lstlast.c \ - ft_lstclear.c \ - ft_lst_at.c \ - ft_lst_sorted_merge.c \ - ft_lst_find.c \ - ft_lstmap.c \ - ft_lst_remove_if.c \ - ft_lstnew.c \ - ft_lst_merge.c \ - ft_lstsize.c \ - ft_lstadd_front.c \ - ft_lstadd_back.c \ - ft_lstiter.c \ - ft_lst_sort.c \ - ft_lst_sorted_insert.c \ - - SRCarr := ft_vec_init.c \ - ft_vec_reserve.c \ - ft_vec_enlarge.c \ - ft_vec_insert.c \ - ft_vec_append.c \ - ft_vec_forget.c \ - ft_vec_erase.c \ - ft_vec_access.c \ - ft_vec_free.c \ - ft_vec_find.c \ - ft_vec_find_index.c \ - ft_vec_copy.c \ - \ - ft_vec_is_equal.c \ - \ - ft_vec_contains.c \ - ft_vec_is_subset.c \ - ft_vec_is_setequal.c \ - \ - ft_vec_setinsert.c \ - \ - ft_mat_init.c \ - ft_mat_append.c \ - ft_mat_insert_col.c \ - ft_mat_insert_row.c \ - ft_mat_zeros.c \ - ft_mat_fill.c \ - ft_mat_access.c \ - ft_mat_free.c \ + SRCstruct:= ft_stack_free.c \ + ft_stack_init.c \ + ft_stack_pop.c \ + ft_stack_pop_forget.c \ + ft_stack_push.c \ + ft_stack_top.c \ + + SRCparse:= ft_parse.c \ + ft_parsing_table_init.c \ + ft_parsing_table_load.c \ ++ ft_parsing_table_load_helpers.c \ + ft_parsing_table_print.c \ + ft_parsing_table_save.c \ + ft_parsing_table_free.c \ + ft_parse_tree_print.c \ + ft_parse_tree_free.c \ + load_rules.c \ ++ load_rules_helpers.c \ + add_line.c \ + actions.c \ + helpers.c \ + parsing_table_constructor/categorize_tokens.c \ + parsing_table_constructor/categorize_helpers.c \ + parsing_table_constructor/conversion_to_table.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 \ + parsing_table_constructor/lookahead2.c \ + parsing_table_constructor/prepare_table.c \ + parsing_table_constructor/solve_gotos.c \ + parsing_table_constructor/ft_parsing_table_generate.c \ + - + SRCgen := ft_swap.c \ + + SRCmath := ft_abs.c \ + ft_sgn.c \ + ft_max.c \ + ft_min.c \ + + SRCstr := ft_strcat_alloc.c \ + ft_strncat_alloc.c \ + ft_strcmp.c \ + ft_strncmp.c \ + ft_strndup.c \ + ft_strchr.c \ + ft_strtrim.c \ + ft_strlen.c \ + ft_strdup.c \ + ft_strlcpy.c \ + ft_strnstr.c \ + ft_striteri.c \ + ft_strmapi.c \ + ft_strjoin.c \ + ft_remove_space.c \ + ft_split.c \ + ft_substr.c \ + ft_strlcat.c \ + ft_strrchr.c \ + + SRCmem := ft_calloc.c \ + ft_memchr.c \ + ft_memcmp.c \ + ft_memset.c \ + ft_memmove.c \ + ft_memcpy.c \ + ft_bzero.c \ + + SRCio := ft_putendl_fd.c \ + ft_putnbr_fd.c \ + ft_printf/conversion.c \ + ft_printf/ft_printf.c \ + ft_printf/padding.c \ + ft_printf/parsing.c \ + ft_printf/formatting.c \ + ft_putchar_fd.c \ + get_next_line.c \ + ft_putstr_fd.c \ + + SRCcheck:= ft_isalnum.c \ + ft_isspace.c \ + ft_isdigit.c \ + ft_isascii.c \ + ft_isalpha.c \ + ft_islower.c \ + ft_isupper.c \ + ft_isprint.c \ + ft_isint.c \ + + SRCconv := ft_itoa_base.c \ + ft_tolower.c \ + ft_toupper.c \ + ft_atoi.c \ + ft_ctoa.c \ + ft_itoa.c \ + ft_uitoa_base.c \ + + SRClst := ft_lst_reverse.c \ + ft_lstdelone.c \ + ft_lst_foreach_if.c \ + ft_lstlast.c \ + ft_lstclear.c \ + ft_lst_at.c \ + ft_lst_sorted_merge.c \ + ft_lst_find.c \ + ft_lstmap.c \ + ft_lst_remove_if.c \ + ft_lstnew.c \ + ft_lst_merge.c \ + ft_lstsize.c \ + ft_lstadd_front.c \ + ft_lstadd_back.c \ + ft_lstiter.c \ + ft_lst_sort.c \ + ft_lst_sorted_insert.c \ + + SRCarr := ft_vec_init.c \ + ft_vec_reserve.c \ + ft_vec_enlarge.c \ + ft_vec_insert.c \ + ft_vec_append.c \ + ft_vec_forget.c \ + ft_vec_erase.c \ + ft_vec_access.c \ + ft_vec_free.c \ + ft_vec_find.c \ + ft_vec_find_index.c \ + ft_vec_copy.c \ + \ + ft_vec_is_equal.c \ + \ + ft_vec_contains.c \ + ft_vec_is_subset.c \ + ft_vec_is_setequal.c \ + \ + ft_vec_setinsert.c \ + \ + ft_mat_init.c \ + ft_mat_append.c \ + ft_mat_insert_col.c \ + ft_mat_insert_row.c \ + ft_mat_zeros.c \ + ft_mat_fill.c \ + ft_mat_access.c \ + ft_mat_free.c \ SOURCES := $(foreach dir, $(SRCDIR), $(addprefix $(dir)/, $($(dir:ft_%=SRC%)))) OBJECTS := $(SOURCES:.c=.o) diff --cc ft_parse/ft_parse_inner.h index 3d5cea9,2e14f5e..7ec51e8 --- a/ft_parse/ft_parse_inner.h +++ b/ft_parse/ft_parse_inner.h @@@ -6,7 -6,7 +6,7 @@@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/20 13:23:20 by ljiriste #+# #+# */ - /* Updated: 2024/08/02 14:21:48 by ljiriste ### ########.fr */ -/* Updated: 2024/11/26 17:03:13 by ljiriste ### ########.fr */ ++/* Updated: 2024/11/28 11:54:35 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@@ -15,22 -15,19 +15,27 @@@ # include "libft.h" --void ft_free_token(void *v_token); --void ft_free_rule(void *v_rule); --void ft_free_state(void *v_state); --void ft_free_stack_element(void *v_el); ++void ft_free_token(void *v_token); ++void ft_free_rule(void *v_rule); ++void ft_free_state(void *v_state); ++void ft_free_stack_element(void *v_el); - t_token ft_token_dup(t_token token); -t_token ft_token_dup(const t_token *token); --ssize_t find_token_index(t_token token, const t_vec *tokens); ++t_token ft_token_dup(const t_token *token); ++ssize_t find_token_index(t_token token, const t_vec *tokens); - t_ft_stat load_rules_fd(t_vec *rules, int fd); - t_ft_stat load_rules_str(t_vec *rules, const char *rules_str); -size_t get_terminal_tokens_num(t_vec *tokens); -t_ft_stat load_rules(t_vec *rules, const char *rules_filename); --int add_line(t_vec *states, const char *line, size_t lookahead_size); --int follow_rule(t_stack *stack, size_t rule_num, -- const t_parsing_table *table); --int push_state(t_stack *stack, size_t state_num, t_token token); ++void parse_constituents(t_vec *constituents, const char *line); ++int is_valid_rule(t_grammar_rule *rule); ++t_grammar_rule parse_rule(const char *line); ++t_ft_stat load_rules_fd(t_vec *rules, int fd); ++t_ft_stat load_rules_name(t_vec *rules, const char *rules_filename); ++t_ft_stat load_rules_str(t_vec *rules, const char *rules_str); ++int add_line(t_vec *states, ++ const char *line, size_t lookahead_size); ++int follow_rule(t_stack *stack, size_t rule_num, ++ const t_parsing_table *table); ++int push_state(t_stack *stack, size_t state_num, t_token token); - int is_consistent(t_parsing_table *table); - size_t get_terminal_tokens_num(t_vec *tokens); - t_vec parse_header(const char *header); ++int is_consistent(t_parsing_table *table); ++size_t get_terminal_tokens_num(t_vec *tokens); ++t_vec parse_header(const char *header); #endif //FT_PARSE_INNER_H diff --cc ft_parse/ft_parsing_table_load.c index 7f916ba,0c28a7d..1a48e97 --- a/ft_parse/ft_parsing_table_load.c +++ b/ft_parse/ft_parsing_table_load.c @@@ -6,7 -6,7 +6,7 @@@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/20 12:34:17 by ljiriste #+# #+# */ - /* Updated: 2024/08/02 14:13:13 by ljiriste ### ########.fr */ -/* Updated: 2024/07/07 09:44:09 by ljiriste ### ########.fr */ ++/* Updated: 2024/11/28 11:43:45 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@@ -17,38 -17,60 +17,38 @@@ #include #include -static char *get_token_type(const char *line) +t_ft_stat ft_parsing_table_load_str(t_parsing_table *table, - const char *table_str, const char *rules_str) ++ const char *table_str, const char *rules_str) { + char **table_split; size_t i; - char *type; - i = 0; - while (line[i] && line[i] != ';') - ++i; - type = ft_strndup(line, i); - return (type); -} - -static t_vec parse_header(const char *header) -{ - t_vec tokens; - t_token token; - char *condensed_line; - size_t i; - - condensed_line = ft_remove_space(header); - ft_vec_init(&tokens, sizeof(t_token)); - token.str = NULL; - i = 0; - while (condensed_line[i] && condensed_line[i] != ';') - ++i; - while (condensed_line[i]) + load_rules_str(&table->rules, rules_str); + table_split = ft_split(table_str, "\n"); + if (!table_split) + return (alloc_fail); + table->tokens = parse_header(table_split[0]); + table->terminal_tokens_num = get_terminal_tokens_num(&table->tokens); + i = 1; + while (table_split[i]) { + add_line(&table->states, table_split[i], + table->terminal_tokens_num + 1); ++i; - token.type = get_token_type(condensed_line + i); - while (condensed_line[i] && condensed_line[i] != ';') - ++i; - ft_vec_append(&tokens, &token); } - free(condensed_line); - return (tokens); + ft_free_split(table_split); + if (is_consistent(table)) + return (success); + return (non_specific_failure); } -static int is_consistent(__attribute__((unused)) t_parsing_table *table) +t_ft_stat ft_parsing_table_load_fd(t_parsing_table *table, + int table_fd, int rules_fd) { - return (1); -} + char *line; -t_ft_stat ft_parsing_table_load(t_parsing_table *table, - const char *filename, - const char *rules_filename) -{ - int fd; - char *line; - - load_rules(&table->rules, rules_filename); - fd = open(filename, O_RDONLY); - if (fd < 0) - return (file_error); - line = get_next_line(fd); + load_rules_fd(&table->rules, rules_fd); + line = get_next_line(table_fd); table->tokens = parse_header(line); table->terminal_tokens_num = get_terminal_tokens_num(&table->tokens); free(line); diff --cc ft_parse/ft_parsing_table_load_helpers.c index 8436368,0000000..36267d3 mode 100644,000000..100644 --- a/ft_parse/ft_parsing_table_load_helpers.c +++ b/ft_parse/ft_parsing_table_load_helpers.c @@@ -1,75 -1,0 +1,59 @@@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_parsing_table_load_helpers.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/02 14:09:21 by ljiriste #+# #+# */ - /* Updated: 2024/08/02 14:21:37 by ljiriste ### ########.fr */ ++/* Updated: 2024/11/28 17:00:08 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_parse_inner.h" +#include "libft.h" +#include +#include +#include + +static char *get_token_type(const char *line) +{ + size_t i; + char *type; + + i = 0; + while (line[i] && line[i] != ';') + ++i; + type = ft_strndup(line, i); + return (type); +} + +t_vec parse_header(const char *header) +{ + t_vec tokens; + t_token token; + char *condensed_line; + size_t i; + + condensed_line = ft_remove_space(header); + ft_vec_init(&tokens, sizeof(t_token)); + token.str = NULL; + i = 0; + while (condensed_line[i] && condensed_line[i] != ';') + ++i; + while (condensed_line[i]) + { + ++i; + token.type = get_token_type(condensed_line + i); + while (condensed_line[i] && condensed_line[i] != ';') + ++i; + ft_vec_append(&tokens, &token); + } + free(condensed_line); + return (tokens); +} + - size_t get_terminal_tokens_num(t_vec *tokens) - { - size_t i; - t_token *token; - - i = 0; - while (i < tokens->size) - { - token = (t_token *)ft_vec_access(tokens, i); - if (!ft_strcmp(token->type, "$")) - return (i); - ++i; - } - return (0); - } - +int is_consistent(__attribute__((unused)) t_parsing_table *table) +{ + return (1); +} diff --cc ft_parse/load_rules.c index 162df5f,857e265..35ed2cc --- a/ft_parse/load_rules.c +++ b/ft_parse/load_rules.c @@@ -6,7 -6,7 +6,7 @@@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/20 13:29:48 by ljiriste #+# #+# */ - /* Updated: 2024/08/02 14:24:01 by ljiriste ### ########.fr */ -/* Updated: 2024/06/21 16:45:29 by ljiriste ### ########.fr */ ++/* Updated: 2024/11/28 11:56:19 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@@ -16,95 -16,72 +16,32 @@@ #include #include --static void parse_constituents(t_vec *constituents, const char *line) - { - size_t i; - size_t j; - t_token token; - - token.str = NULL; - i = 0; - while (line[i]) - { - while (ft_isspace(line[i])) - ++i; - j = i; - while (!ft_isspace(line[i]) && line[i]) - ++i; - if (j == i) - break ; - token.type = ft_strndup(line + j, i - j); - ft_vec_append(constituents, &token); - } - } - - static t_grammar_rule parse_rule(const char *line) - { - t_grammar_rule rule; - t_token token; - size_t i; - size_t j; - - token.str = NULL; - ft_vec_init(&rule.constituents, sizeof(t_token)); - i = 0; - while (ft_isspace(line[i])) - ++i; - j = i; - while (!ft_isspace(line[i])) - ++i; - token.type = ft_strndup(line + j, i - j); - rule.result = token; - while (ft_isspace(line[i])) - ++i; - if (!(line[i++] == '-' && line[i++] == '>')) - return (rule); - parse_constituents(&rule.constituents, line + i); - return (rule); - } - - static int is_valid_rule(t_grammar_rule *rule) - { - size_t i; - - if (!rule->result.type) - return (0); - i = 0; - while (i < rule->constituents.size) - { - if (!((t_token *)ft_vec_access(&rule->constituents, i))->type) - return (0); - ++i; - } - return (1); - } - +t_ft_stat load_rules_str(t_vec *rules, const char *rules_str) { - size_t i; - size_t j; - t_token token; - - token.str = NULL; - i = 0; - while (line[i]) - { - while (ft_isspace(line[i])) - ++i; - j = i; - while (!ft_isspace(line[i]) && line[i]) - ++i; - if (j == i) - break ; - token.type = ft_strndup(line + j, i - j); - ft_vec_append(constituents, &token); - } -} - -static t_grammar_rule parse_rule(const char *line) -{ - t_grammar_rule rule; - t_token token; + char **rules_split; size_t i; - size_t j; - - token.str = NULL; - ft_vec_init(&rule.constituents, sizeof(t_token)); - i = 0; - while (ft_isspace(line[i])) - ++i; - j = i; - while (!ft_isspace(line[i])) - ++i; - token.type = ft_strndup(line + j, i - j); - rule.result = token; - while (ft_isspace(line[i])) - ++i; - if (!(line[i++] == '-' && line[i++] == '>')) - return (rule); - parse_constituents(&rule.constituents, line + i); - return (rule); -} - -static int is_valid_rule(t_grammar_rule *rule) -{ - size_t i; + t_grammar_rule rule; - if (!rule->result.type) - return (0); + rules_split = ft_split(rules_str, "\n"); + if (!rules_split) + return (alloc_fail); i = 0; - while (i < rule->constituents.size) + while (rules_split[i]) { - if (!((t_token *)ft_vec_access(&rule->constituents, i))->type) - return (0); + rule = parse_rule(rules_split[i]); + if (!is_valid_rule(&rule) || ft_vec_append(rules, &rule) != success) + { + ft_vec_free(rules, ft_free_rule); + return (non_specific_failure); + } ++i; } - return (1); + ft_free_split(rules_split); + return (success); } -t_ft_stat load_rules(t_vec *rules, const char *rules_filename) +t_ft_stat load_rules_fd(t_vec *rules, int fd) { - int fd; char *line; t_grammar_rule rule; @@@ -120,6 -100,6 +57,18 @@@ free(line); line = get_next_line(fd); } -- close(fd); return (success); } ++ ++t_ft_stat load_rules_name(t_vec *rules, const char *rules_filename) ++{ ++ int rules_fd; ++ t_ft_stat res; ++ ++ rules_fd = open(rules_filename, O_RDONLY); ++ if (rules_fd < 0) ++ return (file_error); ++ res = load_rules_fd(rules, rules_fd); ++ close(rules_fd); ++ return (res); ++} diff --cc ft_parse/load_rules_helpers.c index 0000000,0000000..ad26ef8 new file mode 100644 --- /dev/null +++ b/ft_parse/load_rules_helpers.c @@@ -1,0 -1,0 +1,77 @@@ ++/* ************************************************************************** */ ++/* */ ++/* ::: :::::::: */ ++/* load_rules_helpers.c :+: :+: :+: */ ++/* +:+ +:+ +:+ */ ++/* By: ljiriste +#+ +:+ +#+ */ ++/* +#+#+#+#+#+ +#+ */ ++/* Created: 2024/11/28 11:52:27 by ljiriste #+# #+# */ ++/* Updated: 2024/11/28 11:56:12 by ljiriste ### ########.fr */ ++/* */ ++/* ************************************************************************** */ ++ ++#include "ft_parse_inner.h" ++#include "libft.h" ++ ++void parse_constituents(t_vec *constituents, const char *line) ++{ ++ size_t i; ++ size_t j; ++ t_token token; ++ ++ token.str = NULL; ++ i = 0; ++ while (line[i]) ++ { ++ while (ft_isspace(line[i])) ++ ++i; ++ j = i; ++ while (!ft_isspace(line[i]) && line[i]) ++ ++i; ++ if (j == i) ++ break ; ++ token.type = ft_strndup(line + j, i - j); ++ ft_vec_append(constituents, &token); ++ } ++} ++ ++int is_valid_rule(t_grammar_rule *rule) ++{ ++ size_t i; ++ ++ if (!rule->result.type) ++ return (0); ++ i = 0; ++ while (i < rule->constituents.size) ++ { ++ if (!((t_token *)ft_vec_access(&rule->constituents, i))->type) ++ return (0); ++ ++i; ++ } ++ return (1); ++} ++ ++t_grammar_rule parse_rule(const char *line) ++{ ++ t_grammar_rule rule; ++ t_token token; ++ size_t i; ++ size_t j; ++ ++ token.str = NULL; ++ ft_vec_init(&rule.constituents, sizeof(t_token)); ++ i = 0; ++ while (ft_isspace(line[i])) ++ ++i; ++ j = i; ++ while (!ft_isspace(line[i])) ++ ++i; ++ token.type = ft_strndup(line + j, i - j); ++ rule.result = token; ++ while (ft_isspace(line[i])) ++ ++i; ++ if (!(line[i++] == '-' && line[i++] == '>')) ++ return (rule); ++ parse_constituents(&rule.constituents, line + i); ++ return (rule); ++} diff --cc ft_parse/parsing_table_constructor/prepare_table.c index 0000000,3581748..6efd8dd mode 000000,100644..100644 --- a/ft_parse/parsing_table_constructor/prepare_table.c +++ b/ft_parse/parsing_table_constructor/prepare_table.c @@@ -1,0 -1,78 +1,78 @@@ + /* ************************************************************************** */ + /* */ + /* ::: :::::::: */ + /* prepare_table.c :+: :+: :+: */ + /* +:+ +:+ +:+ */ + /* By: ljiriste +#+ +:+ +#+ */ + /* +#+#+#+#+#+ +#+ */ + /* Created: 2024/11/26 16:26:32 by ljiriste #+# #+# */ -/* Updated: 2024/11/27 11:24:11 by ljiriste ### ########.fr */ ++/* Updated: 2024/11/28 11:51:00 by ljiriste ### ########.fr */ + /* */ + /* ************************************************************************** */ + + #include "pt_constructor.h" + #include "../ft_parse_inner.h" + #include "libft.h" + + static t_ft_stat init_table(t_parsing_table *table) + { + t_ft_stat res; + + res = ft_vec_init(&table->rules, sizeof(t_grammar_rule)); + if (res != success) + return (res); + res = ft_vec_init(&table->states, sizeof(t_parser_state)); + if (res != success) + return (res); + res = ft_vec_init(&table->tokens, sizeof(t_token)); + if (res != success) + return (res); + return (success); + } + + static t_ft_stat add_zeroth_rule(t_vec *rules) + { + t_ft_stat res; + t_grammar_rule rule; + t_token first_token; + + rule.result.type = NULL; + rule.result.str = NULL; + first_token = ft_token_dup( + &((const t_grammar_rule *)ft_vec_caccess(rules, 0))->result); + if (!first_token.type) + return (alloc_fail); + res = ft_vec_init(&rule.constituents, sizeof(t_token)); + if (res != success) + return (res); + res = ft_vec_append(&rule.constituents, &first_token); + if (res != success) + ft_free_token(&first_token); + res = ft_vec_insert(rules, &rule, 0); + if (res != success) + ft_free_rule(&rule); + return (success); + } + + t_ft_stat prepare_table(t_parsing_table *table, const char *rules_filename) + { + t_ft_stat res; + + res = init_table(table); + if (res != success) + return (res); - res = load_rules(&table->rules, rules_filename); ++ res = load_rules_name(&table->rules, rules_filename); + if (res != success) + return (res); + res = add_zeroth_rule(&table->rules); + if (res != success) + { + ft_vec_free(&table->rules, ft_free_rule); + return (res); + } + res = categorize_tokens(&table->tokens, &table->rules); + table->terminal_tokens_num = get_terminal_tokens_num(&table->tokens); + if (res != success) + ft_vec_free(&table->rules, ft_free_rule); + return (res); + } diff --cc inc/ft_parse.h index 6c3d05f,4b89a56..b06a1a8 --- a/inc/ft_parse.h +++ b/inc/ft_parse.h @@@ -6,7 -6,7 +6,7 @@@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/27 21:21:54 by ljiriste #+# #+# */ - /* Updated: 2024/08/02 13:57:10 by ljiriste ### ########.fr */ -/* Updated: 2024/07/18 12:41:50 by ljiriste ### ########.fr */ ++/* Updated: 2024/11/28 11:46:05 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@@ -92,13 -92,11 +92,14 @@@ typedef struct s_parser_stack_elemen // Tokens should not contain whitespace as it is used as separator t_ft_stat ft_parsing_table_init(t_parsing_table *table); +t_ft_stat ft_parsing_table_load_name(t_parsing_table *table, - const char *filename, ++ const char *filename, const char *rules_filename); + t_ft_stat ft_parsing_table_generate(t_parsing_table *table, const char *rules_filename); -t_ft_stat ft_parsing_table_load(t_parsing_table *table, - const char *filename, - const char *rules_filename); +t_ft_stat ft_parsing_table_load_fd(t_parsing_table *table, + int table_fd, int rules_fd); +t_ft_stat ft_parsing_table_load_str(t_parsing_table *table, + const char *table_str, const char *rules_str); t_parse_tree_node *ft_parse(const t_vec *tokens, const t_parsing_table *table);