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 9212775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:36:08+00:00 2026-06-18T01:36:08+00:00

I’m trying to figure out where to start with this project. Perhaps someone can

  • 0

I’m trying to figure out where to start with this project. Perhaps someone can steer me in the right direction.

I am given a small language that I must write an interpreter for. The language consists of either an expression in parentheses:

(integer integer operator)

or an arithmetic IF statement made up of expressions in the form:

IF exp1 exp2 exp3 exp4

where exp2 is returned if exp1 is negative,
exp3 is returned if exp1 is zero,
and exp4 is returned if exp1 is positive.

The operator is either + or x (for addition and multiplication respectively).

I have to implement a scanner/parser together, then the interpreter that will output the result. The interpreter part will not be difficult, but I’m having trouble figuring out how to start the scanning/parsing process.

I have started by using Java, and have a Scanner object collect the input and store it in a string. Then I split the String into a String array using nothing as the delimiter (so that each single character, symbol, space, etc. is stored in its own index of a string). This may not be the best way to do this, as I cannot figure out where to go from here. The part I cannot grasp is how to return errors if this syntax isn’t followed, or how to detect the parenthesis and/or IF and etc.

Here’s the snippet of code that I described in the last paragraph:

public void run() {
    Scanner sc = new Scanner(System.in);

    while (sc.hasNext()) {
        String sLine = sc.nextLine();
        String[] scanned = sLine.split("");

Input examples:

(7 2 +)

Output: 9

IF (2 -2 +) (5 2 +) (5 -2 x) (5 2 x)

Output: -10

If anybody has a good direction for me to take, please share. 🙂

  • 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-06-18T01:36:09+00:00Added an answer on June 18, 2026 at 1:36 am

    I think using ANTLR, JavaCC, SampleCC or other parser generator tools would be using a sledgehammer to crack a nut. if there is no recursion in grammar definition just a few methods would be sufficient. the following code gives a basic idea (it may not compile or work, I wrote it from scratch as an illustration how to start):

    public int parse(String input) {
    Scanner scanner = new Scanner(input);
    
        return consumeLine(scanner);
    }
    
    public int consumeLine(Scanner scanner) {
        if( scanner.hasNext("(") ) {
            return consumeExpression(scanner);
    
        } else if( scanner.hasNext("IF") ) {
            return consumeIf(scanner);
        }
    }
    
    
    public int consumeExpression(Scanner scanner) {
        scanner.next("(");
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        String op = scanner.next("[+-/*]");
        scanner.next(")");
    
        if( "+".equals(op) ) {
            return a + b;
    
        } else if( "-".equals(op) ) {
            return a - b;
        } ...
    
        throw new RuntimeException("parsing error");
    }
    
    public int consumeIf(Scanner scanner) {
        scanner.next("IF");
        int exp1 = consumeExpression(scanner);
        int exp2 = consumeExpression(scanner);
        int exp3 = consumeExpression(scanner);
        int exp4 = consumeExpression(scanner);
    
        if( exp1 < 0 ) {
            return exp2;
        } else if( exp1 == 0 ) {
            return exp3;
        } ...
    
        throw new RuntimeException("should not be here (TM)");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has

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.