Make ft_parse inputs const, move token_free
authorLukas Jiriste <ljiriste@student.42prague.com>
Fri, 21 Jun 2024 09:49:45 +0000 (11:49 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Fri, 21 Jun 2024 09:49:45 +0000 (11:49 +0200)
After playing a little with the functions I thought it stupid to have
all tokens have their own memory for types that repeat so much.
The token_free function I implemented frees both members of token
which leads to multi-frees when reusing a string for type.

This is why I think it will be better to hide token_free again
and the user can decide what to allocate and what to free.

For this to be possible I have guarantee the tokens vector is not
changed inside the ft_parse function, so I've rewriten it a little
to use const.

ft_parse/ft_parse.c
ft_parse/ft_parse_inner.h
inc/ft_parse.h

index 864f851aeedb1f740e7618041230a8716ea453ff..327fda28af64666c274d2dabb2f8b39ab5568b8d 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/05/20 20:51:36 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/06/21 11:21:24 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/06/21 11:46:04 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -69,33 +69,33 @@ int push_state(t_stack *stack, size_t state_num, t_token token)
        return (0);
 }
 
-ssize_t        find_token_index(t_token token, t_vec *tokens)
+ssize_t        find_token_index(t_token token, const t_vec *tokens)
 {
        size_t  i;
 
        i = 0;
        while (i < tokens->size)
        {
-               if (!ft_strcmp(token.type, ((t_token *)ft_vec_access(tokens, i))->type))
+               if (!ft_strcmp(token.type, ((t_token *)ft_vec_caccess(tokens, i))->type))
                        return (i);
                ++i;
        }
        return (-1);
 }
 
-ssize_t        goto_state(size_t state_num, t_parsing_table *table, t_token token)
+ssize_t        goto_state(size_t state_num, const t_parsing_table *table, t_token token)
 {
-       t_parser_state  *state;
-       ssize_t                 column;
+       const t_parser_state    *state;
+       ssize_t                                 column;
 
-       state = ft_vec_access(&table->states, state_num);
+       state = ft_vec_caccess(&table->states, state_num);
        column = find_token_index(token, &table->tokens);
-       return (*(ssize_t *)ft_vec_access(&state->gotos, column - (table->terminal_tokens_num + 1)));
+       return (*(ssize_t *)ft_vec_caccess(&state->gotos, column - (table->terminal_tokens_num + 1)));
 }
 
-int    follow_rule(t_stack *stack, size_t rule_num, t_parsing_table *table)
+int    follow_rule(t_stack *stack, size_t rule_num, const t_parsing_table *table)
 {
-       t_grammar_rule                  *rule;
+       const t_grammar_rule    *rule;
        t_parser_stack_element  element;
        t_parse_tree_node               *node;
        size_t                                  i;
@@ -104,7 +104,7 @@ int follow_rule(t_stack *stack, size_t rule_num, t_parsing_table *table)
        if (!element.node)
                return (1);
        ft_vec_init(&element.node->children, sizeof(t_parse_tree_node));
-       rule = ft_vec_access(&table->rules, rule_num);
+       rule = ft_vec_caccess(&table->rules, rule_num);
        element.node->token = dup_token(rule->result);
        i = rule->constituents.size;
        while (i > 0)
@@ -112,7 +112,7 @@ int follow_rule(t_stack *stack, size_t rule_num, t_parsing_table *table)
                --i;
                node = ((t_parser_stack_element *)ft_stack_pop(stack, NULL))->node;
                if (ft_strcmp(node->token.type,
-                                       ((t_token *)ft_vec_access(&rule->constituents, i))->type) ||
+                                       ((t_token *)ft_vec_caccess(&rule->constituents, i))->type) ||
                                ft_vec_insert(&element.node->children, node, 0) != success)
                {
                        ft_parse_tree_free(element.node);
@@ -136,22 +136,22 @@ void      initialize_parser_stack(t_stack *stack)
        return ;
 }
 
-t_parse_tree_node      *ft_parse(t_vec *tokens, t_parsing_table *table)
+t_parse_tree_node      *ft_parse(const t_vec *tokens, const t_parsing_table *table)
 {
-       t_stack                         stack;
-       size_t                          i;
-       t_token                         token;
-       t_parser_state          *state;
-       t_parser_action         *action;
-       ssize_t                         column;
-       t_parse_tree_node       *root;
+       t_stack                                 stack;
+       size_t                                  i;
+       t_token                                 token;
+       const t_parser_state    *state;
+       const t_parser_action   *action;
+       ssize_t                                 column;
+       t_parse_tree_node               *root;
 
        initialize_parser_stack(&stack);
        i = 0;
        while (1)
        {
                if (i < tokens->size)
-                       token = *(t_token *)ft_vec_access(tokens, i);
+                       token = *(t_token *)ft_vec_caccess(tokens, i);
                else if (i == tokens->size)
                        token = (t_token){.type = "$", .str = NULL};
                else
@@ -159,7 +159,7 @@ t_parse_tree_node   *ft_parse(t_vec *tokens, t_parsing_table *table)
                        ft_stack_free(&stack, ft_stack_element_free);
                        return (NULL);
                }
-               state = ft_vec_access(&table->states,
+               state = ft_vec_caccess(&table->states,
                        ((t_parser_stack_element *)ft_stack_top(&stack))->state_num);
                column = find_token_index(token, &table->tokens);
                if (column < 0 || (size_t)column > table->terminal_tokens_num)
@@ -167,7 +167,7 @@ t_parse_tree_node   *ft_parse(t_vec *tokens, t_parsing_table *table)
                        ft_stack_free(&stack, ft_stack_element_free);
                        return (NULL);
                }
-               action = ft_vec_access(&state->lookahead, column);
+               action = ft_vec_caccess(&state->lookahead, column);
                if (action->type == parser_reduce)
                {
                        if (follow_rule(&stack, action->number, table))
index 9ebbdb8718bf455cb1c3421d08486ead7b78f4ff..2b15263ff1d7ab2179d73e863330ededa095a66e 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/06/20 13:23:20 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/06/21 09:02:55 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/06/21 11:47:25 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -15,6 +15,7 @@
 
 # include "libft.h"
 
+void           free_token(void *v_token);
 void           free_rule(void *v_rule);
 void           free_state(void *v_state);
 
index b4cffcdc3a3974411c68bc5d6423fad38be7aa2a..d567af4f6581dde633bfc22baf44953d4449d50f 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/05/27 21:21:54 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/06/21 09:57:12 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/06/21 11:47:12 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -95,13 +95,12 @@ t_ft_stat   ft_parsing_table_init(t_parsing_table *table);
 t_ft_stat      ft_parsing_table_load(t_parsing_table *table,
                                const char *filename,
                                const char *rules_filename);
-t_parse_tree_node      *ft_parse(t_vec *tokens, t_parsing_table *table);
+t_parse_tree_node      *ft_parse(const t_vec *tokens, const t_parsing_table *table);
 
 void           ft_parsing_table_print(t_parsing_table *table,
                                unsigned int column_width);
 void   ft_parse_tree_print(t_parse_tree_node *root);
 
-void           free_token(void *v_token);
 void           ft_parse_tree_free(void *v_node);
 void           ft_parsing_table_free(t_parsing_table *table);