Go Premium for a chance to win a PS4. Enter to Win


Building an Interpreter

Posted on 2001-08-15
Medium Priority
Last Modified: 2013-11-20
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 ?
Question by:gilad_no
  • 3

Expert Comment

ID: 6390015
The best way to add scripting options to your program is not to roll your own interpreter, but to leverage Microsoft ActiveScript.

Expert Comment

ID: 6390040
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 :

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

Author Comment

ID: 6391617
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 :)

Accepted Solution

JackThornton earned 200 total points
ID: 6394483

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.


- jack

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question