syntax analysis with simplified shell grammar

+ TODO : bug to fix in lexer.c
This commit is contained in:
LuckyLaszlo
2021-10-24 19:58:19 +02:00
parent 906074d2cb
commit 815cedb8ca
11 changed files with 400 additions and 18 deletions

101
srcs/parsing/parsing.c Normal file
View File

@@ -0,0 +1,101 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parsing.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/24 10:52:40 by lperrey #+# #+# */
/* Updated: 2021/10/24 19:19:08 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
// A quoi bon un arbre binaire ? Je ne vois plus l'utilité.
/* typedef struct s_binary_tree
{
char *content;
struct s_binary_tree *sub;
struct s_binary_tree *sibling;
enum e_token_id id;
} t_binary_tree; */
t_cmd **parsing(t_token *token_list)
{
t_cmd **cmd_arr;
size_t cmd_nbr;
(void)cmd_arr;
(void)cmd_nbr;
/* t_binary_tree *syntax_tree;
syntax_tree = ft_calloc(1, sizeof *syntax_tree);
if (!syntax_tree)
return (0); //WIP ERROR */
if (!valid_syntax(token_list))
return (NULL);
// Pipes count (determine cmd_nbr)
// Struct CMD alloc/fill
// 2.9.1 - 2) Expansion
// 2.9.1 - 3) Redirection
//return (cmd_arr);
return ((t_cmd **)1); //temp test
}
/* -------------------------------------------------------
The grammar symbols
------------------------------------------------------- */
/*
%token WORD
%token LESS // '<'
%token GREAT // '>'
%token DLESS // '<<'
%token DGREAT // '>>'
%token PIPE // '|'
*/
/* -------------------------------------------------------
The Simplified Grammar
------------------------------------------------------- */
/*
%start program
%%
pipeline : command
| pipe_sequence '|' command
;
command : cmd_prefix cmd_name cmd_suffix
| cmd_prefix cmd_name
| cmd_name cmd_suffix
| cmd_name
;
cmd_name : WORD // Apply rule 7a
;
cmd_prefix : io_redirect
| cmd_prefix io_redirect
;
cmd_suffix : io_redirect
| cmd_suffix io_redirect
| WORD
| cmd_suffix WORD
;
io_redirect : io_file
| io_here
;
io_file : '<' filename
| '>' filename
| DGREAT filename
;
filename : WORD // Apply rule 2
;
io_here : DLESS here_end
;
here_end : WORD // Apply rule 3
;
*/

View File

@@ -0,0 +1,95 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* valid_command.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/24 18:52:05 by lperrey #+# #+# */
/* Updated: 2021/10/24 19:21:28 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int valid_io_redirect(t_token **token_list);
static int valid_command_rule1(t_token **token_list);
static int valid_command_rule2(t_token **token_list);
static int valid_command_rule3(t_token **token_list);
static int valid_command_rule4(t_token **token_list);
int valid_command(t_token **token_list)
{
t_token *cmd_start;
cmd_start = *token_list;
if (valid_command_rule1(token_list))
return (1);
*token_list = cmd_start;
if (valid_command_rule2(token_list))
return (1);
*token_list = cmd_start;
if (valid_command_rule3(token_list))
return (1);
*token_list = cmd_start;
if (valid_command_rule4(token_list))
return (1);
return (0);
}
// cmd_prefix cmd_name cmd_suffix
static int valid_command_rule1(t_token **token_list)
{
while (valid_io_redirect(token_list))
{
if (valid_token(token_list, T_WORD))
{
while (valid_token(token_list, T_WORD)
|| valid_io_redirect(token_list))
{
if (valid_command_separator(*token_list))
return (1);
}
}
}
return (0);
}
// cmd_prefix cmd_name
static int valid_command_rule2(t_token **token_list)
{
while (valid_io_redirect(token_list))
{
if (valid_token(token_list, T_WORD))
{
if (valid_command_separator(*token_list))
return (1);
}
}
return (0);
}
// cmd_name cmd_suffix
static int valid_command_rule3(t_token **token_list)
{
if (valid_token(token_list, T_WORD))
{
while (valid_token(token_list, T_WORD) || valid_io_redirect(token_list))
{
if (valid_command_separator(*token_list))
return (1);
}
}
return (0);
}
// cmd_name
static int valid_command_rule4(t_token **token_list)
{
if (valid_token(token_list, T_WORD))
{
if (valid_command_separator(*token_list))
return (1);
}
return (0);
}

View File

@@ -0,0 +1,44 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* valid_io_redirect.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/24 18:52:42 by lperrey #+# #+# */
/* Updated: 2021/10/24 19:21:35 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static int valid_io_file(t_token **token_list);
static int valid_io_here(t_token **token_list);
int valid_io_redirect(t_token **token_list)
{
if (valid_io_file(token_list) || valid_io_here(token_list))
return (1);
return (0);
}
static int valid_io_file(t_token **token_list)
{
if (valid_token(token_list, '<') || valid_token(token_list, '>')
|| valid_token(token_list, T_DGREAT))
{
if (valid_token(token_list, T_WORD))
return (1);
}
return (0);
}
static int valid_io_here(t_token **token_list)
{
if (valid_token(token_list, T_DLESS))
{
if (valid_token(token_list, T_WORD))
return (1);
}
return (0);
}

View File

@@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* valid_pipeline.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/24 18:51:35 by lperrey #+# #+# */
/* Updated: 2021/10/24 19:35:16 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int valid_command(t_token **token_list);
int valid_pipeline(t_token **token_list)
{
while (valid_command(token_list))
{
if (*token_list == NULL)
return (1);
else if ((*token_list)->id != '|' || (*token_list)->next == NULL)
return (0);
valid_token(token_list, '|');
}
return (0);
}

View File

@@ -0,0 +1,44 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* valid_syntax.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/24 13:01:10 by lperrey #+# #+# */
/* Updated: 2021/10/24 19:40:23 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int valid_pipeline(t_token **token_list);
int valid_syntax(t_token *token_list)
{
if (valid_pipeline(&token_list))
return (1);
else
{ // WIP ERROR
ft_putstr_fd("minishell: syntax error near unexpected token \'", 2);
ft_putstr_fd(token_list->content, 2);
ft_putstr_fd("\'\n", 2);
}
return (0);
}
int valid_token(t_token **token_list, enum e_token_id token_id)
{
if (*token_list != NULL && (*token_list)->id == token_id)
{
*token_list = (*token_list)->next;
return (1);
}
return (0);
}
int valid_command_separator(const t_token *token_list)
{
if (token_list == NULL || token_list->id == '|')
return (1);
return (0);
}