tags:

views:

241

answers:

3

I'm beginner in c++ and I hopeless to parse dynamic function form string like

char* func = "app.exe /path:\"@FileExists('filepath', @FileDelete('filepath'), @MsgBox('file not found','error',1))\";

I want to parse

@FileExists('filepath', @FileDelete('filepath'), @MsgBox('file not found','error',1))

can you help me?

sorry for my english

+4  A: 

C++ has no concept of dynamic functions. If you are looking for some kind of function that will take a string and execute it as C++ code, you are out of luck.

anon
I think he may want to make such a function
Evan Teran
not a job for a beginner, I venture
anon
There is one such function, it's called system("g++...")
Eclipse
+3  A: 

I'm presuming that you are looking to execute the code in the string. In C++, Once a program is compiled, there is no record of the names of functions or variables. It's all just addresses.

It is possible to set up your own map of strings to functions. You'd then have to build up a tree of function/names and arguments and manually call the functions. Essentially you have to write an interpretter for some subset of C++, with a pre-defined set of available functions. This absolutely not a beginner's task, but if you're really wanting to, then take a look at Recursive-Descent parsing. It's by far the easiest way for to get started writing an interpreter. If your needs outgrow that, take a look at some of the more powerful parsers like ANTLR or flex/bison

It is possible to embed more dynamic scripting languages in C++. In fact, most scripting languages have some sort of C interface, and anything you can do in C, you can do in C++. Take a look at something like Boost.Python, or this example for VBScript.

Eclipse
+2  A: 

As Josh already said it's probably the best idea to use some dynamic scripting language. You should look at Lua, which is easily embeddable, small and available under the MIT license.

tstenner