Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variable declaration and function call ambiguity #99

Open
peckto opened this issue Apr 20, 2022 · 2 comments
Open

Variable declaration and function call ambiguity #99

peckto opened this issue Apr 20, 2022 · 2 comments

Comments

@peckto
Copy link

peckto commented Apr 20, 2022

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
Copy link

jrwrigh commented Jun 22, 2022

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

@jeff-hykin
Copy link

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

Just FYI this grammar will not ever handle this correctly. It intentionally only uses syntax (not semantics) and because of how C is designed: type vs var in cases like this require semantic information.

Similar cases are turbofish in C++ and Rust.

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

No branches or pull requests

3 participants