// comments

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jacob navia

    // comments

    Recently we had poor Mr "teapot" that was horrified at the heresy
    of lcc-win of accepting // comments.

    C is a nice language, and you can do anything with it, inclusive a
    program that transforms // comments into well behaved /* ... */
    ones...

    ------------------------------------------------cut here

    /*
    This program reads a C source file and writes it modified to stdout
    All // comments will be replaced by / * ... * / comments, to easy the
    porting to old environments or to post it in usenet, where
    // comments can be broken in several lines, and messed up.
    */

    #include <stdio.h>
    #include <stdlib.h>

    /* This function reads a character and writes it to stdout */
    static int Fgetc(FILE *f)
    {
    int c = fgetc(f);
    if (c != EOF)
    putchar(c);
    return c;

    }

    /* This function skips strings */
    static int ParseString(FIL E *f)
    {
    int c = Fgetc(f);
    while (c != EOF && c != '"') {
    if (c == '\\')
    c = Fgetc(f);
    if (c != EOF)
    c = Fgetc(f);
    }
    if (c == '"')
    c = Fgetc(f);
    return c;
    }

    /* Skips multi-line comments */
    static int ParseComment(FI LE *f)
    {
    int c = Fgetc(f);

    while (1) {
    while (c != '*') {
    c = Fgetc(f);
    if (c == EOF)
    return EOF;
    }
    c = Fgetc(f);
    if (c == '/')
    break;
    }
    return Fgetc(f);

    }

    /* Skips // comments. Note that we use fgetc here and NOT Fgetc */
    /* since we want to modify the output before gets echoed */
    static int ParseCppComment (FILE *f)
    {
    int c = fgetc(f);

    while (c != EOF && c != '\n') {
    putchar(c);
    c = fgetc(f);
    if (c == '*') {
    c = fgetc(f);
    if (c == '/') {
    c = fgetc(f);
    putchar(' ');
    }
    else {
    putchar('*');
    }
    }
    }
    if (c == '\n') {
    puts(" */");
    c = Fgetc(f);
    }
    return c;

    }

    /* Checks if a comment is followed after a '/' char */
    static int CheckComment(in t c,FILE *f)
    {
    if (c == '/') {
    c = fgetc(f);
    if (c == '*') {
    putchar('*');
    c = ParseComment(f) ;
    }
    else if (c == '/') {
    putchar('*');
    c = ParseCppComment (f);
    }
    else {
    putchar(c);
    c = Fgetc(f);
    }
    }
    return c;

    }

    /* Skips chars between simple quotes */
    static int ParseQuotedChar (FILE *f)
    {
    int c = Fgetc(f);
    while (c != EOF && c != '\'') {
    if (c == '\\')
    c = Fgetc(f);
    if (c != EOF)
    c = Fgetc(f);
    }
    if (c == '\'')
    c = Fgetc(f);
    return c;

    }

    int main(int argc,char *argv[])
    {
    FILE *f;
    int c;
    if (argc == 1) {
    puts("Usage: give an input file name. Writes to stdout");
    return EXIT_FAILURE;
    }
    f = fopen(argv[1],"r");
    if (f == NULL) {
    puts("Can't find the input file");
    return EXIT_FAILURE;
    }
    c = Fgetc(f);
    while (c != EOF) {
    /* Note that each of the switches must advance the character */
    /* read so that we avoid an infinite loop. */
    switch (c) {
    case '"':
    c = ParseString(f);
    break;
    case '/':
    c = CheckComment(c, f);
    break;
    case '\'':
    c = ParseQuotedChar (f);
    break;
    default:
    c = Fgetc(f);
    }
    }
    fclose(f);
    return 0;

    }

    ----------------------------------------------------------cut here

    Note that trigraphs are not supported. Homework: Add that feature

    Compiled with lcc-win, this program makes only 3 616 bytes. C is
    an efficient language.

    P.S. Note that I have avoided printf... If you add printf the size
    will be a HUGE 37K.

    --
    jacob navia
    jacob at jacob point remcomp point fr
    logiciels/informatique

  • Ben Bacarisse

    #2
    Re: // comments

    jacob navia <jacob@nospam.c omwrites:
    Recently we had poor Mr "teapot" that was horrified at the heresy
    of lcc-win of accepting // comments.
    Can you clear up the question of what the -ansi89 flag is intended to
    do? Your compiler is allowed to accept any extensions it likes and to
    reject and conforming C90 programs it wishes to *unless* you claim that
    lc -ansi89 is intended to be a conforming C90 implementation. What is
    the purpose of -ansi89?
    C is a nice language, and you can do anything with it, inclusive a
    program that transforms // comments into well behaved /* ... */
    ones...
    How do you do that with:

    int main(void) { return 1//* divide? */2; }

    Is this a valid C90 program or an incorrect C90 one that has a //
    comment int it?

    <snip program>
    Note that trigraphs are not supported. Homework: Add that feature
    I also does not treat multi-line // comments correctly.

    --
    Ben.

    Comment

    • Richard Heathfield

      #3
      Re: // comments

      jacob navia said:
      Recently we had poor Mr "teapot" that was horrified at the heresy
      of lcc-win of accepting // comments.
      It is surely not too surprising that at least one user is horrified by the
      idea that a supposedly conforming C90 implementation fails to produce a
      diagnostic message for a syntax error. We knew lcc-win32 didn't conform to
      C99. Now it appears it doesn't appear to conform to C90 either.

      Does it at least have a K&R C mode? If not, it would seem that it isn't
      actually a C compiler after all, and is therefore off-topic in
      comp.lang.c.
      C is a nice language, and you can do anything with it, inclusive a
      program that transforms // comments into well behaved /* ... */
      ones...
      ....and fails not only on trigraphs (as you pointed out) but also on the
      following input:

      /\
      / double-slash comment with line-splice

      --
      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

      Comment

      • jacob navia

        #4
        Re: // comments

        Ben Bacarisse wrote:
        jacob navia <jacob@nospam.c omwrites:
        >
        >Recently we had poor Mr "teapot" that was horrified at the heresy
        >of lcc-win of accepting // comments.
        >
        Can you clear up the question of what the -ansi89 flag is intended to
        do? Your compiler is allowed to accept any extensions it likes and to
        reject and conforming C90 programs it wishes to *unless* you claim that
        lc -ansi89 is intended to be a conforming C90 implementation. What is
        the purpose of -ansi89?
        >
        -ansi89 was implemented to the wishes of a paying customer. They wanted
        a compiler to that standard, and I implemented it for them. The usage of
        that flag is to disable most C99 stuff.

        Note that many compilers at the C89 level accept // comments, for instance
        Microsoft compilers

        --
        jacob navia
        jacob at jacob point remcomp point fr
        logiciels/informatique

        Comment

        • Keith Thompson

          #5
          Re: // comments

          jacob navia <jacob@nospam.c omwrites:
          Ben Bacarisse wrote:
          >jacob navia <jacob@nospam.c omwrites:
          >>
          >>Recently we had poor Mr "teapot" that was horrified at the heresy
          >>of lcc-win of accepting // comments.
          >>
          >Can you clear up the question of what the -ansi89 flag is intended to
          >do? Your compiler is allowed to accept any extensions it likes and to
          >reject and conforming C90 programs it wishes to *unless* you claim that
          >lc -ansi89 is intended to be a conforming C90 implementation. What is
          >the purpose of -ansi89?
          >>
          >
          -ansi89 was implemented to the wishes of a paying customer. They wanted
          a compiler to that standard, and I implemented it for them. The usage of
          that flag is to disable most C99 stuff.
          If your paying customer is satisfied with that, then that's fine.
          However, the ANSI C89 standard (or equivalently the ISO C90 standard)
          requires a diagnostic for a "//" comment (except in the rare cases
          where it can be a legal C89 construct, such as a division operator
          immediately followed by a comment). If your compiler doesn't issue a
          diagnostic for a "//" comment, then it's not a 100% conforming C89
          compiler. I hope that your documentation makes that clear.

          Since it's easy enough to implement full C89 conformance in this area
          by issuing a warning, perhaps just on the first occurrence in a
          translation unit, I fail to understand why you wouldn't go ahead and
          do so, but that's up to you.
          Note that many compilers at the C89 level accept // comments, for instance
          Microsoft compilers
          Then my comment applies to them as well.

          (If you perceive a personal attack in the above, or an attack on
          lcc-win, or on C99, or on // comments, then you've misunderstood me.
          Again.)

          --
          Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
          Nokia
          "We must do something. This is something. Therefore, we must do this."
          -- Antony Jay and Jonathan Lynn, "Yes Minister"

          Comment

          • Richard Heathfield

            #6
            Re: // comments

            Keith Thompson said:
            jacob navia <jacob@nospam.c omwrites:
            <snip>
            >Note that many compilers at the C89 level accept // comments,
            If they are invoked in conforming mode, they *must* diagnose syntax errors.
            >for instance Microsoft compilers
            >
            Then my comment applies to them as well.
            When invoked in conforming mode, Microsoft C (or at least my copy of it)
            issues the necessary diagnostic message if you use // in an erroneous
            syntactical context.

            <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

            Comment

            • jacob navia

              #7
              Re: // comments

              Richard Heathfield wrote:
              Keith Thompson said:
              >
              >jacob navia <jacob@nospam.c omwrites:
              >
              <snip>
              >
              >>Note that many compilers at the C89 level accept // comments,
              >
              If they are invoked in conforming mode, they *must* diagnose syntax errors.
              >
              Maybe. If they do not, please use another compiler.
              >>for instance Microsoft compilers
              >Then my comment applies to them as well.
              >
              When invoked in conforming mode, Microsoft C (or at least my copy of it)
              issues the necessary diagnostic message if you use // in an erroneous
              syntactical context.
              >

              D:\lcc\mc71\tes t>type tt.c
              // aaaaa

              D:\lcc\mc71\tes t>cl -W4 tt.c
              Microsoft (R) C/C++ Optimizing Compiler Version 14.00.50727.762 for x64
              Copyright (C) Microsoft Corporation. All rights reserved.

              tt.c
              tt.c(2) : warning C4206: nonstandard extension used : translation unit
              is empty

              Of course your version dates from 1991... Always the same word games,
              half truths, etc. Pure regulars BS.

              --
              jacob navia
              jacob at jacob point remcomp point fr
              logiciels/informatique

              Comment

              • vippstar@gmail.com

                #8
                Re: // comments

                On Apr 27, 1:27 am, jacob navia <ja...@nospam.c omwrote:
                Recently we had poor Mr "teapot" that was horrified at the heresy
                of lcc-win of accepting // comments.
                You seem horrified too. Or horrifying.
                C is a nice language, and you can do anything with it, inclusive a
                program that transforms // comments into well behaved /* ... */
                ones...
                Actually, you cannot, at least not correctly. It's impossible to guess
                the intend of the programmer in:
                int main(void) { return 1//* divide? */2; }
                As noted by Mr Bacarisse (but I have seen that example in other places
                too)
                <snip code>
                >
                Note that trigraphs are not supported. Homework: Add that feature
                Nor digraphs.
                Compiled with lcc-win, this program makes only 3 616 bytes. C is
                an efficient language.
                I think what you're trying to say here is that lcc-win is efficient,
                and not C. A language cannot be efficient, not in that sense.
                P.S. Note that I have avoided printf... If you add printf the size
                will be a HUGE 37K.
                Maybe your compiler is not that efficient then (or maybe you did not
                mention how you linked the lib).
                Here with gcc I get 8K with no optimizations (using or not using
                printf).
                I wouldn't expect from someone who complains about "the regulars"
                mentioning old systems to prove him wrong, to actually mention 37K as
                a "HUGE" size. 37K is _nothing_ in a modern computer.

                Comment

                • Flash Gordon

                  #9
                  Re: // comments

                  jacob navia wrote, On 27/04/08 08:59:
                  Richard Heathfield wrote:
                  >Keith Thompson said:
                  >>
                  >>jacob navia <jacob@nospam.c omwrites:
                  >>
                  ><snip>
                  >>
                  >>>Note that many compilers at the C89 level accept // comments,
                  >>
                  >If they are invoked in conforming mode, they *must* diagnose syntax
                  >errors.
                  >
                  Maybe. If they do not, please use another compiler.
                  If you state that -ansi89 does *not* mean conformance to C89 (or C90 or
                  C95) then as has already been stated it is allowed to accept whatever
                  you want. If I was one of your paying customers I would not be happy,
                  but I'm not.
                  >>>for instance Microsoft compilers
                  >>Then my comment applies to them as well.
                  >>
                  >When invoked in conforming mode, Microsoft C (or at least my copy of
                  >it) issues the necessary diagnostic message if you use // in an
                  >erroneous syntactical context.
                  >
                  D:\lcc\mc71\tes t>type tt.c
                  // aaaaa
                  >
                  D:\lcc\mc71\tes t>cl -W4 tt.c
                  Microsoft (R) C/C++ Optimizing Compiler Version 14.00.50727.762 for x64
                  Copyright (C) Microsoft Corporation. All rights reserved.
                  >
                  tt.c
                  tt.c(2) : warning C4206: nonstandard extension used : translation unit
                  is empty
                  >
                  Of course your version dates from 1991... Always the same word games,
                  half truths, etc. Pure regulars BS.
                  Not at all. You have just demonstrated that what Richard said is true
                  for the version you have as well. The compiler emitted a diagnostic.
                  There is no requirement for it to produce an error or abort the
                  translation. I have already posted saying that all you have to do is
                  detect it and issue a warning, as has Keith.
                  --
                  Flash Gordon

                  Comment

                  • jacob navia

                    #10
                    Re: // comments

                    Flash Gordon wrote:
                    jacob navia wrote, On 27/04/08 08:59:
                    >Richard Heathfield wrote:
                    >>When invoked in conforming mode, Microsoft C (or at least my copy of
                    >>it) issues the necessary diagnostic message if you use // in an
                    >>erroneous syntactical context.
                    >>
                    >D:\lcc\mc71\te st>type tt.c
                    >// aaaaa
                    >>
                    >D:\lcc\mc71\te st>cl -W4 tt.c
                    >Microsoft (R) C/C++ Optimizing Compiler Version 14.00.50727.762 for x64
                    >Copyright (C) Microsoft Corporation. All rights reserved.
                    >>
                    >tt.c
                    >tt.c(2) : warning C4206: nonstandard extension used : translation unit
                    >is empty
                    >>
                    >Of course your version dates from 1991... Always the same word games,
                    >half truths, etc. Pure regulars BS.
                    >
                    Not at all. You have just demonstrated that what Richard said is true
                    for the version you have as well. The compiler emitted a diagnostic.
                    There is no requirement for it to produce an error or abort the
                    translation. I have already posted saying that all you have to do is
                    detect it and issue a warning, as has Keith.
                    word games, word games
                    // aaaaaaa
                    int a=0;

                    Now, cl doesn't give any warning:
                    D:\lcc\mc71\tes t>cl -W4 -c tt.c
                    Microsoft (R) C/C++ Optimizing Compiler Version 14.00.50727.762 for x6
                    Copyright (C) Microsoft Corporation. All rights reserved.

                    tt.c

                    D:\lcc\mc71\tes t>

                    WORD GAMES as always.

                    --
                    jacob navia
                    jacob at jacob point remcomp point fr
                    logiciels/informatique

                    Comment

                    • santosh

                      #11
                      Re: // comments

                      jacob navia wrote:
                      Flash Gordon wrote:
                      >jacob navia wrote, On 27/04/08 08:59:
                      >>Richard Heathfield wrote:
                      >>>When invoked in conforming mode, Microsoft C (or at least my copy
                      >>>of it) issues the necessary diagnostic message if you use // in an
                      >>>erroneous syntactical context.
                      >>>
                      >>D:\lcc\mc71\t est>type tt.c
                      >>// aaaaa
                      >>>
                      >>D:\lcc\mc71\t est>cl -W4 tt.c
                      >>Microsoft (R) C/C++ Optimizing Compiler Version 14.00.50727.762 for
                      >>x64
                      >>Copyright (C) Microsoft Corporation. All rights reserved.
                      >>>
                      >>tt.c
                      >>tt.c(2) : warning C4206: nonstandard extension used : translation
                      >>unit is empty
                      >>>
                      >>Of course your version dates from 1991... Always the same word
                      >>games, half truths, etc. Pure regulars BS.
                      >>
                      >Not at all. You have just demonstrated that what Richard said is true
                      >for the version you have as well. The compiler emitted a diagnostic.
                      >There is no requirement for it to produce an error or abort the
                      >translation. I have already posted saying that all you have to do is
                      >detect it and issue a warning, as has Keith.
                      >
                      word games, word games
                      // aaaaaaa
                      int a=0;
                      >
                      Now, cl doesn't give any warning:
                      D:\lcc\mc71\tes t>cl -W4 -c tt.c
                      Microsoft (R) C/C++ Optimizing Compiler Version 14.00.50727.762 for x6
                      Copyright (C) Microsoft Corporation. All rights reserved.
                      >
                      tt.c
                      >
                      D:\lcc\mc71\tes t>
                      >
                      WORD GAMES as always.
                      >
                      Well, what do you expect if you do not tell CL to disable language
                      extensions? Add the /Za switch and also the /Wall switch and try again.

                      Comment

                      • Keith Thompson

                        #12
                        Re: // comments

                        jacob navia <jacob@nospam.c omwrites:
                        Flash Gordon wrote:
                        >jacob navia wrote, On 27/04/08 08:59:
                        >>Richard Heathfield wrote:
                        >>>When invoked in conforming mode, Microsoft C (or at least my copy
                        >>>of it) issues the necessary diagnostic message if you use // in an
                        >>>erroneous syntactical context.
                        >>>
                        >>D:\lcc\mc71\t est>type tt.c
                        >>// aaaaa
                        >>>
                        >>D:\lcc\mc71\t est>cl -W4 tt.c
                        >>Microsoft (R) C/C++ Optimizing Compiler Version 14.00.50727.762 for x64
                        >>Copyright (C) Microsoft Corporation. All rights reserved.
                        >>>
                        >>tt.c
                        >>tt.c(2) : warning C4206: nonstandard extension used : translation
                        >>unit is empty
                        >>>
                        >>Of course your version dates from 1991... Always the same word games,
                        >>half truths, etc. Pure regulars BS.
                        >>
                        >Not at all. You have just demonstrated that what Richard said is
                        >true for the version you have as well. The compiler emitted a
                        >diagnostic. There is no requirement for it to produce an error or
                        >abort the translation. I have already posted saying that all you
                        >have to do is detect it and issue a warning, as has Keith.
                        >
                        word games, word games
                        // aaaaaaa
                        int a=0;
                        >
                        Now, cl doesn't give any warning:
                        D:\lcc\mc71\tes t>cl -W4 -c tt.c
                        Microsoft (R) C/C++ Optimizing Compiler Version 14.00.50727.762 for x6
                        Copyright (C) Microsoft Corporation. All rights reserved.
                        >
                        tt.c
                        >
                        D:\lcc\mc71\tes t>
                        >
                        WORD GAMES as always.
                        I don't think the phrase "word games" means what you think it means.

                        You have demonstrated that Microsoft's C compiler, invoked with the
                        options you specified, is not a conforming C89/C90 compiler. If it
                        were, it would have issued a diagnostic for the "//".

                        I don't know whether "cl -W4 -c tt.c" invokes it in what is intended
                        to be C90 conforming mode. If not, you haven't demonstrated anything
                        of any significance.

                        *At most*, you've simply demonstrated that, *if* lcc-win's "-ansi89"
                        option is intended to invoke it in C89 conforming mode, then it
                        exhibits a bug (a failure to conform to the C89 standard) that
                        Microsoft's compiler also exhibits.

                        None of this matters much to me personally. Since it appears to
                        matter a great deal to you, perhaps you can answer these questions and
                        clear this up:

                        1. Is lcc-win's "-ansi89" option intended to invoke the compiler in a
                        mode that conforms to the ANSI C89 standard?

                        2. In that mode, does it issue a diagnostic for a "//" comment?

                        3. If it doesn't, do you acknowledge that that is a conformance
                        failure?

                        If this is a bug, I'm not saying it's terrifying, and I'm not pushing
                        to you fix it. I'm only asking you to clear up this issue.

                        --
                        Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
                        Nokia
                        "We must do something. This is something. Therefore, we must do this."
                        -- Antony Jay and Jonathan Lynn, "Yes Minister"

                        Comment

                        • ymuntyan@gmail.com

                          #13
                          Re: // comments

                          On Apr 27, 4:26 am, Flash Gordon <s...@flash-gordon.me.ukwro te:
                          jacob navia wrote, On 27/04/08 08:59:
                          >
                          Richard Heathfield wrote:
                          Keith Thompson said:
                          >
                          >jacob navia <ja...@nospam.c omwrites:
                          >
                          <snip>
                          >
                          >>Note that many compilers at the C89 level accept // comments,
                          >
                          If they are invoked in conforming mode, they *must* diagnose syntax
                          errors.
                          >
                          Maybe. If they do not, please use another compiler.
                          >
                          If you state that -ansi89 does *not* mean conformance to C89 (or C90 or
                          C95) then as has already been stated it is allowed to accept whatever
                          you want. If I was one of your paying customers I would not be happy,
                          but I'm not.
                          Perhaps that customer wanted warnings for stuff which
                          reduces portability, like variadic macros not understood
                          by MS compiler?
                          (Yes I do realize that where were, and hence are, C compilers
                          which do not understand // comments)

                          Yevgen

                          Comment

                          • ymuntyan@gmail.com

                            #14
                            Re: // comments

                            On Apr 27, 6:02 am, ymunt...@gmail. com wrote:
                            On Apr 27, 4:26 am, Flash Gordon <s...@flash-gordon.me.ukwro te:
                            >
                            >
                            >
                            jacob navia wrote, On 27/04/08 08:59:
                            >
                            Richard Heathfield wrote:
                            >Keith Thompson said:
                            >
                            >>jacob navia <ja...@nospam.c omwrites:
                            >
                            ><snip>
                            >
                            >>>Note that many compilers at the C89 level accept // comments,
                            >
                            >If they are invoked in conforming mode, they *must* diagnose syntax
                            >errors.
                            >
                            Maybe. If they do not, please use another compiler.
                            >
                            If you state that -ansi89 does *not* mean conformance to C89 (or C90 or
                            C95) then as has already been stated it is allowed to accept whatever
                            you want. If I was one of your paying customers I would not be happy,
                            but I'm not.
                            >
                            Perhaps that customer wanted warnings for stuff which
                            reduces portability, like variadic macros not understood
                            by MS compiler?
                            (Yes I do realize that where were, and hence are, C compilers
                            which do not understand // comments)
                            Actually I was wrong, I have no idea if there was a C
                            compiler which didn't understand // comments. By the time
                            when a compiler got to C90 conformance (N years after 1990),
                            // comments were probably already there. What conforming C90
                            compiler doesn't understand // comments? Just curious.

                            Yevgen

                            Comment

                            • Ben Bacarisse

                              #15
                              Re: // comments

                              ymuntyan@gmail. com writes:
                              On Apr 27, 4:26 am, Flash Gordon <s...@flash-gordon.me.ukwro te:
                              >jacob navia wrote, On 27/04/08 08:59:
                              <snip>
                              >If you state that -ansi89 does *not* mean conformance to C89 (or C90 or
                              >C95) then as has already been stated it is allowed to accept whatever
                              >you want. If I was one of your paying customers I would not be happy,
                              >but I'm not.
                              >
                              Perhaps that customer wanted warnings for stuff which
                              reduces portability, like variadic macros not understood
                              by MS compiler?
                              Then they would be disappointed. I am not sure to what extent it is
                              either wise or topical to go into details but -ansi89 does not
                              diagnose variadic macros (as it should). Fortunately, the
                              implementation of them seems to be broken so the user would likely
                              find out if they tried to use them. It seems that -ansi89 also
                              permits long long int, treats restrict as a keyword, does not diagnose
                              the use of compound literals, permits [static N] in function
                              parameters... I stopped looking after a while[1].

                              Its use for checking portability would be rather limited, I think.

                              [1] To be balanced, it does diagnose some non ANSI C constructs.

                              --
                              Ben.

                              Comment

                              Working...