Building an Interpreter

Posted on 2001-08-15
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 50 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Expand macro to ask for filename column 8 37
Smart Camera scanning and reading information 3 100
matchUp  challenge 6 53
mapAB Challlenge 35 128
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
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.

861 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now