tree-sitter-c icon indicating copy to clipboard operation
tree-sitter-c copied to clipboard

Variable declaration and function call ambiguity

Open peckto opened this issue 2 years ago • 1 comments

Hi,

in the C language there is an ambiguity with the syntax T(a);:

  1. if T is a type, this is a variable declaration
  2. else, it is a function call

The tree-sitter c grammar does not handle this ambiguity correctly.

Example:

typedef int myInt;


int main() {
        int(a);
        myInt(d);
        return 0;
}
translation_unit [0, 0] - [9, 0]
  type_definition [0, 0] - [0, 18]
    type: primitive_type [0, 8] - [0, 11]
    declarator: type_identifier [0, 12] - [0, 17]
  function_definition [3, 0] - [7, 1]
    type: primitive_type [3, 0] - [3, 3]
    declarator: function_declarator [3, 4] - [3, 10]
      declarator: identifier [3, 4] - [3, 8]
      parameters: parameter_list [3, 8] - [3, 10]
    body: compound_statement [3, 11] - [7, 1]
      declaration [4, 8] - [4, 15]
        type: primitive_type [4, 8] - [4, 11]
        declarator: parenthesized_declarator [4, 11] - [4, 14]
          identifier [4, 12] - [4, 13]
      expression_statement [5, 8] - [5, 17]
        call_expression [5, 8] - [5, 16]
          function: identifier [5, 8] - [5, 13]
          arguments: argument_list [5, 13] - [5, 16]
            identifier [5, 14] - [5, 15]
      return_statement [6, 8] - [6, 17]
        number_literal [6, 15] - [6, 16]

int(a) is parsed as declaration myInt(d) is parsed as call_expression

Is this a desired limitation of tree-sitter, or is this a bug?

peckto avatar Apr 20 '22 12:04 peckto

Misunderstood your issue. Mine is different to yours, though kinda similar. I'll move it to a different issue.

jrwrigh avatar Jun 22 '22 22:06 jrwrigh