Compiler in C?

This is a discussion on Compiler in C? within the C Programming forums, part of the General Programming Boards category; Hello I am new to C programming, and I am wondering how to (if it is possible and I really ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    12

    Compiler in C?

    Hello I am new to C programming, and I am wondering how to (if it is possible and I really hope it is possible) to have an executable.. when run, write another program... compile it, and run it?

    Say for example, there is a program that will make an executable that works based on the user input?
    Or even an executable that clones itself when run? (I know it could be used maliciously but those are not my intentions)

    Thanks, Jonny
    Last edited by Jonnyb42; 05-13-2009 at 08:10 PM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Jonnyb42 View Post
    Hello I am new to C programming, and I am wondering how to (if it is possible and I really hope it is possible) to have an executable.. when run, write another program... compile it, and run it?

    ~Jonny
    Sure, you could make an external call to a compiler, check the output, and run the result.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    12
    Wow sweet!! Now, how hard is it to write a C compiler in C only? I mean like one which is as simple as possible, doesn't need GUI or special features or whatever, just for a call to compile like you said.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by Jonnyb42 View Post
    Wow sweet!! Now, how hard is it to write a C compiler in C only? I mean like one which is as simple as possible, doesn't need GUI or special features or whatever, just for a call to compile like you said.
    Take a look at tcc (Tiny C Compiler). It's about as small as you'll get, and it's about 10,000 lines.

    Oh and the answer is somewhat, "very hard".

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    12
    thanks guys this is awesome!
    Just some extra questions, Why is it so hard to write a compiler, or why does it take so many lines? Also, if anyone knows robotics, but if you had a microntroller, is it possible to have it hook up to another microcontroller and load its instructions on it, without an external programmer?(in the C language of course)

    (edited):
    Actually, could you tell me how to use TCC, perhaps compile commands in C or how to implement it in code?
    I have Vista in case that would change things.

    Thanks again,
    Last edited by Jonnyb42; 05-13-2009 at 09:03 PM.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    10000 lines for a compiler is very small (hence the name "tiny c compiler").

    A "proper" C compiler is probably 100K-1M+ lines of code. gcc is absolutely enormous, but it also supported more than a dozen different processor families, multiple language variants (e.g. C++, Objective C, standard C) and several dialects/standards (e.g. C std89, C std99, gcc99), many optimization options, etc, etc.

    There are two main difficulties in writing a compiler:
    1. Parsing the language.
    2. Generating meaningful code output.

    C is a not a huge language, but there are some rather complex constructions, and there are quite a few different operators.

    There are other languages that are easier to implement.

    Have a look at the MiniBasic implementation we've (me and a few others on this forum) done here:
    matsp / minibasic / overview — bitbucket.org
    It is about 3300 lines of C++ code.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Can you even write a complete C compiler in C? What about assembly? Most compilers produce assembly code. You'll have to be fluent in this

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,941
    Quote Originally Posted by Brafil
    Can you even write a complete C compiler in C?
    Yes.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    It takes so many lines to write a compiler because there are so many parts to it. There is lexigraphical analysis, parsing, semantic analysis, intermediate representation, instruction selection, register allocation, and output generation. Writing a compiler is always a big task and requires a lot of attention. And yes, a compiler can be written completely in c. There are many of these.

  10. #10
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Ok. I meant "without any other language involved". Such as machine language (not a "real" language, though) as the output. But you're right.

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Brafil View Post
    Ok. I meant "without any other language involved". Such as machine language (not a "real" language, though) as the output. But you're right.
    Brafil has a point here. How could you write a compiler that produces executable objects without including a concrete translation into assembly (not to say I don't believe it, just kind of curious about how)?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    By direct translation into binary. But that would really be awful. At least assembly should be involved.

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Brafil View Post
    Can you even write a complete C compiler in C? What about assembly? Most compilers produce assembly code. You'll have to be fluent in this
    The compiler generates assembler code (or the binary equivalent thereof), but the code to do the actual compilation is normally written in C. Naturally, the VERY FIRST C compiler was NOT written in C, because there was no C compiler to compile it with.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by matsp View Post
    The compiler generates assembler code (or the binary equivalent thereof), but the code to do the actual compilation is normally written in C.
    But surely, sir, there is a ton of __inline asm__ in there?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,941
    Quote Originally Posted by MK27
    But surely, sir, there is a ton of __inline asm__ in there?
    That would not be necessary at all. Remember, we are talking about what can be done, not what is typically done.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiler Paths...
    By Cobra in forum C++ Programming
    Replies: 5
    Last Post: 09-26-2006, 04:04 AM
  2. C Compiler and stuff
    By pal1ndr0me in forum C Programming
    Replies: 10
    Last Post: 07-21-2006, 11:07 AM
  3. I can't get this new compiler to work.
    By Loduwijk in forum C++ Programming
    Replies: 7
    Last Post: 03-29-2006, 05:42 AM
  4. how to call a compiler?
    By castlelight in forum C Programming
    Replies: 3
    Last Post: 11-22-2005, 10:28 AM
  5. Bad code or bad compiler?
    By musayume in forum C Programming
    Replies: 3
    Last Post: 10-22-2001, 09:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21