On the Java design mode--parser mode (interpreter)

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/l1028386804/article/details/45599443
I. Overview

Given a language, define a representation of its grammar and define an interpreter that interprets the sentences in the language using that representation.

Second, applicability

You can use the interpreter pattern when there is a language that needs to be interpreted and executed, and you can represent a sentence in that language as an abstract syntax tree. This mode works best when the following conditions are present:

1. The grammar is simple for complex grammars, and the class hierarchy of grammars becomes large and cannot be managed.

2. Efficiency is not a key issue the most efficient interpreters are usually not implemented by directly interpreting the parse tree, but rather first converting them to another form.

Third, participants

1.AbstractExpression (abstract expression) declares an abstract interpretation operation that is shared by all nodes in the abstract syntax tree.

The 2.TerminalExpression (terminator expression) implements an interpreted operation associated with Terminator in the grammar. Each terminator in a sentence requires an instance of the class.

3.NonterminalExpression (non-terminator expression) is a non-terminator implementation interpretation (interpret) operation in grammar.

4.Context (context) contains some global information outside the interpreter.

5.Client (Customer) constructs (or is given) an abstract syntax tree that represents a particular sentence in the language defined by the grammar. The abstract syntax tree is assembled from instances of nonterminalexpression and terminalexpression. Invokes an interpretation operation.

Four, class diagram

V. Examples

Abstractexpression

Package com.lyz.design.interpreter;/** * Abstractexpression  * @author Liuyazhuang * */public abstract class Expression {    abstract void interpret (Context ctx);}

Expression

Package com.lyz.design.interpreter;/** * Expression  * @author Liuyazhuang * */public class Advanceexpression extends Expression {    void interpret (Context ctx) {        System.out.println ("This is an advanced parser!");}    }

Package com.lyz.design.interpreter;/** * Expression  * @author Liuyazhuang * */public class SimpleExpression extends E xpression {    void interpret (Context ctx) {        System.out.println ("This is the ordinary parser!");}    }

Context

Package Com.lyz.design.interpreter;import java.util.arraylist;import java.util.list;/** * Context  * @author Liuyazhuang * */public class Context {    private String content;    Private list<expression> List = new arraylist<expression> ();    public void SetContent (String content) {        this.content = content;    }    Public String getcontent () {        return this.content;    }    public void Add (Expression eps) {        list.add (eps);    }    Public list<expression> getList () {        return List;    }}

Test

Package com.lyz.design.interpreter;/** * Text * @author Liuyazhuang * */public class Test {public    static void main (St Ring[] (args) {        context ctx = new Context ();        Ctx.add (New SimpleExpression ());        Ctx.add (New Advanceexpression ());        Ctx.add (New SimpleExpression ());        For (Expression eps:ctx.getList ()) {            Eps.interpret (CTX);}}    }

result

This is an ordinary parser! This is the Advanced parser! This is the ordinary parser!


On the Java design mode--parser mode (interpreter)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.