tree-sitter / tree-sitter-c

C grammar for tree-sitter
MIT License
251 stars 108 forks source link

bug: Ambiguity in the meaning of identifiers #240

Closed huanie closed 3 weeks ago

huanie commented 4 weeks ago

Did you check existing issues?

Tree-Sitter CLI Version, if relevant (output of tree-sitter --version)

No response

Describe the bug

declaration or expression statement?

typedef int T;
void f(void) {
  T * b;
}
int T, b;
void f(void) {
  T * b;
}

Steps To Reproduce/Bad Parse Tree

(translation_unit
 (function_definition type: (primitive_type)
  declarator: 
   (function_declarator declarator: (identifier)
    parameters: 
     (parameter_list (
      (parameter_declaration type: (primitive_type))
      )))
  body: 
   (compound_statement {
    (declaration type: (type_identifier)
     declarator: (pointer_declarator * declarator: (identifier))
     ;)
    (return_statement return (number_literal) ;)
    })))

Expected Behavior/Parse Tree

(translation_unit
 (function_definition type: (primitive_type)
  declarator: 
   (function_declarator declarator: (identifier)
    parameters: 
     (parameter_list (
      (parameter_declaration type: (primitive_type))
      )))
  body: 
   (compound_statement {
    (expression_statement
     (binary_expression left: (identifier) operator: * right: (identifier))
     ;)
    (return_statement return (number_literal) ;)
    })))

Repro

int main(void)
{
  a * b;
  return 0;
}
amaanq commented 3 weeks ago

when it comes to true ambiguous situations like this one, you need context-aware information about the source code beforehand, which tree-sitter does not have that a compiler like clang does. this can't be solved.