PDA

Ver la Versión Completa : [Sintaxis] Interpretador


Kuk
23 de febrero de 2017, 09:41
Cobol does not have the ability to evaluate expressions dynamically, i.e. defining an expression in a string and having it evaluated during the runtime. OO Cobol is a whole different story. There is no eval command, but we can create our own way to evaluate things using the Interpreter Pattern!


That sounds great, but what is an evalute command anyways? Good question. Language elements stored in a string are nothing, but a string. An eval command take that string and interpret its content, processing the elements accordling. That means power, the ability to define code blocks and have it processed when needed. The Interpreter Pattern allows you to define your own grammar, and process it in order to do some useful task.

The Interpreter Pattern allows much more than simple arithmetic expressions evaluation, thought this is exactly what we are going to show here. The classes in this solutions are:

IExpression: Interface that declares an operation
Context: Global information used by the expression
TerminalExpression: Represent elements in the grammar that do no to get replace, such as symbols
NonTerminalExpression: Represents elements that will be replaced during the evaluation such as variables or even rules
ComputeFormula: Client class that also contains the parser used to break expression's elements.


The ComputeFormula class deals with a lot of concepts not covered in this article, but I recommend you to understand that class in order to get yourself used to things like stacks, collections, string handling and much more.

OO Cobol code

Everything starts with an Interface definition. IExpression declares the contract to which classes must adhere in order to participate in this solution.


INTERFACE-ID. IExpression as "InterpreterPattern.IExpression".
environment division.
configuration section.
repository.
class ClassContext as "InterpreterPattern.Context".

procedure division.

method-id. EvaluateExpression as "Evaluate".

data division.
linkage section.

01 inContext object reference ClassContext.
01 outValue usage comp-2.

procedure division using inContext returning outValue.
end method EvaluateExpression.
END INTERFACE IExpression.


The following classes implements the IExpression interface: AddExpression.cob, SubtractExpression.cob, , MultiplyExpression.cob, DivideExpression.cob, and PowerExpression.cob. These are also the classes that contains the actual calculation performed over operands.

Below the code for PowerExpression code:

CLASS-ID. PowerExpression AS "InterpreterPattern.PowerExpression"
inherits ClassNonTerminalExpression.
environment division.
configuration section.
repository.
class ClassContext as "InterpreterPattern.Context"
class SystemString as "System.String"
class ClassNonTerminalExpression as "InterpreterPattern.NonTerminalExpression"

interface IExpression as "InterpreterPattern.IExpression".

*> Instance's data and methods
object.
data division.
working-storage section.
copy "types.book".

01 variable object reference SystemString private.

procedure division.

method-id. NEW.
data division.
linkage section.
01 inLeftExpression object reference IExpression.
01 inRightExpression object reference IExpression.

procedure division using inLeftExpression, inRightExpression.

invoke super "NEW" using inLeftExpression, inRightExpression.

end method NEW.

method-id. EvaluateExpresion as "Evaluate" override.
data division.
working-storage section.

01 anExpression object reference IExpression.

linkage section.

01 inContext object reference ClassContext.
01 outValue type SystemDouble.

procedure division using inContext returning outValue.

invoke super "GetLeftNode" returning anExpression
move anExpression::"Evaluate"(inContext) to outValue
invoke super "GetRightNode" returning anExpression

*> This is the real "PowerExpression"
compute outValue = outValue ** anExpression::"Evaluate"(inContext)

end method EvaluateExpresion.
end object.
END CLASS PowerExpression.


Other operations' classes such as AddExpression or MultiplyExpression have pretty much basically the same code structure.

The TerminalExpression class that defines symbols behavior:

CLASS-ID. TerminalExpression AS "InterpreterPattern.TerminalExpression".
environment division.
configuration section.
repository.
class ClassContext as "InterpreterPattern.Context"
class SystemCollectionHashTable as "System.Collections.Hashtable"
class SystemString as "System.String"

interface IExpression as "InterpreterPattern.IExpression".

*> Instance's data and methods
object. implements IExpression.
data division.
working-storage section.
copy "types.book".

01 variable object reference SystemString private.

procedure division.

method-id. NEW.
data division.
linkage section.
01 inName object reference SystemString.

procedure division using inName.

set variable to inName

end method NEW.

method-id. EvaluateExpression as "Evaluate".
data division.
linkage section.

01 inContext object reference ClassContext.
01 outValue type SystemDouble.

procedure division using inContext returning outValue.

move inContext::"GetValue"(variable) to outValue

end method EvaluateExpression.
end object.
END CLASS TerminalExpression.


The NonTerminalExpression class that defines rules:

CLASS-ID. NonTerminalExpression AS "InterpreterPattern.NonTerminalExpression".
environment division.
configuration section.
repository.
class ClassContext as "InterpreterPattern.Context"
class SystemCollectionHashTable as "System.Collections.Hashtable"
class SystemString as "System.String"

interface IExpression as "InterpreterPattern.IExpression".

*> Instance's data and methods
object. implements IExpression.
data division.
working-storage section.
copy "types.book".

01 leftNode object reference IExpression private.
01 rightNode object reference IExpression private.

procedure division.

method-id. NEW.
data division.
linkage section.
01 inLeftExpression object reference IExpression.
01 inRightExpression object reference IExpression.
procedure division using inLeftExpression inRightExpression.

invoke self "SetLeftNode" using inLeftExpression
invoke self "SetRightNode" using inRightExpression

end method NEW.

method-id. SetLeftNode.
data division.
linkage section.

01 inNode object reference IExpression.

procedure division using inNode.

set leftNode to inNode

end method SetLeftNode.
method-id. SetRightNode.
data division.
linkage section.

01 inNode object reference IExpression.

procedure division using inNode.

set rightNode to inNode

end method SetRightNode.
method-id. GetLeftNode.
data division.
linkage section.

01 outNode object reference IExpression.

procedure division returning outNode.

set outNode to leftNode

end method GetLeftNode.
method-id. GetRightNode.
data division.
linkage section.

01 outNode object reference IExpression.

procedure division returning outNode.

set outNode to rightNode

end method GetRightNode.
method-id. EvaluateExpression as "Evaluate".
data division.
linkage section.

01 inContext object reference ClassContext.
01 outValue type SystemDouble.

procedure division using inContext returning outValue.

*> Implemented because NetCobol.Net does not supports Abstract classes

end method EvaluateExpression.
end object.
END CLASS NonTerminalExpression.

Kuk
23 de febrero de 2017, 09:43
The ComputeFormula class is the "client" class of this example. It contains a parser, the expression tree builder and, of course, uses Interpreter pattern to arrange things.

CLASS-ID. ComputeFormula AS "InterpreterPattern.ComputeFormula".
environment division.
configuration section.
repository.
class SystemCollectionHashTable as "System.Collections.Hashtable"
class SystemString as "System.String"
class SystemCollectionStack as "System.Collections.Stack"
class SystemDoubleWrapper as "System.Double"
class SystemObject as "System.Object"

class ClassAddExpression as "InterpreterPattern.AddExpression"
class ClassSubtractExpression as "InterpreterPattern.SubtractExpression"
class ClassMultiplyExpression as "InterpreterPattern.MultiplyExpression"
class ClassDivideExpression as "InterpreterPattern.DivideExpression"
class ClassPowerExpression as "InterpreterPattern.PowerExpression"
class ClassTerminalExpression as "InterpreterPattern.TerminalExpression"
class ClassNonTerminalExpression as "InterpreterPattern.NonTerminalExpression"
class ClassContext as "InterpreterPattern.Context"

interface IExpression as "InterpreterPattern.IExpression"

property StringNull as "Empty"
property StringLength as "Length"
property StackCount as "Count".

*> Instance's data and methods
object.
data division.
working-storage section.
copy "types.book".
01 booleanTrue pic 1 value b"1".
01 booleanFalse pic 1 value b"0".
01 expression object reference SystemString.
01 ctx object reference ClassContext.
01 operators object reference SystemCollectionHashTable private.
01 tempObject object reference SystemObject.

procedure division.

method-id. NEW.

procedure division.
invoke SystemCollectionHashTable "NEW" returning operators

invoke operators "Add" using by value "+" by value "1"
invoke operators "Add" using by value "-" by value "1"
invoke operators "Add" using by value "/" by value "2"
invoke operators "Add" using by value "*" by value "2"
invoke operators "Add" using by value "^" by value "2"
invoke operators "Add" using by value "(" by value "0"
end method NEW.

method-id. SetContext as "SetContext".
data division.
linkage section.

01 inContext object reference ClassContext.

procedure division using inContext.

set ctx to inContext

end method SetContext.
method-id. SetExpression as "SetExpression".
data division.
linkage section.

01 inExpression object reference SystemString.

procedure division using inExpression.

set expression to inExpression

end method SetExpression.

method-id. EvaluateExpression as "Evaluate".

data division.

working-storage section.

01 postFixExp object reference SystemString.
01 rootNode object reference IExpression.

linkage section.

01 outValue type SystemDouble.

procedure division returning outValue.

set postFixExp to self::"infixToPostFix"(expression)
set rootNode to self::"BuildTree"(postFixExp)
set outValue to rootNode::"Evaluate"(ctx)

end method EvaluateExpression.

method-id. GetNonTerminalExpression as "GetNonTerminalExpression" private.

data division.

working-storage section.

01 postFixExp object reference SystemString.
01 rootNode object reference IExpression.
01 singleChar pic x.

linkage section.

01 operation object reference SystemString.
01 leftExpression object reference IExpression.
01 rightExpression object reference IExpression.
01 outValue object reference ClassNonTerminalExpression.

procedure division using operation leftExpression
rightExpression returning outValue.
set outValue to null
set singleChar to operation::"Trim"()

evaluate singleChar

when "+"
invoke ClassAddExpression "NEW" using leftExpression,
rightExpression
returning outValue

when "-"
invoke ClassSubtractExpression "NEW" using leftExpression,
rightExpression
returning outValue
when "*"
invoke ClassMultiplyExpression "NEW" using leftExpression,
rightExpression
returning outValue
when "/"
invoke ClassDivideExpression "NEW" using leftExpression,
rightExpression
returning outValue

when "^"
invoke ClassPowerExpression "NEW" using leftExpression,
rightExpression
returning outValue
end-evaluate

end method GetNonTerminalExpression.

method-id. BuildTree as "BuildTree" private.

data division.

working-storage section.

01 currentChar object reference SystemString.
01 leftOperand object reference IExpression.
01 rightOperand object reference IExpression.
01 exp object reference IExpression.
01 stack object reference SystemCollectionStack.
01 rootNode object reference IExpression.
01 indexer pic s9(09) value zeros.
linkage section.

01 inFormula object reference SystemString.
01 outExpression object reference IExpression.

procedure division using inFormula returning outExpression.
invoke SystemCollectionStack "NEW" returning stack

move zeros to indexer
perform until indexer = StringLength of inFormula
set currentChar to inFormula::"Substring"(indexer, 1)

if (self::"IsOperator"(currentChar) = booleanFalse)
set exp to ClassTerminalExpression::"NEW"(currentChar)

invoke stack "Push" using by value exp
else
set tempObject to stack::"Pop"()
set rightOperand to tempObject as IExpression
set tempObject to stack::"Pop"()
set leftOperand to tempObject as IExpression
set exp to self::"GetNonTerminalExpression"
(currentChar, leftOperand, rightOperand)

invoke stack "Push" using by value exp
end-if

add 1 to indexer

end-perform

set tempObject to stack::"Pop"()
set outExpression to tempObject as IExpression

end method BuildTree.

method-id. IsOperator as "IsOperator" private.
data division.

working-storage section.
01 singleChar pic x.

linkage section.

01 inCharacter object reference SystemString.
01 outBool pic 1.

procedure division using inCharacter returning outBool.
set singleChar to inCharacter

if (singleChar = "+" or "-" or "*" or "/" or "^")
move booleanTrue to outBool
else
move booleanFalse to outBool
end-if

end method IsOperator.

method-id. infixToPostFix as "InFixToPostFix".
data division.
working-storage section.
01 stack object reference SystemCollectionStack.
01 pfExpr object reference SystemString.
01 tempString object reference SystemString.
01 expr object reference SystemString.
01 indexer type SystemInt32.
01 stringVal1 object reference SystemString.
01 stringVal2 object reference SystemString.
01 val1 type SystemDouble.
01 val2 type SystemDouble.
01 currentChar object reference SystemString.

linkage section.
01 inString object reference SystemString.
01 outString object reference SystemString.

procedure division using inString returning outString.
invoke SystemCollectionStack "NEW" returning stack

set pfExpr to StringNull of SystemString
set tempString to StringNull of SystemString
set expr to inString::"Trim"()

move zeros to indexer
perform until indexer = StringLength of inString
set currentChar to inString::"Substring"(indexer, 1)

if (currentChar::"Equals"(" ") = booleanTrue)
add 1 to indexer
exit to test of perform
end-if
if (self::"IsOperator"(currentChar) = booleanFalse)
and (currentChar::"Equals"("(") = booleanFalse)
and (currentChar::"Equals"(")") = booleanFalse)
set pfExpr to SystemString::"Concat"(pfExpr, currentChar)
end-if

if (currentChar::"Equals"("(") = booleanTrue)
invoke stack "Push" using currentChar
end-if

if (currentChar::"Equals"(")") = booleanTrue)
set tempObject to stack::"Pop"()
set tempString to tempObject as SystemString
perform until tempString::"Equals"("(") = booleanTrue
set pfExpr to SystemString::"Concat"(pfExpr, tempString)
set tempObject to stack::"Pop"()
set tempString to tempObject as SystemString
end-perform

set tempString to StringNull of SystemString
end-if

if (self::"IsOperator"(currentChar) = booleanTrue)
if (StackCount of stack >; 0)
set tempObject to stack::"Pop"()
set tempString to tempObject as SystemString
set tempObject to operators::"Get_Item"(tempString)
set stringVal1 to tempObject as SystemString
set tempObject to operators::"Get_Item"(currentChar)
set stringVal2 to tempObject as SystemString

move SystemDoubleWrapper::"Parse"(stringVal1) to val1
move SystemDoubleWrapper::"Parse"(stringVal2) to val2

perform until val1 <; val2
set pfExpr to SystemString::"Concat"(pfExpr, tempString)

move -100 to val1

if (StackCount of stack >; 0)
set tempObject to stack::"Pop"()
set tempString to tempObject as SystemString
set tempObject to operators::"Get_Item"(tempString)
set stringVal1 to tempObject as SystemString
set val1 to SystemDoubleWrapper::"Parse"(stringVal1)
end-if

end-perform

if (val1 <; val2) and (val1 > -100)
invoke stack "Push" using tempString
end-if
end-if

invoke stack "Push" using currentChar
end-if

add 1 to indexer
end-perform

perform until StackCount of stack = 0
set tempObject to stack::"Pop"()
set tempString to tempObject as SystemString
set pfExpr to SystemString::"Concat"(pfExpr, tempString)
end-perform

set outString to pfExpr

end method infixToPostFix.

end object.
END CLASS ComputeFormula.


Testing our expression evaluator

The little application below shows how to use our expression evaluator:

IDENTIFICATION DIVISION.
PROGRAM-ID. MAIN AS "ConsoleApplication1.Main".
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
SPECIAL-NAMES.
REPOSITORY.
class ClassComputeFormula as "InterpreterPattern.ComputeFormula"
class ClassContext as "InterpreterPattern.Context".

DATA DIVISION.
WORKING-STORAGE SECTION.
01 computeFormula object reference ClassComputeFormula.
01 context object reference ClassContext.
01 result comp-2.
01 displayResult pic s9(09)v99.

PROCEDURE DIVISION.
invoke ClassComputeFormula "NEW" returning computeFormula
invoke ClassContext "NEW" returning context

invoke computeFormula "SetExpression" using "((a ^ b) + c) * d"

invoke context "CreateVariable" using "a", 10
invoke context "CreateVariable" using "b", 2
invoke context "CreateVariable" using "c", 2
invoke context "CreateVariable" using "d", 8.5

invoke computeFormula "SetContext" using context
invoke computeFormula "Evaluate" returning result

move result to displayResult

display displayResult

END PROGRAM MAIN.


What else can I do with that Pattern?

You name it! Maybe a script language to provide some automation support in your application. Maybe you could create basic Cobol evaluators. That would be something really cool! There are no limits!

Download the source

Cool things with OO Cobol - Source Code (http://cobolrocks.codeplex.com/SourceControl/changeset/view/59104#)

Some references in the web

Interpreter .NET Design Pattern in C# and VB - dofactory.com (http://www.dofactory.com/Patterns/PatternInterpreter.aspx)