correction syntax + mise a la norme parser

This commit is contained in:
Antoine Riard 2017-03-17 18:43:21 +01:00
parent 0967785551
commit 7f236af741
19 changed files with 278 additions and 161 deletions

View file

@ -291,6 +291,7 @@ parser/pop_stack.c\
parser/produce_sym.c\
parser/push_stack.c\
parser/read_stack.c\
parser/redir_init.c\
parser/stack_init.c\
parser/tree_wrapper.c

78
42sh/STBUG Normal file
View file

@ -0,0 +1,78 @@
95003 main.c 111start of shell
95003 main.c 114JOBC is ON
95003 produce_sym.c 137produce stack : LINEBREAK && token : TK_NEWLINE
95003 ft_parse.c 43 new sym NEWLINE_LIST
95003 eval_sym.c 1246eval head LINEBREAK && sym NEWLINE_LIST
95003 eval_sym.c 1246eval head TERMINUS && sym PROGRAM
95003 build_tree.c 91 build TK_NEWLINE
95003 lexer_default.c 28 no special state, l
95003 produce_sym.c 137produce stack : LINEBREAK && token : TK_WORD
95003 ft_parse.c 43 new sym CMD_NAME
95003 eval_sym.c 1246eval head LINEBREAK && sym CMD_NAME
95003 eval_sym.c 1246eval head LINEBREAK && sym CMD_SUPERIOR
95003 build_tree.c 91 build TK_WORD
95003 add_cmd.c 97 type is CMD
95003 add_cmd.c 98 data is ls
95003 produce_sym.c 137produce stack : CMD_SUPERIOR && token : TK_NEWLINE
95003 ft_parse.c 43 new sym LINEBREAK
95003 eval_sym.c 1246eval head CMD_SUPERIOR && sym LINEBREAK
95003 eval_sym.c 1246eval head CMD_SUPERIOR && sym SEPARATOR_OP
95003 eval_sym.c 1246eval head CMD_SUPERIOR && sym SEPARATOR
95003 eval_sym.c 1246eval head LINEBREAK && sym END_COMMAND
95003 eval_sym.c 1246eval head LINEBREAK && sym PIPE_SEQUENCE
95003 eval_sym.c 1246eval head LINEBREAK && sym PIPELINE
95003 eval_sym.c 1246eval head LINEBREAK && sym AND_OR
95003 eval_sym.c 1246eval head LINEBREAK && sym LIST
95003 eval_sym.c 1246eval head LINEBREAK && sym COMPLETE_COMMAND
95003 eval_sym.c 1246eval head LINEBREAK && sym COMPLETE_COMMANDS
95003 build_tree.c 91 build TK_NEWLINE
95003 produce_sym.c 137produce stack : COMPLETE_COMMANDS && token : LINEBREAK
95003 ft_parse.c 43 new sym LINEBREAK
95003 eval_sym.c 1246eval head COMPLETE_COMMANDS && sym LINEBREAK
95003 eval_sym.c 1246eval head TERMINUS && sym PROGRAM
95003 is_builtin.c 40 in builtin
95003 process_launch.c 50 p->type=2
95003 plaunch_file.c 25 plaunch_file
95003 process_launch.c 62 launcher forked!
95003 process_setgroup.c 26 setpgid(95012, 0)
95012 plaunch_file.c 31 fork! [ls]
95012 process_setgroup.c 26 setpgid(0, 0)
95012 process_setgroup.c 31 tcsetpgrp[0]
95012 process_setgroup.c 33 after tcsetpgrp
95003 exec_leaf.c 50 check
95003 lexer_default.c 28 no special state, b
95003 produce_sym.c 137produce stack : LINEBREAK && token : TK_WORD
95003 ft_parse.c 43 new sym CMD_NAME
95003 eval_sym.c 1246eval head LINEBREAK && sym CMD_NAME
95003 eval_sym.c 1246eval head LINEBREAK && sym CMD_SUPERIOR
95003 build_tree.c 91 build TK_WORD
95003 add_cmd.c 97 type is CMD
95003 add_cmd.c 98 data is bash
95003 produce_sym.c 137produce stack : CMD_SUPERIOR && token : TK_NEWLINE
95003 ft_parse.c 43 new sym LINEBREAK
95003 eval_sym.c 1246eval head CMD_SUPERIOR && sym LINEBREAK
95003 eval_sym.c 1246eval head CMD_SUPERIOR && sym SEPARATOR_OP
95003 eval_sym.c 1246eval head CMD_SUPERIOR && sym SEPARATOR
95003 eval_sym.c 1246eval head LINEBREAK && sym END_COMMAND
95003 eval_sym.c 1246eval head LINEBREAK && sym PIPE_SEQUENCE
95003 eval_sym.c 1246eval head LINEBREAK && sym PIPELINE
95003 eval_sym.c 1246eval head LINEBREAK && sym AND_OR
95003 eval_sym.c 1246eval head LINEBREAK && sym LIST
95003 eval_sym.c 1246eval head LINEBREAK && sym COMPLETE_COMMAND
95003 eval_sym.c 1246eval head LINEBREAK && sym COMPLETE_COMMANDS
95003 build_tree.c 91 build TK_NEWLINE
95003 produce_sym.c 137produce stack : COMPLETE_COMMANDS && token : LINEBREAK
95003 ft_parse.c 43 new sym LINEBREAK
95003 eval_sym.c 1246eval head COMPLETE_COMMANDS && sym LINEBREAK
95003 eval_sym.c 1246eval head TERMINUS && sym PROGRAM
95003 is_builtin.c 40 in builtin
95003 process_launch.c 50 p->type=2
95003 plaunch_file.c 25 plaunch_file
95003 process_launch.c 62 launcher forked!
95003 process_setgroup.c 26 setpgid(95180, 0)
95180 plaunch_file.c 31 fork! [bash]
95180 process_setgroup.c 26 setpgid(0, 0)
95180 process_setgroup.c 31 tcsetpgrp[0]
95180 process_setgroup.c 33 after tcsetpgrp
95003 exec_leaf.c 50 check
95003 list_toolz.c 21 free hist

View file

@ -60,14 +60,10 @@ struct s_errormatch
char *error;
};
/* extern t_aggrematch g_aggrematch[]; */
/* extern t_prodmatch g_prodmatch[]; */
/* extern t_stackmatch g_stackmatch[]; */
void parser_init(t_parser *parser);
void parser_destroy(t_parser *parser);
int stack_init(t_parser *parser);
int redir_init(t_type type, t_redir *redir);
int ft_parse(t_btree **ast, t_list **token, t_parser *parser);
int produce_sym(t_list **stack, t_sym *new_sym, t_list **lst);

View file

@ -6,7 +6,7 @@
/* By: alao <alao@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/10 09:04:42 by alao #+# #+# */
/* Updated: 2017/03/16 10:19:54 by gwojda ### ########.fr */
/* Updated: 2017/03/17 18:12:31 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,13 +6,12 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/26 00:07:05 by ariard #+# #+# */
/* Updated: 2017/03/16 14:41:00 by jhalford ### ########.fr */
/* Updated: 2017/03/17 17:21:23 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
t_rvwords g_rvwords[] =
{
{"while", TK_WHILE},
@ -30,8 +29,7 @@ t_rvwords g_rvwords[] =
{"null", 0},
};
static int recognization_rvwords(t_token *pv_tk)
static int recognization_rvwords(t_token *pv_tk, t_token *at_tk)
{
if (!pv_tk || (pv_tk->type == TK_NEWLINE || pv_tk->type == TK_AMP
|| pv_tk->type == TK_SEMI || pv_tk->type == TK_PIPE
@ -39,18 +37,35 @@ static int recognization_rvwords(t_token *pv_tk)
|| pv_tk->type == TK_DO || pv_tk->type == TK_IF
|| pv_tk->type == TK_FI || pv_tk->type == TK_THEN
|| pv_tk->type == TK_ELIF || pv_tk->type == TK_ELSE
|| pv_tk->type == TK_DSEMI))
|| pv_tk->type == TK_DSEMI) || (pv_tk->type == TK_PAREN_CLOSE
&& at_tk->type == TK_PAREN_OPEN))
return (1);
return (0);
}
static int match_words(t_token *token)
{
int i;
if (token->type == TK_WORD)
{
i = 0;
while (g_rvwords[i].type)
{
if (ft_strcmp(token->data, g_rvwords[i].word) == 0)
token->type = g_rvwords[i].type;
i++;
}
}
return (0);
}
int get_reserved_words(t_list **alst)
{
t_token *token;
t_token *pv_tk;
t_token *ante_token;
t_list *temp;
int i;
temp = *alst;
pv_tk = NULL;
@ -58,26 +73,15 @@ int get_reserved_words(t_list **alst)
while (temp)
{
token = temp->content;
//no more comp &
if (recognization_rvwords(pv_tk))
{
if (token->type == TK_WORD)
{
i = 0;
while (g_rvwords[i].type)
{
if (ft_strcmp(token->data, g_rvwords[i].word) == 0)
token->type = g_rvwords[i].type;
i++;
}
}
}
if (ante_token && (ante_token->type == TK_CASE || ante_token->type == TK_FOR)
if (recognization_rvwords(pv_tk, ante_token))
match_words(token);
if (ante_token && (ante_token->type == TK_CASE
|| ante_token->type == TK_FOR)
&& ft_strncmp(token->data, "in", 2) == 0)
token->type = TK_IN;
if (pv_tk && pv_tk->type == TK_FOR && token->type == TK_WORD)
token->type = TK_NAME;
ante_token = pv_tk;
ante_token = pv_tk;
pv_tk = token;
temp = temp->next;
}

View file

@ -90,6 +90,7 @@ static int handle_instruction()
return (ret);
if (do_lexer_routine(stream) > 0)
continue ;
token_print(data->token);
if (do_parser_routine() > 0)
break ;
}

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/15 20:49:15 by ariard #+# #+# */
/* Updated: 2017/03/15 16:46:16 by ariard ### ########.fr */
/* Updated: 2017/03/17 17:29:43 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -44,7 +44,7 @@ int superflous_token(t_btree **ast, t_list **lst)
return (0);
}
static int no_del_token(t_btree **ast, t_list **lst)
static int emblematic_token(t_btree **ast, t_list **lst)
{
t_astnode *node;
t_token *token;
@ -65,42 +65,43 @@ static int no_del_token(t_btree **ast, t_list **lst)
return (0);
}
int add_cmd(t_btree **ast, t_list **lst)
static int add_data_node(t_btree **ast, t_list **lst)
{
t_token *token;
t_astnode *node;
int i;
i = -1;
while (++i < 14)
{
if (g_distrostree[i].test(ast, lst) == 1)
{
DG("add to %d", i);
return (g_distrostree[i].add(ast, lst));
}
}
if (!*ast)
gen_node(ast);
else if (no_del_token(ast, lst))
return (add_cmd(&(*ast)->right, lst));
token = (*lst)->content;
node = (*ast)->item;
if (token->type == TK_IF)
add_if(ast, lst);
else if (token->type != TK_WORD && token->type != TK_NAME)
node->type = token->type;
else if (node->type != TK_CASE && node->type != TK_PAREN_OPEN && node->type != TK_FOR)
else if (node->type != TK_CASE && node->type != TK_PAREN_OPEN
&& node->type != TK_FOR)
node->type = CMD;
if (token->type == TK_WORD || token->type == TK_ASSIGNMENT_WORD || token->type == TK_NAME)
{
DG("type is %s", read_state(node->type));
DG("data is %s", token->data);
if (token->type == TK_WORD || token->type == TK_ASSIGNMENT_WORD
|| token->type == TK_NAME)
ft_ld_pushback(&node->data.cmd.token,
gen_tab(token->data, token->esc, token->esc2, 1));
}
if ((token->type == TK_WORD || token->type == TK_NAME)
if ((token->type == TK_WORD || token->type == TK_NAME)
&& (node->type == TK_CASE || node->type == TK_PAREN_OPEN))
node->pattern = 1;
return (0);
}
int add_cmd(t_btree **ast, t_list **lst)
{
int i;
i = -1;
while (++i < 14)
{
if (g_distrostree[i].test(ast, lst) == 1)
return (g_distrostree[i].add(ast, lst));
}
if (!*ast)
gen_node(ast);
else if (emblematic_token(ast, lst))
return (add_cmd(&(*ast)->right, lst));
return (add_data_node(ast, lst));
}

View file

@ -6,13 +6,13 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/24 23:43:07 by ariard #+# #+# */
/* Updated: 2017/03/15 20:07:38 by ariard ### ########.fr */
/* Updated: 2017/03/17 17:42:22 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int isfunc_name(t_btree **ast, t_list **lst)
int isfunc_name(t_btree **ast, t_list **lst)
{
t_astnode *node;
t_token *token;
@ -33,7 +33,7 @@ int isfunc_name(t_btree **ast, t_list **lst)
return (0);
}
int isfunc(t_btree **ast, t_list **lst)
int isfunc(t_btree **ast, t_list **lst)
{
t_astnode *node;
@ -52,7 +52,7 @@ int isfunc(t_btree **ast, t_list **lst)
return (0);
}
int add_func_cmd(t_btree **ast, t_list **lst)
int add_func_cmd(t_btree **ast, t_list **lst)
{
t_astnode *node;
t_token *token;
@ -86,7 +86,7 @@ t_list *is_already_func(t_btree **new)
t_btree **ast;
char **new_name;
char **old_name;
tmp = data_singleton()->lst_func;
new_name = token_to_argv(((t_astnode *)(*new)->item)->data.cmd.token, 1);
while (tmp)
@ -94,7 +94,8 @@ t_list *is_already_func(t_btree **new)
ast = tmp->content;
if (!*ast)
return (NULL);
old_name = token_to_argv(((t_astnode *)(*ast)->item)->data.cmd.token, 1);
old_name = token_to_argv(((t_astnode *)
(*ast)->item)->data.cmd.token, 1);
if (!new_name || !new_name[0] || !old_name || !old_name[0])
return (NULL);
if (!ft_strcmp(new_name[0], old_name[0]))
@ -104,7 +105,7 @@ t_list *is_already_func(t_btree **new)
return (NULL);
}
int add_one_func(t_btree **ast, t_list **lst)
int add_one_func(t_btree **ast, t_list **lst)
{
t_btree *func_ast;
t_list *old_func;
@ -112,7 +113,8 @@ int add_one_func(t_btree **ast, t_list **lst)
(void)lst;
func_ast = btree_map(*ast, node_copy);
if ((old_func = is_already_func(&func_ast)))
ft_lst_delif(&data_singleton()->lst_func, old_func->content, &ft_addrcmp, &ast_free);
ft_lst_delif(&data_singleton()->lst_func,
old_func->content, &ft_addrcmp, &ast_free);
ft_lsteadd(&data_singleton()->lst_func, ft_lstnew(&func_ast, sizeof(*ast)));
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/17 22:17:14 by ariard #+# #+# */
/* Updated: 2017/03/15 01:59:52 by ariard ### ########.fr */
/* Updated: 2017/03/17 17:45:41 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -27,7 +27,7 @@ int isloop(t_btree **ast, t_list **lst)
return (1);
if (node->type == TK_FOR && (token->type == TK_WORD
|| token->type == TK_NAME) && node->pattern == 0)
return (0);
return (0);
if ((node->type == TK_NEWLINE || node->type == TK_SEMI
|| node->type == TK_AMP || node->type == TK_PIPE)
&& isloop(&(*ast)->right, lst) == 1)

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/17 16:39:05 by ariard #+# #+# */
/* Updated: 2017/03/16 18:34:10 by ariard ### ########.fr */
/* Updated: 2017/03/17 17:54:49 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -98,16 +98,6 @@ static int add_redir_type_number(t_btree **ast, t_list **lst)
return (0);
}
int redir_init(t_type type, t_redir *redir)
{
redir->n = (type == TK_LESS || type == TK_DLESS
|| type == TK_LESSAND) ? STDIN : STDOUT;
redir->type = type;
redir->heredoc_data = NULL;
redir->word = NULL;
return (0);
}
int add_redir_type(t_btree **ast, t_list **lst)
{
t_astnode *node;

View file

@ -6,16 +6,12 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/11 15:58:38 by ariard #+# #+# */
/* Updated: 2017/03/14 22:02:07 by ariard ### ########.fr */
/* Updated: 2017/03/17 18:23:50 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
/*
* In case of grammar error, manange it
*/
t_aggrematch g_aggrematch[] =
{
{TK_WORD, CMD_SUFFIX, CMD_SUFFIX, 0},
@ -23,9 +19,9 @@ t_aggrematch g_aggrematch[] =
{TK_WORD, TK_IN, FOR_WORDLIST, TK_IN},
{TK_WORD, FOR_WORDLIST, FOR_WORDLIST, FOR_WORDLIST},
{TK_SEMI, FOR_WORDLIST, SEQUENTIAL_SEP, 0},
{TK_DSEMI, CMD_SUPERIOR, PIPE_SEQUENCE, CMD_SUPERIOR},
{TK_DSEMI, PIPE_SEMI_SEQUENCE, PIPE_SEQUENCE, PIPE_SEMI_SEQUENCE},
{TK_DSEMI, PIPE_CLOSE_SEQUENCE, PIPE_SEQUENCE, PIPE_CLOSE_SEQUENCE},
{TK_DSEMI, CMD_SUPERIOR, CLOSE_LIST, TK_PAREN_CLOSE},
{TK_DSEMI, PIPE_SEMI_SEQUENCE, CLOSE_LIST, TK_PAREN_CLOSE},
{TK_DSEMI, PIPE_CLOSE_SEQUENCE, CLOSE_LIST, TK_PAREN_CLOSE},
{TK_PAREN_OPEN, TK_IN, PATTERN_CASE, 0},
{TK_PAREN_OPEN, CASE_LIST_NS, PATTERN_CASE, 0},
{TK_PAREN_OPEN, CMD_SUPERIOR, OPEN_FUNC, 0},
@ -42,7 +38,6 @@ t_aggrematch g_aggrematch[] =
{TK_DONE, COMPOUND_LIST, DO_GROUP, TK_DO},
{TK_ESAC, TK_IN, CASE_CLAUSE, TK_CASE},
{TK_ESAC, CASE_LIST_NS, CASE_CLAUSE, TK_CASE},
{TK_RBRACE, COMPOUND_LIST, BRACE_GROUP, TK_LBRACE},
{TK_PAREN_CLOSE, COMPOUND_LIST, SUBSHELL, TK_PAREN_OPEN},
{TK_PAREN_CLOSE, CMD_SUPERIOR, SUBSHELL, TK_PAREN_OPEN},
{TK_PAREN_CLOSE, PIPE_SEMI_SEQUENCE, SUBSHELL, TK_PAREN_OPEN},
@ -56,11 +51,11 @@ t_aggrematch g_aggrematch[] =
{TK_RBRACE, COMPOUND_LIST, BRACE_CLAUSE, TK_LBRACE},
{TK_RBRACE, CMD_SUPERIOR, BRACE_CLAUSE, TK_LBRACE},
{TK_AND_IF, CMD_SUPERIOR, AND_OR_MINOR, CMD_SUPERIOR},
{TK_AND_IF, PIPE_CLOSE_SEQUENCE, AND_OR_MINOR, PIPE_CLOSE_SEQUENCE},
{TK_AND_IF, PIPE_CLOSE_SEQUENCE, AND_OR_MINOR, PIPE_CLOSE_SEQUENCE},
{TK_AND_IF, PIPE_SEMI_SEQUENCE, AND_OR_MINOR, PIPE_SEMI_SEQUENCE},
{TK_OR_IF, PIPE_SEMI_SEQUENCE, AND_OR_MINOR, PIPE_SEMI_SEQUENCE},
{TK_OR_IF, CMD_SUPERIOR, AND_OR_MINOR, CMD_SUPERIOR},
{TK_OR_IF, PIPE_CLOSE_SEQUENCE, AND_OR_MINOR, PIPE_CLOSE_SEQUENCE},
{TK_OR_IF, PIPE_CLOSE_SEQUENCE, AND_OR_MINOR, PIPE_CLOSE_SEQUENCE},
{SEPARATOR_OP, CMD_SUPERIOR, SEPARATOR, 0},
{SEPARATOR_OP, COMPOUND_LIST, SEPARATOR, 0},
{SEPARATOR_OP, CASE_LIST_NS, SEPARATOR, 0},
@ -100,7 +95,9 @@ t_aggrematch g_aggrematch[] =
{NEWLINE_LIST, NAME, SEQUENTIAL_SEP, 0},
{NEWLINE_LIST, IN, SEQUENTIAL_SEP, 0},
{NEWLINE_LIST, TERM, SEPARATOR, 0},
{NEWLINE_LIST, SEQUENCE, SEQUENCE, SEQUENCE},
{NEWLINE_LIST, COMPOUND_LIST, SEPARATOR, 0},
{NEWLINE_LIST, FUNC_NAME, FUNC_NAME, FUNC_NAME},
{NEWLINE_LIST, CASE_LIST_NS, SEPARATOR, 0},
{NEWLINE_LIST, COMPLETE_CONDITION, COMPLETE_CONDITION, COMPLETE_CONDITION},
{NEWLINE_LIST, CONDITION, CONDITION, CONDITION},
@ -226,7 +223,6 @@ t_aggrematch g_aggrematch[] =
{SUBSHELL, ALL, COMPOUND_COMMAND, 0},
{BRACE_CLAUSE, ALL, COMPOUND_COMMAND, 0},
{COMPOUND_COMMAND, FUNC_NAME, COMMAND, FUNC_NAME},
// {COMPOUND_COMMAND, ALL, COMMAND, 0},
{AND_OR_MINOR, PIPE_SEMI_SEQUENCE, AND_OR_MAJOR, PIPE_SEMI_SEQUENCE},
{AND_OR_MINOR, PIPE_CLOSE_SEQUENCE, AND_OR_MAJOR, PIPE_CLOSE_SEQUENCE},
{AND_OR_MINOR, LINEBREAK, AND_OR_MAJOR, 0},
@ -381,32 +377,35 @@ int aggregate_sym(t_list **stack, t_sym *new_sym, t_parstate *state)
if (!*stack || !*new_sym || !*state)
return (1);
i = 0;
i = -1;
head = (*stack)->content;
// DG("aggregate head %s && sym %s",
// read_state(*head), read_state(*new_sym));
while (g_aggrematch[i].top)
{
while (g_aggrematch[++i].top)
if (*new_sym == g_aggrematch[i].top
&& MATCH_STACK(*head, g_aggrematch[i].under))
{
// DG("MATCH : %s", read_state(g_aggrematch[i].new_sym));
*new_sym = g_aggrematch[i].new_sym;
if (g_aggrematch[i].erase_sym)
{
pop_stack(stack, g_aggrematch[i].erase_sym);
head = (*stack)->content;
// DG("stack after pop: %s", read_state(*head));
}
if (eval_sym(stack, *new_sym))
{
*state = ERROR;
if (eval_sym(stack, *new_sym) && !(*state = ERROR))
return (1);
}
aggregate_sym(stack, new_sym, state);
return (0);
}
i++;
}
return (0);
}
/*
** DG("aggregate head %s && sym %s",
** read_state(*head), read_state(*new_sym));
*/
/*
** DG("MATCH : %s", read_state(g_aggrematch[i].new_sym));
*/
/*
** DG("stack after pop: %s", read_state(*head));
*/

View file

@ -6,12 +6,15 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 20:15:35 by ariard #+# #+# */
/* Updated: 2017/03/16 23:24:01 by ariard ### ########.fr */
/* Updated: 2017/03/17 18:08:11 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
#define SYNTERR_0 "{red}syntax error near unexpected token `%s'{eoc}\n"
#define SYNTERR_1 "{red}syntax error near unexpected EOF{eoc}"
static t_errormatch g_errormatch[] =
{
{TK_NEWLINE, "newline"},
@ -64,18 +67,18 @@ int error_syntax(t_list **lst)
{
if (g_errormatch[i].token == token->type)
{
ft_dprintf(2, "{red}syntax error near unexpected token « %s »{eoc}\n",
g_errormatch[i].error);
ft_dprintf(2, SYNTERR_0, g_errormatch[i].error);
return (0);
}
i++;
}
ft_dprintf(2, "{red}syntax error near unexpected token « %s »\n{eoc}", token->data);
ft_dprintf(2, SYNTERR_0,
token->data);
return (0);
}
int error_eof(void)
{
ft_dprintf(2, "{red}syntax error near unexpected EOF{eoc}");
ft_dprintf(2, SYNTERR_1);
return (1);
}

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/11 16:11:21 by ariard #+# #+# */
/* Updated: 2017/03/16 21:24:10 by ariard ### ########.fr */
/* Updated: 2017/03/17 18:12:41 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -255,6 +255,7 @@ t_stackmatch g_stackmatch[] =
{TK_IF, TK_BANG},
{TK_IF, TK_PAREN_OPEN},
{TK_IF, TK_LBRACE},
{TK_IF, FUNC_NAME},
{TK_IF, COMPLETE_COMMANDS},
{TK_IF, SEPARATOR_OP},
{TK_IF, NEWLINE_LIST},
@ -297,6 +298,7 @@ t_stackmatch g_stackmatch[] =
{TK_CASE, TK_BANG},
{TK_CASE, TK_PAREN_OPEN},
{TK_CASE, TK_LBRACE},
{TK_CASE, FUNC_NAME},
{TK_CASE, COMPLETE_COMMANDS},
{TK_CASE, TK_DO},
{TK_CASE, TK_THEN},
@ -322,6 +324,7 @@ t_stackmatch g_stackmatch[] =
{TK_WHILE, LINEBREAK},
{TK_WHILE, TK_PAREN_OPEN},
{TK_WHILE, TK_LBRACE},
{TK_WHILE, FUNC_NAME},
{TK_WHILE, COMPLETE_COMMANDS},
{TK_WHILE, TK_BANG},
{TK_WHILE, SEPARATOR_OP},
@ -343,6 +346,7 @@ t_stackmatch g_stackmatch[] =
{TK_UNTIL, LINEBREAK},
{TK_UNTIL, TK_PAREN_OPEN},
{TK_UNTIL, TK_LBRACE},
{TK_UNTIL, FUNC_NAME},
{TK_UNTIL, COMPLETE_COMMANDS},
{TK_UNTIL, TK_BANG},
{TK_UNTIL, SEPARATOR_OP},
@ -361,10 +365,33 @@ t_stackmatch g_stackmatch[] =
{TK_UNTIL, TK_PAREN_CLOSE},
{TK_UNTIL, COMPLETE_CONDITION},
{TK_UNTIL, CONDITION},
{FUNC_NAME, LINEBREAK},
{FUNC_NAME, TK_BANG},
{FUNC_NAME, TK_PAREN_OPEN},
{FUNC_NAME, TK_LBRACE},
{FUNC_NAME, FUNC_NAME},
{FUNC_NAME, COMPLETE_COMMANDS},
{FUNC_NAME, SEPARATOR_OP},
{FUNC_NAME, NEWLINE_LIST},
{FUNC_NAME, SEQUENCE},
{FUNC_NAME, TK_WHILE},
{FUNC_NAME, FUNC_NAME},
{FUNC_NAME, TK_UNTIL},
{FUNC_NAME, FUNC_NAME},
{FUNC_NAME, TK_DO},
{FUNC_NAME, TK_ELIF},
{FUNC_NAME, TK_ELSE},
{FUNC_NAME, TK_THEN},
{FUNC_NAME, TK_PAREN_CLOSE},
{FUNC_NAME, COMPLETE_CONDITION},
{FUNC_NAME, CONDITION},
{FUNC_NAME, COMPOUND_LIST},
{FUNC_NAME, CASE_LIST_NS},
{TK_FOR, LINEBREAK},
{TK_FOR, TK_BANG},
{TK_FOR, TK_PAREN_OPEN},
{TK_FOR, TK_LBRACE},
{TK_FOR, FUNC_NAME},
{TK_FOR, COMPLETE_COMMANDS},
{TK_FOR, SEPARATOR_OP},
{TK_FOR, NEWLINE_LIST},
@ -395,7 +422,6 @@ t_stackmatch g_stackmatch[] =
{TK_LBRACE, TK_UNTIL},
{TK_LBRACE, TK_DO},
{TK_LBRACE, TK_PAREN_CLOSE},
{TK_LBRACE, TK_LBRACE},
{TK_LBRACE, TK_IF},
{TK_LBRACE, TK_ELIF},
{TK_LBRACE, TK_THEN},
@ -599,6 +625,7 @@ t_stackmatch g_stackmatch[] =
{NEWLINE_LIST, TK_PAREN_OPEN},
{NEWLINE_LIST, TK_LBRACE},
{NEWLINE_LIST, PROGRAM},
{NEWLINE_LIST, SEQUENCE},
{HERE_END, TK_DLESS},
{HERE_END, TK_DLESS},
{HERE_END, TK_DLESSDASH},
@ -813,14 +840,6 @@ t_stackmatch g_stackmatch[] =
{COMPLETE_CONDITION, COMPLETE_COMMANDS},
{COMPLETE_CONDITION, COMPLETE_CONDITION},
{COMPLETE_CONDITION, SEQUENCE},
{BRACE_GROUP, LINEBREAK},
{BRACE_GROUP, TK_PAREN_OPEN},
{BRACE_GROUP, TK_LBRACE},
{BRACE_GROUP, COMPLETE_COMMANDS},
{BRACE_GROUP, TK_BANG},
{BRACE_GROUP, SEPARATOR_OP},
{BRACE_GROUP, NEWLINE_LIST},
{BRACE_GROUP, SEQUENCE},
{FNAME, LINEBREAK},
{FNAME, TK_PAREN_OPEN},
{FNAME, TK_LBRACE},
@ -833,6 +852,7 @@ t_stackmatch g_stackmatch[] =
{FUNCTION_DEFINITION, LINEBREAK},
{FUNCTION_DEFINITION, TK_PAREN_OPEN},
{FUNCTION_DEFINITION, TK_LBRACE},
{FUNCTION_DEFINITION, FUNC_NAME},
{FUNCTION_DEFINITION, COMPLETE_COMMANDS},
{FUNCTION_DEFINITION, TK_BANG},
{FUNCTION_DEFINITION, SEPARATOR_OP},
@ -841,6 +861,7 @@ t_stackmatch g_stackmatch[] =
{UNTIL_CLAUSE, LINEBREAK},
{UNTIL_CLAUSE, TK_PAREN_OPEN},
{UNTIL_CLAUSE, TK_LBRACE},
{UNTIL_CLAUSE, FUNC_NAME},
{UNTIL_CLAUSE, COMPLETE_COMMANDS},
{UNTIL_CLAUSE, TK_BANG},
{UNTIL_CLAUSE, SEPARATOR_OP},
@ -885,6 +906,7 @@ t_stackmatch g_stackmatch[] =
{IF_CLAUSE, TK_BANG},
{IF_CLAUSE, TK_PAREN_OPEN},
{IF_CLAUSE, TK_LBRACE},
{IF_CLAUSE, FUNC_NAME},
{IF_CLAUSE, COMPLETE_COMMANDS},
{IF_CLAUSE, SEPARATOR_OP},
{IF_CLAUSE, NEWLINE_LIST},
@ -907,6 +929,7 @@ t_stackmatch g_stackmatch[] =
{BRACE_CLAUSE, LINEBREAK},
{BRACE_CLAUSE, TK_PAREN_OPEN},
{BRACE_CLAUSE, TK_LBRACE},
{BRACE_CLAUSE, FUNC_NAME},
{BRACE_CLAUSE, COMPLETE_COMMANDS},
{BRACE_CLAUSE, TK_BANG},
{BRACE_CLAUSE, SEPARATOR_OP},
@ -936,6 +959,7 @@ t_stackmatch g_stackmatch[] =
{CASE_CLAUSE, TK_BANG},
{CASE_CLAUSE, TK_PAREN_OPEN},
{CASE_CLAUSE, TK_LBRACE},
{CASE_CLAUSE, FUNC_NAME},
{CASE_CLAUSE, COMPLETE_COMMANDS},
{CASE_CLAUSE, SEPARATOR_OP},
{CASE_CLAUSE, NEWLINE_LIST},
@ -963,6 +987,7 @@ t_stackmatch g_stackmatch[] =
{FOR_CLAUSE, TK_BANG},
{FOR_CLAUSE, TK_PAREN_OPEN},
{FOR_CLAUSE, TK_LBRACE},
{FOR_CLAUSE, FUNC_NAME},
{FOR_CLAUSE, COMPLETE_COMMANDS},
{FOR_CLAUSE, SEPARATOR_OP},
{FOR_CLAUSE, NEWLINE_LIST},
@ -1004,6 +1029,7 @@ t_stackmatch g_stackmatch[] =
{SUBSHELL, NEWLINE_LIST},
{SUBSHELL, SEQUENCE},
{SUBSHELL, TK_LBRACE},
{SUBSHELL, TK_PAREN_OPEN},
{SUBSHELL, SEPARATOR_OP},
{SUBSHELL, NEWLINE_LIST},
{SUBSHELL, TK_DO},
@ -1243,7 +1269,6 @@ int eval_sym(t_list **stack, t_sym new_sym)
if (!*stack)
return (1);
head = (*stack)->content;
DG("eval head %s && sym %s", read_state(*head), read_state(new_sym));
i = 0;
while (g_stackmatch[i].top)
{
@ -1253,3 +1278,7 @@ int eval_sym(t_list **stack, t_sym new_sym)
}
return (1);
}
/*
** DG("eval head %s && sym %s", read_state(*head), read_state(new_sym));
*/

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/11 16:17:38 by ariard #+# #+# */
/* Updated: 2017/03/16 22:11:41 by ariard ### ########.fr */
/* Updated: 2017/03/17 18:00:34 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -40,22 +40,16 @@ int ft_parse(t_btree **ast, t_list **token, t_parser *parser)
while (*token)
{
produce_sym(&parser->stack, parser->new_sym, token);
DG("new sym %s", read_state(*parser->new_sym));
if (eval_sym(&parser->stack, *parser->new_sym))
return ((parser->state = ERROR));
else
{
if (aggregate_sym(&parser->stack, parser->new_sym, &parser->state))
return (0);
push_stack(&parser->stack, *parser->new_sym);
}
// ft_read_stack(parser->stack);
if (aggregate_sym(&parser->stack, parser->new_sym, &parser->state))
return (0);
push_stack(&parser->stack, *parser->new_sym);
if (*(head = (parser->stack)->content) == PROGRAM)
parser->state = SUCCESS;
else
parser->state = UNDEFINED;
build_tree(ast, token);
/* btree_print(STDBUG, *ast, &ft_putast); */
if ((end_instruction(&parser->stack) && !(*token)->next))
insert_linebreak(token);
else

View file

@ -6,41 +6,42 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/08 16:21:05 by ariard #+# #+# */
/* Updated: 2017/03/16 17:51:54 by ariard ### ########.fr */
/* Updated: 2017/03/17 18:42:58 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int pop_heredoc(t_list **lst)
static char *join_heredoc_data(char *old_data, char *token_data)
{
char *del;
char *new;
del = old_data;
new = ft_str3join(old_data, token_data, "\n");
ft_strdel(&del);
return (new);
}
int pop_heredoc(t_list **lst)
{
t_token *token;
t_list **head;
t_redir *redir;
char *tmp;
token = (*lst)->content;
if (token->type == HEREDOCDATA && data_singleton()->parser.heredoc_queue != NULL)
if (token->type == HEREDOCDATA
&& data_singleton()->parser.heredoc_queue != NULL)
{
head = &data_singleton()->parser.heredoc_queue;
if (!(head = &data_singleton()->parser.heredoc_queue))
return (0);
redir = *(t_redir**)(*head)->content;
if (head && token)
{
/* DG("comparing w/ input [%s], head @ %p", token->data, *head); */
/* DG("redir1 @ %p word=[%s]", redir, redir->word); */
if (ft_strcmp((char *)token->data, redir->word) == 0)
{
ft_lst_delif(head, (t_redir**)(*head)->content, ft_addrcmp, ft_lst_cfree);
/* DG("matched heredoc EOF, new head @ %p", *head); */
}
else
{
tmp = redir->heredoc_data;
redir->heredoc_data = ft_str3join(redir->heredoc_data,
(char*)token->data, "\n");
ft_strdel(&tmp);
}
}
if (ft_strcmp((char *)token->data, redir->word) == 0)
ft_lst_delif(head, (t_redir**)(*head)->content,
ft_addrcmp, ft_lst_cfree);
else
redir->heredoc_data = join_heredoc_data(redir->heredoc_data,
(char*)token->data);
ft_lstdel(lst, &token_free);
return (1);
}

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/16 19:30:17 by ariard #+# #+# */
/* Updated: 2017/03/16 23:21:28 by ariard ### ########.fr */
/* Updated: 2017/03/17 17:55:38 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,9 +14,9 @@
void parser_destroy(t_parser *parser)
{
parser->state = SUCCESS;
parser->state = SUCCESS;
ft_lstdel(&parser->stack, &ft_lst_cfree);
ft_lstdel(&data_singleton()->parser.heredoc_queue, &ft_lst_cfree);
free(parser->new_sym);
parser->new_sym = NULL;
parser->new_sym = NULL;
}

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 17:58:34 by ariard #+# #+# */
/* Updated: 2017/03/16 21:31:55 by ariard ### ########.fr */
/* Updated: 2017/03/17 18:09:37 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -62,9 +62,10 @@ t_prodmatch g_prodmatch[] =
{TK_ASSIGNMENT_WORD, SEPARATOR_OP, CMD_PREFIX},
{TK_ASSIGNMENT_WORD, TK_WHILE, CMD_PREFIX},
{TK_ASSIGNMENT_WORD, TK_UNTIL, CMD_PREFIX},
{TK_ASSIGNMENT_WORD, TK_DO, CMD_PREFIX},
{TK_ASSIGNMENT_WORD, TK_FOR, CMD_PREFIX},
{TK_ASSIGNMENT_WORD, TK_IF, CMD_PREFIX},
{TK_ASSIGNMENT_WORD, TK_FI, CMD_PREFIX},
{TK_ASSIGNMENT_WORD, TK_THEN, CMD_PREFIX},
{TK_ASSIGNMENT_WORD, TK_ELIF, CMD_PREFIX},
{TK_ASSIGNMENT_WORD, TK_ELSE, CMD_PREFIX},
{TK_ASSIGNMENT_WORD, NEWLINE_LIST, CMD_PREFIX},
@ -93,6 +94,7 @@ t_prodmatch g_prodmatch[] =
{TK_NEWLINE, TK_ELIF, NEWLINE_LIST},
{TK_NEWLINE, TK_ELSE, NEWLINE_LIST},
{TK_NEWLINE, TK_THEN, NEWLINE_LIST},
{TK_NEWLINE, FUNC_NAME, NEWLINE_LIST},
{TK_NEWLINE, CMD_NAME, NEWLINE_LIST},
{TK_NEWLINE, COMPLETE_COMMANDS, NEWLINE_LIST},
{TK_NEWLINE, LINEBREAK, NEWLINE_LIST},
@ -102,7 +104,7 @@ t_prodmatch g_prodmatch[] =
{TK_NEWLINE, CMD_SUPERIOR, LINEBREAK},
{TK_NEWLINE, PIPE_SEMI_SEQUENCE, LINEBREAK},
{TK_NEWLINE, PIPE_CLOSE_SEQUENCE, LINEBREAK},
{TK_NEWLINE, SEQUENCE, LINEBREAK},
{TK_NEWLINE, SEQUENCE, NEWLINE_LIST},
{TK_NEWLINE, CASE_LIST_NS, NEWLINE_LIST},
{TK_NEWLINE, COMPOUND_LIST, NEWLINE_LIST},
{TK_NEWLINE, COMPLETE_CONDITION, NEWLINE_LIST},
@ -127,24 +129,17 @@ int produce_sym(t_list **stack, t_sym *new_sym, t_list **lst)
t_sym *head;
int i;
if (!*stack)
DG("stack absente");
if (!*stack || !*lst)
return (1);
token = (*lst)->content;
head = (*stack)->content;
DG("produce stack : %s && token : %s", read_state(*head),
read_state(token->type));
i = 0;
*new_sym = 0;
while (g_prodmatch[i].new_sym)
{
if (token->type == g_prodmatch[i].token
&& *head == g_prodmatch[i].stack)
{
// DG("MATCH : %s", read_state(g_prodmatch[i].new_sym));
*new_sym = g_prodmatch[i].new_sym;
}
i++;
}
if (!*new_sym)

View file

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* redir_init.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/17 17:46:50 by ariard #+# #+# */
/* Updated: 2017/03/17 17:49:10 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int redir_init(t_type type, t_redir *redir)
{
redir->n = (type == TK_LESS || type == TK_DLESS
|| type == TK_LESSAND) ? STDIN : STDOUT;
redir->type = type;
redir->heredoc_data = NULL;
redir->word = NULL;
return (0);
}

View file

@ -6,13 +6,13 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/16 20:31:32 by ariard #+# #+# */
/* Updated: 2017/03/16 22:11:59 by ariard ### ########.fr */
/* Updated: 2017/03/17 18:24:46 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int stack_init(t_parser *parser)
int stack_init(t_parser *parser)
{
ft_lstdel(&parser->stack, NULL);
push_stack(&parser->stack, TERMINUS);