Parsing Doubt

T

Tarique

Hello.

#include<stdio.h>

int main(void)
{
int i=- -2;
printf("%d",i);
return 0;
}

The program above prints 2

int main(void)
{
int i=--2;
printf("%d",i);
return 0;
}

This one gives an obvious error ('--' needs l-value )

What i would like to know is that isn't int i=- -2;
supposed to mean the same as int i= --2;
Why does the white space become significant here?
 
M

Mark Bluemel

Tarique said:
What i would like to know is that isn't int i=- -2;
supposed to mean the same as int i= --2;
Why does the white space become significant here?

Because it distinguishes between two tokens denoting unary subtraction
operations ("- -") and a single token denoting an autodecrement
operation ("--").
 
D

dj3vande

(Language nitpick on the subject line: In most non-Indian dialects of
English, "doubt" doesn't make sense in this context; "question" is the
correct word to use.)

What i would like to know is that isn't int i=- -2;
supposed to mean the same as int i= --2;
Why does the white space become significant here?

When the code is broken up into tokens[1], the compiler will always
take the largest token that it recognizes. So when it sees `--', it
will never interpret that as two `-' operators; it will always treat it
as a single '--' operator. (For the same reason, if you say `---', it
will be tokenized as `--' followed by `-', and never `-' `-' `-' or `-'
`--'.)
When there's a space in between, that prevents the tokenizer from
recognizing a `--' (since the -- operator is spelled `--', without
whitespace in between), so it recognizes it as `-' `-' instead.

This is usually what you want, and in cases where it isn't, it's easier
for both the programmer and the compiler writer to have the programmer
break up tokens that should be distinct instead of having the compiler
try to guess what the programmer meant.


dave
(this does annoy C++ programmers who like to write nested templates, though.)


[1] Compiler geeks will be happy to tell you that this is not part of
parsing, but happens first; parsing takes the tokens (not the
original text that the tokens were extracted from) and identifies
the structure in their arrangement. Deciding what they actually
mean happens after parsing.
 
K

Kenneth Brody

For the same reason "a+++++b" is not the same as "a++ + ++b". In
fact, it's not even valid syntax. The correct term eludes me at
the moment, but it has to do with the fact that the parser wants
the longest input string to be used as a token, meaning that it
would be treated as if it were "a ++ ++ + b".
Because it distinguishes between two tokens denoting unary subtraction
operations ("- -") and a single token denoting an autodecrement
operation ("--").

Years ago, I used compilers which would have caused the above to
invoke UB, because "=-" was a version of the "-=" operator. (I
also used some compilers that gave a warning about the deprecated
use of such.)

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
R

Richard Heathfield

Kenneth Brody said:
For the same reason "a+++++b" is not the same as "a++ + ++b". In
fact, it's not even valid syntax. The correct term eludes me at
the moment,

Most people just call it "maximum munch".

<snip>
 
R

Richard Tobin

What i would like to know is that isn't int i=- -2;
supposed to mean the same as int i= --2;
Why does the white space become significant here?

This is a consequence of the definition of preprocessing token in the
standard: "--" is a preprocessing token, but "- -" isn't.
Preprocessing tokens become tokens which are then the input to the
production rule grammar.

-- Richard
 
E

Eric Sosman

Tarique said:
Hello.

#include<stdio.h>

int main(void)
{
int i=- -2;
printf("%d",i);
return 0;
}

The program above prints 2

int main(void)
{
int i=--2;
printf("%d",i);
return 0;
}

This one gives an obvious error ('--' needs l-value )

What i would like to know is that isn't int i=- -2;
supposed to mean the same as int i= --2;
Why does the white space become significant here?

White space separates tokens. `long int' has two
tokens, `longint' has one token with an entirely different
meaning. `- -' has two tokens, `--' has one token with an
entirely different meaning.
 
C

CBFalconer

Mark said:
Because it distinguishes between two tokens denoting unary
subtraction operations ("- -") and a single token denoting an
autodecrement operation ("--").

Which points to the useful practice "Always separate operators with
spaces".
 
T

Tarique

Tarique said:
#include<stdio.h>

int main(void)
{
int i=- -2;
printf("%d",i);
return 0;
} ...snip...

The program above prints 2
What i would like to know is that isn't int i=- -2;
supposed to mean the same as int i= --2;
Why does the white space become significant here?

Thank You everybody.This was a trick question i found somewhere.I marked
the wrong option for the output of the program - "Compiler Error"
When i ran the program it produced 2 :( and hence i was confused!
Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top