|
Thanks for this info. Still as a "Parser" it is still hard to decide
modified 19-Jan-21 21:04pm.
|
|
|
|
|
It depends on the language.
C: no expectation.
C++: no expectation.
C#: 21
Java: 21, I think?
|
|
|
|
|
C: no expectation
I think for C the case is most clear, no Operator "++" or "--" existed, so White spaces have been simply skipped.
Thanks for your Response.
modified 19-Jan-21 21:04pm.
|
|
|
|
|
Well no it's allowed, but modifying the same variable several times without sequence points in between is undefined
|
|
|
|
|
0x01AA wrote: or C the case is most clear, no Operator "++" or "--" existed Really?
|
|
|
|
|
But what is "+++" is it "+" followed by "++" or is it "++" by "+"?
Beforfe c++/c#: "- ---- --- - --x" was a correct Expression, white spaces could be ignored... only one aspect, when you Need to parse
modified 19-Jan-21 21:04pm.
|
|
|
|
|
Everyone seems to be distracted from the real issue.
Find the moron who wrote that abomination, and leave their head on a pike outside the castle walls as a warning to others.
Software Zen: delete this;
|
|
|
|
|
"Find the moron":
Yes, I mentioned it is not of practical use. But if you write a Parser/compliler you Need to decide how to handle it
modified 19-Jan-21 21:04pm.
|
|
|
|
|
As I see it, that shouldn't be a difficult thing for a compiler. Based on operator precedence rules, evaluate the expression. There isn't any real ambiguity that you'd have to worry about, if you follow the language spec.
If you were developing a style tool, on the other hand, the tool should write the coder's resignation letter for him and leave it at that.
Software Zen: delete this;
|
|
|
|
|
Thank you very much for your Response. "There isn't any real ambiguity...".
In C: "---", "- ", " -" is ok.
In c++?
It's only a doubt from my side, to decide which way to take
modified 19-Jan-21 21:04pm.
|
|
|
|
|
0x01AA wrote: if you write a Parser/compliler
then you can simply use 42 .
|
|
|
|
|
Sorry but you will be one of the first who compline if such an Expression can not be compiled correctly
modified 19-Jan-21 21:04pm.
|
|
|
|
|
I know how my compiler would handle this...
throw new EthicalException("The compiler refused to read this code, and suggested that the programmer be defenestrated.");
|
|
|
|
|
That sounds Logical, but sorry: If one states Operator precedence for a language and you do something like this....
modified 19-Jan-21 21:04pm.
|
|
|
|
|
It isn;t operator precedence that's the problem: it's that most languages specify the syntax, but don't specify when the operation should take place - in fact C# is the only language I know of that does: https://msdn.microsoft.com/en-us/library/aa691369(v=vs.71).aspx[^] As a result, using multiple pre- or post-fix operators in the same statement (or even expression) is effectively relying on side effects.
Which means that different compilers for the same language will produce different results (and in theory different version of the same compiler could!)
I have seen different results using different C compilers in the past, which is truly horrible. Defenestration is too good for 'em!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
As mentioned earlier, I am aware there is no practical significance. I simply asking me, how to solve that in my "own Parser".
modified 19-Jan-21 21:04pm.
|
|
|
|
|
Pick a set of rules, and be absolutely consistent. Document if expressions are evaluated right-to-left or left-to-right, and exactly when the various operations which make up pre- and post-fix operators are performed. It doesn't actually matter what rules you pick, as long as you define them and stick to them.
(Or better, ban the user from using more than one pre- or post-fix operator on the same variable in the same expression - create an error "ERR666: Cthulhu expressions are not permitted" )
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Thank you.
[Edit]
You are very right. The only Thing I feel uncomfortable is, before C++/C#, let us say at the time of "C" Whitespces" did not have such an affect (of course also that time whitespaces had some meaning). But nowadays one needs to pay more attention.
modified 19-Jan-21 21:04pm.
|
|
|
|
|
|
baked
modified 19-Jan-21 21:04pm.
|
|
|
|
|
I answered a very similar one a couple of days ago: How the expression works c= ++a + ++a;[^]
(I'm already working on an article / tip to cover this as it gets asked too often)
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Thank you very much for your reply. I'm looking Forward for your Article, please give me a hint when it is available.
I'm Aware it is not really from practical importance (swiss english ). But I think you know the feeling as a programmer to make something "correct until to the end"... and in this case, this is something for me which I struggle
Thanks again.
modified 19-Jan-21 21:04pm.
|
|
|
|
|
|
Great!
modified 19-Jan-21 21:04pm.
|
|
|
|
|
0x01AA wrote: what is the value of a=++a + ++a + ++a where a=5
Since this is undefined behavior (in C and C++), as others have said, global thermonuclear war would be as valid output as any other.
|
|
|
|