Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Parsing Doubt

Reply
Thread Tools

Parsing Doubt

 
 
Tarique
Guest
Posts: n/a
 
      01-17-2008
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?
 
Reply With Quote
 
 
 
 
Mark Bluemel
Guest
Posts: n/a
 
      01-17-2008
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 ("--").
 
Reply With Quote
 
 
 
 
dj3vande@csclub.uwaterloo.ca.invalid
Guest
Posts: n/a
 
      01-17-2008
(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 <fmnrmv$36q$(E-Mail Removed)>, Tarique <(E-Mail Removed)> 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?


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
 
Reply With Quote
 
Kenneth Brody
Guest
Posts: n/a
 
      01-17-2008
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: <(E-Mail Removed)>

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      01-17-2008
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
 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      01-17-2008
In article <fmnrmv$36q$(E-Mail Removed)>, Tarique <(E-Mail Removed)> 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?


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
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      01-17-2008
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.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      01-17-2008
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

 
Reply With Quote
 
Tarique
Guest
Posts: n/a
 
      01-18-2008
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dotnet doubt can any body clarify my doubt challa462@gmail.com ASP .Net 0 08-22-2012 06:02 AM
doubt about doubt Bob Nelson C Programming 11 07-30-2006 08:17 PM
sscanf parsing doubt Simone Mehta C Programming 12 10-03-2004 09:04 AM
sscanf parsing doubt Simone Mehta C Programming 0 09-27-2004 02:59 AM
Perl expression for parsing CSV (ignoring parsing commas when in double quotes) GIMME Perl 2 02-11-2004 05:40 PM



Advertisments