tree-sitter / tree-sitter/tree-sitter-cpp

bug: not parsing well pointer to an array

Open
#288 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
JavaScript
Stars
453
Forks
185
PR merge metrics
No merged PRs in 30d

Description

Did you check existing issues?
  • I have read all the tree-sitter docs if it relates to using the parser
  • I have searched the existing issues of tree-sitter-cpp
Tree-Sitter CLI Version, if relevant (output of tree-sitter --version)

No response

Describe the bug

tree-sitter-cpp doesn't parse well pointer to array (Repro and the resulting Bad Parse Tree).
Worth to mention that parsing it as part of multi declaration statement is actually done right:

int (*x)[] = 0, y = 1;

Will be parsed to:

declaration [0, 0] - [0, 22]
    type: primitive_type [0, 0] - [0, 3]
    declarator: init_declarator [0, 4] - [0, 14]
      declarator: array_declarator [0, 4] - [0, 10]
        declarator: parenthesized_declarator [0, 4] - [0, 8]
          pointer_declarator [0, 5] - [0, 7]
            declarator: identifier [0, 6] - [0, 7]
      value: number_literal [0, 13] - [0, 14]
    declarator: init_declarator [0, 16] - [0, 21]
      declarator: identifier [0, 16] - [0, 17]
      value: number_literal [0, 20] - [0, 21]
Steps To Reproduce/Bad Parse Tree
expression_statement [1, 0] - [1, 11]
    subscript_expression [1, 0] - [1, 10]
      argument: call_expression [1, 0] - [1, 8]
        function: primitive_type [1, 0] - [1, 3]
        arguments: argument_list [1, 4] - [1, 8]
          pointer_expression [1, 5] - [1, 7]
            argument: identifier [1, 6] - [1, 7]
      indices: subscript_argument_list [1, 8] - [1, 10]
Expected Behavior/Parse Tree
declaration [0, 0] - [0, 27]
    type: primitive_type [0, 0] - [0, 3]
    declarator: init_declarator [0, 4] - [0, 14]
      declarator: array_declarator [0, 4] - [0, 10]
        declarator: parenthesized_declarator [0, 4] - [0, 8]
          pointer_declarator [0, 5] - [0, 7]
            declarator: identifier [0, 6] - [0, 7]
      value: number_literal [0, 13] - [0, 14]
Repro
int (*x)[];

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reported Repro, int (*x)[];, and compare its current parse tree with the expected declaration tree. Trace the C++ grammar rules involved in pointer and array declarators, then confirm that this input produces the expected tree without regressing the multi-declaration example.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.