Skip to content

bug: Parse error with >= operator #311

@alexle0nte

Description

@alexle0nte

Did you check existing issues?

  • I have read all the tree-sitter docs if it relates to using the parser
  • I have searched the existing issues of tree-sitter-cpp

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

tree-sitter 0.25.3 (2a835ee029dca1c325e6f1c01dbce40396f6123e)

Describe the bug

In the following code snippet

int main() {
    int a = 0, b = 1, c = 2, d = 3, e = 4;

    if (a || (b < c && e >= d)) { /* ... */ }

    return 0;
}

The parse tree does not correctly identify the binary expression e >= d and reports = as the operator instead of >=.
Note that if we replace >= with <=, the error does not occur.

Steps To Reproduce/Bad Parse Tree

  1. Write the code above in the playground Code section.
  2. Observe the obtained parse tree:
translation_unit [0, 0] - [7, 0]
  function_definition [0, 0] - [6, 1]
    type: primitive_type [0, 0] - [0, 3]
    declarator: function_declarator [0, 4] - [0, 10]
      declarator: identifier [0, 4] - [0, 8]
      parameters: parameter_list [0, 8] - [0, 10]
        ( [0, 8] - [0, 9]
        ) [0, 9] - [0, 10]
    body: compound_statement [0, 11] - [6, 1]
      { [0, 11] - [0, 12]
      declaration [1, 4] - [1, 42]
        type: primitive_type [1, 4] - [1, 7]
        declarator: init_declarator [1, 8] - [1, 13]
          declarator: identifier [1, 8] - [1, 9]
          = [1, 10] - [1, 11]
          value: number_literal [1, 12] - [1, 13]
        , [1, 13] - [1, 14]
        declarator: init_declarator [1, 15] - [1, 20]
          declarator: identifier [1, 15] - [1, 16]
          = [1, 17] - [1, 18]
          value: number_literal [1, 19] - [1, 20]
        , [1, 20] - [1, 21]
        declarator: init_declarator [1, 22] - [1, 27]
          declarator: identifier [1, 22] - [1, 23]
          = [1, 24] - [1, 25]
          value: number_literal [1, 26] - [1, 27]
        , [1, 27] - [1, 28]
        declarator: init_declarator [1, 29] - [1, 34]
          declarator: identifier [1, 29] - [1, 30]
          = [1, 31] - [1, 32]
          value: number_literal [1, 33] - [1, 34]
        , [1, 34] - [1, 35]
        declarator: init_declarator [1, 36] - [1, 41]
          declarator: identifier [1, 36] - [1, 37]
          = [1, 38] - [1, 39]
          value: number_literal [1, 40] - [1, 41]
        ; [1, 41] - [1, 42]
      if_statement [3, 4] - [3, 45]
        if [3, 4] - [3, 6]
        condition: condition_clause [3, 7] - [3, 31]
          ( [3, 7] - [3, 8]
          value: binary_expression [3, 8] - [3, 30]
            left: identifier [3, 8] - [3, 9]
            operator: || [3, 10] - [3, 12]
            right: parenthesized_expression [3, 13] - [3, 30]
              ( [3, 13] - [3, 14]
              assignment_expression [3, 14] - [3, 29]
                left: template_function [3, 14] - [3, 26]
                  name: identifier [3, 14] - [3, 15]
                  arguments: template_argument_list [3, 16] - [3, 26]
                    < [3, 16] - [3, 17]
                    binary_expression [3, 18] - [3, 24]
                      left: identifier [3, 18] - [3, 19]
                      operator: && [3, 20] - [3, 22]
                      right: identifier [3, 23] - [3, 24]
                    > [3, 25] - [3, 26]
                operator: = [3, 26] - [3, 27]          <---------- Incorrectly shows = as the operator
                right: identifier [3, 28] - [3, 29]
              ) [3, 29] - [3, 30]
          ) [3, 30] - [3, 31]
        consequence: compound_statement [3, 32] - [3, 45]
          { [3, 32] - [3, 33]
          comment [3, 34] - [3, 43]
          } [3, 44] - [3, 45]
      return_statement [5, 4] - [5, 13]
        return [5, 4] - [5, 10]
        number_literal [5, 11] - [5, 12]
        ; [5, 12] - [5, 13]
      } [6, 0] - [6, 1]

Expected Behavior/Parse Tree

The parse tree should correctly interpret the e >= d binary expression and identify >= as the operator.

Repro

int main() {
    int a = 0, b = 1, c = 2, d = 3, e = 4;

    if (a || (b < c && e >= d)) { /* ... */ }

    return 0;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions