Jump to content
  • Advertisement
Sign in to follow this  
jeremic

Interpreter help (c++ scripting)

This topic is 3589 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I'm trying to create a simple interpreter for my VERY simple scripting language. Heres a script that I want to run display 5 + $myvar This should just simply print 14 on my console. $myvar is an internal static variable which holds the value 9. How would I parse this line so that my output is predictable. When I grab this line from the user its stored in a char array. How do I convert an instruction (such as addition) from a char array to an actual instruction? And how would I parse $myvar to be equal to 9? I was thinking of using strtok but maybe theres an easier way. I'm open to suggestions, guidance, comments, and pointers. Thank you!!

Share this post


Link to post
Share on other sites
Advertisement
1. If you need to ask how to parse something, you might need to take a few steps back; it's important to know that before designing your syntax.

2. In general, you want to develop a parser for your syntax. strtok is sucky even for its original design. There are parser builders like yacc or antlr that are designed to develop parsers. boost::spirit is another alternative.

3. To get that to parse... hmm 'as you expect' is probably the best description of it, you'll need to determine an order of operations that places + ahead of display (or simply method invocation). Something to consider before making the parser.

4. To make an instruction into a executable token, you'll end up with some mechanism to convert the syntax tree you get from the parser into something usable. It might be replacement, it might be doing a lookup in a map and running a stored functor, it might be a giant if/else tree. Again, if you don't have a naive guess about how to do it, this is perhaps beyond you at the moment. Most of the parser builders include an example calculator which should serve as a good starting point.

5. This sort of thing is a well studied topic, and exists online in a lot of places in varying degrees of usefulness. General keywords: lexer/lexing, parser/parsing, abstract syntax tree, how to build a compiler, how to build an interpreter ...

Share this post


Link to post
Share on other sites
Tokenising the input before parsing is usually a good idea; you don't want the parser to fiddle with individual chars. Tokenising is just removing whitespace and converting to a sequence of basic symbols like 'display', '5', '+', '$myvar'.

Also, your code will end up a lot more complex if you want to support complex expressions like 100 / (5 + $myvar * 2). You'll need something to sort out the order-of-operations and a tree of classes to evaluate each operator. If you want to keep it simple, just stick to 'x add y' and 'x multiply by y' basic commands.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!