I’m having trouble trying to resolve some overlapping tokens regarding the minus sign: “-”. I have to satisfy the following criteria:
- It should act as a subtraction operator (example: “1 - 2 - 4”)
- 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.