/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/27 11:16:53 by ljiriste #+# #+# */
-/* Updated: 2024/07/18 11:47:10 by ljiriste ### ########.fr */
+/* Updated: 2024/07/18 12:16:01 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
return (success);
return (res);
}
+ append_token(lookahead, token);
rule.position = 0;
i = 1;
while (i < rules->size)
return (success);
}
-void remove_empty_token(t_vec *lookahead)
+void remove_token(t_vec *lookahead, const t_token *removed_token)
{
size_t i;
const t_token *token;
{
--i;
token = ft_vec_caccess(lookahead, i);
- if (!cmp_token_type(token, &empty_token))
+ if (!cmp_token_type(token, removed_token))
ft_vec_erase(lookahead, i, ft_free_token);
}
}
i = rule->position;
while (ft_vec_contains(lookahead, &empty_token, void_cmp_token_type) && i < rule->rule->constituents.size)
{
- remove_empty_token(lookahead);
+ remove_token(lookahead, &empty_token);
token = ft_vec_caccess(&rule->rule->constituents, i);
- res = add_first(lookahead, token, rules, tokens);
- if (res != success)
- return (res);
+ if (!ft_vec_contains(lookahead, token, void_cmp_token_type))
+ {
+ res = add_first(lookahead, token, rules, tokens);
+ if (res != success)
+ return (res);
+ }
++i;
}
return (success);
return (success);
}
+void remove_nonterminals(t_vec *lookahead, const t_vec *tokens)
+{
+ size_t i;
+ const t_token *token;
+
+ i = 0;
+ while (i < tokens->size)
+ {
+ token = ft_vec_caccess(tokens, i);
+ if (!cmp_token_type(token, &eof_token))
+ break ;
+ ++i;
+ }
+ ++i;
+ while (i < tokens->size)
+ {
+ token = ft_vec_caccess(tokens, i);
+ remove_token(lookahead, token);
+ ++i;
+ }
+ return ;
+}
+
t_ft_stat add_lookahead(t_lr1_item *new, t_lr1_item *item, const t_vec *rules, const t_vec *tokens)
{
t_ft_stat res;
return (res);
++item->core.position;
res = expand_lookahead(&new->lookahead, &item->core, rules, tokens);
+ remove_nonterminals(&new->lookahead, tokens);
--item->core.position;
if (res != success)
{
}
if (ft_vec_contains(&new->lookahead, &empty_token, void_cmp_token_type))
{
- remove_empty_token(&new->lookahead);
+ remove_token(&new->lookahead, &empty_token);
res = add_to_lookahead(&item->lookahead, &new->lookahead);
}
return (res);