protection stack symbolique

This commit is contained in:
Antoine Riard 2017-03-06 17:57:51 +01:00
parent e675eceb87
commit 8873f31f79
15 changed files with 87 additions and 91 deletions

16
42sh/fi
View file

@ -1,16 +0,0 @@
42sh
Makefile
STDBUG
TESTSHELL
donovan_segaults_06-02
fi
file
includes
libft
objs
parser_init.c
pdf
sample
src
test_framework.sh
update_makefile.sh

View file

@ -1,15 +0,0 @@
42sh
Makefile
STDBUG
TESTSHELL
donovan_segaults_06-02
file
includes
libft
objs
parser_init.c
pdf
sample
src
test_framework.sh
update_makefile.sh

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */ /* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */
/* Updated: 2017/03/05 17:29:09 by jhalford ### ########.fr */ /* Updated: 2017/03/06 17:52:13 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -63,6 +63,13 @@ struct s_lexer
t_list *heredoc_stack; t_list *heredoc_stack;
}; };
struct s_rvwords
{
char *word;
int type;
};
extern t_rvwords g_rvwords[];
extern int (*g_lexer[])(t_list **alst, t_lexer *lexer); extern int (*g_lexer[])(t_list **alst, t_lexer *lexer);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/13 17:11:48 by jhalford #+# #+# */ /* Created: 2016/12/13 17:11:48 by jhalford #+# #+# */
/* Updated: 2017/03/06 15:31:28 by ariard ### ########.fr */ /* Updated: 2017/03/06 17:51:59 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -26,6 +26,7 @@ typedef enum e_mode t_mode;
typedef struct s_lexer t_lexer; typedef struct s_lexer t_lexer;
typedef enum e_lexstate t_lexstate; typedef enum e_lexstate t_lexstate;
typedef struct s_token t_token; typedef struct s_token t_token;
typedef struct s_rvwords t_rvwords;
typedef struct s_ld t_ld; typedef struct s_ld t_ld;
typedef struct s_astnode t_astnode; typedef struct s_astnode t_astnode;

View file

@ -6,62 +6,77 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/26 00:07:05 by ariard #+# #+# */ /* Created: 2017/01/26 00:07:05 by ariard #+# #+# */
/* Updated: 2017/02/21 21:06:16 by ariard ### ########.fr */ /* Updated: 2017/03/06 17:56:14 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "lexer.h" #include "lexer.h"
int get_reserved_words(t_list **alst)
t_rvwords g_rvwords[] =
{
{"while", TK_WHILE},
{"done", TK_DONE},
{"do", TK_DO},
{"if", TK_IF},
{"then", TK_THEN},
{"fi", TK_FI},
{"elif", TK_ELIF},
{"else", TK_ELSE},
{"until", TK_UNTIL},
{"case", TK_CASE},
{"esac", TK_ESAC},
{"for", TK_FOR},
{"null", 0},
};
static int recognization_rvwords(t_token *pv_tk)
{
if (!pv_tk || (pv_tk->type == TK_NEWLINE || pv_tk->type == TK_AMP
|| pv_tk->type == TK_SEMI || pv_tk->type == TK_WHILE
|| pv_tk->type == TK_DONE || 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))
return (1);
return (0);
}
int get_reserved_words(t_list **alst)
{ {
t_token *token; t_token *token;
t_token *previous_token; t_token *pv_tk;
t_token *ante_token; t_token *ante_token;
t_list *temp; t_list *temp;
int i;
temp = *alst; temp = *alst;
previous_token = NULL; pv_tk = NULL;
ante_token = NULL; ante_token = NULL;
while (temp) while (temp)
{ {
token = temp->content; token = temp->content;
if (!previous_token || (previous_token->type & RW_SEP)) //no more comp &
if (recognization_rvwords(pv_tk))
{ {
if (token->type == TK_WORD) if (token->type == TK_WORD)
{ {
if (ft_strncmp(token->data, "while", 5) == 0) i = 0;
token->type = TK_WHILE; while (g_rvwords[i].type)
else if (ft_strncmp(token->data, "done", 4) == 0) {
token->type = TK_DONE; if (ft_strcmp(token->data, g_rvwords[i].word) == 0)
else if (ft_strncmp(token->data, "do" , 2) == 0) token->type = g_rvwords[i].type;
token->type = TK_DO; i++;
else if (ft_strncmp(token->data, "if", 2) == 0) }
token->type = TK_IF;
else if (ft_strncmp(token->data, "then", 4) == 0)
token->type = TK_THEN;
else if(ft_strncmp(token->data, "fi", 4) == 0)
token->type = TK_FI;
else if (ft_strncmp(token->data, "elif", 4) == 0)
token->type = TK_ELIF;
else if (ft_strncmp(token->data, "else", 4) == 0)
token->type = TK_ELSE;
else if (ft_strncmp(token->data, "until", 5) == 0)
token->type = TK_UNTIL;
else if (ft_strncmp(token->data, "case", 4) == 0)
token->type = TK_CASE;
else if (ft_strncmp(token->data, "esac", 4) == 0)
token->type = TK_ESAC;
else if (ft_strncmp(token->data, "for", 3) == 0)
token->type = TK_FOR;
} }
} }
if (ante_token && (ante_token->type == TK_CASE || ante_token->type == TK_FOR) if (ante_token && (ante_token->type == TK_CASE || ante_token->type == TK_FOR)
&& ft_strncmp(token->data, "in", 2) == 0) && ft_strncmp(token->data, "in", 2) == 0)
token->type = TK_IN; token->type = TK_IN;
if (previous_token && previous_token->type == TK_FOR && token->type == TK_WORD) if (pv_tk && pv_tk->type == TK_FOR && token->type == TK_WORD)
token->type = TK_NAME; token->type = TK_NAME;
ante_token = previous_token; ante_token = pv_tk;
previous_token = token; pv_tk = token;
temp = temp->next; temp = temp->next;
} }
return (0); return (0);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */ /* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */
/* Updated: 2017/03/05 16:47:43 by ariard ### ########.fr */ /* Updated: 2017/03/06 17:55:29 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -47,9 +47,9 @@ int handle_instruction(int fd)
if (get_lexer_stack(lexer) > 1) if (get_lexer_stack(lexer) > 1)
continue ; continue ;
lexer.state = DEFAULT; lexer.state = DEFAULT;
token_print(token);
if (get_reserved_words(&token)) if (get_reserved_words(&token))
return (1); return (1);
token_print(token);
if (insert_newline(&token)) if (insert_newline(&token))
return (1); return (1);
if (ft_parse(&ast, &token, &parser)) if (ft_parse(&ast, &token, &parser))

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/15 20:49:15 by ariard #+# #+# */ /* Created: 2017/02/15 20:49:15 by ariard #+# #+# */
/* Updated: 2017/03/06 15:57:48 by ariard ### ########.fr */ /* Updated: 2017/03/06 16:40:03 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -316,21 +316,22 @@ int aggregate_sym(t_list **stack, t_sym *new_sym, t_parstate *state)
i = 0; i = 0;
head = (*stack)->content; head = (*stack)->content;
// DG("aggregate head %s && sym %s", DG("aggregate head %s && sym %s",
// read_state(*head), read_state(*new_sym)); read_state(*head), read_state(*new_sym));
while (g_aggrematch[i].top) while (g_aggrematch[i].top)
{ {
if (*new_sym == g_aggrematch[i].top if (*new_sym == g_aggrematch[i].top
&& MATCH_STACK(*head, g_aggrematch[i].under)) && MATCH_STACK(*head, 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; *new_sym = g_aggrematch[i].new_sym;
if (g_aggrematch[i].erase_sym) if (g_aggrematch[i].erase_sym)
{ {
pop_stack(stack, g_aggrematch[i].erase_sym); if (pop_stack(stack, g_aggrematch[i].erase_sym))
return (1);
head = (*stack)->content; head = (*stack)->content;
// DG("stack after pop: %s", read_state(*head)); DG("stack after pop: %s", read_state(*head));
} }
if (eval_sym(stack, *new_sym)) if (eval_sym(stack, *new_sym))
return ((*state = ERROR)); return ((*state = ERROR));

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 20:15:35 by ariard #+# #+# */ /* Created: 2017/02/09 20:15:35 by ariard #+# #+# */
/* Updated: 2017/03/05 18:23:57 by ariard ### ########.fr */ /* Updated: 2017/03/06 17:50:43 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -1027,7 +1027,7 @@ int eval_sym(t_list **stack, t_sym new_sym)
int i; int i;
head = (*stack)->content; head = (*stack)->content;
// DG("eval head %s && sym %s", read_state(*head), read_state(new_sym)); DG("eval head %s && sym %s", read_state(*head), read_state(new_sym));
i = 0; i = 0;
while (g_stackmatch[i].top) while (g_stackmatch[i].top)
{ {

View file

@ -37,12 +37,13 @@ int ft_parse(t_btree **ast, t_list **token, t_parser *parser)
while (*token) while (*token)
{ {
produce_sym(&parser->stack, parser->new_sym, token); produce_sym(&parser->stack, parser->new_sym, token);
// DG("new sym %s", read_state(*parser->new_sym)); DG("new sym %s", read_state(*parser->new_sym));
if (eval_sym(&parser->stack, *parser->new_sym)) if (eval_sym(&parser->stack, *parser->new_sym))
return ((parser->state = ERROR)); return ((parser->state = ERROR));
else else
{ {
aggregate_sym(&parser->stack, parser->new_sym, &parser->state); if (aggregate_sym(&parser->stack, parser->new_sym, &parser->state))
return (1);
push_stack(&parser->stack, *parser->new_sym); push_stack(&parser->stack, *parser->new_sym);
} }
// ft_read_stack(parser->stack); // ft_read_stack(parser->stack);

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/21 16:14:04 by ariard #+# #+# */ /* Created: 2017/02/21 16:14:04 by ariard #+# #+# */
/* Updated: 2017/03/03 14:28:00 by ariard ### ########.fr */ /* Updated: 2017/03/06 17:04:33 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,8 +16,7 @@ void parser_init(t_parser *parser)
{ {
parser->state = SUCCESS; parser->state = SUCCESS;
parser->new_sym = ft_memalloc(sizeof(t_sym)); parser->new_sym = ft_memalloc(sizeof(t_sym));
parser->stack = NULL;
push_stack(&parser->stack, TERMINUS); push_stack(&parser->stack, TERMINUS);
push_stack(&parser->stack, LINEBREAK); push_stack(&parser->stack, LINEBREAK);
// parser->stack = ft_memalloc(sizeof(t_sym) * 1000);
// push_stack(parser->stack, LINEBREAK);
} }

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 19:12:44 by ariard #+# #+# */ /* Created: 2017/02/09 19:12:44 by ariard #+# #+# */
/* Updated: 2017/03/05 16:28:52 by ariard ### ########.fr */ /* Updated: 2017/03/06 17:09:00 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,16 +17,19 @@ int pop_stack(t_list **stack, t_sym erase_sym)
t_sym *head; t_sym *head;
t_list *temp; t_list *temp;
head = (*stack)->content; while ((*stack) && *(head = (*stack)->content) != erase_sym)
while ((*stack) && *head != erase_sym) {
temp = *stack;
(*stack) = (*stack)->next;
ft_lstdelone(&temp, NULL);
}
if (*stack)
{ {
temp = *stack; temp = *stack;
(*stack) = (*stack)->next; (*stack) = (*stack)->next;
ft_lstdelone(&temp, NULL); ft_lstdelone(&temp, NULL);
head = (*stack)->content;
} }
temp = *stack; else
(*stack) = (*stack)->next; return (1);
ft_lstdelone(&temp, NULL);
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */ /* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/09 17:58:34 by ariard #+# #+# */ /* Created: 2017/02/09 17:58:34 by ariard #+# #+# */
/* Updated: 2017/03/05 15:12:59 by ariard ### ########.fr */ /* Updated: 2017/03/06 16:43:10 by ariard ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -107,8 +107,8 @@ int produce_sym(t_list **stack, t_sym *new_sym, t_list **lst)
token = (*lst)->content; token = (*lst)->content;
head = (*stack)->content; head = (*stack)->content;
// DG("produce stack : %s && token : %s", read_state(*head), DG("produce stack : %s && token : %s", read_state(*head),
// read_state(token->type)); read_state(token->type));
i = 0; i = 0;
*new_sym = 0; *new_sym = 0;
while (g_prodmatch[i].new_sym) while (g_prodmatch[i].new_sym)
@ -116,7 +116,7 @@ int produce_sym(t_list **stack, t_sym *new_sym, t_list **lst)
if (token->type == g_prodmatch[i].token if (token->type == g_prodmatch[i].token
&& *head == g_prodmatch[i].stack) && *head == g_prodmatch[i].stack)
{ {
// DG("MATCH : %s", read_state(g_prodmatch[i].new_sym)); DG("MATCH : %s", read_state(g_prodmatch[i].new_sym));
*new_sym = g_prodmatch[i].new_sym; *new_sym = g_prodmatch[i].new_sym;
} }
i++; i++;

View file