Thread: General question about programming languages...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    16

    General question about programming languages...

    I could not find any better topic to ask this. This question is generic and not only related to C programming. If it is not suited to this forum please feel free to remove it.

    My question is related to which programming language compilers are programmed. I mean, Is a GCC programmed in assembly? Or ForTram? How about C++ compilers, are they programmed in C, or a lower level programming language. Usually a high level programming language as Java, C# is programmed in C++? Is there any common sense about this, or is it completely random?

    Let me know if i was kind confusing.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Well, yes it is confusing....

    When working with Pascal one of the tests of the compiler was to make it compile a copy of itself... That is, most Pascal compilers were written in previous versions of Pascal. I believe the first one was written in C.

    C compilers (and not a few others) are usually written in mixtures of C and ASM.

    C++ was originally programmed in C but now it's largely like Pascal, written in C++

    So... no there's no real logic to it. You can write a compiler in any language that compiles to machine code.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    16
    Wow, i would never expect that a compiler it is writen on a previous version of its own language. .. very weird. .... well thank you about the answer!

    Any other comments are still accepted!!

    Thank you!

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Is there any common sense about this, or is it completely random?
    A compiler developer is going to write in whatever language he prefers for the task; often, this is the target language, which makes some sort of sense. If you're writing a compiler for a language, you're probably comfortable with that language.

    There will probably be some limitations. Ruby (which is not a compiled language) is written in C, presumably in part for speed—although large parts of its standard library are in Ruby. There's also the problem of bootstrapping: how can you build a compiler for the first time? Generally it's not hard: you use another language, and from that point on, you can write a compiler in its own language (so we've had C-based C compilers for decades). But for end-users it's another matter. Most development systems will have C, so often a language will provide a stripped-down C version of its compiler. This compiler is built and used to build a full-featured compiler in its own language.

    Compilers aren't always implemented in terms of themselves, though: Clang, the LLVM-base C compiler, is written in C++. That works best for the developers, so it's what they chose.

    The common sense is, use what you know. It's not random, but choices based on a developer's preferences combined with the reality of what various languages are able to do.

  5. #5
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    This is something that confused me when I was learning--how can a compiler for a certain language be written in that language? You have to realize that all it's doing is analyzing text and converting it to machine instructions, which it dumps to a file. If you want to oversimplify it, it's just table lookups.

    Ruby (which is not a compiled language) is written in C, presumably in part for speed
    Just had to comment, that's too funny to say considering that Ruby is one of the slowest languages available currently. I think BASIC is faster than Ruby.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by Sink0 View Post
    I could not find any better topic to ask this. This question is generic and not only related to C programming. If it is not suited to this forum please feel free to remove it.

    My question is related to which programming language compilers are programmed. I mean, Is a GCC programmed in assembly? Or ForTram? How about C++ compilers, are they programmed in C, or a lower level programming language. Usually a high level programming language as Java, C# is programmed in C++? Is there any common sense about this, or is it completely random?

    Let me know if i was kind confusing.
    The very first C compiler was written in assembly. Rewritten in C, it was then, well...recompiled. It's a sort of "boot-strapping" kind of thing, really; once you are able to write the compiler in the language itself, whole new possibilities open up; you can use all of the fancy data structures and methods to reimplement any aspect of the compiler that you wish. Pretty useful technique, actually.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    A better question would be: what was the very first compiler written in? A needle & a magnet?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  8. #8
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by cpjust View Post
    A better question would be: what was the very first compiler written in? A needle & a magnet?
    In was built by using pure assembly.

    But i have an even better one:
    How was the first assembler built? ( I can't imagine people programming in machine code! )
    Devoted my life to programming...

  9. #9
    Registered User
    Join Date
    Jul 2007
    Posts
    131
    Quote Originally Posted by CommonTater View Post
    C compilers (and not a few others) are usually written in mixtures of C and ASM..
    I would drop the assembly part out of that.

    Quote Originally Posted by Sipher View Post
    How was the first assembler built? ( I can't imagine people programming in machine code! )
    First people programmed in machine code, then came hand assembling and finally assemblers.

    And in fact first C++ compiler was written in C++.
    Last edited by fronty; 10-23-2010 at 08:16 AM.

  10. #10
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by fronty View Post
    And in fact first C++ compiler was written in C++.
    How was that possible?!
    Devoted my life to programming...

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Sipher
    How was that possible?!
    Read Stroustrup's answer to the question Which language did you use to write C++?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Well, how about that?! You never know enough!...
    Devoted my life to programming...

  13. #13
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    The question becomes how does one program in machine code on today's PCs which don't have any front panel of toggle switches to enter in binary op codes and registers? It can't be done any more.

    I guess at some point someone has to transfer a ROM chip with boot code to a new machine and give it at least some smarts that way. Smarts enough to be able to respond to keyboard, some ports, etc. Disk drives already come with controllers that are micro processors themselves and have firmware programs in them.

    I came from the good ol' days where mini computers had lights and switches on their front panels. The thing can be shipped totally empty. You put in a rudimentary loader into 64 words of memory. That's enough to read a paper tape through the teletype machine. The paper tape contained the BASIC interpreter and minimal I/O drivers for card reader, printer, cassette tape storage system.
    Last edited by nonoob; 10-25-2010 at 04:06 PM.

  14. #14
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    You can still build one yourself. I know a prof of mine has been building a computer on a breadboard using discrete transistors.

  15. #15
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by CommonTater View Post
    So... no there's no real logic to it. You can write a compiler in any language that compiles to machine code.
    More specifically, you can write a compiler in any language. It doesn't have to compile to machine code...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. general question about how struct works.
    By nullifyed in forum C Programming
    Replies: 14
    Last Post: 06-21-2010, 06:03 AM
  2. General a.out redirection question under Linux
    By merixa in forum C++ Programming
    Replies: 3
    Last Post: 11-07-2005, 05:36 PM
  3. A general question
    By vampire in forum C Programming
    Replies: 7
    Last Post: 11-06-2005, 09:53 PM
  4. general question regarding a function parameter
    By mlupo in forum C Programming
    Replies: 7
    Last Post: 10-13-2002, 07:32 PM