Class: Dhaka::Evaluator

### Home Classes Methods

### In Files

- lib/dhaka/evaluator/evaluator.rb

### Parent

SimpleDelegator

### Methods

- ::define_evaluation_rules
- ::new
- #evaluate

### Class/Module Index

Quicksearch

- Dhaka
- Dhaka::CompiledLexer
- Dhaka::CompiledParser
- Dhaka::Evaluator
- Dhaka::Grammar
- Dhaka::GrammarSymbol
- Dhaka::Lexeme
- Dhaka::Lexer
- Dhaka::LexerRun
- Dhaka::LexerSpecification
- Dhaka::LexerSupport
- Dhaka::LexerSupport::AcceptAction
- Dhaka::LexerSupport::CheckpointAction
- Dhaka::LexerSupport::DFARun
- Dhaka::LexerSupport::InvalidRegexException
- Dhaka::LexerSupport::LookaheadAcceptAction
- Dhaka::LexerSupport::RegexParser
- Dhaka::LexerSupport::RegexTokenizer
- Dhaka::LexerSupport::State
- Dhaka::LexerSupport::StateMachine
- Dhaka::NoStartProductionsError
- Dhaka::ParseErrorResult
- Dhaka::ParseSuccessResult
- Dhaka::ParseTreeCompositeNode
- Dhaka::ParseTreeLeafNode
- Dhaka::Parser
- Dhaka::ParserMethods
- Dhaka::PrecedenceBuilder
- Dhaka::Production
- Dhaka::ProductionBuilder
- Dhaka::Token
- Dhaka::Tokenizer
- Dhaka::TokenizerErrorResult
- Dhaka::TokenizerState
- Dhaka::TokenizerSuccessResult

No matching classes.

# Dhaka::Evaluator

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

### Public Class Methods

define_evaluation_rules(options = {}) click to toggle source

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.

    # File lib/dhaka/evaluator/evaluator.rb, line 68
    def define_evaluation_rules(options = {})
      yield
      check_definitions(options)
    end

new() click to toggle source

    # File lib/dhaka/evaluator/evaluator.rb, line 118
    def initialize

    end

### Public Instance Methods

evaluate(node) click to toggle source

Evaluate a parse tree node.

    # File lib/dhaka/evaluator/evaluator.rb, line 108
    def evaluate node
      @node_stack ||= []
      @node_stack << node
      __setobj__(@node_stack.last)
      result      = send(node.production.name)
      @node_stack.pop
      __setobj__(@node_stack.last)
      result
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.