Skip to content

Commit 59beae9

Browse files
committed
Add RC(1) context depth
1 parent 73aa93b commit 59beae9

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

cfg/preprocess.h

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@ class ReducibilityChecker1
781781
/**
782782
* @brief Reset the context of the array. Should be performed at the start of parsing
783783
*/
784-
void reset_ctx() { context.fill(std::numeric_limits<std::size_t>::max()); }
784+
void reset_ctx() { context.fill(0); }
785785

786786
/**
787787
* @brief Check if a symbol can be reduced in the current context.
@@ -804,31 +804,38 @@ class ReducibilityChecker1
804804
static_assert(ctx_pos < std::tuple_size_v<TRules>, "RC(1) : could not find reverse rule");
805805
// Check if the context exists
806806
// Note: it cannot solve rules with common prefixes
807-
if (context[ctx_pos] != std::numeric_limits<std::size_t>::max())
808-
{
809-
// Context already exists, we should start from position
810-
// It can be disabled for lazy evaluation
811-
if constexpr (do_prettyprint)
812-
std::cout << " rc : " << rule.type() << " : currently in ctx" << std::endl;
813-
return true;
814-
} else {
815-
// No context found, pick the first appearance of char
816-
if constexpr (do_prettyprint)
817-
std::cout << " rc : " << rule.type();
807+
808+
auto perform_check_at_symbol_start = [&](){
818809
// Out of bounds check: the rule cannot fit in current stack
819810
if (first_pos() >= stack_size)
820811
return false; // Continue
821812

822813
std::size_t stack_i = stack_size - 1 - first_pos();
823814
std::size_t parsed = descend(stack_i, std::get<1>(terms2nterms.get(rule)->terms));
824815
if constexpr (do_prettyprint)
825-
std::cout << ", i: " << stack_i << ", parsed: " << parsed << "/" << first_pos();
816+
std::cout << ", i: " << stack_i << ", parsed: " << parsed << "/" << first_pos() << std::endl;
826817
if (parsed >= first_pos()) // We can theoretically reduce everything up to this symbol OR there is no match at all
827818
{
828-
context[ctx_pos] = stack_i; // Successful match
819+
context[ctx_pos]++; // Successful match
829820
return true;
830821
}
831822
return false;
823+
};
824+
825+
if (context[ctx_pos] > 0)
826+
{
827+
// Context already exists, we should start from position
828+
// It can be disabled for lazy evaluation
829+
if constexpr (do_prettyprint)
830+
std::cout << " rc : " << rule.type() << " : currently in ctx" << std::endl;
831+
// If there is a match at symbol start, then we have descended inside and need to increment the ctx
832+
perform_check_at_symbol_start(); // ctx++
833+
return true;
834+
} else {
835+
// No context found, pick the first appearance of char
836+
if constexpr (do_prettyprint)
837+
std::cout << " rc : " << rule.type();
838+
return perform_check_at_symbol_start();
832839
}
833840
});
834841

@@ -874,7 +881,7 @@ class ReducibilityChecker1
874881
void do_apply_reduce(const TSymbol& symbol)
875882
{
876883
if constexpr (std::is_same_v<std::decay_t<std::tuple_element_t<depth, TRules>>, std::decay_t<TSymbol>>)
877-
context[depth] = std::numeric_limits<std::size_t>::max(); // Reset context
884+
context[depth] = (context[depth] > 0 ? context[depth] - 1 : 0); // Reset context
878885
else
879886
{
880887
if constexpr (depth + 1 < std::tuple_size_v<TRules>)

0 commit comments

Comments
 (0)