Conversation
MergeEquivalentStates merges states that are reached from the same single predecessor through identical edges, assuming they are reached by identical prefix sets. The start state is additionally reached by the empty prefix, so merging it with a sibling wrongly exposes the sibling's suffixes at the start. For example, the FSM of `root ::= [a]* "a" "b" "c"` (language a+bc) accepted "bc", and the FSM of the regex (ab)*abc accepted "c" after merging.
Contributor
There was a problem hiding this comment.
Pull request overview
Note
Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.
Fixes an FSM minimization bug where the start state could be incorrectly merged with an equivalent-prefix sibling state, causing the resulting automaton to accept strings outside the grammar/regex language (especially when the start has incoming edges due to loops).
Changes:
- Prevents
FSMWithStartEnd::MergeEquivalentStates“equivalent successor” merging from considering the start state on either side of the candidate pair. - Adds C++ regression tests covering both a minimal constructed FSM and regex-derived FSMs with start-state loops.
- Adds an end-to-end Python regression test covering an EBNF grammar with a leading
[*]construct.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
cpp/fsm.cc |
Excludes the start state from the incoming-edge-based merge rule that was invalid for the start state. |
tests/cpp/test_fsm.cc |
Adds regression tests demonstrating the incorrect acceptance and ensuring the start state stays unmerged. |
tests/python/test_grammar_matcher_ebnf.py |
Adds an end-to-end regression test to prevent accepting "bc" for root ::= [a]* "a" "b" "c". |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
29
to
+34
| grammar = xgr.Grammar.from_ebnf(grammar_str) | ||
| assert _is_grammar_accept_string(grammar, "bab") | ||
| assert not _is_grammar_accept_string(grammar, "abb") | ||
| assert _is_grammar_accept_string(grammar, "cab") | ||
|
|
||
|
|
||
| def test_char_class_star_prefix_requires_first_char(): |
Comment on lines
+545
to
+551
| // States 1 and 2 are still merged. | ||
| EXPECT_EQ(merged.NumStates(), 4); | ||
| EXPECT_TRUE(merged.AcceptString("abc")); | ||
| EXPECT_TRUE(merged.AcceptString("ababc")); | ||
| EXPECT_FALSE(merged.AcceptString("c")); | ||
| EXPECT_FALSE(merged.AcceptString("ab")); | ||
| EXPECT_FALSE(merged.AcceptString("")); |
15 tasks
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
FSMWithStartEnd::MergeEquivalentStatesmerges sibling states that are reached from the same single predecessor through identical edges, on the assumption that they are reached by exactly the same set of prefixes and can therefore share one node. This assumption does not hold for the start state: it is additionally reached by the empty prefix, which is invisible to the incoming-edge comparison. When the start state has incoming edges (any loop back to the start, e.g. a leading[...]*element or a leading starred group), it could be merged with a sibling state, which wrongly exposes the sibling's outgoing paths at the very beginning of matching. The resulting FSM accepts strings outside the grammar's language.Reproductions with the current main:
root ::= [a]* "a" "b" "c"(languagea+bc): the compiled rule FSM accepts"bc".(ab)*abc(viaRegexFSMBuilder::Build+SimplifyEpsilon+MergeEquivalentStates): the merged FSM accepts"c".The fix excludes the start state from this merge rule, on both sides of the candidate pair. The other merge rules are unaffected because they are justified by equal suffix languages (identical outgoing edges to a common single successor, and leaf-state merging), which stay valid for the start state.
Test
MergeEquivalentStatesKeepsStartStateUnmerged(minimal 5-state shape) andMergeEquivalentStatesStartStateWithSelfLoop([a]*abcand(ab)*abcthrough the regex pipeline). All 63 C++ tests pass.test_char_class_star_prefix_requires_first_charcoveringroot ::= [a]* "a" "b" "c"through the grammar matcher.test_grammar_matcher_ebnf.py,test_grammar_matcher_basic.py,test_grammar_matcher_regex.py,test_grammar_matcher_json.py,test_grammar_matcher_json_schema.pyall pass (323 tests).