Skip to content

Check that a symbol of an indexed family is not also used alone - #16

Open
aleksisch wants to merge 1 commit into
SparseLinearAlgebra:homka122/CFL_optimizationsfrom
aleksisch:cfl-check-indexed-symbols
Open

aleksisch wants to merge 1 commit into
SparseLinearAlgebra:homka122/CFL_optimizationsfrom
aleksisch:cfl-check-indexed-symbols

Conversation

@aleksisch

Copy link
Copy Markdown

LAGraphX.h documents a constraint on indexed rules: "1) If symbol A is indexed in one rule, it
must be indexed in all rules where it appears."
LAGraph_CFL_reachability_adv does not check it.
A grammar that breaks it gets a correct answer without OPT_BLOCK, and a silently wrong one with
it.

1. See it

cat > bug.c <<'EOF'
#include <stdio.h>
#include <stdlib.h>
#include <LAGraph.h>
#include <LAGraphX.h>

int main(int argc, char **argv) {
    int8_t opt = argc > 1 ? (int8_t)atoi(argv[1]) : 1;
    char msg[LAGRAPH_MSG_LEN];
    LAGraph_Init(msg);
    // symbols: S=0, C_0..C_1=1..2, D_0..D_1=3..4, X=5, [_0=6, [_1=7, ]_0=8, ]_1=9
    enum { NI = LAGraph_EWNCF_INDEX_NONTERM, PA = LAGraph_EWNCF_INDEX_PROD_A, PB = LAGraph_EWNCF_INDEX_PROD_B };
    LAGraph_rule_EWCNF rules[] = {
        {5, 6, -1, 0, 0},       // X   -> [_0   ([_0 used alone: breaks constraint 1)
        {0, 1, 3, 2, PA | PB}, // S   -> C_i D_i
        {1, 6, -1, 2, NI | PA}, // C_i -> [_i
        {3, 8, -1, 2, NI | PA}, // D_i -> ]_i
    };
    GrB_Matrix adj[10], out[10] = {0};
    for (int i = 0; i < 10; i++) GrB_Matrix_new(&adj[i], GrB_BOOL, 3, 3);
    GrB_Matrix_setElement_BOOL(adj[7], true, 0, 1); // 0 -[_1-> 1
    GrB_Matrix_setElement_BOOL(adj[9], true, 1, 2); // 1 -]_1-> 2
    GrB_Info info = LAGraph_CFL_reachability_adv(out, adj, 10, rules, 4, msg, opt);
    bool s = false;
    if (info == GrB_SUCCESS) GrB_Matrix_extractElement_BOOL(&s, out[0], 0, 2);
    printf("optimizations=%d: info=%d S(0,2)=%d\n", opt, (int)info, s);
    return 0;
}
EOF
gcc bug.c -o bug -I$GRAPHBLAS/include/suitesparse -Iinclude -Lbuild/experimental -Lbuild/src \
    -llagraphx -llagraph -L$GRAPHBLAS/lib -lgraphblas \
    -Wl,-rpath,build/experimental:build/src:$GRAPHBLAS/lib

Before this change:

$ ./bug 1      # OPT_EMPTY
optimizations=1: info=0 S(0,2)=1
$ ./bug 9      # OPT_EMPTY | OPT_BLOCK
optimizations=9: info=0 S(0,2)=0

After it, both runs return GrB_INVALID_VALUE with the message
Symbol 6 is used both in an indexed family and alone.:

$ ./bug 1
optimizations=1: info=-3 S(0,2)=0
$ ./bug 9
optimizations=9: info=-3 S(0,2)=0

2. The change

  • find_mixed_symbol marks every symbol as used alone and/or as a member of an indexed family
    (base .. base+indexed_count-1), and returns one symbol that is both.
  • LAGraph_CFL_reachability_adv calls it right after the existing rule validation, and returns
    GrB_INVALID_VALUE when such a symbol exists. Valid grammars behave exactly as before.
  • New test CFL_reachability_mixed_indexed_symbol in test_CFL_reachability_adv.c. With the
    rule X -> [_0 it expects the error for all 16 masks; without that rule it expects (0, 2) for
    all 16 masks.

3. Why OPT_BLOCK goes wrong

With OPT_BLOCK, get_new_symbols gives each symbol one representation: the one from the first
rule that mentions it. X -> [_0 comes first, so [_0 becomes a plain symbol, and the block for
the family [_i is never built. C_i -> [_i then reads only [_0, and the pair over [_1 ]_1
is lost without an error. With other rule orders the same grammar fails with -3 from a later
step, or gives the plain rule the whole block.

Testing

test_CFL_reachability_adv:

  • The new test fails without the change, and passes with it.
  • 20 of the 21 tests pass.
  • CFG_reachability_null_pointers fails on strstr(msg, "Adjacency matrices with these indexes are null: 0, 1"). It fails the same way on the branch without this change.

LAGraphX.h documents constraint 1 of indexed rules: a symbol indexed in one
rule must be indexed in every rule where it appears. It was not checked: a
grammar that breaks it gets a correct answer without OPT_BLOCK and a silently
wrong one with it. LAGraph_CFL_reachability_adv now returns GrB_INVALID_VALUE
for such a grammar, right after the existing rule validation.
@aleksisch aleksisch closed this Nov 25, 2026
@aleksisch aleksisch reopened this Nov 25, 2026
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.

1 participant