Class Dhaka::Evaluator
In: ../trunk/lib/dhaka/evaluator/evaluator.rb
Parent: SimpleDelegator

Abstract base class for evaluators.

Defining an evaluator is an easy way to perform syntax-directed evaluation without having to generate an abstract syntax tree representation of the input.

An evaluation rule for a given production named bar is defined by calling for_bar with a block that performs the evaluation. For detailed examples, see the evaluators in the test suite.

The following is an evaluator for arithmetic expressions. When a parse tree node is encountered that corresponds to the production named addition, the block passed to for_addition is invoked. The evaluate method is then recursively called on the child nodes, in this case the operands to the addition operation. The result is obtained by adding the evaluation results of the child nodes.

   class ArithmeticPrecedenceEvaluator < Dhaka::Evaluator

     self.grammar = ArithmeticPrecedenceGrammar

     define_evaluation_rules do

       for_subtraction do
         evaluate(child_nodes[0]) - evaluate(child_nodes[2])
       end

       for_addition do
         evaluate(child_nodes[0]) + evaluate(child_nodes[2])
       end

       for_division do
         evaluate(child_nodes[0]).to_f/evaluate(child_nodes[2])
       end

       for_multiplication do
         evaluate(child_nodes[0]) * evaluate(child_nodes[2])
       end

       for_literal do
         child_nodes[0].token.value.to_i
       end

       for_parenthetized_expression do
         evaluate(child_nodes[1])
       end

       for_negated_expression do
         -evaluate(child_nodes[1])
       end

       for_power do
         evaluate(child_nodes[0])**evaluate(child_nodes[2])
       end

     end

   end

Methods

Public Class methods

Define evaluation rules within a block passed to this method. The evaluator will define default evaluation rules for pass-through productions (i.e. productions with expansions consisting of exactly one grammar symbol). The default evaluation rule for such productions is to simply return the result of calling evaluate on the unique child node. Setting the :raise_error option to true tells the evaluator to throw an exception if you neglect to define a rule for a non-pass-through production (one where the expansion consists of multiple symbols), listing all the productions that absolutely need to be defined before you can continue.

Public Instance methods

Evaluate a parse tree node.

[Validate]