English 中文(简体)
Stop and continue while evaluation an expression tree
原标题:

At the office, we ve applied simple Domain Specific Languages (DSLs) to several problem domains we encountered.

Basically, we parse (lex/yacc) the custom script into an expression tree. Every node in the expression tree has an .evaluate() method that gets called recursively, until the program is done. Nice, and simple as pie. Which is a good thing, because I know next to nothing about parsing techniques, compiler construction and whatnot (good thing my coworkers know a thing or two).

Now here is the challenge: the implementation we are currently working on needs the ability to

  • stop at any position in the tree
  • persist all state
  • and restore state/positon at any given time in the future.

Persiting the actual state should not be too hard, but the position in the tree (or the "callstack") befuddles me. How would one go about implementing such a system? Storing the position in the tree using some sort of ID s for every node? Or is evaluating the tree itself the wrong approach, and should we somehow transform it into something linear?

I m guessing this is a rather common problem, but I don t know what to look for. Any help on the correct terminology, nudges in the right direction, keywords to search on, design patterns to look at etc... are most welcome!

(Doing this in Win32/Dephi, but hopefully we can keep this language agnostic)

最佳回答

The position in the tree can be specified using a list of child numbers. Your evaluates would look something like this (quickly written; not compiled/tested):

public class Context {
    private Context parent; // set in constructor
    private int child;  // with get/set
}

public ReturnType evaluate(Context context) 
    context.setChild(context.getChild() + 1);
    context = new Context(context); // push a new context for calls
    // do evaluation - when calling kids, pass context            
}

The above should track the context as a list of numbers that tell which child you re currently processing.

Rather than implement that in each node type, I d recommend writing a decorator (or a superclass using a template method) to do it for you, such as:

// decorator
public class ContextTrackerEvaluator<T> implements Evaluator<T> {
    private Evaluator realEvaluator;
    public ContextTrackerEvaluator(Evaluator realEvaluator) {
        this.realEvaluator = realEvaluator;
    }
    public T evaluate(Context context) {
        context.setChild(context.getChild() + 1);
        context = new Context(context); // push a new context for calls
        realEvaluator.evaluate(context);
    }
}

// OR superclass w/ template method
public class EvaluatorBase<T> {
    public final T evaluate(Context context) {
        context.setChild(context.getChild() + 1);
        context = new Context(context); // push a new context for calls
        doEvaluate(context);
    }
    // subclasses override doEvaluate to do their real work
    protected abstract T doEvaluate(Context context);
}

If you use the decorator, make sure you decorate every node...

That gives you access to the context list.

You can then add a "stop context" parameter that you can compare (so you d have two contexts passed in and you could compare them to see if they match.

Hope this helps! -- Scott

问题回答

暂无回答




相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

How to get instance from string in C#?

Is it possible to get the property of a class from string and then set a value? Example: string s = "label1.text"; string value = "new value"; label1.text = value; <--and some code that makes ...

XML DOM parsing br tag

I need to parse a xml string to obtain the xml DOM, the problem I m facing is with the self closing html tag like <br /> giving me the error of Tag mismatch expected </br>. I m aware this ...

Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

Locating specific string and capturing data following it

I built a site a long time ago and now I want to place the data into a database without copying and pasting the 400+ pages that it has grown to so that I can make the site database driven. My site ...

热门标签