dendroid 0.0.2 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 23f7c3fcf4a7f0df335e5b2fbc7f25f89a60f4d66cc9bfd1ce37680fbb133b83
4
- data.tar.gz: fc4a399d2a5812d4c0e01b28bc261e810386b3d54c353ba2e54190d3df508e3f
3
+ metadata.gz: 869334c2ae25a3765d7105eca383f1ba89162ae9a9446a2b5268a0d463261cc7
4
+ data.tar.gz: 7811bca0c671959b90618a8e667c57da65ed2c5d8ccc4171a86e0c8a3a80a6c0
5
5
  SHA512:
6
- metadata.gz: c4f9ec0db43dcf581d2c5a3866fb383f757bd0a99b4526348b41326a22b0c39b04200768b1cb7a1dfd03ae3272d60de0fc09f73c9a7d9df3269bdf1721f8f19b
7
- data.tar.gz: dd5a819b7946149c38e39822489d2ef08838abb87f6f6e549e2dbade55fa77edf99d91503664506bfe0b07a5c66d5408a7b409c0b090297f70d488696ecd88bc
6
+ metadata.gz: 582c360f25cab435de732a6a7837461302a4af9325f6ddf93448be3c9f674acf59c05e6aa756d21a7ea805c1c55373bed298c4c659844fb67781f51c3444bf67
7
+ data.tar.gz: 52a3e9325988a534998dbbfb35471220c087702f7a97df0acc77c62ef55202be4cb5a858fe16e77ee525f4bfb98ff6fa7756fb99689ce980a8e02cd38ab24aa0
data/.rubocop.yml CHANGED
@@ -1,3 +1,7 @@
1
+ Layout/EndOfLine:
2
+ Enabled: true
3
+ EnforcedStyle: lf
4
+
1
5
  Metrics/BlockLength:
2
6
  Enabled: true
3
7
  Max: 50
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Dendroid
4
+ module Syntax
5
+ # In a context-free grammar, a rule has its left-hand side (LHS)
6
+ # that consists solely of one non-terminal symbol.
7
+ # and the right-hand side (RHS) consists of one or more sequence of symbols.
8
+ # The symbols in RHS can be either terminal or non-terminal symbols.
9
+ # The rule stipulates that the LHS is equivalent to the RHS,
10
+ # in other words every occurrence of the LHS can be substituted to
11
+ # corresponding RHS.
12
+ class Rule
13
+ # @return [Dendroid::Syntax::NonTerminal] The left-hand side of the rule.
14
+ attr_reader :head
15
+ alias lhs head
16
+
17
+ # Create a Rule instance.
18
+ # @param lhs [Dendroid::Syntax::NonTerminal] The left-hand side of the rule.
19
+ def initialize(lhs)
20
+ @head = valid_head(lhs)
21
+ end
22
+
23
+ # Return the text representation of the rule
24
+ # @return [String]
25
+ def to_s
26
+ head.to_s
27
+ end
28
+
29
+ # The set of all grammar symbols that occur in the rhs.
30
+ # @return [Array<Dendroid::Syntax::GrmSymbol>]
31
+ def rhs_symbols
32
+ symbols = rhs.reduce([]) do |result, alt|
33
+ result.concat(alt.members)
34
+ end
35
+ symbols.uniq
36
+ end
37
+
38
+ # The set of all non-terminal symbols that occur in the rhs.
39
+ # @return [Array<Dendroid::Syntax::NonTerminal>]
40
+ def nonterminals
41
+ rhs_symbols.reject(&:terminal?)
42
+ end
43
+
44
+ # The set of all terminal symbols that occur in the rhs.
45
+ # @return [Array<Dendroid::Syntax::Terminal>]
46
+ def terminals
47
+ rhs_symbols.select(&:terminal?)
48
+ end
49
+
50
+ private
51
+
52
+ def valid_head(lhs)
53
+ if lhs.terminal?
54
+ err_msg = "Terminal symbol '#{lhs}' may not be on left-side of a rule."
55
+ raise StandardError, err_msg
56
+ end
57
+
58
+ lhs
59
+ end
60
+ end # class
61
+ end # module
62
+ end # module
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '..\..\spec_helper'
4
+ require_relative '..\..\..\lib\dendroid\syntax\terminal'
5
+ require_relative '..\..\..\lib\dendroid\syntax\non_terminal'
6
+ require_relative '..\..\..\lib\dendroid\syntax\rule'
7
+
8
+ describe Dendroid::Syntax::Rule do
9
+ let(:num_symb) { Dendroid::Syntax::Terminal.new('NUMBER') }
10
+ let(:expr_symb) { Dendroid::Syntax::NonTerminal.new('expression') }
11
+
12
+ subject { described_class.new(expr_symb) }
13
+
14
+ context 'Initialization:' do
15
+ it 'is initialized with a non-terminal' do
16
+ expect { described_class.new(expr_symb) }.not_to raise_error
17
+ end
18
+
19
+ it 'knows its head (aka lhs)' do
20
+ expect(subject.head).to eq(expr_symb)
21
+ end
22
+ end # context
23
+ end # describe
data/version.txt CHANGED
@@ -1 +1 @@
1
- 0.0.2
1
+ 0.0.3
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dendroid
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dimitri Geshef
@@ -25,10 +25,12 @@ files:
25
25
  - lib/dendroid/dendroid.rb
26
26
  - lib/dendroid/syntax/grm_symbol.rb
27
27
  - lib/dendroid/syntax/non_terminal.rb
28
+ - lib/dendroid/syntax/rule.rb
28
29
  - lib/dendroid/syntax/symbol_seq.rb
29
30
  - lib/dendroid/syntax/terminal.rb
30
31
  - spec/dendroid/syntax/grm_symbol_spec.rb
31
32
  - spec/dendroid/syntax/non_terminal_spec.rb
33
+ - spec/dendroid/syntax/rule_spec.rb
32
34
  - spec/dendroid/syntax/symbol_seq_spec.rb
33
35
  - spec/dendroid/syntax/terminal_spec.rb
34
36
  - spec/spec_helper.rb