Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7038969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:44:25+00:00 2026-05-28T01:44:25+00:00

I’m currently working on an assignment to create a basic interpreter with 8 keywords

  • 0

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);
                            }

                        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:44:25+00:00Added an answer on May 28, 2026 at 1:44 am

    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):

    for(line:lineList)
        CommandObject commandObject=map.get(line.split()[0]) // do this more clearly
        commandObject.execute(variableHash, line) // Parse and execute the line
    

    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)

    map = new HashMap<String, CommandObject>
    

    then add an instance of each class to the map:

    map.put("LET", new LetCommand());
    map.put("INTEGER", new Integercommand());
    

    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:

    execute(String commandLine, HashMap variables);
    

    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)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I am currently running into a problem where an element is coming back from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.