From e4afc1b801ef2bf069ad36a1f7a565de051a55d4 Mon Sep 17 00:00:00 2001 From: vera-rubin <322284620+vera-rubin@users.noreply.github.com> Date: Thu, 15 Oct 2026 06:40:07 +0000 Subject: [PATCH 1/2] Fix token filtering when reusing repetition helpers Include keep_all_tokens in repetition cache keys so pinned and ordinary rules don't share helpers with incompatible token filtering. Cover recursive and large ranged repetitions in both rule orders. --- lark/load_grammar.py | 9 +++++---- tests/test_parser.py | 12 ++++++++++++ 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/lark/load_grammar.py b/lark/load_grammar.py index b98f166c1..b2f695a28 100644 --- a/lark/load_grammar.py +++ b/lark/load_grammar.py @@ -244,8 +244,9 @@ def _add_rule(self, key, name, expansions): return t def _add_recurse_rule(self, type_: str, expr: Tree): + key = (expr, self.rule_options and self.rule_options.keep_all_tokens) try: - return self.rules_cache[expr] + return self.rules_cache[key] except KeyError: new_name = self._name_rule(type_) t = NonTerminal(new_name) @@ -253,7 +254,7 @@ def _add_recurse_rule(self, type_: str, expr: Tree): ST('expansion', [expr]), ST('expansion', [t, expr]) ]) - return self._add_rule(expr, new_name, tree) + return self._add_rule(key, new_name, tree) def _add_repeat_rule(self, a, b, target, atom): """Generate a rule that repeats target ``a`` times, and repeats atom ``b`` times. @@ -267,7 +268,7 @@ def _add_repeat_rule(self, a, b, target, atom): new_rule: target target target atom atom atom atom """ - key = (a, b, target, atom) + key = (a, b, target, atom, self.rule_options and self.rule_options.keep_all_tokens) try: return self.rules_cache[key] except KeyError: @@ -300,7 +301,7 @@ def _add_repeat_opt_rule(self, a, b, target, target_opt, atom): | target target target atom atom atom """ - key = (a, b, target, atom, "opt") + key = (a, b, target, atom, "opt", self.rule_options and self.rule_options.keep_all_tokens) try: return self.rules_cache[key] except KeyError: diff --git a/tests/test_parser.py b/tests/test_parser.py index b12752b07..8160cb3be 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -1732,6 +1732,18 @@ def test_keep_all_tokens(self): tree = l.parse('aaa') self.assertEqual(tree.children, ['a', 'a', 'a']) + def test_repetition_keep_all_tokens(self): + for repeat, count in [('+', 2), ('*', 2), ('~50', 50), ('~0..50', 2)]: + if PARSER == 'cyk' and repeat in ('*', '~0..50'): + continue # CYK doesn't support empty rules + rules = ['dropped: _A' + repeat, '!kept: _A' + repeat] + for rule_order in (rules, rules[::-1]): + with self.subTest(repeat=repeat, rules=rule_order): + l = _Lark('\n'.join(['start: dropped "," kept'] + rule_order + ['_A: "a"'])) + dropped, kept = l.parse('a' * count + ',' + 'a' * count).children + self.assertEqual(dropped, Tree('dropped', [])) + self.assertEqual(kept, Tree('kept', [Token('_A', 'a')] * count)) + def test_token_flags(self): l = _Lark("""!start: "a"i+ From 9ca7e7b88702e48fd45c3f94087789f58462f2ed Mon Sep 17 00:00:00 2001 From: vera-rubin <322284620+vera-rubin@users.noreply.github.com> Date: Tue, 1 Dec 2026 05:04:10 -0500 Subject: [PATCH 2/2] Key repetition helpers by the token filtering of their terminals Terminals compare by name only, so equal expressions can still differ in which tokens they filter out: through keep_all_tokens, or because an anonymous string resolves to a named terminal. Add each terminal's effective filter_out to the cache key instead of keep_all_tokens itself. Rules that filter the repeated expression the same way share a helper again, so a pinned rule and an ordinary rule with a common repeated prefix no longer collide in LALR. Move the test sizes off REPEAT_BREAK_THRESHOLD. --- lark/load_grammar.py | 17 ++++++++++++++--- tests/test_parser.py | 36 ++++++++++++++++++++++++++++++++++-- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/lark/load_grammar.py b/lark/load_grammar.py index b2f695a28..c554ecb51 100644 --- a/lark/load_grammar.py +++ b/lark/load_grammar.py @@ -243,8 +243,19 @@ def _add_rule(self, key, name, expansions): self.rules_cache[key] = t return t + def _token_filtering(self, expr: Union[Tree, Symbol]) -> Tuple[bool, ...]: + """Returns, for each terminal in expr, whether the current rule filters it out. + + Terminals compare by name only, so two equal expressions may still differ in + which tokens they keep. Helper rules may only be shared when this is the same. + """ + keep_all_tokens = bool(self.rule_options and self.rule_options.keep_all_tokens) + symbols = expr.scan_values(lambda v: True) if isinstance(expr, Tree) else [expr] + return tuple(sym.filter_out and not keep_all_tokens + for sym in symbols if isinstance(sym, Terminal)) + def _add_recurse_rule(self, type_: str, expr: Tree): - key = (expr, self.rule_options and self.rule_options.keep_all_tokens) + key = (expr, self._token_filtering(expr)) try: return self.rules_cache[key] except KeyError: @@ -268,7 +279,7 @@ def _add_repeat_rule(self, a, b, target, atom): new_rule: target target target atom atom atom atom """ - key = (a, b, target, atom, self.rule_options and self.rule_options.keep_all_tokens) + key = (a, b, target, atom, self._token_filtering(atom)) try: return self.rules_cache[key] except KeyError: @@ -301,7 +312,7 @@ def _add_repeat_opt_rule(self, a, b, target, target_opt, atom): | target target target atom atom atom """ - key = (a, b, target, atom, "opt", self.rule_options and self.rule_options.keep_all_tokens) + key = (a, b, target, atom, "opt", self._token_filtering(atom)) try: return self.rules_cache[key] except KeyError: diff --git a/tests/test_parser.py b/tests/test_parser.py index 8160cb3be..9195447e3 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -1733,8 +1733,8 @@ def test_keep_all_tokens(self): self.assertEqual(tree.children, ['a', 'a', 'a']) def test_repetition_keep_all_tokens(self): - for repeat, count in [('+', 2), ('*', 2), ('~50', 50), ('~0..50', 2)]: - if PARSER == 'cyk' and repeat in ('*', '~0..50'): + for repeat, count in [('+', 2), ('*', 2), ('~60', 60), ('~0..60', 2)]: + if PARSER == 'cyk' and repeat in ('*', '~0..60'): continue # CYK doesn't support empty rules rules = ['dropped: _A' + repeat, '!kept: _A' + repeat] for rule_order in (rules, rules[::-1]): @@ -1744,6 +1744,38 @@ def test_repetition_keep_all_tokens(self): self.assertEqual(dropped, Tree('dropped', [])) self.assertEqual(kept, Tree('kept', [Token('_A', 'a')] * count)) + def test_repetition_named_and_anonymous_terminal(self): + # "a" resolves to the terminal A, but only the anonymous use is filtered out + for repeat, count in [('+', 2), ('*', 2), ('~60', 60), ('~0..60', 2)]: + if PARSER == 'cyk' and repeat in ('*', '~0..60'): + continue # CYK doesn't support empty rules + rules = ['named: A' + repeat, 'anon: "a"' + repeat] + for rule_order in (rules, rules[::-1]): + with self.subTest(repeat=repeat, rules=rule_order): + l = _Lark('\n'.join(['start: named "," anon'] + rule_order + ['A: "a"'])) + named, anon = l.parse('a' * count + ',' + 'a' * count).children + self.assertEqual(named, Tree('named', [Token('A', 'a')] * count)) + self.assertEqual(anon, Tree('anon', [])) + + def test_repetition_shared_by_keep_all_tokens_rule(self): + # Both rules filter x and NAME the same way, so they must share the repetition. + # Otherwise, LALR can't decide between the two copies (Reduce/Reduce collision) + for repeat, count in [('+', 2), ('~60', 60), ('~2..60', 2)]: + if PARSER == 'cyk' and repeat == '~2..60': + continue # CYK doesn't support empty rules + for item, expected in [('x', Tree('x', [])), ('NAME', Token('NAME', 'x'))]: + with self.subTest(repeat=repeat, item=item): + l = _Lark("""start: a | b + a: %s "a" + !b: %s "b" + x: "x" + NAME: "x" + """ % (item + repeat, item + repeat)) + a, = l.parse('x' * count + 'a').children + self.assertEqual(a, Tree('a', [expected] * count)) + b, = l.parse('x' * count + 'b').children + self.assertEqual(b, Tree('b', [expected] * count + [Token('B', 'b')])) + def test_token_flags(self): l = _Lark("""!start: "a"i+