Skip to content

Grammar fix - #5

Merged
Frodan merged 2 commits into
Decurity:mainfrom
NinaTea:grammar-fix
Oct 18, 2026
Merged

Frodan merged 2 commits into
Decurity:mainfrom
NinaTea:grammar-fix

Conversation

@NinaTea

@NinaTea NinaTea commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

I'm working on a static analyzer for circom and ran into a precedence issue in this grammar: array access (a[i]) and member access (a.b) bind weaker than assignments and binary operators.

array_access_expression has no precedence (it is 0), and member_expression uses prec.dynamic(1), which only breaks ties at runtime and doesn't resolve conflicts when the parser is generated. Assignments have 1 and binary operators 2–7, so when the parser sees [ or . right after an operator, it closes the operation first and applies the access to the whole thing. For example,

div[k] <-- longdiv[0][k];

The tree today (each node shows the code it covers):

div[k] <-- longdiv[0][k]
├── div[k] <-- longdiv[0
│   ├── div[k] <-- longdiv
│   │   ├── div[k]
│   │   │   ├── div
│   │   │   └── k
│   │   └── longdiv
│   └── 0
└── k

And it is read as ((div[k] <-- longdiv)[0])[k]. But it should be:

div[k] <-- longdiv[0][k]
├── div[k]
│   ├── div
│   └── k
└── longdiv[0][k]
    ├── longdiv[0]
    │   ├── longdiv
    │   └── 0
    └── k

The fix

Gave both rules the highest precedence

  • PREC.MEMBER: from 1 to 14
  • member_expression: from prec.dynamic(1, ...) to prec.left(PREC.MEMBER, ...)
  • array_access_expression: wrapped in prec.left(PREC.MEMBER, ...)
  • Removed [$.member_expression, $._expression] from conflicts; precedence resolves it now and tree-sitter generate flagged it as unnecessary.
    -src/ was regenerated with tree-sitter-cli 0.23.0, the same version the current parser.c was generated with, to keep the diff to what actually changed.

Tests

Five corpus tests had the old, incorrect trees saved as their expected output, so I updated them with tree-sitter test --update

@Frodan
Frodan merged commit 75f447f into Decurity:main Oct 18, 2026
1 check passed
@Frodan

Frodan commented Oct 18, 2026

Copy link
Copy Markdown
Collaborator

Thank you for the fix, merged.

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