From: Lukas Jiriste Date: Fri, 21 Jun 2024 08:34:09 +0000 (+0200) Subject: Implement printing function for parsing tree X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=30931d514b237451f128d489ceeca40de8e40b85;p=Libft.git Implement printing function for parsing tree This is mainly done for debugging. This does not scale to arbitrary large trees automatically - the output will be garbled. --- diff --git a/Makefile b/Makefile index a33caea..00ad5e0 100644 --- a/Makefile +++ b/Makefile @@ -21,6 +21,7 @@ SRCparse:= ft_parse.c \ ft_parsing_table_load.c \ ft_parsing_table_print.c \ ft_parsing_table_free.c \ + ft_parse_tree_print.c \ load_rules.c \ add_line.c \ diff --git a/ft_parse/ft_parse_tree_print.c b/ft_parse/ft_parse_tree_print.c new file mode 100644 index 0000000..f8e7885 --- /dev/null +++ b/ft_parse/ft_parse_tree_print.c @@ -0,0 +1,69 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_parse_tree_print.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/21 09:51:43 by ljiriste #+# #+# */ +/* Updated: 2024/06/21 10:33:09 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_parse.h" +#include "libft.h" + +#define MAX_WIDTH 80 + +static void print_last_at_depth(t_parse_tree_node *node, size_t depth, char branches[MAX_WIDTH]); +static void print_node_at_depth(t_parse_tree_node *node, size_t depth, char branches[MAX_WIDTH]); + +static void print_children_at_depth(t_parse_tree_node *node, size_t depth, char branches[MAX_WIDTH]) +{ + size_t i; + + i = 0; + while (i + 1 < node->children.size) + { + print_node_at_depth(ft_vec_access(&node->children, i), depth + 1, branches); + ++i; + } + if (i < node->children.size) + print_last_at_depth(ft_vec_access(&node->children, i), depth + 1, branches); + if (depth > 0) + branches[ft_strlen(branches) - 1] = '\0'; + branches[ft_strlen(branches) - 1] = '\0'; + return ; +} + +static void print_last_at_depth(t_parse_tree_node *node, size_t depth, char branches[MAX_WIDTH]) +{ + ft_printf("%s-%s\n", branches, node->token.type); + branches[ft_strlen(branches) - 1] = ' '; + ft_strlcat(branches, " |", MAX_WIDTH); + print_children_at_depth(node, depth, branches); +} + +static void print_node_at_depth(t_parse_tree_node *node, size_t depth, char branches[MAX_WIDTH]) +{ + if (depth == 0) + { + ft_printf("%s\n", node->token.type); + ft_strlcat(branches, "|", MAX_WIDTH); + } + else + { + ft_printf("%s-%s\n", branches, node->token.type); + ft_strlcat(branches, " |", MAX_WIDTH); + } + print_children_at_depth(node, depth, branches); +} + +void ft_parse_tree_print(t_parse_tree_node *root) +{ + char branches[MAX_WIDTH]; + + branches[0] = '\0'; + print_node_at_depth(root, 0, branches); + return ; +} diff --git a/inc/ft_parse.h b/inc/ft_parse.h index 69fea39..b4cffcd 100644 --- a/inc/ft_parse.h +++ b/inc/ft_parse.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/27 21:21:54 by ljiriste #+# #+# */ -/* Updated: 2024/06/21 09:04:04 by ljiriste ### ########.fr */ +/* Updated: 2024/06/21 09:57:12 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -96,8 +96,10 @@ 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); + 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);