parse-cmd: Various fixes and a rotation of the parsing tree

This commit is contained in:
Jérôme Guélen 2025-04-08 12:14:38 +02:00
parent 90d213bf98
commit 10a3c9c411
No known key found for this signature in database
12 changed files with 155 additions and 48 deletions

View file

@ -15,7 +15,8 @@ IFLAGS = -I$(LIBFTDIR)
LINCLUDE = -L$(LIBFTDIR) LINCLUDE = -L$(LIBFTDIR)
ifeq ($(CFLAGS),) ifeq ($(CFLAGS),)
CFLAGS = -Wall -Wextra -Werror $(DEBUG) CFLAGS = $(DEBUG)
# CFLAGS = -Wall -Wextra -Werror $(DEBUG)
endif endif
export CFLAGS export CFLAGS
srcs = \ srcs = \
@ -83,6 +84,8 @@ srcs = \
src/subst/wildcard_exp_utils.c \ src/subst/wildcard_exp_utils.c \
src/subst/wildcard_exp_utils2.c \ src/subst/wildcard_exp_utils2.c \
src/treedrawing.c \ src/treedrawing.c \
src/parser/cmd_parsing.c
objs = $(srcs:.c=.o) objs = $(srcs:.c=.o)
export objs export objs

View file

@ -29,7 +29,7 @@ LIST_OP -> ||
## Grammar after removal of left recursivity ## Grammar after removal of left recursivity
The same priorities as the previous version except it is now LL(1) and The same priorities as the previous version except it is now LL(1) and
therefore compatible with descending syntax analysisi (LL(1)). therefore compatible with descending syntax analysis (LL(1)).
``` ```
LINE -> CMDS eol LINE -> CMDS eol
@ -41,7 +41,7 @@ OPT_PIPELINE -> | GROUP_OR_SIMPLE OPT_PIPELINE
OPT_PIPELINE -> ε OPT_PIPELINE -> ε
GROUP_OR_SIMPLE -> (CMDS) REDIR GROUP_OR_SIMPLE -> (CMDS) REDIR
GROUP_OR_SIMPLE -> SIMPLE GROUP_OR_SIMPLE -> SIMPLE
SIMPLE -> REDIR word REDIR SIMPLE_LST SIMPLE -> REDIR word REDIR SIMPLST
SIMPLE_LST -> word REDIR SIMPLE_LST SIMPLE_LST -> word REDIR SIMPLE_LST
SIMPLE_LST -> ε SIMPLE_LST -> ε
REDIR -> > word REDIR REDIR -> > word REDIR

View file

@ -3,15 +3,14 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* builtin_cd.c :+: :+: :+: */ /* builtin_cd.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/31 16:20:17 by khais #+# #+# */ /* Created: 2025/03/31 16:20:17 by khais #+# #+# */
/* Updated: 2025/03/31 20:04:28 by khais ### ########.fr */ /* Updated: 2025/04/07 17:35:16 by jguelen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "builtins.h" #include "builtins.h"
#include "ft_printf.h"
#include "libft.h" #include "libft.h"
#include <unistd.h> #include <unistd.h>
#include "../../env/env_manip.h" #include "../../env/env_manip.h"

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* minishell.c :+: :+: :+: */ /* minishell.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */ /* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/06 13:44:06 by kcolin #+# #+# */ /* Created: 2025/02/06 13:44:06 by kcolin #+# #+# */
/* Updated: 2025/04/01 13:36:52 by khais ### ########.fr */ /* Updated: 2025/04/07 18:07:30 by jguelen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -23,6 +23,7 @@
#include "env/env_convert.h" #include "env/env_convert.h"
#include "postprocess/expansion/expand_vars.h" #include "postprocess/expansion/expand_vars.h"
/* /*
** Parse shell commands from line. ** Parse shell commands from line.
** **
@ -75,12 +76,33 @@ static void app_init(t_minishell *app, char **envp)
app->env = env_from_envp(envp); app->env = env_from_envp(envp);
} }
t_cmd *minishell_parse(t_minishell *app, char *command_line);
int main(int argc, char *argv[], char **envp) int main(int argc, char *argv[], char **envp)
{ {
char *line; char *line;
t_simple_cmd *cmd; t_simple_cmd *cmd;
t_minishell app; t_minishell app;
(void)argc;
(void)argv;
app_init(&app, envp);
line = "echo coucou";
minishell_parse(&app, line);
return (0);
(void)argc; (void)argc;
(void)argv; (void)argv;
app_init(&app, envp); app_init(&app, envp);

View file

@ -6,7 +6,7 @@
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */ /* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/28 14:55:31 by khais #+# #+# */ /* Created: 2025/03/28 14:55:31 by khais #+# #+# */
/* Updated: 2025/04/04 16:05:25 by jguelen ### ########.fr */ /* Updated: 2025/04/07 18:09:12 by jguelen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,6 +16,7 @@
# include "libft.h" # include "libft.h"
# include "env/env.h" # include "env/env.h"
# include "parser/wordlist/wordlist.h" # include "parser/wordlist/wordlist.h"
# include "parser/wordsplit/wordsplit.h"
# include <sys/stat.h> # include <sys/stat.h>
# include <fcntl.h> # include <fcntl.h>

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* cmd_parsing.c :+: :+: :+: */ /* cmd_parsing.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */ /* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/31 10:28:28 by jguelen #+# #+# */ /* Created: 2025/03/31 10:28:28 by jguelen #+# #+# */
/* Updated: 2025/04/04 18:38:59 by jguelen ### ########.fr */ /* Updated: 2025/04/08 10:21:41 by jguelen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -27,72 +27,152 @@ void parse_error(t_minishell *app, t_worddesc *token)
t_connector which_connector_type(t_worddesc *token) t_connector which_connector_type(t_worddesc *token)
{ {
return ((t_connector)0);
} }
t_cmd *minishell_simple_lst_parse(t_minishell *app, t_cmd_builder *builder) t_cmd *minishell_simple_lst_parse(t_minishell *app, t_cmd_builder *builder)
{ {
return ((t_cmd *)NULL);
} }
t_redirect *minishell_redir_parse(t_minishell *app, t_cmd_builder *builder) t_redirect *minishell_redir_parse(t_minishell *app, t_cmd_builder *builder)
{ {
} ft_printf("%s() %s\n", __FUNCTION__, builder->tokens->word->word);
t_cmd *subtree;
// subtree = minishell_group_or_smp_parse(app, builder);
return ((t_redirect *)NULL);
}
#define FT_TOKENTYPE_WORD 65
t_cmd *minishell_simple_parse(t_minishell *app, t_cmd_builder *builder) t_cmd *minishell_simple_parse(t_minishell *app, t_cmd_builder *builder)
{ {
ft_printf("%s() %s\n", __FUNCTION__, builder->tokens->word->word);
t_cmd *subtree;
t_worddesc *old_worddesc;
subtree = minishell_redir_parse(app, builder);
// a virer
builder->tokens->word->token_type = FT_TOKENTYPE_WORD;
if (builder->tokens->word->token_type != FT_TOKENTYPE_WORD)
return (parse_error(app, builder->tokens->word), NULL);
old_worddesc = wordlist_pop(builder->tokens);
subtree = minishell_redir_parse(app, builder);
return (subtree);
} }
t_cmd *minishell_opt_pipeline_parse(t_minishell *app, t_cmd_builder *builder) t_cmd *minishell_opt_pipeline_parse(t_minishell *app, t_cmd_builder *builder)
{ {
return ((t_cmd *)NULL);
} }
t_cmd *minishell_group_or_smp_parse(t_minishell *app, t_cmd_builder *builder) t_cmd *minishell_group_or_smp_parse(t_minishell *app, t_cmd_builder *builder)
{ {
ft_printf("%s() %s\n", __FUNCTION__, builder->tokens->word->word);
t_cmd *subtree;
subtree = minishell_simple_parse(app, builder);
return (subtree);
} }
t_cmd *minishell_opt_cmds_parse(t_minishell *app, t_cmd_builder *builder) t_cmd *minishell_opt_cmds_parse(t_minishell *app, t_cmd_builder *builder,
t_connector *connec)
{ {
ft_printf("%s() %s\n", __FUNCTION__, builder->tokens->word->word);
return ((t_cmd *)NULL);
} }
t_cmd *minishell_pipeline_parse(t_minishell *app, t_cmd_builder *builder) t_cmd *minishell_pipeline_parse(t_minishell *app, t_cmd_builder *builder)
{ {
ft_printf("%s() %s\n", __FUNCTION__, builder->tokens->word->word);
t_cmd *subtree;
subtree = minishell_group_or_smp_parse(app, builder);
return (subtree);
} }
/* /*
** Parse list of commands or pipeline. ** Parse list of commands or pipeline.
*/ */
// TODO refactor
t_cmd *minishell_cmds_parse(t_minishell *app, t_cmd_builder *builder) t_cmd *minishell_cmds_parse(t_minishell *app, t_cmd_builder *builder)
{ {
ft_printf("%s() %s\n", __FUNCTION__, builder->tokens->word->word);
t_cmd *subtree; t_cmd *subtree;
t_cmd *opt; t_cmd *opt;
t_cmd *list;
t_connector connec;
subtree = minishell_pipeline_parse(app, builder); subtree = minishell_pipeline_parse(app, builder);
if (!subtree) if (!subtree)
return (NULL); //ft_errno? return (NULL); //ft_errno?
ft_errno(FT_ESUCCESS); opt = minishell_opt_cmds_parse(app, builder, &connec);
opt = minishell_opt_cmds_parse(app, builder); if (!opt && ft_errno_get() != FT_ESUCCESS) //error probably already signaled
if (!opt && ft_errno_get() != FT_ESUCCESS)
return (NULL); return (NULL);
if () if (!opt) //Simple Pipeline of some form
return (subtree); return (subtree);
while (opt)
{
list = ft_calloc(1, sizeof(t_cmd));
if (!list)
{
app->last_return_value = 1;
ft_errno(FT_ENOMEM);
return (ft_perror("minishell_cmds_parse"), NULL);
}
list->type = FT_CONNECTION;
list->connector = connec;
list->first = subtree;
list->second = opt;
subtree = list;
opt = minishell_opt_cmds_parse(app, builder, &connec);
}
if (ft_errno_get() != FT_ESUCCESS)
return (NULL);
return (list);
} }
/* /*
** TODO Recheck error cases to see if allocation error occurred or something ** TODO Recheck error cases to see if allocation error occurred or something
** else and if this is necessary. ** else and if this is necessary.
** TODO Include destruction of builder contents if necessary.
*/ */
t_cmd *minishell_parse(t_minishell *app, char *command_line) t_cmd *minishell_parse(t_minishell *app, char *command_line)
{ {
t_cmd_builder builder; t_cmd_builder builder;
t_cmd *root_cmd; t_cmd *root_cmd;
ft_errno(FT_ESUCCESS);
if (!command_line)
return (NULL);
builder.tokens = minishell_wordsplit(command_line); builder.tokens = minishell_wordsplit(command_line);
if (!builder.tokens) if (!builder.tokens)
return (NULL); return (NULL);
root_cmd = minishell_cmds_parse(app, &builder); root_cmd = minishell_cmds_parse(app, &builder);
if (!root_cmd || builder.tokens) if (!root_cmd)
return (NULL); // destroy builder contents?
if (builder.tokens)
{ {
parse_error(app, builder.tokens->word); parse_error(app, builder.tokens->word);
return (NULL); return (NULL); /////// destroy builder contents?
} }
return (root_cmd); return (root_cmd);
} }

View file

@ -6,7 +6,7 @@
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */ /* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/28 17:49:55 by jguelen #+# #+# */ /* Created: 2025/03/28 17:49:55 by jguelen #+# #+# */
/* Updated: 2025/04/04 18:25:25 by jguelen ### ########.fr */ /* Updated: 2025/04/07 17:59:08 by jguelen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,6 +14,7 @@
# define CMD_PARSING_H # define CMD_PARSING_H
# include "../minishell.h" # include "../minishell.h"
# include "../ft_errno.h"
typedef struct s_cmd_builder typedef struct s_cmd_builder
{ {
@ -32,12 +33,16 @@ t_cmd *minishell_parse(t_minishell *app, char *command_line);
t_cmd *minishell_cmds_parse(t_minishell *app, t_cmd_builder *builder); t_cmd *minishell_cmds_parse(t_minishell *app, t_cmd_builder *builder);
t_cmd *minishell_pipeline_parse(t_minishell *app, t_cmd_builder *builder); t_cmd *minishell_pipeline_parse(t_minishell *app, t_cmd_builder *builder);
t_cmd *minishell_opt_cmds_parse(t_minishell *app, t_cmd_builder *builder); t_cmd *minishell_opt_cmds_parse(t_minishell *app, t_cmd_builder *builder,
t_cmd *minishell_group_or_smp_parse(t_minishell *app, t_cmd_builder *builder); t_connector *connec);
t_cmd *minishell_opt_pipeline_parse(t_minishell *app, t_cmd_builder *builder); t_cmd *minishell_group_or_smp_parse(t_minishell *app,
t_cmd_builder *builder);
t_cmd *minishell_opt_pipeline_parse(t_minishell *app,
t_cmd_builder *builder);
t_cmd *minishell_simple_parse(t_minishell *app, t_cmd_builder *builder); t_cmd *minishell_simple_parse(t_minishell *app, t_cmd_builder *builder);
t_cmd *minishell_redir_parse(t_minishell *app, t_cmd_builder *builder); t_redirect *minishell_redir_parse(t_minishell *app, t_cmd_builder *builder);
t_cmd *minishell_simple_lst_parse(t_minishell *app, t_cmd_builder *builder); t_cmd *minishell_simple_lst_parse(t_minishell *app,
t_cmd_builder *builder);
t_connector which_connector_type(t_worddesc *token); t_connector which_connector_type(t_worddesc *token);

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* cmdgroup.c :+: :+: :+: */ /* cmdgroup.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/11 15:18:02 by khais #+# #+# */ /* Created: 2025/03/11 15:18:02 by khais #+# #+# */
/* Updated: 2025/03/28 15:01:55 by khais ### ########.fr */ /* Updated: 2025/04/07 17:36:19 by jguelen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,7 +16,6 @@
#include "libft.h" #include "libft.h"
#include "paren.h" #include "paren.h"
#include "../../treedrawing.h" #include "../../treedrawing.h"
#include "libft/libft.h"
/* /*
** Recursively parse a cmdgroup ** Recursively parse a cmdgroup

View file

@ -3,15 +3,14 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* pipeline_parse.c :+: :+: :+: */ /* pipeline_parse.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/24 14:36:43 by khais #+# #+# */ /* Created: 2025/02/24 14:36:43 by khais #+# #+# */
/* Updated: 2025/03/04 13:19:46 by khais ### ########.fr */ /* Updated: 2025/04/07 17:39:10 by jguelen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "pipeline_parse.h" #include "pipeline_parse.h"
#include "ft_printf.h"
#include "pipeline.h" #include "pipeline.h"
#include "pipeline_parse_baseops.h" #include "pipeline_parse_baseops.h"
#include "../../ft_errno.h" #include "../../ft_errno.h"

View file

@ -3,16 +3,15 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* worddesc.c :+: :+: :+: */ /* worddesc.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/13 17:20:36 by khais #+# #+# */ /* Created: 2025/02/13 17:20:36 by khais #+# #+# */
/* Updated: 2025/03/27 13:58:41 by khais ### ########.fr */ /* Updated: 2025/04/07 17:40:03 by jguelen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "worddesc.h" #include "worddesc.h"
#include "libft.h" #include "libft.h"
#include "libft/libft.h"
#include <stdlib.h> #include <stdlib.h>
#include "../../treedrawing.h" #include "../../treedrawing.h"

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* worddesc.h :+: :+: :+: */ /* worddesc.h :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/27 13:57:44 by khais #+# #+# */ /* Created: 2025/03/27 13:57:44 by khais #+# #+# */
/* Updated: 2025/03/27 13:57:49 by khais ### ########.fr */ /* Updated: 2025/04/07 18:40:53 by jguelen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -40,7 +40,7 @@ typedef struct s_worddesc
char *word; char *word;
/* /*
** flags for this word ** flags for this word
** **s_worddesc
** See above for flag definitions ** See above for flag definitions
*/ */
char flags; char flags;
@ -48,7 +48,7 @@ typedef struct s_worddesc
** a character mask for word to designate the status ** a character mask for word to designate the status
** of its characters and wether or not they are subject to modifications ** of its characters and wether or not they are subject to modifications
** **
** Possible flags are ''', '"', '$', ' ' ** Possible flags are ''', '"', '$', '&', ' '
** **
** ' ' the default, no flag, no meaning ** ' ' the default, no flag, no meaning
** ''' corresponding character is single-quoted ** ''' corresponding character is single-quoted
@ -56,6 +56,7 @@ typedef struct s_worddesc
** '$' corresponding character is a result of $var expansion ** '$' corresponding character is a result of $var expansion
*/ */
char *marker; char *marker;
int token_type;
} t_worddesc; } t_worddesc;
t_worddesc *worddesc_create(char *word, char flags, char *marker); t_worddesc *worddesc_create(char *word, char flags, char *marker);

View file

@ -6,12 +6,11 @@
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */ /* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/13 17:07:01 by khais #+# #+# */ /* Created: 2025/02/13 17:07:01 by khais #+# #+# */
/* Updated: 2025/03/21 10:43:41 by jguelen ### ########.fr */ /* Updated: 2025/04/07 17:40:30 by jguelen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "wordlist.h" #include "wordlist.h"
#include "ft_printf.h"
#include "libft.h" #include "libft.h"
#include "unistd.h" #include "unistd.h"
#include <stdlib.h> #include <stdlib.h>