Negative Numbers and negated expressions

SyntaxEditor for WPF Forum

Posted 2 years ago by Laif Harwood
Version: 17.2.0
Avatar

Are there any recommendations or examples on how to deal with negative numbers and negated expressions?

int a = -1

int b = -(1  + 1)

Should the first example be handled in a regex number pattern? Or should both cases be handled in an expression production? I'm playing around with different options but thought I'd check and see if there were any existing examples or recommendations. Thanks!

Comments (1)

Posted 2 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Laif,

We would generally lex the "-" as an operator token and the "1" as a number.  That way, the negative operator token can be used to drive a unary operator expression in your parser grammar and would work for both scenarios you list.  The first scenario would be a unary operator expression with negative operator around a child literal expression.  The second scenario would be a unary operator expression with negative operator around a child binary operator expression with addition operator, and that expression would have two child literal expressions.

Our Getting Started #4 QuickStarts show a Simple language grammar that has some expressions in it.  It shows some binary expressions and number expressions.  The unary operator expression would need to be added though.


Actipro Software Support

The latest build of this product (v24.1.2) was released 2 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.