@@ -781,7 +781,7 @@ class ReducibilityChecker1
781
781
/* *
782
782
* @brief Reset the context of the array. Should be performed at the start of parsing
783
783
*/
784
- void reset_ctx () { context.fill (std::numeric_limits<std:: size_t >:: max () ); }
784
+ void reset_ctx () { context.fill (0 ); }
785
785
786
786
/* *
787
787
* @brief Check if a symbol can be reduced in the current context.
@@ -804,31 +804,38 @@ class ReducibilityChecker1
804
804
static_assert (ctx_pos < std::tuple_size_v<TRules>, " RC(1) : could not find reverse rule" );
805
805
// Check if the context exists
806
806
// 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 = [&](){
818
809
// Out of bounds check: the rule cannot fit in current stack
819
810
if (first_pos () >= stack_size)
820
811
return false ; // Continue
821
812
822
813
std::size_t stack_i = stack_size - 1 - first_pos ();
823
814
std::size_t parsed = descend (stack_i, std::get<1 >(terms2nterms.get (rule)->terms ));
824
815
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 ;
826
817
if (parsed >= first_pos ()) // We can theoretically reduce everything up to this symbol OR there is no match at all
827
818
{
828
- context[ctx_pos] = stack_i ; // Successful match
819
+ context[ctx_pos]++ ; // Successful match
829
820
return true ;
830
821
}
831
822
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 ();
832
839
}
833
840
});
834
841
@@ -874,7 +881,7 @@ class ReducibilityChecker1
874
881
void do_apply_reduce (const TSymbol& symbol)
875
882
{
876
883
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
878
885
else
879
886
{
880
887
if constexpr (depth + 1 < std::tuple_size_v<TRules>)
0 commit comments