Fix handling if the zeroth rule
authorLukas Jiriste <ljiriste@student.42prague.com>
Thu, 11 Jul 2024 18:08:39 +0000 (20:08 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Sun, 21 Jul 2024 18:21:22 +0000 (20:21 +0200)
Removing the zeroth rule before the translation makes all the items
point one rule ahaed. So instead the rule number is decreased by one
the zeroth rule reduce is converted to accept.

ft_parse/ft_parsing_table_generate.c

index f2615081248f76f5b10ce2c2ef90555ba7d2a485..f08af84f2bc06f52f90c8815c189fc7da411a1ba 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/06/27 11:16:53 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/07/11 19:56:28 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/07/11 20:03:26 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -778,6 +778,7 @@ size_t      get_rule_index(const t_grammar_rule *rule, const t_vec *rules)
 void   add_reduce(t_vec *lookahead, const t_lr1_item *item, const t_vec *tokens, const t_vec *rules)
 {
        size_t  i;
+       size_t  rule_num;
        const t_token   *token;
        t_parser_action *action;
 
@@ -786,8 +787,14 @@ void       add_reduce(t_vec *lookahead, const t_lr1_item *item, const t_vec *tokens, c
        {
                token = ft_vec_caccess(&item->lookahead, i);
                action = ft_vec_access(lookahead, get_token_position(token, tokens));
-               action->type = parser_reduce;
-               action->number = get_rule_index(item->core.rule, rules);
+               rule_num = get_rule_index(item->core.rule, rules);
+               if (rule_num == 0)
+                       action->type = parser_accept;
+               else
+               {
+                       action->type = parser_reduce;
+                       action->number = rule_num - 1;
+               }
                ++i;
        }
        return ;
@@ -932,10 +939,10 @@ t_ft_stat ft_parsing_table_generate(t_parsing_table *table, const char *rules_fi
        res = construct_states(&states, &table->rules, &table->tokens);
        if (res != success)
                return (res);
-       remove_zeroth_rule(&table->rules);
        res = translate_to_table(table, &states);
        if (res != success)
                return (res);
        ft_vec_free(&states, void_free_generator_state);
+       remove_zeroth_rule(&table->rules);
        return (success);
 }