473,394 Members | 1,740 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Parsing Doubt

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?
Jan 17 '08 #1
8 1521
Tarique wrote:
>
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 ("--").
Jan 17 '08 #2
(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.)

In article <fm**********@aioe.org>, Tarique <pe*****@yahoo.comwrote:
>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.

--
Dave Vandervies dj3vande at eskimo dot com
Probably still cooler than where daemons come from, so we may still be able
to say "linux has a cooler mascot than BSD" with a straight face.
--Ingvar the Grey in the scary devil monastery
Jan 17 '08 #3
Mark Bluemel wrote:
>
Tarique wrote:

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?
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:Th*************@gmail.com>

Jan 17 '08 #4
Kenneth Brody said:
Mark Bluemel wrote:
>>
Tarique wrote:
>
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?

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>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jan 17 '08 #5
In article <fm**********@aioe.org>, Tarique <pe*****@yahoo.comwrote:
>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
--
:wq
Jan 17 '08 #6
Tarique wrote:
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.

--
Er*********@sun.com
Jan 17 '08 #7
Tarique wrote:
#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.
Jan 18 '08 #8
Mark Bluemel wrote:
Tarique wrote:
>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 ("--").
Which points to the useful practice "Always separate operators with
spaces".

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Jan 18 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Luis P. Mendes | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I only know a little bit of xml and I'm trying to parse a xml document in order to save its elements in a file (dictionaries inside a list)....
12
by: Simone Mehta | last post by:
hi All, I am parsing a CSV file. I want to read every row into a char array of reasonable size and then extract strings from it. <snippet> char foo="hello,world,bye,bye,world"; ........
2
by: James D. Marshall | last post by:
One question on parsing, the line has double carriage returns that show up as squares, I am seeing this through watch1, how would you code a replace statement to get rid of these, its driving me...
26
by: SL33PY | last post by:
Hi, I'm having a problem parsing strings (comming from a flat text input file) to doubles. the code: currentImportDetail.Result = CType(line.Substring(7, 8).Trim(" "), System.Double) What...
5
by: _DS | last post by:
I'm currently using a switch with about 50 case statements in a stretch of code that's parsing XML attributes. Each case is a string. I'm told that switch statements will actually use hash tables...
9
by: ankitdesai | last post by:
I would like to parse a couple of tables within an individual player's SHTML page. For example, I would like to get the "Actual Pitching Statistics" and the "Translated Pitching Statistics"...
2
by: lckarthikeyan | last post by:
Hi... I Have One Doubt In Parsing Function In C... In My Project Some Object Like This Form 1.3.6.10.184.. But Using Parsing Function I Want To Change Like This..1_3_6_10_184..please Let...
1
by: lckarthikeyan | last post by:
I Have one doubt in parsing function in c... In my program i am using one object identifier like this 1.3.6.7.184 now i want to change like this using parsing function 1_3_6_10_184.. any...
1
by: charlesvc | last post by:
Hallo I am working on web design on mobile. So i came to know for webpage in html or xhtml needs to be parsed to show on mobile So my doubt is the parsing will give only tags text...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.