I have a simpile question. I have a string like this.
"Num == 1"
Is it possible to extract this in any way so it will executable code in:
int Num = 1;
if ( Num == 1 ) // To extract that string and put that string inside like this here ?
{}
I have a simpile question. I have a string like this.
"Num == 1"
Is it possible to extract this in any way so it will executable code in:
int Num = 1;
if ( Num == 1 ) // To extract that string and put that string inside like this here ?
{}
To my knowledge, you can't do that in C/C++, at least not in standard C/C++. I'll be happy to learn along side you if I'm wrong.
>Is it possible to extract this in any way so it will executable code
Yes, but it's extremely complicated because you basically need to write a C++ interpreter.
I dont really know myself. My idéa without knowing anything how this could be done was to split this up in:
"Number" "==" "1" and put this to a vector in anyway and let them point to real variables etc in the acutal code and then let the vector do a loop inside () where it goes through each element like this where the above text is what each string point to.
Perheps this only works in my fantasie.
Number == 1
"Number" "==" "1"
To my knowledge, you can't do that in C/C++, at least not in standard C/C++. I'll be happy to learn along side you if I'm wrong.
Somehow you would have to change the string with the value of Number into a variable with the name of Number. Narue says that to do that you basically need to write your own interpreter, which is basically a compiler, which is a lot of work. If you explain your project maybe someone can offer you another approach, either within C/C++ or using some other language it it's more appropriate to do so.
What I basically will do is to interact with an if-statement from the applicaton through a .txt file wich could be read in into a string.
What I will write in this .txt file could be: (Assume everything I write is existing variables inside the C++ code)
So this will be put ta A string and from there be parsed out to idenify with exisisting variables already defined in the C++ code.
((Number1 >= Number2) && (Number3[1] > Number4[1]))
So for the if statement that exist inside the C++ code it will look like this:
So the code between if ( ) is always this string that will be "translated" like this.
if ( ((Number1 >= Number2) && (Number3[1] > Number4[1])) )
{
}
Is an interpreter the only way to achive something like this ?
Somehow you would have to change the string with the value of Number into a variable with the name of Number. Narue says that to do that you basically need to write your own interpreter, which is basically a compiler, which is a lot of work. If you explain your project maybe someone can offer you another approach, either within C/C++ or using some other language it it's more appropriate to do so.
>Is an interpreter the only way to achive something like this ?
Yes. Depending on the kind of complexity you need, it may be vastly simpler than a full C++ parser, but you're still looking at parsing at least partial C++ expressions. If you really need this kind of functionality, you'd best look at another language that supports an exec function, such as Perl or Python.
Okay, thank you. I will look around a little and see :)
I wonder though if there coulde be any good sites on how to begin writing an interpreter
on the internet for just C++. A really beginner guide perheps.
Okay, thank you. I will look around a little and see :)
I wonder though if there coulde be any good sites on how to begin writing an interpreter
on the internet for just C++. A really beginner guide perheps.
I doubt it; beginners don't write interpreters.
>I wonder though if there coulde be any good sites on how to begin writing
>an interpreter on the internet for just C++. A really beginner guide perheps.
The two most complicated pieces of software you can write would probably be an operating system and a C++ compiler. Just to give you an idea of the water you're trying to jump into.
Your first step should be to severely limit the expressions you want to support. Then grab the relevant grammar from the C++ grammar, and come up with a parsing scheme. If your grammar is simple enough, you can use a straightforward state machine or stack based parser parser. Both of those are relatively easy to understand and accessible to beginners.
A good introduction to language parsing would be writing a calculator that supports infix notation. Another is a boolean expression evaluator.
Yes I find it almost impossible to know how to write such as an interpreter though I dont know how it will excatly work.
I think it is to difficult to learn from a Forum, I am not sure. I need to first only know the whole process in "exact described" words how ex "Num == 1" will be translated and understood as executable code.
Anyway it feels good to know that this probably is one of the moste complicated thing you can write :)
>I wonder though if there coulde be any good sites on how to begin writing
>an interpreter on the internet for just C++. A really beginner guide perheps.
The two most complicated pieces of software you can write would probably be an operating system and a C++ compiler. Just to give you an idea of the water you're trying to jump into.Your first step should be to severely limit the expressions you want to support. Then grab the relevant grammar from the C++ grammar, and come up with a parsing scheme. If your grammar is simple enough, you can use a straightforward state machine or stack based parser parser. Both of those are relatively easy to understand and accessible to beginners.
A good introduction to language parsing would be writing a calculator that supports infix notation. Another is a boolean expression evaluator.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.