Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 266
  • Last Modified:

Writing a language compiler


I have a project to write a C compiler for windows with limited functionality. My problem is, that I can write parsers using bison or ANTLOR, but how can I write code to produce an executable from it. That is, I wan't to know, I I wan't to output an excutable file for windows. Is there any help available anywhere.

regards and thanks

Abhinay Sinha
1 Solution
Harisha M GCommented:
Hi abhinaysinha,

This may be helpful (Check out free compilers with source code)

This might be a good place to start getting an understanding of the win32 executable format.


Your compiler should ultimately output machine code, in a file that conforms to the Microsoft specification.
The *easy* thing to do is to have your language compiler convert the target language into another language such as vb.net or java, or C++ and then use the corresponding compiler to generate the .EXE for you. Id recommend using the Gnu C++ compiler if you want to go down this path.

For example for your pseudo c like language, you write a converter that translates that language and writes out standard ansi c++ and then you use any standard c++ compiler against your translated source code which will generate the binary .exe for you.

On the other hand you could spend a few years writing a compiler that directly spits out x86 assembly, encapsulated in a machine specific executable format such as windows .exe but why bother when its already done for you, unless its an academic exercise. I had to write compilers in my Computer Science courses but none that had to write out a windows .exe directly. We just wrote our compiler to write out byte code, and then wrote an interpretor to execute our byte code.

In either case you might be interested in trying to make your compiler output .NET client runtime byte code, or Java byte code directly and get the .NET linker, or Jit linker to make a pass against your byte code and generate an .exe for you.

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Treat the parsings as a stack, containing certain information that cannot be processed until a certain condition is met, such as end of line of close paren or right brace (depends on language).

You have to come up with a syntax to decode what to do with a statement based upon the formats discovered during the parsing. It must be unambiguous, so keep it real simple, defer how to do nesting, if you can, until later.

Checl out BNF

Consider making another step, of a creation of a table, where each row is a coded statement to be processed by your fixed set of rules
abhinaysinhaAuthor Commented:
Thanks for the reply to all.

But the links pointed by SCDMETA is the most appropiate for me.
>  conforms to the Microsoft specification

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now