How to write a C++ compiler

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Simon Kwong

    How to write a C++ compiler

    Hi,

    I am learning c++ and would like to write a simple C++ compiler. How should
    I start?

    Please help.

    Simon


  • Leor Zolman

    #2
    Re: How to write a C++ compiler

    On Thu, 4 Mar 2004 04:25:27 +0800, "Simon Kwong" <sktw@hotmail.c om> wrote:
    [color=blue]
    >Hi,
    >
    >I am learning c++ and would like to write a simple C++ compiler. How should
    >I start?
    >
    >Please help.[/color]

    Start by writing a simple <something simpler than C++> compiler. The first
    compiler I wrote (back in 1978) was for a subset of BASIC that generated
    intermediate code and then interpreted it. I was able to cannibalize a few
    pieces of that when I tackled writing a small C compiler the next year.

    After that I swore off writing compilers for the rest of my life ;-)
    -leor
    [color=blue]
    >Simon
    >[/color]

    Leor Zolman
    BD Software
    leor@bdsoft.com
    www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
    C++ users: Download BD Software's free STL Error Message
    Decryptor at www.bdsoft.com/tools/stlfilt.html

    Comment

    • Mike Wahler

      #3
      Re: How to write a C++ compiler

      "Simon Kwong" <sktw@hotmail.c om> wrote in message
      news:c25eis$sej $1@hfc.pacific. net.hk...[color=blue]
      > Hi,
      >
      > I am learning c++ and would like to write a simple C++ compiler.[/color]

      No such thing.
      [color=blue]
      > How should
      > I start?[/color]

      With extensive research. Look up terms 'lexical' 'analysis',
      'parsing', 'grammar', etc.

      BTW why do you feel you'll be capable of writing a C++
      compiler before you've actually learned (very well) how to
      *use* the language?

      Also note that there's no requirement to *use* C++ (or any other
      particular language) to write a C++ compiler. (Although you'll
      almost certainly need to learn assembly language for the platform(s)
      you intend to target.)

      -Mike


      Comment

      • Alf P. Steinbach

        #4
        Re: How to write a C++ compiler

        * "Simon Kwong" <sktw@hotmail.c om> schriebt:[color=blue]
        >
        > I am learning c++ and would like to write a simple C++ compiler.[/color]

        A C++ compiler is definitely a non-trivial piece of software.

        If you're learning C++ then you have no chance of creating a C++ compiler
        in C++.

        It's like: "I'm learning science and would like to make a moon rocket".


        [color=blue]
        > How should I start?[/color]

        Many universities offer courses where you get to create a compiler.

        The "Dragon book",
        <url: http://www.amazon.com/exec/obidos/tg/detail/-/0201100886/002-2510887-2360043?v=glanc e>
        is a good place to start for learning about writing compilers.

        For learning C++, on the other hand, it very much depends on what you already
        know. If you are proficient in some other programming language then the
        recommended book is "Accelerate d C++" by Koenig and, darn, memory failure. Moi?
        Otherwise take a look at e.g. "You can do it!" by Francis Glassborow. Not that
        I've read the book but Francis is a very competent person who writes very clearly
        and honestly. See <url: http://www.spellen.org/youcandoit/>.

        Comment

        • Dave

          #5
          Re: How to write a C++ compiler


          "Simon Kwong" <sktw@hotmail.c om> wrote in message
          news:c25eis$sej $1@hfc.pacific. net.hk...[color=blue]
          > Hi,
          >
          > I am learning c++ and would like to write a simple C++ compiler. How[/color]
          should[color=blue]
          > I start?
          >
          > Please help.
          >
          > Simon
          >
          >[/color]

          Compilers in general, and C++ compilers in particular, are among the most
          complex pieces of software out there. If what you implement is truly C++ in
          its entirety, you will have created a *very* non-trivial piece of software.
          A small subset of C++ would be much more manageable. Specifically, I would
          leave out inheritance, templates, namespaces and exceptions for sure and
          would pick-and-choose other stuff to leave out. I suggest starting by
          taking a university course in compiler construction. Make sure it's a
          course that involves a project (any decent course will). After such a
          course, you will realize that you are still a long way from being able to
          write a C++ compiler.

          As a point of interest and curiosity, can anybody think of problem domains
          more complex than full-fledged optimizing compilers for extremely robust
          languages such as C++? Of course, "complex" is subjective, but I suppose a
          reasonable interpretation would be that in addition to being large (i.e. a
          large number of lines of code are necessary) an in-depth knowledge of
          several areas of Computer Science is required (formal languages, parsing,
          classic algorithms / data structures, architecture, etc...).


          Comment

          • Alf P. Steinbach

            #6
            Re: How to write a C++ compiler

            * "Dave" <better_cs_now@ yahoo.com> schriebt:[color=blue]
            >
            > As a point of interest and curiosity, can anybody think of problem domains
            > more complex than full-fledged optimizing compilers[/color]

            It's difficult, but yes, I'd say a real-time operating system would qualify.

            In the other direction, perhaps the OP is interested in something _easier_
            than a full-fledged optimizing compiler.

            The first useful language I made was a hierarchical thing for defining function
            keys on Hewlett Packard text terminals. It was part of a generalized help
            system. A "program" consisted of a simple text file with nested definitions,
            which were interpreted once and "compiled" into internal data structures. Of
            course, today we'd just use someone else's XML parser or such. But I think for
            someone learning C++ even using someone else's XML parses might be challenge
            enough, and it might actually be easier to just write the interpreter directly.

            Comment

            • Mike Wahler

              #7
              Re: How to write a C++ compiler


              "Alf P. Steinbach" <alfps@start.no > wrote in message
              news:40464411.4 03807468@news.i ndividual.net.. .[color=blue]
              > * "Simon Kwong" <sktw@hotmail.c om> schriebt:
              >
              > For learning C++, on the other hand, it very much depends on what you[/color]
              already[color=blue]
              > know. If you are proficient in some other programming language then the
              > recommended book is "Accelerate d C++" by Koenig and, darn, memory failure.[/color]
              Moi?

              Andrew Koenig & Barbara Moo.



              I'll second Alf's recommedation of this great book.

              -Mike


              Comment

              • E. Robert Tisdale

                #8
                Re: How to write a C++ compiler

                Simon Kwong wrote:
                [color=blue]
                > I am learning c++ and would like to write a simple C++ compiler.
                > How should I start?[/color]

                Excellent idea!
                Send email to Bjarne Stroustrup.
                Ask him for any good leads on text books
                for compiler design and implementation
                that feature a simple C++ compiler design.

                Comment

                • Claudio Puviani

                  #9
                  Re: How to write a C++ compiler

                  "Alf P. Steinbach" <alfps@start.no > wrote[color=blue]
                  > * "Dave" <better_cs_now@ yahoo.com> schriebt:[color=green]
                  > >
                  > > As a point of interest and curiosity, can anybody think of problem domains
                  > > more complex than full-fledged optimizing compilers[/color]
                  >
                  > It's difficult, but yes, I'd say a real-time operating system would qualify.[/color]

                  Having worked on both, I can tell you without hesitation that writing a
                  real-time operating system is far less complex than writing a compiler for a
                  non-trivial language. Once you get the scheduler and IPC right, the rest falls
                  in place, though what's left is time consuming. I don't see any small subset of
                  C++ that would allow all the other features to become trivial to implement.

                  Claudio Puviani


                  Comment

                  • Makhno

                    #10
                    Re: How to write a C++ compiler

                    > Also note that there's no requirement to *use* C++ ...[color=blue]
                    > to write a C++ compiler.[/color]

                    That's a relief. Would've been tricky writing the first one otherwise.


                    Comment

                    • Julie

                      #11
                      Re: How to write a C++ compiler

                      Simon Kwong wrote:[color=blue]
                      >
                      > Hi,
                      >
                      > I am learning c++ and would like to write a simple C++ compiler. How should
                      > I start?
                      >
                      > Please help.
                      >
                      > Simon[/color]

                      If you are set on writing a C++ compiler, you might want to look at an open
                      source compiler, and slowly modify it until you get the hang of the language
                      and the compiler, then start on your own.

                      Conversely, something that may prove as much (if not more) of a learning
                      experience is to write a C++ front-end that takes (simple) C++ code as input,
                      and outputs C code. This is how C++ was originally implemented. This would
                      give you a pretty good immersion into C++, you would be able to understand the
                      implementation details of the language, and you wouldn't have to get tied to a
                      particular architecture or worry about machine code generation, linking, and
                      similar that have little to do w/ the language.

                      Comment

                      • Dan Cernat

                        #12
                        Re: How to write a C++ compiler


                        "Makhno" <root@127.0.0.1 > wrote in message
                        news:c25rlj$3gi $1@newsg4.svr.p ol.co.uk...[color=blue][color=green]
                        > > Also note that there's no requirement to *use* C++ ...
                        > > to write a C++ compiler.[/color]
                        >
                        > That's a relief. Would've been tricky writing the first one otherwise.
                        >[/color]
                        AFAIK the first Pascal compiler was written in Pascal. They compiled it by
                        hand. I guess it wasn't very complicated. Could be a urban legend, though.


                        Comment

                        • Julie

                          #13
                          Re: How to write a C++ compiler

                          Dan Cernat wrote:[color=blue]
                          >
                          > "Makhno" <root@127.0.0.1 > wrote in message
                          > news:c25rlj$3gi $1@newsg4.svr.p ol.co.uk...[color=green][color=darkred]
                          > > > Also note that there's no requirement to *use* C++ ...
                          > > > to write a C++ compiler.[/color]
                          > >
                          > > That's a relief. Would've been tricky writing the first one otherwise.
                          > >[/color]
                          > AFAIK the first Pascal compiler was written in Pascal. They compiled it by
                          > hand. I guess it wasn't very complicated. Could be a urban legend, though.[/color]

                          Bootstrapping. Not the quickest way to work on a compiler I presume, but
                          arguably a good way to test your code and compiler at the same time.

                          From what I recall, a few of the one-man-compiler-teams back in the 80's used
                          this method -- Walter Bright and Zortech C/C++ iirc, et al.

                          Comment

                          • Claudio Puviani

                            #14
                            Re: How to write a C++ compiler

                            "Julie" <julie@aol.co m> wrote[color=blue]
                            > Dan Cernat wrote:[color=green]
                            > >
                            > > "Makhno" <root@127.0.0.1 > wrote in message
                            > > news:c25rlj$3gi $1@newsg4.svr.p ol.co.uk...[color=darkred]
                            > > > > Also note that there's no requirement to *use* C++ ...
                            > > > > to write a C++ compiler.
                            > > >
                            > > > That's a relief. Would've been tricky writing the first one otherwise.
                            > > >[/color]
                            > > AFAIK the first Pascal compiler was written in Pascal. They compiled it by
                            > > hand. I guess it wasn't very complicated. Could be a urban legend, though.[/color]
                            >
                            > Bootstrapping. Not the quickest way to work on a compiler I presume, but
                            > arguably a good way to test your code and compiler at the same time.
                            >
                            > From what I recall, a few of the one-man-compiler-teams back in the 80's used
                            > this method -- Walter Bright and Zortech C/C++ iirc, et al.[/color]

                            In the late 1970s, Drs Yuen and Chung bootstrapped their Tiny Pascal by writing
                            a limited version in Microsoft BASIC and then rewriting the compiler in Tiny
                            Pascal. They also had a BASIC p-code to Z80 translator and a native Z80 p-code
                            interpreter. The whole adventure is chronicled in the excellent, but no longer
                            published, "The Byte Book of Pascal".

                            A few copies still seem to be in circulation in various used book stores:
                            http://search.barnesandnoble.com/Oop...nInquiry%2Easp.

                            Claudio Puviani


                            Comment

                            • KTC

                              #15
                              Re: How to write a C++ compiler

                              "Makhno" <root@127.0.0.1 > for some reason wrote:
                              [color=blue][color=green]
                              >> Also note that there's no requirement to *use* C++ ...
                              >> to write a C++ compiler.[/color]
                              >
                              > That's a relief. Would've been tricky writing the first one
                              > otherwise.
                              >
                              >[/color]

                              The first C++ compiler was written using C++ !

                              Driving innovation for over 140 years, AT&T Labs is the global leader in development and research of connectivity and technological advancement


                              --
                              Experience is a good school but the fees are high.
                              - Heinrich Heine

                              Comment

                              Working...