How to create a compiler in vb.net

Before answering this question, understand that I am not asking how to create my own programming language, I am asking how, using vb.net code, I can create a compiler for a language such as vb.net. In fact, the user enters the code, he receives the .exe. NO MEANS, I want to write my own language, because it seems that other questions related to the compiler are being asked here. I also do not want to use the vb.net compiler itself and I do not want to duplicate the IDE.

The exact goal of what I want to do is quite difficult to explain, but all I need is to push in the right direction to write a compiler (if possible, from scratch) that can simply enter input and create .exe. I opened the .exe files as plain text before (my own programs) to see if I could get any sense from what I assumed would be a human readable text, but I was clearly disappointed to see random ascii although it’s clear why this is all I found.

I know that a .exe file is just lines of code processed by the computer on which it is included, but my question here really comes down to the following: what code is .exe? How could I do this in a text editor if I wanted to? (No, I do not want to do this, but if I understand the process, my goals will be much easier to achieve.) What makes an executable file an executable file? Where is the code logic?

This is supposed to be a programming issue, not a computer issue, so I did not post it on SuperUser. I know a lot of information about the System.IO namespace, so I know how to create a file and write to it, I just do not know what exactly I would like to place inside this file in order to make it work as an executable file.

I apologize if this question is "confusing", "dumb" or "obvious", but I could not find any information about the actual contents of the executable anywhere.

One of my Google searches

Something looked promising

EDIT: The second link here, while it looked good, was a complete failure. I am not going to spend hours of my time hitting keys and recording results. "Use" Alt "and 3-digit combinations to create characters that do not appear on the keyboard, but which you need in the program." (step 4) How do I know which characters I need?

Thank you very much for your help, and my apologies if this question is nooby or "bad".

To summarize simply: I want to create a program in vb.net that can compile code in a specific language into a single executable file. What are the methods that can allow me to do this, and if they are not, how can I start writing my own from scratch?

+4
source share
9 answers

What you are asking is a rather complicated question. Of course, at its core, it looks pretty simple:

  • Interpret the code itself
  • Write interpreted code

but each of these steps can be quite intense. Step 1 should be somewhat achievable with some time and a lot of elbow lubrication - you need to analyze the code for several control instructions based on the language specification. See http://en.wikipedia.org/wiki/Parsing for more details. Essentially, you are transforming the entered code into a common format that represents the functionality you want.

After you have analyzed the code, the next step is to accept this analyzed code and convert it into machine-executable code (usually an assembly, although with VB.NET you can write Microsoft Intermediate Language code as output, and then run it in the CLR). This is what actually creates the executable so that the computer runs the program.

Unfortunately, the best advice to solve this problem is either:

  • Go buy some books in a programming language, machine code, assembly language, compilers, etc., then spend several months or years reading and experimenting until the knowledge you get from books leads to writing a successful compiler.
  • Enrolling in a computer science program at a local university. Writing compilers and programming languages ​​is usually covered at the initial level during the second or third year of study in the field of computer science, and then much deeper at the graduate level.

Good luck

EDIT: if all you are looking for is a way to write code and then write a way to execute it, you can try writing a translator for one of the existing scripting languages ​​- Ruby, Python, Lua, etc.

+8
source
Process.Start(String.Format("vbc.exe {0}", sourceFilePath)) 
+4
source

Here is one book that explains all this at a very basic level. Including sample code that you can get in a few hours.

As with many programmers, you will need many months of training to complete what you want. Good luck

+2
source

Let me begin by saying that this is perfectly doable. Ignore the skeptics.

Good, so it looks like you have bitten off more than you can chew. I would suggest overestimating your goal a bit. It seems that you want to know how compilers work, and writing the VB.net compiler is your project to get started.

Instead, try writing an interpreter that is much smaller and simpler. Here's how you do it:

  • Write a parser for a very small language, perhaps only to support assignment operators. Use parser tools like Antlr . This will build an AST ("abstract syntax tree" - that is, several classes or objects representing the syntax of the program, no crap, like half-columns, like a tree), which will look something like this:

alt text

  • then you go through the AST and just do it. Google for the "AST walker". So, to assign x = 5 create a hash table entry for "x", assign it 5, then go to the next statement.

  • Keep adding features as you go.

After you get the full language, you probably have learned enough to understand compiler books. Do not use the book of dragons, try instead the book of Appel or Cooper / Torchon. If you prefer there are online books, I have never tried them.

When you move on to writing a compiler, you simply change the bit that the AST executes to the one that generates the assembly (or C, if you prefer), which will do the same thing when it starts.

I will give that this seems like a daunting task, but if you stick to something simple to get you started, you will get something that works for a few days at most. In a few months, you will build it on something like what you are looking for. Good luck.

At the end of your question:

You don't seem to know how executables are created from code. Since the 80s, people have not agreed with six in their compilers, and, hopefully, not so much. Basically, after parsing the code, you will go through a series of steps that make the code simpler. In the end, you have something close to assembly. Then you create the assembly, and the assembler and linker conspire to turn it into an executable file.

+2
source

The Visual Basic.NET compiler comes free as part of the .NET Framework . You don't even need SDKs or Express Editions. The compiler for VB (and C #) is located in c: \ windows \ microsoft.net \ framework [version] \ vbc.exe (or csc.exe for C #). Therefore, any computer that can run the VB.NET program can compile it .. The .NET Framework also includes the System.CodeDom namespace , which provides a way from inside the program to compile the program either from a document model or from a string (or file) into an assembly. NET (e.g. .exe or .dll) and generate the code in both VB and C #.

Hi,

Anthony D. Green | Program manager | Visual Basic Compiler

+2
source

You create a compiler for vb.net in the same way as any other compiler. You need a lexer / parser. Entire books have been written on this subject, the most famous of which are probably The Dragon Book .

To give a definitive answer: No, you cannot create a decent compiler that will generate an executable using Notepad. You need a compiler to convert from human-readable text to a machine language (assembly or IL) that the linker or interpreter can execute.

+1
source

You can try checking out my tutorial at http://www.icemanind.com

This is a tutorial on creating your own virtual machine and assembler written in 100% C #.

+1
source

Cyclone, I wonder what you are trying to achieve? You say: “The exact purpose of what I want to do is quite difficult to explain” and “essentially, the user enters the code, he receives .exe.”

If you just want the user to be able to enter the code and then execute it, you can consider the existing scripting language. VBScript is built into Windows, and the language is pretty similar to VB.Net, or there are some great free languages ​​that you can download, like Python .

If the user really needs to be able to create .exe - I think it could be a script, perhaps then why not use an existing free compiler like FreeBasic , or even Visual Basic.Net Express Edition .

0
source

I am doing a tutorial for this on my website http://dgblogs.weebly.com . It is written in C #, and you can create your own programming language on your computer!

You will never see this syntax in my tutorials:

 Console.Readline(); 

My site is currently offline right now, GOOD LUCK!

Thanks, DgBlogs

0
source

All Articles