Add printing for parsing table, fix formatting
authorLukas Jiriste <ljiriste@student.42prague.com>
Sun, 16 Jun 2024 06:18:32 +0000 (08:18 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Sun, 16 Jun 2024 06:18:32 +0000 (08:18 +0200)
The ft_print_parsing_table is useful for debugging.

Makefile
ft_parse/ft_print_parsing_table.c [new file with mode: 0644]
inc/ft_parse.h

index 1895cf6c135dabc2bc414e0d8b3730e391ee669f..b62cdd13fcc05dfac888ef6b9b84dd35fe6f28c5 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -9,111 +9,112 @@ 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
 
-SRCparse:=     ft_parse.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_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                   \
+SRCparse:=     ft_parse.c                                      \
+                       ft_print_parsing_table.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_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 --git a/ft_parse/ft_print_parsing_table.c b/ft_parse/ft_print_parsing_table.c
new file mode 100644 (file)
index 0000000..e864b7e
--- /dev/null
@@ -0,0 +1,92 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_print_parsing_table.c                           :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/06/16 07:19:50 by ljiriste          #+#    #+#             */
+/*   Updated: 2024/06/16 08:17:46 by ljiriste         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "ft_parse.h"
+#include "libft.h"
+#include <sys/types.h>
+
+static void    print_rule(t_grammar_rule *rule, unsigned int column_width)
+{
+       size_t  i;
+
+       ft_printf("%-*s -> ", column_width - 1, rule->result.type);
+       i = 0;
+       while (i < rule->constituents.size)
+               ft_printf("%-*s ", column_width - 1,
+                       ((t_token *)ft_vec_access(&rule->constituents, i++))->type);
+       ft_printf("\n");
+       return ;
+}
+
+static void    print_action(t_parser_action *action, unsigned int column_width)
+{
+       if (action->type == parser_reduce)
+               ft_printf("r%-*u ", column_width - 2, action->number);
+       else if (action->type == parser_shift)
+               ft_printf("s%-*u ", column_width - 2, action->number);
+       else if (action->type == parser_accept)
+               ft_printf("%-*s ", column_width - 1, "acc");
+       else
+               ft_printf("%*s", column_width, "");
+       return ;
+}
+
+static void    print_state(t_parser_state *state,
+               unsigned int column_width, size_t state_num)
+{
+       size_t  i;
+       t_vec   *vec;
+       ssize_t *gt;
+
+       ft_printf("%-*u ", column_width, state_num);
+       vec = &state->lookahead;
+       i = 0;
+       while (i < vec->size)
+               print_action(ft_vec_access(vec, i++), column_width);
+       vec = &state->gotos;
+       i = 0;
+       while (i < vec->size)
+       {
+               gt = ft_vec_access(vec, i++);
+               if (*gt < 0)
+                       ft_printf("%*s", column_width, "");
+               else
+                       ft_printf("%-*u ", column_width - 1, *gt);
+       }
+       ft_printf("\n");
+       return ;
+}
+
+void   ft_print_parsing_table(t_parsing_table *table,
+               unsigned int column_width)
+{
+       size_t                  i;
+
+       if (column_width < 3)
+               column_width = 3;
+       i = 0;
+       while (i < table->rules.size)
+               print_rule(ft_vec_access(&table->rules, i++), column_width);
+       i = 0;
+       ft_printf("\n%-*s ", column_width, "State");
+       while (i < table->tokens.size)
+               ft_printf("%-*s ", column_width,
+                       ((t_token *)ft_vec_access(&table->tokens, i++))->type);
+       ft_printf("\n");
+       i = 0;
+       while (i < table->states.size)
+       {
+               print_state(ft_vec_access(&table->states, i), column_width, i);
+               ++i;
+       }
+       return ;
+}
index 91e796813d9fcf212d17f3cca4c36a97fdd10ba1..b06c05ec6bd78e820d2c31b6494b5b0ad4838c6d 100644 (file)
@@ -6,14 +6,14 @@
 /*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/05/27 21:21:54 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/06/14 15:29:56 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/06/16 08:17:25 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
 #ifndef FT_PARSE_H
 # define FT_PARSE_H
 
-# include "libft.h"
+# include "ft_arr.h"
 
 typedef struct s_token
 {
@@ -77,7 +77,9 @@ typedef struct s_parsing_table
 }                      t_parsing_table;
 
 t_parsing_table        ft_load_parsing_table(const char *filename,
-               const char *rules_filename);
+                                       const char *rules_filename);
+void                   ft_print_parsing_table(t_parsing_table *table,
+                                       unsigned int column_width);
 void                   ft_free_parsing_table(t_parsing_table *table);
 
 #endif // FT_PARSE_H