(NET) NET (2016)

Interpreter in VB.NET

A way to include language elements in a program

Return


Given a language, define a representation for its grammar along with an interpreter that uses the representation to interpret sentences in the language. Interpreter patterns, which using a defined grammer, provides the interpreter that processes parsed statements.







Please download project with this source code from https://github.com/ViacheslavUKR/StandardDisignOopPattern



   1:      ' Interpreter Design Pattern.
   2:      ' See description in //www.vb-net.com/ProgramTheory/Interpreter.htm
   3:      Class MainApp
   4:          ' Entry point into console application.
   5:          Public Shared Sub Main()
   6:              Dim context As New Context()
   7:              ' Usually a tree 
   8:              Dim list As New ArrayList()
   9:              ' Populate 'abstract syntax tree' 
  10:              list.Add(New TerminalExpression())
  11:              list.Add(New NonterminalExpression())
  12:              list.Add(New TerminalExpression())
  13:              list.Add(New TerminalExpression())
  14:              ' Interpret
  15:              For Each exp As AbstractExpression In list
  16:                  exp.Interpret(context)
  17:              Next
  18:              ' Wait for user
  19:              Console.ReadKey()
  20:          End Sub
  21:      End Class
  22:   
  23:      ' The 'Context' class
  24:      Class Context
  25:      End Class
  26:   
  27:      ' The 'AbstractExpression' abstract class
  28:      MustInherit Class AbstractExpression
  29:          Public MustOverride Sub Interpret(context As Context)
  30:      End Class
  31:   
  32:      ' The 'TerminalExpression' class
  33:      Class TerminalExpression
  34:          Inherits AbstractExpression
  35:          Public Overrides Sub Interpret(context As Context)
  36:              Console.WriteLine("Called Terminal.Interpret()")
  37:          End Sub
  38:      End Class
  39:   
  40:      ' The 'NonterminalExpression' class
  41:      Class NonterminalExpression
  42:          Inherits AbstractExpression
  43:          Public Overrides Sub Interpret(context As Context)
  44:              Console.WriteLine("Called Nonterminal.Interpret()")
  45:          End Sub
  46:      End Class




See also:
Creational Patterns Structural Patterns Behavioral Patterns


Theory context:



Comments ( )
Link to this page: //www.vb-net.com/ProgramTheory/Interpreter.htm
< THANKS ME>