From e7a7fc04df78c83c4c3fbc4c73eff509123537ab Mon Sep 17 00:00:00 2001 From: "ariard@student.42.fr" Date: Thu, 9 Feb 2017 18:10:28 +0100 Subject: [PATCH] proto parser en cours --- 42sh/Makefile | 3 ++ 42sh/includes/lexer.h | 2 +- 42sh/includes/parser.h | 11 ++++-- 42sh/src/main/shell_script.c | 2 +- 42sh/src/parser/aggregate_sym.c | 33 ++++++++++++++++++ 42sh/src/parser/eval_sym.c | 25 ++++++++++++++ 42sh/src/parser/ft_parse.c | 34 ++++++++++++------- .../{produce_prim_sym.c => produce_sym.c} | 9 +++-- 42sh/src/parser/push_stack.c | 19 +++++++++++ 42sh/src/parser/read_stack.c | 11 +++--- 42sh/src/parser/superflous_sym.c | 20 +++++++++++ 11 files changed, 143 insertions(+), 26 deletions(-) create mode 100644 42sh/src/parser/aggregate_sym.c create mode 100644 42sh/src/parser/eval_sym.c rename 42sh/src/parser/{produce_prim_sym.c => produce_sym.c} (80%) create mode 100644 42sh/src/parser/push_stack.c create mode 100644 42sh/src/parser/superflous_sym.c diff --git a/42sh/Makefile b/42sh/Makefile index 672d19c9..d55a28b1 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -171,6 +171,9 @@ main/sig_handler.c\ parser/parse.c\ parser/ft_parse.c\ parser/produce_prim_sym.c\ +parser/eval_sym.c\ +parser/push_stack.c\ +parser/superflous_sym.c\ parser/get_instruction.c\ parser/get_sub_instruction.c\ parser/parse_dgreat.c\ diff --git a/42sh/includes/lexer.h b/42sh/includes/lexer.h index b614d632..a6b1c776 100644 --- a/42sh/includes/lexer.h +++ b/42sh/includes/lexer.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */ -/* Updated: 2017/02/09 15:26:01 by ariard ### ########.fr */ +/* Updated: 2017/02/09 17:06:08 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/includes/parser.h b/42sh/includes/parser.h index eef07eaf..fe6b869c 100644 --- a/42sh/includes/parser.h +++ b/42sh/includes/parser.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/01 12:15:54 by jhalford #+# #+# */ -/* Updated: 2017/02/09 15:58:13 by ariard ### ########.fr */ +/* Updated: 2017/02/09 18:04:35 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -78,9 +78,14 @@ typedef unsigned long long int t_sym; int ft_parse(t_btree **ast, t_list **token); -int produce_prim_sym(t_sym *new_sym, t_list **lst); +int produce_sym(t_sym *new_sym, t_list **lst); +int eval_sym(t_sym stack, t_sym new_sym); +int aggregate_sym(t_sym stack, t_sym *new_sym); +int superflous_sym(t_sym stack, t_sym new_sym); -int ft_read_stack(t_sym stack[], int size); +int push_stack(t_sym *stack, t_sym *new_sym); + +int ft_read_stack(t_sym *stack); char *read_state(t_sym current); enum e_parstate diff --git a/42sh/src/main/shell_script.c b/42sh/src/main/shell_script.c index e86194d7..57eb7fe1 100644 --- a/42sh/src/main/shell_script.c +++ b/42sh/src/main/shell_script.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/22 23:06:34 by ariard #+# #+# */ -/* Updated: 2017/02/09 15:26:55 by ariard ### ########.fr */ +/* Updated: 2017/02/09 16:45:16 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/aggregate_sym.c b/42sh/src/parser/aggregate_sym.c new file mode 100644 index 00000000..1afd8229 --- /dev/null +++ b/42sh/src/parser/aggregate_sym.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* aggregate_sym.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/09 17:39:18 by ariard #+# #+# */ +/* Updated: 2017/02/09 18:10:13 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parser.h" + +//descente recursive pour multi aggregation + +int aggregate_sym(t_sym stack, t_sym *new_sym) +{ + int k; + + if (stack == 0 && *new_sym == CMD_NAME) + { + new_sym = SIMPLE_COMMAND; + k = aggregate_sym(stack, new_sym); + return (k); + } + if (stack == SIMPLE_COMMAND && *new_sym == PROGRAM) + { + new_sym = PROGRAM + } + return (0); + +} diff --git a/42sh/src/parser/eval_sym.c b/42sh/src/parser/eval_sym.c new file mode 100644 index 00000000..0ca80845 --- /dev/null +++ b/42sh/src/parser/eval_sym.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* eval_sym.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/09 16:26:30 by ariard #+# #+# */ +/* Updated: 2017/02/09 17:50:23 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parser.h" + +int eval_sym(t_sym stack, t_sym new_sym) +{ +// DG("eval head %s && eval sym %s", read_state(stack), read_state(new_sym)); + if (stack == 0 && new_sym == CMD_NAME) + return (0); + if (stack == SYM_GREAT && new_sym == CMD_NAME) + return (0); + if (stack == CMD_NAME && new_sym == SYM_GREAT) + return (0); + return (1); +} diff --git a/42sh/src/parser/ft_parse.c b/42sh/src/parser/ft_parse.c index 70a78875..b56a2d19 100644 --- a/42sh/src/parser/ft_parse.c +++ b/42sh/src/parser/ft_parse.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 14:30:22 by ariard #+# #+# */ -/* Updated: 2017/02/09 16:02:49 by ariard ### ########.fr */ +/* Updated: 2017/02/09 18:03:53 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,32 +15,42 @@ int ft_parse(t_btree **ast, t_list **token) { t_sym *new_sym; + t_sym *stack; t_parstate state; + int k; (void)ast; state = UNDEFINED; - new_sym = ft_memalloc(sizeof(t_sym)); + new_sym = ft_memalloc(sizeof(t_sym)); + stack = ft_memalloc(sizeof(t_sym) * 1000); + *stack = 0; + k = 0; while (*token) { - produce_prim_sym(new_sym, token); - DG("new sym : %s", read_state(*new_sym)); -/* if (eval_sym(head_stack, new_sym)) + produce_sym(new_sym, token); + if (eval_sym(*stack, *new_sym)) state = ERROR; + DG("head state : %s", read_state(*stack)); else { - aggregate_sym(head_stack, new_sym, struct_sym); - if (struct_sym) - pop(struct_sym.sym); - else if (!same_sym(new_sym, head_stack)) - push(new_sym); + k = aggregate_sym(*stack, new_sym) + if (k) + { + while (k--) + pop_stack(stack--); + push_stack(++stack, new_sym); + } + else if (!superflous_sym(new_sym, stack)) + push_stack(++stack, new_sym); } - build_tree(token, ast); - if (head_stack == PROGRAM) +/* if (head_stack == PROGRAM) state = PROGRAM; if (state == ERROR) return (error_syntax(token)); if (state == PROGRAM) return (0); + build_tree(token, ast); */ } + ft_read_stack(stack); return (0); } diff --git a/42sh/src/parser/produce_prim_sym.c b/42sh/src/parser/produce_sym.c similarity index 80% rename from 42sh/src/parser/produce_prim_sym.c rename to 42sh/src/parser/produce_sym.c index 0aab7439..8e9e4988 100644 --- a/42sh/src/parser/produce_prim_sym.c +++ b/42sh/src/parser/produce_sym.c @@ -1,18 +1,18 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* produce_prim_sym.c :+: :+: :+: */ +/* produce_sym.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2017/02/09 14:55:10 by ariard #+# #+# */ -/* Updated: 2017/02/09 16:05:27 by ariard ### ########.fr */ +/* Created: 2017/02/09 17:58:34 by ariard #+# #+# */ +/* Updated: 2017/02/09 17:58:45 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ #include "parser.h" -int produce_prim_sym(t_sym *new_sym, t_list **lst) +int produce_sym(t_sym *new_sym, t_list **lst) { t_token *token; @@ -33,6 +33,5 @@ int produce_prim_sym(t_sym *new_sym, t_list **lst) *new_sym = SYM_LESSAND; else if (token->type == TK_LESS) *new_sym = SYM_LESS; - ft_lst_delif(lst, (*lst)->content, &ft_addrcmp, &token_free); return (0); } diff --git a/42sh/src/parser/push_stack.c b/42sh/src/parser/push_stack.c new file mode 100644 index 00000000..07605faf --- /dev/null +++ b/42sh/src/parser/push_stack.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* push_stack.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/09 16:48:30 by ariard #+# #+# */ +/* Updated: 2017/02/09 17:55:44 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parser.h" + +int push_stack(t_sym *stack, t_sym *new_sym) +{ + *stack = *new_sym; + return (0); +} diff --git a/42sh/src/parser/read_stack.c b/42sh/src/parser/read_stack.c index cdf7a796..d8f8ee2e 100644 --- a/42sh/src/parser/read_stack.c +++ b/42sh/src/parser/read_stack.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 15:32:10 by ariard #+# #+# */ -/* Updated: 2017/02/09 16:02:30 by ariard ### ########.fr */ +/* Updated: 2017/02/09 17:33:30 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,9 +33,12 @@ char *read_state(t_sym current) return (NULL); } -int ft_read_stack(t_sym stack[], int size) +int ft_read_stack(t_sym *stack) { - while (stack[size]) - ft_putstr(read_state(stack[size--])); + while (*stack) + { + ft_putstr(read_state(*stack--)); + ft_putchar(10); + } return (0); } diff --git a/42sh/src/parser/superflous_sym.c b/42sh/src/parser/superflous_sym.c new file mode 100644 index 00000000..5c4e10a6 --- /dev/null +++ b/42sh/src/parser/superflous_sym.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* superflous_sym.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/02/09 17:47:33 by ariard #+# #+# */ +/* Updated: 2017/02/09 17:56:25 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parser.h" + +int superflous_sym(t_sym stack, t_sym new_sym) +{ + if (stack == new_sym); + return (1); + return (0); +}