DEV Community

Ryan Bae
Ryan Bae

Posted on

Question: How do you Handle unary operators (specifically right-associative ones)?

I made my own parser, but I keep going to this same bump: chained unary subtraction and inversion/negation (- and !) are REALLY, REALLY hard to implement. Something like ---2 is lexed as -u -u -u 2, but the problem is; how do you negate a negation? I've made a pseudo-answer by marking their indexes in a dictionary and optimizing it so that -u -u -u becomes just -u, but thats more of a short-cut and also it still doesn't really solve the problem. Even if I optimized it, I would get 2 for the number list and -u in the operator list (in my parser), and the limitation for my parser is that the operator list is one element less than the number list (because it goes term operator term operator, etc).

Top comments (0)