4 ms·
If you know you are expecting an expression, then it is easy for the parser to understand ‘-‘ as a unary negation if it is the first symbol, otherwise it must b
by zwkrt 7y ago
If you know you are expecting an expression, then it is easy for the parser to understand ‘-‘ as a unary negation if it is the first symbol, otherwise it must be a binary subtraction operator.
Expr: ‘-‘ Expr {
return -1 * $2;
}
| Expr ‘-‘ Expr {
return $1 - $2;
}
| ‘(‘ Expr ‘)’ {
return $2;
}
| ...