Building a compiler steps [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have been thinking about building my own compiler for a while and a few days ago I finally started on it. My compiler works like this:
Parse code from my own file. (With a .exe file made with c++)
Create assembly code
Create a file containing those assembly code
Compile that assembly file if it is made (done with a vbs script)
Link the .obj file
And we have our .exe file
Now I am having difficulties with finding the best way to parse my code. I haven't really made this yet but I will put my ideas here.
Find all variables and declare them. variables will be preceded with a 'var ' (for now). uninitialized variables will be put in the .data? section and initialized ones in the .data section.
Find the main procedure and start executing the functions and operations.
Now I was simply wondering if someone can improve my ideas. Or if someone has a better idea to make some kind of compiler and your own programming language.

Get yourself a copy of A. V. Aho, M. S. Lam, R. Sethi, J. D. Ullman: Compilers: Principles, Techniques, and Tools and start studying
The book covers the necessary theoretical background, especially:
Context-free grammars
Recursive-descent, LL, LR parsing
Symbol handling
Intermediate representation

Related

How is visual programming using nodes and links converted into code? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
as title says, i want to know what is logic behind visual programming and how is it being converted into code?
Use for example unreal engine 4 blueprints, how it all works behind scenes?
I somehow think everything is made using linked structures with pointers... For example:" if " statement needs link of statement and to output links, wether that statement is true or false... But if we do all like that, programming every keyword seperatly, i think it would take too much time.
I hope you understand what im thinking.
I would from the point of view of c++ explaination if possible..
Usually data flow graphs are not converted into C, C++ or similar before being compiled. A visual representation of a data flow graph is much closer to an Abstract Syntax Tree (AST) than written programming language code.
In fact, for most programming language compilers among the first steps carried out is the translation of a the written code into an AST, and it's a trivial task to feed such a AST into a program like graphviz to generate a call graph or similar from it.
So in a sense, that data flow graph of Unreal, or LabVIEW or similar programming environments is already one step closer to the executable code. Hence regarding your questions it's better suited to reformulate it as "how is written programming language parsed and translated into a data flow graph – like you can draw in Unreal engine – or an AST?".
Unfortunately the answer to this would go vastly beyond the scope of StackOverflow, and I'll simply refer you to the standard literature on compiler development.

Programatically creating and compiling from a program in C++ [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Let's say we've got a first program called Program1.exe which contains the necessary information to create and compile another application called Program2.exe. Actually it could also load that information from a txt file or whatever.
Googling, I've found that this is "easy" to do in C#, using Visual Studio:
How to programatically build and compile another c# project from the current project
Programmatically Invoke the C# Compiler
The problem is that I'm not using (and can't use) C#, but C++. Summing it up, my question is if that I can do this same thing using C++.
I would prefer to do it without additional libraries, but if that's not possible, or if it's too hard to do, you can also recommend any library allowing it.
I think you'll probably have noticed it, but my goal is to use it under Windows so I don't care if it's not portable.
Thanks everybody.
It's trivial (if maybe a bit odd) for a C++ program to compile and run another based on code stored in a text file. Debugging that other program, however, isn't.

How to approach a C++ parser [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am wanting to have a go at a C++ parser for a formatter I am making.
You can obviously open a file and use getline(..) or get(), is this reasonable way of starting things off and then working out a system using vector arrays and hence creating loads of arrays and somehow structuring out and processing what you are doing from there. For example say I wanted to find ever function in a source file, all functions have the common syntax, "(){" once whitespace has been removed, so do you just look for common delimeters to parse out the sections into arrays. I suppose I will learn as I go.
Or I also assume there are tried and tested ways of doing this, and I would likley just be reinventing the wheel as they say.
C++ is a language that is quite hard to parse in the first place. So if you want anything other that really trivial C++ code to be "understood" by your parser, you are definitely better off starting with an existing product.
The Clang frontend library would perhaps be a good starting point.
There are also a number of "source to source" conversion examples based on clang. Here's one of them: http://eli.thegreenplace.net/2012/06/08/basic-source-to-source-transformation-with-clang/

Compilation process for C++ application? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I have been programming for 2 years now and always have faced difficulty when dealing with the compilation process.
I did not study Computer Science during my Engineering, but necessity drove me towards learning C++.
I tried understanding the compilation process from some blogs, but they were always in a language I could not understand.
So I searched this site for a similar question, but could find none.
So I would like to know how the text from a .cpp is converted to a binary executable?
Basically, the preprocessor runs first resolving all your #includes, #defines, etc with simple text substitution. Then the compiler creates a compilation-unit for each .cpp file which pretty much boils everything down to machine-code except for "connections" or linkages between shared data and functions. There may be many levels of optimisation for speed and/or space performed. This is repeated for all your .cpp files. Finally, a link phase ties all these compilation-units and the libraries they use together into an executable.

structure reading C++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have the following problem:
I have a configuration file that consists a description of fields , which I read it and then parse it. I want to move it into the code to compile it inside.
How would you do that as bug structure ??? or else ?
Thanks
I wouldn't move it into the code, I'd leave the configuration file as a configuration file.
If you really must do this, you can just embed the file as a string resource into the application and use that - that way you'd change only a minimal amount of existing code. The way you do this depends upon your platform.
If thats not feasible (for whatever reason) I'd set up a single configuration class / namespace to contain all the values.
It's not very clear what are you exactly asking.
If you are looking for on-the-fly code execution (like eval() function in some languages), then there is no such thing in C++. It's not an interpreted language which can be read and executed line-by-line, it needs to be compiled every time code changes. While it technically is possible to write self-changing code, it's probably not worth the effort.