Design Pattern Examples
Overview of object-oriented design patterns
Interpreter_Exercise Class Reference

Example of using the Interpreter Pattern in C#. More...

Collaboration diagram for Interpreter_Exercise:
Collaboration graph

Public Member Functions

void Run ()
 Executes the example for the Interpreter Pattern in C#.
 

Private Member Functions

string _TokensToString (int[] tokens)
 Helper method to convert a list of ints to a string representation.
 

Static Private Attributes

static int[][] _sentenceTokenLists
 A list of pre-defined token lists. Each token list represents a single sentence constructed from the 40 most common words in the English language. I don't use all 40 words, though; that would be silly.
 

Detailed Description

Example of using the Interpreter Pattern in C#.

The interpreter is instantiated then fed a series of arrays containing integer tokens. Each token represents a single word or punctuation mark. The interpreter converts that array of tokens to an actual sentence by interpreting the meaning of the tokens.

This is a very simple interpreter that handles the first token in a special way and supports punctuation. It is an example of a linear interpreter where tokens can appear in any order (it's up to the creator of the token list to make sure the outcome makes any sense).

The output shows the token list followed by the sentence produced from the tokens.

Definition at line 31 of file Interpreter_Exercise.cs.

Member Function Documentation

◆ _TokensToString()

string _TokensToString ( int[]  tokens)
inlineprivate

Helper method to convert a list of ints to a string representation.

Parameters
tokensArray of ints to work with.
Returns
A string representation of the integer list.

Definition at line 39 of file Interpreter_Exercise.cs.

Referenced by Interpreter_Exercise.Run().

◆ Run()

void Run ( )
inline

Member Data Documentation

◆ _sentenceTokenLists

int [][] _sentenceTokenLists
staticprivate
Initial value:
= new int[][] {
new int[] { 39, 18, 17, 27, 2, 7, 101 },
new int[] { 32, 17, 1, 0, 34, 2, 1, 37, 101 },
new int[] { 36, 17, 8, 5, 32, 2, 18, 7, 101 },
new int[] { 11, 12, 17, 9, 36, 12, 1, 6, 20, 100 },
new int[] { 26, 27, 7, 21, 36, 17, 27, 10, 101 },
new int[] { 23, 28, 32, 26, 32, 18, 10, 100 }
}

A list of pre-defined token lists. Each token list represents a single sentence constructed from the 40 most common words in the English language. I don't use all 40 words, though; that would be silly.

Definition at line 63 of file Interpreter_Exercise.cs.

Referenced by Interpreter_Exercise.Run().


The documentation for this class was generated from the following file: