How should I structure my Grammar to use "-" for negative expressions and as a subtraction operator?

I’m having trouble trying to resolve some overlapping tokens regarding the minus sign: “-”. I have to satisfy the following criteria:

  1. It should act as a subtraction operator (example: “1 - 2 - 4”)
  2. It should also be used to allow negative values (example: “-2”)
    2a. It will eventually support negative values for other expressions (example: “-someNumFunc()” or “-someNumberSystemVariable”)

The main issue is that it becomes ambiguous when I am trying to put case 1 and case 2 together. For example, I want to support this as a valid syntax: “1 - 2 - -4”.

It should resolve the syntax tree to something like this:

                Program(
                    BinaryExpression(
                        BinaryExpression(
                            IntegerLiteral,
                            ArithOp,
                            IntegerLiteral
                        ),
                        ArithOp,
                        NegationExpression(IntegerLiteral)
                    )
                )
@precedence {
    add @left
}

@top Program { resolvedNode* }

@skip { space }

resolvedNode {
    IntegerLiteral |
    NegationExpression |
    BinaryExpression
}

NegationExpression {
    "-" resolvedNode
}

BinaryExpression {
    resolvedNode !add ArithOp<"+" | "-"> resolvedNode
}

@tokens {
    space { @whitespace+ }
    IntegerLiteral { @digit+ }
    ArithOp<expr> { expr }
    
    @precedence { IntegerLiteral, ArithOp }
}

@detectDelim

I was trying to see if ambiguity markers would solve my problem but I wasn’t sure how to use it syntatically for my case, or if it would even help.

Just make sure you use the same token for the binary and unary case (for example, instead of an ArithOp<"-" | "+"> token, just use raw "-" | "+" literal tokens, or give them a name and use the same Minus token in both situations).

Ah, thanks for the quick reply. I got by with using ArithOp<“-”> for both of them.