Solved

Building an Interpreter

Posted on 2001-08-15
4
479 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

617 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