Solved

Building an Interpreter

Posted on 2001-08-15
4
454 Views
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 ?
0
Comment
Question by:gilad_no
  • 3
4 Comments
 
LVL 3

Expert Comment

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

Expert Comment

by:JackThornton
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 :
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
 

Author Comment

by:gilad_no
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 :)
0
 
LVL 3

Accepted Solution

by:
JackThornton earned 50 total points
ID: 6394483
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

707 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

18 Experts available now in Live!

Get 1:1 Help Now