Interpreter mode analysis, structure diagram, and basic code

Source: Internet
Author: User
Zookeeper

Definition: defines a language, a syntax expression, and an interpreter. This interpreter uses this representation to explain sentences in the language.
Where applicable: When a language requires interpretation and execution, and you can represent sentences in the language as an abstract syntax tree, you can use the interpreter mode. If a specific type of problem occurs frequently enough, it may be worthwhile to express each instance of the problem as a sentence in a simple language. In this way, an interpreter can be built to solve the problem by interpreting these sentences. Such as regular expressions and browser applications.
The advantage is that the syntax can be easily expanded and changed, because the pattern uses classes to represent grammar rules, you can use inheritance to change or extend the syntax. It is also easy to implement grammar, because the implementation of classes on each node in the definition abstract syntax tree is similar in general, and these are easy to directly write.
The disadvantage is that the interpreter defines at least one class for each rule in the grammar, so the grammar containing many rules may be difficult to manage and maintain. It is recommended that you use other technologies such as syntax analysis programs or compiler generators to process the syntax when it is very complex.
Structure:


Basic code:

Using system;
Using system. Collections. Generic;
Using system. text;

Namespace interpreter Mode
{
Class Program
{
Static void main (string [] ARGs)
{
Context context = new context ();
Ilist <shortactexpression> List = new list <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. Read ();
}
}

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 ;}
}
}

Abstract class abstractexpression
{
Public abstract void interpret (context );
}

Class terminalexpression: abstractexpression
{
Public override void interpret (context)
{
Console. writeline ("terminal interpreter ");
}
}

Class nonterminalexpression: abstractexpression
{
Public override void interpret (context)
{
Console. writeline ("non-terminal interpreter ");
}
}
}

Interpreter mode analysis, structure diagram, and basic code

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.