I’m currently working on an assignment to create a basic interpreter with 8 keywords (case insensitive) and the 4 arithmetic operators. A program in this language would look something like this (similar to BASIC’s syntax really):
# (signals start of a comment line)
LET
INTEGER
STRING
PRINT
END
So anyway I’m currently trying to tokenize the lines of text to be parsed. I already parsed all the lines of text into an ArrayList and tokenized the Strings. My current problem now is that StringTokenizer tokenizes all the strings in advance (I’m using whitespace as a delimiter), when what I need is for it to find my keyword, which is always the first word at the beginning of the line of code, and certain issues make that undesirable; I don’t think that using String.split() would be much help either.
The way I was planning on doing it was to have the interpreter find my first token, and go from there to the appropriate class via the HashMap (cf. my previous question regarding the use of switch statements for my interpreter here: Switch or if statements in writing an interpreter in java; it was suggested by other members that I use a Map) to remove the keyword token and execute. Would setting up a second temporary ArrayList or array specifically to hold variables be a good idea? I don’t want or need it to be overly complicated.
Thanks in advance for the suggestions.
public static void main (String[]args)
{
try
{
ArrayList<String> demo= new ArrayList <String>();
FileReader fr= new FileReader("hi.tpl");
BufferedReader reader= new BufferedReader(fr);
String line;
while ((line=reader.readLine()) !=null)//read file line by line
{
//Add to ArrayList
demo.add(line);
}
reader.close();
boolean checkEnd= demo.contains("END");//check if arraylist contains END statement
if(line=null && checkEnd== false)
{
System.out.println(" Unexpected end of file: no END statement");
System.exit(0);
}
ListIterator<String>arrayListIt=demo.listIterator();
while (arrayListIt.hasNext())
for (String file: demo)// begin interpreting the program file here
{
StringTokenizer st=new StringTokenizer(file);
while(st.hasMoreTokens())
{
int firstWord=file.indexOf();
String command = file;
if (firstSpace > 0)
{
command= file.substring(0, firstSpace);
}
TokenHandler tokens= tokens.get(command.toUpperCase());
if(tokens != null)
{
tokens.execute(file);
}
}
So if I were doing it I’d use a more OO approach.
What if you created a “Class” for each of those commands that all implemented the same interface? The interface–let’s call it CommandObject would have an execute() method.
You could then use a pre-loaded map that mapped the command like “Let” to an instance of the Let class.
Now your main loop becomes something like this (pseudo):
These command objects would have to share a set of variables–making a singleton would work but is somewhat of an anti-pattern, I suggest you pass them in as a hashmap (variableHash above) instead.
The nice thing about this approach is that adding a new “Command” is very simple and mostly independent.
Edit (re. comment):
The first thing you would do is create a hashmap and “Install” each of your commands. For example: (still psudeo-code, I assume you’d prefer to do the assignment yourself)
then add an instance of each class to the map:
where the right-hand classes implement the “CommandObject” interface.
Notice that since each CommandObject is an instance that is re-used every time that keyword is found you should probably not store ANY state (Don’t have any instance variables), this implies that your CommandObject will only require a single method, something like:
This is probably the easiest way (I edited the text above from my original suggestion to reflect this).
If this parser got more complex it would be totally valid to add more functionality to “CommandObject”s, you could keep state variables as long as you had a reset() method (my original suggestion but it seems overly complicated for what you are doing)
Note that the map of keywords to command objects could be replaced by reflection but don’t try to do that for a school assignment, the complexity of reflection makes it not worth your time and it’s likely you’d be downgraded because the teacher doesn’t understand it. I implemented a system like this where every keyword linked to a test (allowing you to chain tests, loop over tests and even define and carry variables that were passed into and manipulated by those tests–in this case reflection was worth it because adding a new test did not require updating the cache)