|
The question has been posted and answered too many times already. The explanation can be found by Google if you really care.
|
|
|
|
|
|
42, half of.
Will be interesting to see whether there's any variation of results among all of these:
VS 2015, 2013, 2010, 2008, 2005, 2003, VC 6, VC 4 VC 1.5 (16-bit), GCC, cc (on iOS Yosemite downwards), Unix compilers (all flavours over the years), cc (on Linux over the years), ...
But, the big question - who will do such a test? Which of these compilers gives wrong results?
|
|
|
|
|
You are so right! But in case you have to write the Parser how you do it?
modified 19-Jan-21 21:04pm.
|
|
|
|
|
Writing a parser - not me.
Too tough for a 50 year old educated in Mechanical Engineering
|
|
|
|
|
Avijnata wrote: Which of these compilers gives wrong results?
All the results are correct.
I can pass it through HP C and Borland C/C++
|
|
|
|
|
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.
|
|
|
|
|