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?

Greg
  • 9,068
  • 6
  • 49
  • 91
Mahmoud
  • 11
  • 3
  • 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? – David Thornley Apr 23 '09 at 16:16
  • Yes, I want to extract function from the text and execute – Mahmoud Apr 23 '09 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. – jon hanson Apr 23 '09 at 16:54
  • Please see this link http://stackoverflow.com/questions/615040/writing-a-mini-language and http://stackoverflow.com/questions/391432/developing-a-simple-parser How parse expressions of my text through the idea in link? @FileExists('','','') @FileDelete('') @MsgBox('','',0) – Mahmoud Apr 23 '09 at 19:27

3 Answers3

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.

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.

tstenner
  • 10,080
  • 10
  • 57
  • 92
  • 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 https://nilesoft.org/docs/expressions – Mahmoud Aug 10 '21 at 02:10
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.

Eclipse
  • 44,851
  • 20
  • 112
  • 171
  • 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 https://nilesoft.org/docs/expressions – Mahmoud Aug 10 '21 at 02:13