2

I was wondering what the easiest way to parse/interpret a custom language would be. Are there any libraries that could help me out?

I essentially want the end-user to be able to write scripts using a custom syntax that I design (the syntax will probably be similar to c++) and I would need my C++ protogram to interpret the script, much like how there is a C++ library for parsing LUA.

Are there any libraries/examples to help me, or will I just have to do manual tokenizing, parsing and interpreting?

Brad
  • 10,015
  • 17
  • 54
  • 77
  • Lets try this again. The big list of compiler resources (most of which also apply to interpreters) is [Learning to write a compiler](http://stackoverflow.com/q/1669/2509). – dmckee --- ex-moderator kitten Nov 09 '10 at 21:12
  • 1
    To write anything with the complexity of C++ is way beyond the ability of 99.8% of developers in general (just like 80% of stats are made up). But this is way-way more complex than you think it is. Even trying to write an interpreter for C is going to be hard. I would rather find an existing scripting language and integrate it into your code. – Martin York Nov 09 '10 at 21:45
  • Here is the C++ grammar (probably not 100% current): http://www.csci.csusb.edu/dick/c++std/cd2/gram.html Here is the C tokens (for flex) http://www.lysator.liu.se/c/ANSI-C-grammar-l.html – Martin York Nov 09 '10 at 21:52

3 Answers3

6

There are numerous frameworks to help, but remember that language design is not to be taken lightly - are you sure you're not better off just using Lua?

rici
  • 234,347
  • 28
  • 237
  • 341
Yann Ramin
  • 32,895
  • 3
  • 59
  • 82
2

Use tools like Lex and Yacc to generate a scanner/parser for you

Hannes de Jager
  • 2,903
  • 6
  • 37
  • 57
1

For defining your own parser, take a look at Boost Spirit.

But the practical way is to use an existing language that's been widely used for such things. E.g. JavaScript. Then Google's JavaScript binding is reportedly excellent.

Googling "Google's JavaScript binding" served up, as second hit, a link to Wikipedia's article on V8.

Cheers & hth.,

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331