grammar if then elif part one

This commit is contained in:
ariard@student.42.fr 2017-02-19 19:32:45 +01:00
parent c90ad14f93
commit f6aefd7739
15 changed files with 137 additions and 31 deletions

View file

@ -200,7 +200,8 @@ parser/tree_wrapper.c\
parser/add_sep.c\
parser/add_cmd.c\
parser/add_file.c\
parser/add_loop.c
parser/add_loop.c\
parser/add_condition.c
SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE))
OBJS = $(addprefix $(OBJ_DIR), $(SRC_BASE:.c=.o))

Binary file not shown.

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 12:15:54 by jhalford #+# #+# */
/* Updated: 2017/02/19 16:29:14 by ariard ### ########.fr */
/* Updated: 2017/02/19 18:43:02 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -214,8 +214,10 @@ int add_cmd(t_btree **ast, t_list **lst);
int add_file(t_btree **ast, t_list **lst);
int add_loop_cmd(t_btree **ast, t_list **lst);
int add_loop_sep(t_btree **ast, t_list **lst);
int add_condition_cmd(t_btree **ast, t_list **lst);
int isloop(t_btree **ast);
int isdir(t_btree **ast);
int iscondition(t_btree **ast);
int join_ast(t_btree **ast, t_btree **new_node);
int gen_node(t_btree **ast);

View file

@ -1,13 +1,13 @@
while ls
do
if ls
then
echo hello
else
while ls
do
echo hello world
pwd
done
pwd
elif ls
then
pwd
elif ls
then
pwd
else ls
then
pwd
fi
done

Binary file not shown.

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/15 20:49:15 by ariard #+# #+# */
/* Updated: 2017/02/19 17:41:59 by ariard ### ########.fr */
/* Updated: 2017/02/19 18:46:04 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -25,7 +25,9 @@ int add_cmd(t_btree **ast, t_list **lst)
return (add_file(ast, lst));
else if (isloop(ast))
return (add_loop_cmd(ast, lst));
else if ((node = (*ast)->item)->type != TK_DO)
else if (iscondition(ast))
return (add_condition_cmd(ast, lst));
else if ((node = (*ast)->item)->type != TK_DO && node->type != TK_THEN)
return (add_cmd(&(*ast)->right, lst));
my_tab = NULL;
token = (*lst)->content;

View file

@ -0,0 +1,56 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* add_condition.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/19 18:12:52 by ariard #+# #+# */
/* Updated: 2017/02/19 18:46:01 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "parser.h"
int iscondition(t_btree **ast)
{
t_astnode *node;
node = NULL;
if (*ast)
{
node = (*ast)->item;
if ((node->type == TK_NEWLINE || node->type == TK_SEMI
|| node->type == TK_AMP) && iscondition(&(*ast)->right) == 1)
return (1);
if ((node->type == TK_IF || node->type == TK_ELIF)
&& node->full == 1)
return (2);
if ((node->type == TK_IF || node->type == TK_ELIF)
&& node->full == 0)
return (1);
}
return (0);
}
int add_condition_cmd(t_btree **ast, t_list **lst)
{
t_token *token;
t_astnode *node;
token = (*lst)->content;
node = (*ast)->item;
if (token->type == TK_IF && node->type == TK_IF)
node->nest++;
if (token->type == TK_FI && node->type == TK_IF && node->nest > 0)
node->nest--;
else if (token->type == TK_FI && node->type == TK_IF && node->nest == 0)
return ((node->full = 1));
if (token->type == TK_THEN)
return (add_cmd(&(*ast)->right, lst));
else if (!(*ast)->right && iscondition(&(*ast)->left) != 2)
return (add_cmd(&(*ast)->left, lst));
else
return (add_cmd(&(*ast)->right, lst));
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/02/19 17:52:52 by ariard ### ########.fr */
/* Updated: 2017/02/19 18:38:25 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -21,8 +21,8 @@ int isloop(t_btree **ast)
{
node = (*ast)->item;
DG("TEST LOOP");
if ((node->type == TK_NEWLINE || node->type == TK_SEMI ||
node->type == TK_AMP) && (isloop(&(*ast)->right) == 1))
if ((node->type == TK_NEWLINE || node->type == TK_SEMI
|| node->type == TK_AMP) && isloop(&(*ast)->right) == 1)
return (1);
if (node->type == TK_WHILE && node->full == 1)
{

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/15 19:12:07 by ariard #+# #+# */
/* Updated: 2017/02/19 17:51:51 by ariard ### ########.fr */
/* Updated: 2017/02/19 18:46:06 by ariard ### ########.fr */
/* */
/* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 17:39:18 by ariard #+# #+# */
/* Updated: 2017/02/19 17:35:28 by ariard ### ########.fr */
/* Updated: 2017/02/19 19:32:24 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -21,6 +21,7 @@ t_aggrematch g_aggrematch[] =
{TK_PIPE, CMD_SUPERIOR, SIMPLE_COMMAND, CMD_SUPERIOR},
{TK_FI, ELSE_PART, IF_CLAUSE, TK_IF},
{TK_FI, COMPOUND_LIST, IF_CLAUSE, TK_IF},
{TK_FI, CMD_SUPERIOR, IF_CLAUSE, TK_IF},
{TK_DONE, CMD_SUPERIOR, DO_GROUP, TK_DO},
{TK_DONE, COMPOUND_LIST, DO_GROUP, TK_DO},
//Esac ?
@ -52,6 +53,10 @@ t_aggrematch g_aggrematch[] =
{LINEBREAK, COMPOUND_LIST, COMPOUND_LIST, COMPOUND_LIST},
{NEWLINE_LIST, CMD_NAME, CMD_SUPERIOR, CMD_NAME},
{NEWLINE_LIST, TK_DO, TK_DO, TK_DO},
{NEWLINE_LIST, TK_THEN, TK_THEN, TK_THEN},
{NEWLINE_LIST, TK_IF, TK_IF, TK_IF},
{NEWLINE_LIST, TK_ELIF, TK_ELIF, TK_ELIF},
{NEWLINE_LIST, TK_ELSE, TK_ELSE, TK_ELSE},
{NEWLINE_LIST, TK_WHILE, TK_WHILE, TK_WHILE},
{NEWLINE_LIST, NEWLINE_LIST, NEWLINE_LIST, NEWLINE},
{NEWLINE_LIST, NAME, SEQUENTIAL_SEP, 0},
@ -104,6 +109,9 @@ t_aggrematch g_aggrematch[] =
{CMD_NAME, NEWLINE_LIST, CMD_SUPERIOR, 0},
{CMD_NAME, TK_WHILE, CMD_SUPERIOR, 0},
{CMD_NAME, TK_DO, CMD_SUPERIOR, 0},
{CMD_NAME, TK_IF, CMD_SUPERIOR, 0},
{CMD_NAME, TK_ELIF, CMD_SUPERIOR, 0},
{CMD_NAME, TK_THEN, CMD_SUPERIOR, 0},
{CMD_NAME, COMPOUND_LIST, CMD_SUPERIOR, 0},
{CMD_NAME, TK_PIPE, CMD_SUPERIOR, 0},
{CMD_NAME, PIPE_SEMI_SEQUENCE, CMD_SUPERIOR, 0},
@ -143,6 +151,9 @@ t_aggrematch g_aggrematch[] =
{COMMAND, COMPOUND_LIST, PIPE_SEMI_SEQUENCE, 0},
{COMMAND, TK_WHILE, PIPE_SEMI_SEQUENCE, 0},
{COMMAND, TK_DO, PIPE_SEMI_SEQUENCE, 0},
{COMMAND, TK_IF, PIPE_SEMI_SEQUENCE, 0},
{COMMAND, TK_THEN, PIPE_SEMI_SEQUENCE, 0},
{COMMAND, TK_ELIF, PIPE_SEMI_SEQUENCE, 0},
{COMMAND, TK_BANG, PIPE_SEMI_SEQUENCE, 0},
{COMMAND, SEPARATOR_OP, PIPE_SEMI_SEQUENCE, 0},
{COMMAND, NEWLINE_LIST, PIPE_SEMI_SEQUENCE, 0},
@ -152,6 +163,9 @@ t_aggrematch g_aggrematch[] =
{END_COMMAND, TK_WHILE, PIPE_SEQUENCE, 0},
{END_COMMAND, LINEBREAK, PIPE_SEQUENCE, 0},
{END_COMMAND, TK_DO, PIPE_SEQUENCE, 0},
{END_COMMAND, TK_IF, PIPE_SEQUENCE, 0},
{END_COMMAND, TK_ELIF, PIPE_SEQUENCE, 0},
{END_COMMAND, TK_THEN, PIPE_SEQUENCE, 0},
{END_COMMAND, COMPOUND_LIST, COMPOUND_LIST, COMPOUND_LIST},
{PIPE_SEQUENCE, TK_WHILE, PIPELINE, 0},
{PIPE_SEQUENCE, TK_BANG, PIPELINE, TK_BANG},
@ -160,9 +174,15 @@ t_aggrematch g_aggrematch[] =
{PIPE_SEQUENCE, LINEBREAK, PIPELINE, 0},
{PIPE_SEQUENCE, AND_OR_MAJOR, PIPELINE, 0},
{PIPE_SEQUENCE, TK_DO, PIPELINE, 0},
{PIPE_SEQUENCE, TK_IF, PIPELINE, 0},
{PIPE_SEQUENCE, TK_ELIF, PIPELINE, 0},
{PIPE_SEQUENCE, TK_THEN, PIPELINE, 0},
{PIPE_SEQUENCE, COMPOUND_LIST, PIPELINE, 0},
{PIPELINE, TK_WHILE, AND_OR, 0},
{PIPELINE, TK_DO, AND_OR, 0},
{PIPELINE, TK_IF, AND_OR, 0},
{PIPELINE, TK_ELIF, AND_OR, 0},
{PIPELINE, TK_THEN, AND_OR, 0},
{PIPELINE, COMPOUND_LIST, AND_OR, 0},
{PIPELINE, LINEBREAK, AND_OR, 0},
// {PIPELINE, LINEBREAK, AND_OR, AND_OR},
@ -171,6 +191,9 @@ t_aggrematch g_aggrematch[] =
{AND_OR_MAJOR, AND_OR_MAJOR, AND_OR_MAJOR, AND_OR_MAJOR},
{AND_OR, TK_DO, COMPOUND_LIST, 0},
{AND_OR, TK_WHILE, COMPOUND_LIST, 0},
{AND_OR, TK_IF, COMPOUND_LIST, 0},
{AND_OR, TK_ELIF, COMPOUND_LIST, 0},
{AND_OR, TK_THEN, COMPOUND_LIST, 0},
{AND_OR, COMPOUND_LIST, COMPOUND_LIST, 0},
{AND_OR, SEPARATOR_OP, LIST, LIST},
{AND_OR, NEWLINE_LIST, LIST, 0},
@ -188,20 +211,20 @@ int aggregate_sym(t_sym **stack, t_sym *new_sym, t_parstate *state)
int i;
i = 0;
// DG("aggregate head %s && sym %s",
// read_state(**stack), read_state(*new_sym));
DG("aggregate head %s && sym %s",
read_state(**stack), read_state(*new_sym));
while (g_aggrematch[i].top)
{
if (*new_sym == g_aggrematch[i].top
&& MATCH_STACK(**stack, g_aggrematch[i].under))
{
// DG("MATCH : %s", read_state(g_aggrematch[i].new_sym));
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);
// DG("stack after pop: %s", read_state(**stack));
DG("stack after pop: %s", read_state(**stack));
}
if (eval_sym(**stack, *new_sym))
return ((*state = ERROR));

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/15 18:32:59 by ariard #+# #+# */
/* Updated: 2017/02/19 17:34:55 by ariard ### ########.fr */
/* Updated: 2017/02/19 18:40:15 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -24,6 +24,10 @@ t_treematch g_treematch[] =
{TK_WHILE, &add_cmd},
{TK_DO, &add_cmd},
{TK_DONE, &add_cmd},
{TK_IF, &add_cmd},
{TK_ELIF, &add_cmd},
{TK_THEN, &add_cmd},
{TK_FI, &add_cmd},
{TK_NEWLINE, &add_sep},
{0, NULL},
};

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 16:26:30 by ariard #+# #+# */
/* Updated: 2017/02/19 17:43:42 by ariard ### ########.fr */
/* Updated: 2017/02/19 19:31:38 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -175,6 +175,8 @@ t_stackmatch g_stackmatch[] =
{TK_ELIF, COMPOUND_LIST},
{TK_FI, ELSE_PART},
{TK_FI, COMPOUND_LIST},
{TK_FI, CMD_SUPERIOR},
{TK_FI, END_COMMAND},
{TK_DO, CMD_SUPERIOR},
{TK_DO, COMPOUND_LIST},
{TK_DO, NAME},
@ -502,7 +504,7 @@ int eval_sym(t_sym stack, t_sym new_sym)
{
int i;
// DG("eval head %s && sym %s", read_state(stack), read_state(new_sym));
DG("eval head %s && sym %s", read_state(stack), read_state(new_sym));
i = 0;
while (g_stackmatch[i].top)
{

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 14:30:22 by ariard #+# #+# */
/* Updated: 2017/02/19 17:32:56 by ariard ### ########.fr */
/* Updated: 2017/02/19 18:41:29 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -33,6 +33,7 @@ int ft_parse2(t_btree **ast, t_list **token)
t_sym *stack;
t_parstate state;
(void)ast;
state = UNDEFINED;
new_sym = ft_memalloc(sizeof(t_sym));
stack = ft_memalloc(sizeof(t_sym) * 1000);
@ -56,8 +57,8 @@ int ft_parse2(t_btree **ast, t_list **token)
return (error_syntax(token));
if (state == SUCCESS)
ft_putstr("success");
build_tree(ast, token);
btree_print(STDBUG, *ast, &ft_putast);
// build_tree(ast, token);
// btree_print(STDBUG, *ast, &ft_putast);
if ((end_instruction(*stack) && !(*token)->next) || *stack == PROGRAM)
insert_linebreak(token);
else

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 17:58:34 by ariard #+# #+# */
/* Updated: 2017/02/19 17:35:31 by ariard ### ########.fr */
/* Updated: 2017/02/19 19:31:40 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -31,6 +31,9 @@ t_prodmatch g_prodmatch[] =
{TK_N_WORD, AND_OR_MAJOR, CMD_NAME},
{TK_N_WORD, TK_WHILE, CMD_NAME},
{TK_N_WORD, TK_DO, CMD_NAME},
{TK_N_WORD, TK_IF, CMD_NAME},
{TK_N_WORD, TK_THEN, CMD_NAME},
{TK_N_WORD, TK_ELIF, CMD_NAME},
{TK_N_WORD, COMPOUND_LIST, CMD_NAME},
{TK_N_WORD, NEWLINE_LIST, CMD_NAME},
@ -51,6 +54,10 @@ t_prodmatch g_prodmatch[] =
{TK_NAME, TK_FOR, NAME},
{TK_NEWLINE, TK_DO, NEWLINE_LIST},
{TK_NEWLINE, TK_WHILE, NEWLINE_LIST},
{TK_NEWLINE, TK_IF, NEWLINE_LIST},
{TK_NEWLINE, TK_FI, NEWLINE_LIST},
{TK_NEWLINE, TK_ELIF, NEWLINE_LIST},
{TK_NEWLINE, TK_ELSE, NEWLINE_LIST},
{TK_NEWLINE, CMD_NAME, NEWLINE_LIST},
{TK_NEWLINE, COMPLETE_COMMANDS, NEWLINE_LIST},
{TK_NEWLINE, LINEBREAK, NEWLINE_LIST},

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 15:32:10 by ariard #+# #+# */
/* Updated: 2017/02/18 20:03:24 by ariard ### ########.fr */
/* Updated: 2017/02/19 19:31:42 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,6 +14,14 @@
char *read_state(t_sym current)
{
if (current == TK_FI)
return ("TK_FI");
if (current == TK_IF)
return ("TK_IF");
if (current == TK_ELIF)
return ("TK_ELIF");
if (current == TK_THEN)
return ("TK_THEN");
if (current == COMPOUND_COMMAND)
return ("COMPOUND_COMMAND");
if (current == WHILE_CLAUSE)