Whole file
lark-parser/lark
The author described this change as “BUGFIX: Fixes a subtle bug in the LALR(1) parser. See the new test for details.”. It counts as a record because the checks below fail on the code as it stood at e20b54be2 and pass on 64d141e48, with nothing else changed between the two runs.
Projectlark-parser/lark
Fix saved2017-05-22
Sharing licenceMIT · LICENSE
Change size+3 −3
What the code was meant to do, written into the code itself as a save note
BUGFIX: Fixes a subtle bug in the LALR(1) parser. See the new test for details.
The change
| 34 | 34 | ||
| 35 | 35 | raise UnexpectedToken(token, expected, seq, i) | |
| 36 | 36 | ||
| 37 | - | def reduce(rule, size): | |
| 37 | + | def reduce(rule, size, end=False): | |
| 38 | 38 | if size: | |
| 39 | 39 | s = value_stack[-size:] | |
| 40 | 40 | del state_stack[-size:] | |
| ⋯ | |||
| 44 | 44 | ||
| 45 | 45 | res = self.callbacks[rule](s) | |
| 46 | 46 | ||
| 47 | - | if len(state_stack) == 1 and rule.origin == self.analysis.start_symbol: | |
| 47 | + | if end and len(state_stack) == 1 and rule.origin == self.analysis.start_symbol: | |
| 48 | 48 | return res | |
| 49 | 49 | ||
| 50 | 50 | _action, new_state = get_action(rule.origin) | |
| ⋯ | |||
| 73 | 73 | while True: | |
| 74 | 74 | _action, rule = get_action('$end') | |
| 75 | 75 | assert _action == 'reduce' | |
| 76 | - | res = reduce(*rule) | |
| 76 | + | res = reduce(*rule, end=True) | |
| 77 | 77 | if res: | |
| 78 | 78 | assert state_stack == [self.analysis.init_state_idx] and not value_stack, len(state_stack) | |
| 79 | 79 | return res | |
The check that tells the two apart
fail→pass·tests/test_parser.py::TestLalrContextual::test_reduce_cycle
fail→pass·tests/test_parser.py::TestLalrStandard::test_reduce_cycle
Check file tests/test_parser.py, taken without changes from the fix and copied onto the older code, so the exact same check runs against both versions.
Origin and history
The code before ite20b54be2fa9e6594a10bdd9cc8c36a3ffe46bc0
Broken version dated2017-05-06
Modulelark.parsers.lalr_parser
Units changedParser
Fingerprint3b575f8fce448ac4
Checked2026-08-18 by goldset/0.1
Every field above is generated by our program. None of it is written by hand.
Other bugs found in lark-parser/lark
- 2026-07-08Fix lexer_callbacks silently dropped for keyword terminals
- 2026-06-24Transformer_NonRecursive
- 2026-06-04Raise GrammarError for non-terminal names in %declare
- 2026-06-02Fix empty SPPF node from xearley ignore carry-over (fixes #1598)
- 2026-05-24fix: raise GrammarError for template usage inside terminals
- 2024-10-25Adjust behaviour in #1481 to raise a NotImplementedError