1

I'm trying to parse a dynamic function from a string, for example:

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))

How can I do this?

4
  • What do you mean by "parse"? What do you want to end up with when this is over? A parse tree? A list of tokens? Do you want to execute something based on this? Commented Apr 23, 2009 at 16:16
  • Yes, I want to extract function from the text and execute Commented Apr 23, 2009 at 16:38
  • You need to be more specific on what exactly you want to do. Those @ functions look like Windows script functions, in which case this may be a Windows-specific question and not (just) a C++ question. Commented Apr 23, 2009 at 16:54
  • Please see this link stackoverflow.com/questions/615040/writing-a-mini-language and stackoverflow.com/questions/391432/developing-a-simple-parser How parse expressions of my text through the idea in link? @FileExists('','','') @FileDelete('') @MsgBox('','',0) Commented Apr 23, 2009 at 19:27

3 Answers 3

4

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.

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

2 Comments

I think he may want to make such a function
There is one such function, it's called system("g++...")
4

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.

1 Comment

Thanks, I've already built and interpreted expressions from strings to functions by abstract syntax tree(ast). It was used in the Nilesoft Shell, and this link shows its use nilesoft.org/docs/expressions
3

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.

1 Comment

Thanks @Eclipse, I've already built and interpreted expressions from strings to functions by abstract syntax tree(ast). It was used in the Nilesoft Shell, and this link shows its use nilesoft.org/docs/expressions

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.