Class: Dendroid::Syntax::Rule

Inherits:
Object
  • Object
show all
Defined in:
lib/dendroid/syntax/rule.rb

Overview

In a context-free grammar, a rule has its left-hand side (LHS) that consists solely of one non-terminal symbol. and the right-hand side (RHS) consists of one or more sequence of symbols. The symbols in RHS can be either terminal or non-terminal symbols. The rule stipulates that the LHS is equivalent to the RHS, in other words every occurrence of the LHS can be substituted to corresponding RHS.

Direct Known Subclasses

Choice, Production

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lhs) ⇒ Rule

Create a Rule instance.

Parameters:



19
20
21
# File 'lib/dendroid/syntax/rule.rb', line 19

def initialize(lhs)
  @head = valid_head(lhs)
end

Instance Attribute Details

#headDendroid::Syntax::NonTerminal (readonly) Also known as: lhs

Returns The left-hand side of the rule.

Returns:



14
15
16
# File 'lib/dendroid/syntax/rule.rb', line 14

def head
  @head
end

Instance Method Details

#nonterminalsArray<Dendroid::Syntax::NonTerminal>

The set of all non-terminal symbols that occur in the rhs.

Returns:



40
41
42
# File 'lib/dendroid/syntax/rule.rb', line 40

def nonterminals
  rhs_symbols.reject(&:terminal?)
end

#rhs_symbolsArray<Dendroid::Syntax::GrmSymbol>

The set of all grammar symbols that occur in the rhs.

Returns:



31
32
33
34
35
36
# File 'lib/dendroid/syntax/rule.rb', line 31

def rhs_symbols
  symbols = rhs.reduce([]) do |result, alt|
    result.concat(alt.members)
  end
  symbols.uniq
end

#terminalsArray<Dendroid::Syntax::Terminal>

The set of all terminal symbols that occur in the rhs.

Returns:



46
47
48
# File 'lib/dendroid/syntax/rule.rb', line 46

def terminals
  rhs_symbols.select(&:terminal?)
end

#to_sString

Return the text representation of the rule

Returns:

  • (String)


25
26
27
# File 'lib/dendroid/syntax/rule.rb', line 25

def to_s
  head.to_s
end