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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:16:30+00:00 2026-06-11T03:16:30+00:00

I got interested in this discussion about AST construction and evaluation in various languages

  • 0

I got interested in this discussion about AST construction and evaluation in various languages. I am working on a solution in Java just to see what I can learn from this problem.

The code I have below compiles, but produces an incorrect result (namely an “oops” exception). It doesn’t work because Java lacks runtime dispatch. Is there any simple workaround for that? How about complicated workarounds? E.g. using generics to give hints to the compiler? I’m just guessing here.

Some ideas that I’ve ruled out: (1) Use instanceof to dispatch by argument type at run time. (2) Build a look-up table which maps argument types to appropriate handlers. (3) Put an evaluation function in each subclass of E which evaluates that subclass appropriately.

I’ve ruled out (1) and (2) because I want to get the compiler and/or runtime to do that work for me. I’ve ruled out (3) because I want to separate the evaluation code from the expression representation; the idea being that there might be multiple operations (reordering, simplification) on the representation.

Here’s what I have so far. As noted above, this code produces an incorrect result.

import java.util.*;

public class EV
{
    public static Integer ev (E e, Map <String, Integer> env) { throw new RuntimeException ("oops: " + e); }
    public static Integer ev (V e, Map <String, Integer> env) { return env.get (e.name); }
    public static Integer ev (C e, Map <String, Integer> env) { return e.value; }
    public static Integer ev (P e, Map <String, Integer> env) { return ev (e.a1, env) + ev (e.a2, env); }
    public static Integer ev (T e, Map <String, Integer> env) { return ev (e.a1, env) * ev (e.a2, env); }

    public static void main (String [] a)
    {
        E e = new P (new T (new C (2), new V ("a")), new V ("b"));
        Map <String, Integer> env = new Hashtable <String, Integer> ();
        env.put ("a", 123);
        env.put ("b", 456);
        System.out.println ("ev (e, env) => " + ev (e, env));
    }
}

class E {}

class V extends E
{
    String name;
    public V (String name) { this.name = name; }
}

class C extends E
{
    Integer value;
    public C (Integer value) { this.value = value; }
}

class P extends E
{
    E a1, a2;
    public P (E a1, E a2) { this.a1 = a1; this.a2 = a2; }
}

class T extends E
{
    E a1, a2;
    public T (E a1, E a2) { this.a1 = a1; this.a2 = a2; }
}
  • 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-11T03:16:31+00:00Added an answer on June 11, 2026 at 3:16 am

    For doing this in a single-dispatch OO language like Java, this is a classic use-case for the Visitor Pattern, especially if you are also interested in the pretty-print operations mentioned in your comment. It might apply to some of the other operations mentioned in your comment, though they’re a less natural fit.

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

Sidebar

Related Questions

I got interested in this question someone posted yesterday about diamond-square algorithms, Node.js /
Based on feedback I got from this question , I'm interested in using Lazy
I just found Data::Section and I got interested in it. Unfortunately, I simply cannot
This is a theoretical question, I've already got a solution to my problem that
This subject turned into a heated discussion at the office, so I'm interested to
I got interested in this from noticing how images are scaled when you resize
just joined this awesome site… Ive recently been working in XNA on C#, making
I saw this question and it got me interested as surface caused a big
I got interested in this small example of an algorithm in Python for looping
I got interested in this after I saw Square use the headphone jack on

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.