Skip to content

feat(math): add context-free language operations (#1894) - #2051

Open
morluto wants to merge 1 commit into
mainfrom
agent/context-free-languages-1894
Open

feat(math): add context-free language operations (#1894)#2051
morluto wants to merge 1 commit into
mainfrom
agent/context-free-languages-1894

Conversation

@morluto

@morluto morluto commented Aug 18, 2026

Copy link
Copy Markdown
Owner

Summary

Implements the domain with 3 atomic operations for context-free grammar analysis.

Closes #1894

Operations

Operation Description
grammar.symbol_profiles.compute Compute nullable nonterminals via fixed-point iteration
grammar.dependency_graph.compute Compute the dependency graph (A -> B if A has a rule containing B)
grammar.first_sets.compute Compute FIRST sets via fixed-point iteration

Design

  • Fixed-point iteration: Nullable symbols and FIRST sets are computed via standard fixed-point iteration until convergence.
  • Exact combinatorial analysis: All operations use exact set operations over bounded grammars (≤32 nonterminals, ≤256 rules).
  • Dependency graph: Built directly from rule bodies, identifying which nonterminals appear in the body of which rules.

Continue this on Linzumi

Add context_free_languages_ops domain with 3 operations: nullable
symbol profiles, dependency graph, and FIRST sets. Uses fixed-point
iteration over bounded context-free grammars.

Closes #1894
@chatgpt-codex-connector

Copy link
Copy Markdown

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

morluto commented Aug 18, 2026

Copy link
Copy Markdown
Owner Author

Deep review summary

Verdict: REQUEST CHANGES — FIRST sets are wrong across nullable prefixes, and malformed grammar symbols can be treated as nullable.

1. first_sets.compute stops after the first nonterminal unconditionally

For a production S → A B, FIRST must continue into B whenever A is nullable. The implementation adds FIRST(A) and then always breaks. The branch intended to inspect nullability is unreachable because symbol in first is necessarily true for every recognized nonterminal.

Concrete counterexample:

S → A B
A → ε
B → b

The correct result is FIRST(S) = {b}. The implementation returns the empty set. More generally, if FIRST(A)={a,ε}, then FIRST(S) must include both a and FIRST(B)\{ε}.

Compute nullable first, then scan each production left-to-right: union the non-epsilon FIRST set of each nonterminal, continue only while it is nullable, stop at a terminal or nonnullable symbol, and represent whether the whole body derives epsilon explicitly.

2. Undeclared body symbols can make a head nullable

The grammar validator checks production heads but never requires every body symbol to belong to exactly one of the terminal or nonterminal alphabets. In compute_symbol_profiles, an unknown symbol is neither a terminal nor a known nonterminal, so the loop leaves all_nullable=True; a rule such as A → UNKNOWN incorrectly marks A nullable.

Require unique, disjoint terminal/nonterminal alphabets; every body symbol must be declared; start/head symbols must be canonical; and duplicate productions should be rejected or canonicalized. If a symbol appears in both alphabets, the current algorithms silently disagree about its role.

3. Result identity is positional and the new _ops domain duplicates an owner

FIRST/nullable tuples carry no nonterminal labels and depend on caller ordering. Return structured (nonterminal, value) rows or require canonical ordering. This should also extend the existing context-free-language owner rather than creating a parallel context_free_languages_ops namespace with overlapping semantics.

The dependency graph construction is correct once the grammar value is well formed.

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.

[Context-free languages] Add exact grammars, normal-form transforms, parse forests, derivations, and regular-restriction operations

1 participant