1

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.

2
  • 1
    This is an extremely non-trivial exercise, and raises the obvious question: what is the overall goal here? There is probably a better overall solution. Commented May 28, 2012 at 22:31
  • 1
    Did you try C interpreters ? See stackoverflow.com/questions/69539/… . Commented May 28, 2012 at 22:37

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.