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

C Syntax Tree needs to be Updated #129

Closed
Raghava-Ch opened this issue Apr 10, 2023 · 3 comments
Closed

C Syntax Tree needs to be Updated #129

Raghava-Ch opened this issue Apr 10, 2023 · 3 comments

Comments

@Raghava-Ch
Copy link

C Language syntax tree hierarchy should be modified for the functions which returns pointer.
C function definition with pointer type nodes are expected to be part of the "type: primitive_type". But in current implementation pointer declarator out of the primitive type node as shown in the example below.

Because of this node entry which is out of the primitive type common query to list all the function definitions is not able to identify it as a function definition.

example function definition:

const char ** fn_returns_pointer() { }

parsed AST:

translation_unit [0, 0] - [3, 0]
  function_definition [0, 0] - [2, 1]
    type_qualifier [0, 0] - [0, 5]
    type: primitive_type [0, 6] - [0, 10]
    declarator: pointer_declarator [0, 11] - [0, 34] ---------------------
      declarator: pointer_declarator [0, 12] - [0, 34]                   | these pointer_declarators are not expected to be here
        declarator: function_declarator [0, 14] - [0, 34]-----------------
          declarator: identifier [0, 14] - [0, 32]
          parameters: parameter_list [0, 32] - [0, 34]
    body: compound_statement [1, 0] - [2, 1]
@touzeauv
Copy link

touzeauv commented May 15, 2024

I just had a look at the grammar, intending to fix this issue (and issue #126 which I think is related).

After exploring a bit the grammar and the C syntax, I believe the behavior of tree-sitter is actually correct in the sense that it produces a concrete syntax tree that is closer to what the syntax expressed.

I think it is useful to consider the following example:

int* x, y;

In C, this declares a pointer (x) to an int, and an int (y), i.e. the "star" only apply to x.

Although this is very uncommon, it is possible to use a similar syntax to declare functions:

int* f(), g();
// Equivalent to
int* f();
int g();  // <- Note the return type

I thus believe grouping the "star" with the type specifier is incorrect because the type specifier might be common to several declarators. Having one type specifier per declarator might be useful and more intuitive, but the result would be closer to an AST than a CST.

@Raghava-Ch
Copy link
Author

Raghava-Ch commented May 20, 2024

In fact I realised what @touzeauv is correct, since tree-sitter is intended to use it as syntax highlighter.

But I am using tree-sitter for the code analysis because it has flexible license and easy to expand for the other languages. Since my requirements are a little different than the actual tree-sitter purpose.

Anyway there is no other library like tree-sitter, If it can serve the complete computer languages requirements it would be great.

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