So let's say I have a string containing some code in C, predictably read from a file that has other things in it besides normal C code. How would I turn this string into code usable by the program? Do I have to write an entire interpreter, or is there a library that already does this for me? The code in question may call subroutines that I declared in my actual C file, so one that only accounts for stock C commands may not work.
-
1This is an extremely non-trivial exercise, and raises the obvious question: what is the overall goal here? There is probably a better overall solution. – Oliver Charlesworth May 28 '12 at 22:31
-
1Did you try C interpreters ? See http://stackoverflow.com/questions/69539/have-you-used-any-of-the-c-interpreters-not-compilers/72792#72792 . – Scharron May 28 '12 at 22:37
2 Answers
Whoo. With C this is actually pretty hard.
You've basically got a couple of options:
interpret the code
To do this, you'll hae to write an interpreter, and interpreting C is a fairly hard problem. There have been C interpreters available in the past, but I haven't read about one recently. In any case, unless you reallY really need this, writing your own interpreter is a big project.
Googling does show a couple of open-source (partial) C interpreters, like picoc
compile and dynamically load
If you can capture the code and wrap it so it makes a syntactically complete C source file, then you can compile it into a C dynamically loadable library: a DLL in Windows, or a .so in more variants of UNIX. Then you could load the result at runtime.
Now, what normally would lead someone to do this is a need to be able to express some complicated scripting functions. Have you considered the possibility of using a different language? Python, Scheme (guile) and Lua are easily available to add as a scripting language to a C application.

- 110,348
- 25
- 193
- 263
-
K, never mind then! I could certainly reduce the input to a bunch of string keywords, but the ensuing code is going to be so much uglier than what it could have been, but I see why this is so hard. I guess the difficulty of doing this to a language is inversely proportional to how high-level it is! Thanks. – mszegedy May 28 '12 at 23:13
C has nothing of this nature. That's because C is compiled, and the compiler needs to do a lot of building of the code before the code starts running (hence receives a string as input) that it can't really change on the fly that easily. Compiled languages have a rigidity to them while interpreted languages have a flexibility.
You're thinking of Perl, Python PHP etc. and so called "fourth generation languages." I'm sure there's a technical term in c.s. for this flexibility, but C doesn't have it. You'll need to switch to one of these languages (and give up performance) if you have a task that requires this sort of string use much. Check out Perl's /e
flag with regexes, for instance.
In C, you'll need to design your application so you don't need to do this. This is generally quite doable, as for its non-OO-ness and other deficiencies many huge, complex applications run on well-written C just fine.

- 59,258
- 35
- 162
- 290