Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open source C compiler in C#? [closed]

Tags:

I've been getting into compiler creation. I've found some terrific beginner stuff and advanced stuff but nothing in the middle. I've created 3 different simple proof-of-concept compilers for toy languages but I want to expose myself to something real.

The most straight forward real language in terms of syntax seems to be C. Since the language I'm most comfortable with right now is C#, I'd love to study the source code of a real non-tutorial C compiler written in C#. Does one (with source code available) exist?


Ideally I'd like a C compiler, not a .NET or C# compiler, but with the source code written in C#.
I know C# --> C feels a little backwards but it'll allow me to ease deeper into compilers starting with a familiar language before I go changing that too.

Although I'm not looking for C#/.NET compilers, here are some in case someone sees this question who is looking for that:

  • Create a Language Compiler for the .NET Framework
  • Mono C# Compiler
  • Source for a C# compiler written in pure C# (.NET v1) (thanks Luiscencio)
like image 678
Dinah Avatar asked Mar 17 '10 17:03

Dinah


People also ask

Is the C compiler open-source?

The most notable example is GCC's C compiler, which is all under the GNU General Public License (GPL), an open-source license.

What is a open-source compiler?

An open source C compiler is a piece of software intended to turn human readable programming code into binary code that can be executed natively by a specific computer system. The C programming language and compilers strive to be platform independent, allowing for a programming paradigm of write once, run anywhere.

Are C compilers free?

Pelles C is a free development kit for Windows and Windows Mobile containing an optimizing C compiler, a macro assembler, a linker, a resource compiler, a message compiler, a make utility and installs builders for both Windows and Windows Mobile.

Which compiler is used in C?

There are many compilers for C, but we will focus on a free open source version called the Gnu C compiler. (Actually we will use the Gnu C++ compiler, but all C programs compile using this compiler).


1 Answers

You are going to have a hard time finding sample code. Compiler writers use bootstrapping. The first C compiler was written in B. Which was then used to write the first C++ compiler. Which was used to write the C# compiler. Which is very commonly used to write compilers for managed code.

This is not a process that ever goes backwards. Although side-ways was common, C compilers often were used to cross-compile a compiler for another operating system.

I think I used this book, it has terrific C compiler code in the appendices. Written in C. I used parts of it when writing a Basic compiler I needed in a large project. The expression parser is hard to get right, it has an elegant solution for the operator precedence rules.

Targeting a managed language is the easier way to get this going. The language shouldn't matter too much, it is getting it working that is the real challenge. Even though it is a lot easier to get managed code working. If you want to target C, you'll need black-belt machine code skillz and deep insight in the object file format and the linker.

like image 111
Hans Passant Avatar answered Oct 08 '22 10:10

Hans Passant