tree-sitter / tree-sitter-c

C grammar for tree-sitter
MIT License
237 stars 106 forks source link

Variable declaration and function call ambiguity #99

Open peckto opened 2 years ago

peckto commented 2 years ago

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?

jrwrigh commented 2 years ago

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