Related articles |
---|
Just Starting Writing An Interpreter alankemp@bigfoot.com (Alan Kemp) (1997-10-26) |
Re: Just Starting Writing An Interpreter burley@cygnus.com (Craig Burley) (1997-10-29) |
From: | "Alan Kemp" <alankemp@bigfoot.com> |
Newsgroups: | comp.compilers |
Date: | 26 Oct 1997 22:06:47 -0500 |
Organization: | Compilers Central |
Keywords: | interpreter, question |
Hi,
I have just started trying to write a simple interpreter as a personal
project. I read a lot of books on the subject and consulted many
internet sites that claimed to be able to teach you how to write an
interpreter, but none of them approached the subject in a manner that
I could understand well enough to get started. However, using a
combination of those books and my own, reasonable knowledge of
programming I have constructed what I belive to be quite a good model
for how an interpreter should work and would like your opinions on it
before I attempt to start coding it in C or C++.
This is the defintion of the very simple language I am working with:
It will have no comments
It will have only one command:
PRINT (text to be printed goes here);
All commands will end with a semi-colon
The program will terminate upon finding the word:
END
The 'Hello World!' program would be written as:
PRINT (Hello World!);
END
Here is the model I constructed for how an interpreter works:
--------------------
main {
get filename to interpret
open filename
call tokenizer
return 0
}
tokenizer {
char a
char b
int lineNo
int charNo
DO
read character at lineNo, charNo and asign it to a
if a=";" then {lineNo++; charNo=1}
if a= eof then end program
if a isalpha then b += a
if a=" " then //have reached end of command
if b="print" call funtion print passing lineNo and charNo
else if b="end" jump out of loop
else unexpected term jump out of loop
LOOP whilst (1)
return 0;
}
print (int lineNo, charNo) {
char a
char b
charNo++ //to get past the first bracket
DO
read character at lineNo, charNo assign to a
if a isalpha the b = b + a
if a = ")" then display b and return control to tokeniser
LOOP whilst (1)
}
--------------------
I am using a seperate function to deal with every possible command
(at the moment only one) is this the best way to do it?
One of the main problems I have is reading the character at postion
lineNo, charNo. Is there way of doing this is C/C++?
If I have made any wrong assumptions about how things go together or
about how to acomplish the various tasks please say.
Even if this is not the 'correct' way to code an interpreter would it
work?
Many thanks for any ideas or advice you may have,
Alan
-----
Alan Kemp
alankemp@bigfoot.com
--
Return to the
comp.compilers page.
Search the
comp.compilers archives again.