19th. interpreter Mode

Source: Internet
Author: User

Interpreter mode:

Interpreter: for a given language, define a representation of its syntax and define an interpreter which uses this interpreter to interpret sentences in the language.

 

Knowledge point:

String Matching, email identification, and phone number matching. Regular Expressions are an application of interpreter mode.

The interpreter mode is easy to change and expand.

The interpreter mode defines at least one class for each rule in the grammar, so the grammar containing many rules may be difficult to manage and maintain.

 

Demo1:

//An abstract expression that declares an abstract interpretation operation.

Abstract class abstractexpression

{

Public abstract void interpret (contextcontext );

}

// The Terminator expression, which can be used to explain the terminator in grammar.

// The interpret () method. Each terminator in grammar has a specific Terminator expression corresponding to it.

Class terminalexpression: abstractexpression

{

Public override void interpret (context)

{

Console. writeline ("terminal interpreter ");

}

}

// Non-terminator expression. A specific non-terminator expression class is required for every rule R1, R2. ..

Class nonterminalexpression: abstractexpression

{

Public override void interpret (context)

{

Console. writeline ("non-terminal interpreter ");

}

}

 

// Contains global information other than the interpreter

Class Context

{

Private string input;

Public String Input

{

Get {return input ;}

Set {input = value ;}

}

 

Private string output;

Public String output

{

Get {return output ;}

Set {output = value ;}

}

}

 

Static void Main (String [] ARGs)

{

Context context = new context ();

Ilist <shortactexpression> List = newlist <shortactexpression> ();

List. Add (New terminalexpression ());

List. Add (New nonterminalexpression ());

List. Add (New terminalexpression ());

List. Add (New terminalexpression ());

 

Foreach (effecactexpression exp in List)

{

Exp. interpret (context );

}

Console. readkey ();

}

 

Related Article

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.