Skip to content

[Fix] Do not merge the FSM start state with equivalent-prefix states - #752

Open
Ubospica wants to merge 1 commit into
mlc-ai:mainfrom
Ubospica:fix/merge-equivalent-start-state
Open

Ubospica wants to merge 1 commit into
mlc-ai:mainfrom
Ubospica:fix/merge-equivalent-start-state

Conversation

@Ubospica

Copy link
Copy Markdown
Collaborator

Summary

FSMWithStartEnd::MergeEquivalentStates merges 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" (language a+bc): the compiled rule FSM accepts "bc".
  • Regex pipeline (ab)*abc (via RegexFSMBuilder::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

  • New C++ regression tests MergeEquivalentStatesKeepsStartStateUnmerged (minimal 5-state shape) and MergeEquivalentStatesStartStateWithSelfLoop ([a]*abc and (ab)*abc through the regex pipeline). All 63 C++ tests pass.
  • New end-to-end Python test test_char_class_star_prefix_requires_first_char covering root ::= [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.py all pass (323 tests).

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.
@Ubospica
Ubospica requested a review from Seven-Streams as a code owner July 28, 2026 13:55
Copilot AI review requested due to automatic review settings July 28, 2026 13:55
@Ubospica
Ubospica requested a review from DarkSharpness as a code owner July 28, 2026 13:55

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 thread tests/cpp/test_fsm.cc
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(""));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants