• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 506
  • Last Modified:

Building an Interpreter

Hi all,

 Where can I learn to build a simple interpreter ?
 I want to add scripting option to my application. I found various sources, but non of them was sufficient. I don't want to build a compiler. I know how to process input, but I don't know how to handle loops and jumps in runtime. I found an example in Java but it was too complicated. Does any one knows a simple example in C ?
0
gilad_no
Asked:
gilad_no
  • 3
1 Solution
 
JackThorntonCommented:
The best way to add scripting options to your program is not to roll your own interpreter, but to leverage Microsoft ActiveScript.
0
 
JackThorntonCommented:
In order to use active scripting, you need to do the following things:

(1) Add "automation" support to your program. That means create one or more COM/ActiveX object(s) that exposes the functionality you want to give your script writers, and make sure these objects present either a dual or straight IDispatch-derived interface.

(2) Follow the directions and samples from the MSDN scripting site :
http://msdn.microsoft.com/scripting/default.htm

Also look up IActiveScript and IActiveScriptDebug using the search program of your choice to find articles and code examples.

The ActiveScripting interfaces give you a polymorphic interface to multiple scripting engines - that means your customers can write scripts in *any* ActiveScripting language available either from Microsoft or third parties. That includes javascript, VBScript, Perl, Tk/tcl, Python, Forth, etc. etc. etc. And it is very easy to implement. And you don't have to write your own interpreter, which can be a college-course subject all by itself.

- jack
0
 
gilad_noAuthor Commented:
I need to build my own interperter. I want to add an option to enhance the scripting language. I know the basic idea of parsing text, but I don't know how implementate it at runtime. If I run the script in memory, how can I jump to other place in the script ? I know how to build a compiler but not an interperter. Another reason I don't want to use ready-made one, is that I want to learn how it works :)
0
 
JackThorntonCommented:
Hmmm...

If it were me, I would be tempted to pre-scan and parse the script and store token streams in a convenient container (e.g. list or vector) of "statement" or context structures. Each node would contain one complete statement of tokens, along with the source location (either line & column or absolute character offset; length of statement; and, if appropriate, an integer index to identify which source file) and a pointer to the innermost symbol scope. I would also start building the symbol table as I was doing the pre-parse; location identifiers (e.g. function names or goto targets) within the symbol table would point back to the context/statement structure either as a straight pointer or, if you're using STL, a container iterator. Depending on how your script language is designed, you could either start executing from the beginning of the container or you would look up the starting function (e.g. "main") in the symbol table and start executing statements sequentially from there. Calls and jumps would do the same thing - look up the pointer to the next statement structure to execute. If you encounter an error, you can report it using the length/location/source ID stored for the statement to find and print the offending string and/or location (if you wanted to burn a little more memory to give better script debugging, you could store location/length for each token in the statement; this would allow you to give location of the exact token that caused the problem; allow you to write a fancier syntax-colored script editor or debugging viewer, etc.)

Not to try to oversell ActiveScript (you can roll your eyes now, if you want :^D) , you could write your interpreter as a legal ActiveScript engine, and then gain all of the support advantages that entails (e.g. free sophisticated debugging, easy access to dispatchable COM objects, etc.). You could even amuse yourself no end writing ASP web pages in your very own scripting language and have them work ;-)  Seriously, though, even if you don't take that route, there is at least one sample script engine available as source from Microsoft (a Forth interpreter) and many of the third-party scripting engines are open-source as well - these would provide excellent examples of real-world scripting language implementations you could study.

hth

- jack
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now