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

Representation of a simple interpreter. More...

#include <Interpreter_Class.h>

Collaboration diagram for Interpreter_Class:
Collaboration graph

Public Member Functions

std::string Interpret (IntList tokens)
 Given an array of integer tokens, convert the tokens into a single std::string of space-delimited words, following simple rules of formatting.
 

Public Attributes

const int PERIOD = 100
 
const int QUESTION = 101
 

Private Member Functions

std::string _InterpretToken (int token)
 Helper method to convert the token into its corresponding word or punctuation mark.
 

Static Private Attributes

static const char * _commonwords []
 The 40 most common words in English (in order but that doesn't really matter here). A token is nothing more than an index into this list.
 

Detailed Description

Representation of a simple interpreter.

This interpreter takes an array of integer tokens and converts each token into a word or punctuation mark. The interpreter then arranges the words into a space-separated list in a single string. In other words, the tokens are converted into a sentence, with the first word capitalized and no space between the last two "words" under the assumption the last word is actually a punctuation mark.

Interpreter Rules:

  1. Each token must be in the range of 0 through 39 (maximum number of words known by the interpreter) or must be 100 ('.') or 101 ('?').
  2. The word corresponding to the first token is always capitalized.
  3. A single space appears between each word.
  4. No space appears between the last two tokens.

Definition at line 38 of file Interpreter_Class.h.

Member Function Documentation

◆ _InterpretToken()

std::string _InterpretToken ( int  token)
private

Helper method to convert the token into its corresponding word or punctuation mark.

Parameters
tokenThe token to interpret.
Returns

A std::string containing the corresponding word or punctuation. If the token is not recognized, the std::string returns "<UNKNOWN TOKEN #>", where

Definition at line 62 of file Interpreter_Class.cpp.

References Interpreter_Class::_commonwords, _countof, Helpers::formatstring(), Interpreter_Class::PERIOD, and Interpreter_Class::QUESTION.

Referenced by Interpreter_Class::Interpret().

◆ Interpret()

std::string Interpret ( IntList  tokens)

Given an array of integer tokens, convert the tokens into a single std::string of space-delimited words, following simple rules of formatting.

Parameters
tokensThe list of integers to interpret.
Returns
A std::string containing a sentence built from the tokens.

Definition at line 93 of file Interpreter_Class.cpp.

References Interpreter_Class::_InterpretToken(), and Helpers::titlecase().

Referenced by DesignPatternExamples_cpp::Interpreter_Exercise().

Member Data Documentation

◆ _commonwords

const char * _commonwords
staticprivate

The 40 most common words in English (in order but that doesn't really matter here). A token is nothing more than an index into this list.

Definition at line 49 of file Interpreter_Class.h.

Referenced by Interpreter_Class::_InterpretToken().

◆ PERIOD

const int PERIOD = 100

Definition at line 41 of file Interpreter_Class.h.

Referenced by Interpreter_Class::_InterpretToken().

◆ QUESTION

const int QUESTION = 101

Definition at line 42 of file Interpreter_Class.h.

Referenced by Interpreter_Class::_InterpretToken().


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