473,661 Members | 2,431 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1543
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**********@a ioe.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**********@a ioe.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
5974
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). When I access a url from python 2.3.3 running in Linux with the following lines:
12
8711
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"; ..... sscanf(foo,"%s%*%s%*%s%*%s%*%s",s1,s2,s3,s4,s5); <snippet/> This is giving me junk .
2
1054
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 batty. snippet of text line that shows up in watch USAHOME1 Users NT Advanced Server -  5.1  2600, Service Pack 2  Uniprocessor Free  Microsoft Windows XP
26
6864
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 is in my Watch:
5
3919
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 when number of cases is around 10 or more, so I haven't changed the code. Just wondering if anyone knows about how that's structured internally. It does seem a bit slow.
9
4054
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" portions of Babe Ruth page (http://www.baseballprospectus.com/dt/ruthba01.shtml) and store that info in a CSV file. Also, I would like to do this for numerous players whose IDs I have stored in a text file (e.g.: cobbty01, ruthba01, speaktr01, etc.)....
2
1817
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 Me Knoe
1
1258
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 idea..pleas elet me know..this is very urgent OS:LINUX(CENT OS 4.3) LANGUAGE (C)
1
8727
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 and source etc. only, we have simulate it as a web page in canvas. I want to know whether my idea is correct or not. I found PocketLearn J2ME HTML Component. Here my doubt is whether the package webserver with this is available for dowload to use...
0
8432
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8758
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8545
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6185
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4179
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4346
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1986
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1743
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.