|I should add that a similar problem arises when one operator is a superset of another. For instance, I have "a" on the operand stack, and the input stream starts with "--". This is ambiguous: -- could be a post decrement on "a", a pre decrement on "b" (ruled out because it is immediately preceded by an operand token), or it could be "binary subtract" followed by "unary negate". "a--b" should only be tokenized as the last case: "a - (-b)". So, is there something other than an ad-hoc way, without looking some indefinite number of tokens ahead, to tell what this operator(s) is/are? I suppose I could split the execution stream (possibly with a child process?) and follow both until one gives an error. |
a--b... a shifted onto operand stack, leaving --b...
assume -- decrement operator
postfix? a-- is permissible. next token is an operand without a binary operator before it, so error, backtrack (abandon)
prefix? requires operator immediately before, so fails
binary subtract? eat one '-' as binary subtract, leaving -b...
next token is '-' but not '--': is either binary subtract or unary negate
binary doesn't work because an operator not ) or operand was immediately before
unary works... so far. now input is b...
Anyway, it gets rather ad-hoc to come up with all these rules and special cases to tell what '--' means, so I'm hoping there's something more elegant. Thanks!