removing comment

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • daskumardilip@gmail.com

    #1

    removing comment

    how to remove comment from c source code.

  • David Marsh

    #2
    Re: removing comment

    daskumardilip@g mail.com wrote:
    how to remove comment from c source code.
    >
    Select it and press "delete."

    David

    Comment

    • vim

      #3
      Re: removing comment

      Use text processing languages
      lex and yaac
      Use regular expressions in that

      Comment

      • xianwei

        #4
        Re: removing comment

        On Sep 26, 8:55 pm, daskumardi...@g mail.com wrote:
        how to remove comment from c source code.
        If you use VIM, I think the built-in regxp is a good idea.
        But I not good at it, so I can't help you~~~~

        Comment

        • Walter Roberson

          #5
          Re: removing comment

          In article <1190814438.015 492.177410@n39g 2000hsh.googleg roups.com>,
          vim <vgstech@gmail. comwrote:
          >Use text processing languages
          >lex and yaac
          >Use regular expressions in that
          You cannot reliably detect comments using regular expressions.


          #define COMMENT /*
          #define ENDCOMMENT */
          COMMENT
          this is a comment
          #ifdef __GCC
          ENDCOMMENT
          #error The license of this example forbids use under GCC
          #else
          Ah, that's better
          ENDCOMMENT
          #endif
          #include <stdio.h>
          int main(void) { printf("Hello /*\"Zippity\"*/World"); return 0; }


          The points:
          - comments can be triggered by macros
          - comment formation is affected by preprocessor conditions that a
          textual parser cannot know the answer to
          - inside string literals, comment-like sequences are just
          regular characters
          - determining whether you are inside a string literal requires
          matching double-quotes, which is made non-trivial due to
          backslashes

          Summary:
          You cannot remove comments from source without something approaching
          the power of the preprocessor, with complete knowledge of the
          defines in effect.
          --
          I was very young in those days, but I was also rather dim.
          -- Christopher Priest

          Comment

          • Richard Heathfield

            #6
            Re: removing comment

            Walter Roberson said:

            <snip>
            - comments can be triggered by macros
            Comment removal occurs before macro expansion.

            <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

            • CBFalconer

              #7
              Re: removing comment

              daskumardilip@g mail.com wrote:
              >
              how to remove comment from c source code.
              Consider the DELETE key.

              --
              Chuck F (cbfalconer at maineline dot net)
              Available for consulting/temporary embedded and systems.
              <http://cbfalconer.home .att.net>



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

              Comment

              • Eric Sosman

                #8
                Re: removing comment

                Walter Roberson wrote On 09/26/07 10:07,:
                In article <1190814438.015 492.177410@n39g 2000hsh.googleg roups.com>,
                vim <vgstech@gmail. comwrote:
                >
                >>Use text processing languages
                >>lex and yaac
                >>Use regular expressions in that
                >
                >
                You cannot reliably detect comments using regular expressions.
                >
                >
                #define COMMENT /*
                #define ENDCOMMENT */
                This much defines COMMENT as an object-like
                macro with an empty replacement list.
                COMMENT
                This expands COMMENT, producing nothing at all.
                this is a comment
                This is syntax error, and there's no point
                in further analysis.
                [...]
                The points:
                - comments can be triggered by macros
                If "triggered by" means "generated by expansion of,"
                this is false.
                - comment formation is affected by preprocessor conditions that a
                textual parser cannot know the answer to
                False.
                - inside string literals, comment-like sequences are just
                regular characters
                True. The same holds for character literals, too:

                char c = '/* no comment starts here';
                char d = '// nor here';
                - determining whether you are inside a string literal requires
                matching double-quotes, which is made non-trivial due to
                backslashes
                True.
                Summary:
                You cannot remove comments from source without something approaching
                the power of the preprocessor, with complete knowledge of the
                defines in effect.
                False. The preprocessor has no knowledge of comments,
                no influence on comments, and nothing to do with comments.
                You need only a lexical analyzer; no knowledge or processing
                of macros is required or desired.

                --
                Eric.Sosman@sun .com

                Comment

                • Tor Rustad

                  #9
                  Re: removing comment

                  Walter Roberson wrote:
                  In article <1190814438.015 492.177410@n39g 2000hsh.googleg roups.com>,
                  vim <vgstech@gmail. comwrote:
                  >Use text processing languages
                  >lex and yaac
                  >Use regular expressions in that
                  >
                  You cannot reliably detect comments using regular expressions.
                  Of course we can, I have even written such a lex spec myself. :)

                  IIRC, the "lex & yacc" book, even provide an example for the /**/
                  comments, and handling the // comment case is easier.

                  #define COMMENT /*
                  #define ENDCOMMENT */
                  COMMENT
                  this is a comment
                  #ifdef __GCC
                  ENDCOMMENT

                  Wrong. After translation phase:

                  1. Trigraph replacement
                  2. Line-splicing
                  3. replace WS sequences & comments with a space

                  this reads:

                  #define COMMENT
                  COMMENT
                  this is a comment


                  Macro expansion comes in translation phase 4.

                  --
                  Tor <torust [at] online [dot] no>

                  Comment

                  • Walter Roberson

                    #10
                    Re: removing comment

                    In article <N7ydnZGjJuZAEm fb4p2dnAA@telen or.com>,
                    Tor Rustad <tor_rustad@hot mail.comwrote:
                    >Walter Roberson wrote:
                    >You cannot reliably detect comments using regular expressions.
                    >Of course we can, I have even written such a lex spec myself. :)
                    lex does not use strict regular expressions. lex and yacc together
                    are LALR, which requires a pushdown stack, a feature missing
                    in regular expressions.
                    --
                    Programming is what happens while you're busy making other plans.

                    Comment

                    • Lew Pitcher

                      #11
                      Re: removing comment

                      On Sep 26, 8:55 am, daskumardi...@g mail.com wrote:
                      how to remove comment from c source code.
                      Search the web for Richard Heathfields page on solutions to problems
                      posed in K&R2
                      You are looking for the solutions to exercise 1-23. Many of the
                      regular clc participants contributed solutions, although (IIRC) not
                      many offered bulletproof implementations .

                      Look here -http://users.powernet.co.uk/eton/kandr2/krx123.html if
                      nowhere else <grin>

                      HTH
                      --
                      Lew

                      Comment

                      • Keith Thompson

                        #12
                        Re: removing comment

                        Eric Sosman <Eric.Sosman@su n.comwrites:
                        Walter Roberson wrote On 09/26/07 10:07,:
                        [...]
                        >Summary:
                        > You cannot remove comments from source without something approaching
                        > the power of the preprocessor, with complete knowledge of the
                        > defines in effect.
                        >
                        False. The preprocessor has no knowledge of comments,
                        no influence on comments, and nothing to do with comments.
                        You need only a lexical analyzer; no knowledge or processing
                        of macros is required or desired.
                        I agree with your conclusion, but not necessarily with your statement
                        that the preprocessor has no knowledge of comments.

                        The standard defines 8 translation phases. Comments are replaced by
                        space characters in phase 3; preprocessing directives are executed and
                        macros are expanded in phase 4. But the standard doesn't define what
                        "the preprocessor" is. In an implementation which has a distinct
                        "preprocess or" component, perhaps a separate program, that program
                        could handle phases 1 through 4, or 1 through 5, or even 1 through 6.

                        (See C99 5.1.1.2 for the descriptions of the translation phases.)

                        --
                        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                        San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
                        "We must do something. This is something. Therefore, we must do this."
                        -- Antony Jay and Jonathan Lynn, "Yes Minister"

                        Comment

                        • Keith Thompson

                          #13
                          Re: removing comment

                          daskumardilip@g mail.com writes:
                          how to remove comment from c source code.
                          As you've seen from the variety of responses, you need to be more
                          specific in your question. You can remove comments manually, or you
                          can write a program to do it automatically, or you can use some
                          existing program. Or you can print out the C source code and write
                          over the comments with a black marker.

                          What exactly are you trying to do, and *why* do you want to remove
                          comments? The only good reason I can think of for doing so is if
                          you're writing a compiler, but in that case you've got a lot of work
                          ahead of you.

                          --
                          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                          San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
                          "We must do something. This is something. Therefore, we must do this."
                          -- Antony Jay and Jonathan Lynn, "Yes Minister"

                          Comment

                          • Richard Heathfield

                            #14
                            Re: removing comment

                            Lew Pitcher said:
                            On Sep 26, 8:55 am, daskumardi...@g mail.com wrote:
                            >how to remove comment from c source code.
                            >
                            Search the web for Richard Heathfields page on solutions to problems
                            posed in K&R2
                            You are looking for the solutions to exercise 1-23. Many of the
                            regular clc participants contributed solutions, although (IIRC) not
                            many offered bulletproof implementations .
                            >
                            Look here -http://users.powernet.co.uk/eton/kandr2/krx123.html if
                            nowhere else <grin>
                            Please don't look there. Look here instead:



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

                            • Tor Rustad

                              #15
                              Re: removing comment

                              Walter Roberson wrote:
                              In article <N7ydnZGjJuZAEm fb4p2dnAA@telen or.com>,
                              Tor Rustad <tor_rustad@hot mail.comwrote:
                              >Walter Roberson wrote:
                              [snipped context and good advice by vim added]
                              >>vim said:
                              >>>"Use text processing languages
                              >>>lex and yaac
                              >>>Use regular expressions in that"
                              >>You cannot reliably detect comments using regular expressions.
                              >
                              >Of course we can, I have even written such a lex spec myself. :)
                              >
                              lex does not use strict regular expressions. lex and yacc together
                              are LALR, which requires a pushdown stack, a feature missing
                              in regular expressions.
                              Well, yacc is a LALR parser, but that's beside the point, since yacc
                              isn't needed here. OP's request can be done with lex alone, see e.g.
                              "lex and yacc" example 2-9, or look at



                              for ideas. Another alternative, is writing a small C program, an
                              untested initial version, took me ca. 15 minutes to write.

                              --
                              Tor <torust [at] online [dot] no>

                              Comment

                              Working...