Parsing Doubt

Discussion in 'C Programming' started by Tarique, Jan 17, 2008.

  1. Tarique

    Tarique Guest

    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?
     
    Tarique, Jan 17, 2008
    #1
    1. Advertising

  2. Tarique

    Mark Bluemel Guest

    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 ("--").
     
    Mark Bluemel, Jan 17, 2008
    #2
    1. Advertising

  3. Tarique

    Guest

    (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$>, 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?


    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, 2008
    #3
  4. 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:>
     
    Kenneth Brody, Jan 17, 2008
    #4
  5. 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
     
    Richard Heathfield, Jan 17, 2008
    #5
  6. In article <fmnrmv$36q$>, 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?


    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
     
    Richard Tobin, Jan 17, 2008
    #6
  7. Tarique

    Eric Sosman Guest

    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.

    --
     
    Eric Sosman, Jan 17, 2008
    #7
  8. Tarique

    CBFalconer Guest

    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
     
    CBFalconer, Jan 17, 2008
    #8
  9. Tarique

    Tarique Guest

    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.
     
    Tarique, Jan 18, 2008
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Simone Mehta

    sscanf parsing doubt

    Simone Mehta, Sep 26, 2004, in forum: C Programming
    Replies:
    12
    Views:
    729
    Aslam Sheikh Durrani
    Oct 3, 2004
  2. Bob Nelson

    doubt about doubt

    Bob Nelson, Jul 28, 2006, in forum: C Programming
    Replies:
    11
    Views:
    641
  3. Replies:
    0
    Views:
    580
  4. Peter Otten
    Replies:
    2
    Views:
    131
    Cousin Stanley
    Aug 10, 2013
  5. Terry Reedy
    Replies:
    0
    Views:
    123
    Terry Reedy
    Aug 10, 2013
Loading...

Share This Page