Class: Dendroid::Syntax::Rule
- Inherits:
-
Object
- Object
- Dendroid::Syntax::Rule
- 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
Instance Attribute Summary collapse
-
#head ⇒ Dendroid::Syntax::NonTerminal
(also: #lhs)
readonly
The left-hand side of the rule.
Instance Method Summary collapse
-
#initialize(lhs) ⇒ Rule
constructor
Create a Rule instance.
-
#nonterminals ⇒ Array<Dendroid::Syntax::NonTerminal>
The set of all non-terminal symbols that occur in the rhs.
-
#rhs_symbols ⇒ Array<Dendroid::Syntax::GrmSymbol>
The set of all grammar symbols that occur in the rhs.
-
#terminals ⇒ Array<Dendroid::Syntax::Terminal>
The set of all terminal symbols that occur in the rhs.
-
#to_s ⇒ String
Return the text representation of the rule.
Constructor Details
#initialize(lhs) ⇒ Rule
Create a Rule instance.
19 20 21 |
# File 'lib/dendroid/syntax/rule.rb', line 19 def initialize(lhs) @head = valid_head(lhs) end |
Instance Attribute Details
#head ⇒ Dendroid::Syntax::NonTerminal (readonly) Also known as: lhs
Returns The left-hand side of the rule.
14 15 16 |
# File 'lib/dendroid/syntax/rule.rb', line 14 def head @head end |
Instance Method Details
#nonterminals ⇒ Array<Dendroid::Syntax::NonTerminal>
The set of all non-terminal symbols that occur in the rhs.
40 41 42 |
# File 'lib/dendroid/syntax/rule.rb', line 40 def nonterminals rhs_symbols.reject(&:terminal?) end |
#rhs_symbols ⇒ Array<Dendroid::Syntax::GrmSymbol>
The set of all grammar symbols that occur in the rhs.
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 |
#terminals ⇒ Array<Dendroid::Syntax::Terminal>
The set of all terminal symbols that occur in the rhs.
46 47 48 |
# File 'lib/dendroid/syntax/rule.rb', line 46 def terminals rhs_symbols.select(&:terminal?) end |
#to_s ⇒ String
Return the text representation of the rule
25 26 27 |
# File 'lib/dendroid/syntax/rule.rb', line 25 def to_s head.to_s end |