Make a Command Interpreter

Step-by-Step Guide to Interpreter Design

© Scott Hermanson

Dec 29, 2008
The process of interpreter creation follows a fairly straight-forward design pattern. This interpreter blueprint can be implemented in almost every language.

Interpreters are used to process instructions which the user enters. Typical examples of interpreters include BASIC and PHP.

Learning to construct interpreters is a valued programming skill because it allows the designer to directly define the grammar that the computer understands. For this reason, it is also frequently used (with other techniques) in the field of artificial intelligence.

Interpreters can be simple, only understanding rudimentary commands such as "print this" or "add that". They can also understand complex commands if they are designed properly. In a way, a significant way, teaching the computer how to interpret commands gives it intelligence.

To design an interpreter, it is suggested that a compiler program is used. Code for the compiler used will be specific to its native language, but an outline is provided below.

Before beginning, create an empty sequence or list for the the commands:

-- Build the empty command database( s ) All_possible_commands = {}

Step 1: Add Interpreter Commands

Some find it helpful to write out a list of commands that the new interpreter will be able to process. These are then written into the program. Each argument (object a) is an object that can be passed through to the procedure. Functions can be used instead of procedures to add a debugging logger if desired.

procedure my_proc( object a ) print(1,remove_possible_quotes(a)) end procedure

The intended use of the above procedure is to print whatever object is passed through the procedure. A likely command for the interpreter would follow the form [myPrintCommand “Hello World”]. After the procedure is written, the command is added to the database.

A routine_id is the integer identity of a procedure to be called. The last object sent is the number of parameters to send. The first string entered is what the user will type in to run the command.

All_possible_commands &= { { "print", routine_id("my_proc"), 1 } }

Step 2: Parser Creation

A parser is used to process the entire string command sent to the interpreter. In this instance, it would change the command print “Hello World” to {“print”,“Hello World”}.

function parse(object x) return remove_combine_quoted_words( remove_spaces( x ) ) end function function check_parameters( object x, object y ) if length(x) = y then return x end if if length( x ) < y then while length( x ) < y do x &= {{}} end while return x end if if length( x ) > y then return x[1..y] end if end if end function

Step 3: Prompt Design

The prompt command is finally added. This procedure will first ask for a command, check to make sure it's in the right form, and proceed by calling the pre-written procedure.

procedure prompt_stuff() object x, y while 1 do x = prompt_string(">") x = parse( x ) if equal( lower(x[1], "quit" ) then exit end if y = search( All_possible_commands, x[1], 1 ) if y then call_proc( All_possible_commands[y][2], check_parameters( x[2..length(x)], All_possible_commands[y][3] ) ) end if end while end procedure

All that is left is to call the prompt:

prompt_stuff()

To assign values, a database can be created as follows:

-- {“variable”,”value”} sequence variable_db = {}

The variables can be modified, for example, with an assign procedure:

procedure assign_variable( object a, object b ) object x = search( variables_db, a, 1 ) if x then variables_db[x][2] = b else variables_db &= {a,b} end if end procedure

and called with a retrieve command:

procedure retrieve variable( object sender_variable, object variable_to_be_assigned ) object x = search( variables_db, sender_variable, 1 ) if x then assign_variable( variable_to_be_assigned, variable_db[x][2] ) else assign_variable( variable_to_be_assigned, “” ) end if end procedure

Now that the engine for the interpreter is built, new commands can be designed by repeating step 2. The new interpreter can be used as a stand-alone program or placed inside of other source code and called with the 'prompt_stuff()' command.

Most of the work done on interpreters is not in the command section, the typical aim is not to produce a vast array of commands (at first). Modifications to the parsing section creates ease of use and allows for user-defined procedures, but that's for another article.


The copyright of the article Make a Command Interpreter in Computer Programming is owned by Scott Hermanson. Permission to republish Make a Command Interpreter in print or online must be granted by the author in writing.




Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo