Repository navigation
Conversation
…on leaders Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
To pick a leader for a left-recursive SCC, pegen walked every path from every node in the SCC. The number of paths grows factorially with the SCC size. A leader is a rule that sits on every cycle, so we can test each candidate directly: remove it and check whether what's left is acyclic.
Overall that's O(V·(V+E)) per SCC. It picks the same leader as before (smallest name that lies on every cycle) and raises the same error.
find_cycles_in_scchad no other callers, so I removed it.CPython's own grammar barely notices this, because its biggest SCC has two rules. Generated
Parser/parser.cis byte-identical before and after. Grammars with wider mutual left recursion hit a wall, though.Evidence
Parser generation, median of 7, release build, macOS arm64.
nis the number of mutually left-recursive rules:Grammar/python.gramNew test
test_left_recursion_analysis_workcounts name comparisons on a fully connected 8-rule grammar that has no leader:AssertionError: 1067658 not less than 2000Also added
test_left_recursion_leader_order(leader choice stays the same) andtest_large_left_recursive_grammar(12-rule SCC still generates a working parser).I checked the old and new
compute_left_recursivesagainst each other on all 512 three-rule graphs and 256 random four-rule graphs. Flags, SCCs and errors matched every time.test_peg_generator -u cpu: 125 run, all pass.Merge Danger
Door: two-way
Build-time tooling only. Generated parser output is unchanged.
Blast Radius: tooling
This only matters to people running pegen on their own grammars. For them, leader choice and error messages stay the same.
AI disclosure: the profiling, the patch and the equivalence check were done with AI tools (Codex, Claude).
🤖 Generated with Claude Code