rley 0.0.04 → 0.0.05

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.
@@ -7,34 +7,32 @@ require_relative '../../../lib/rley/parser/token'
7
7
 
8
8
  module Rley # Open this namespace to avoid module qualifier prefixes
9
9
  module Parser # Open this namespace to avoid module qualifier prefixes
10
+ describe Token do
10
11
 
11
- describe Token do
12
+ let(:lexeme) { '"some text"' }
13
+ let(:sample_terminal) { Syntax::Terminal.new('if') }
12
14
 
13
- let(:lexeme) { '"some text"' }
14
- let(:sample_terminal) { Syntax::Terminal.new('if') }
15
+ context 'Initialization:' do
15
16
 
16
- context 'Initialization:' do
17
17
 
18
+ # Default instantiation rule
19
+ subject { Token.new(lexeme, sample_terminal) }
18
20
 
19
- # Default instantiation rule
20
- subject { Token.new(lexeme, sample_terminal) }
21
+ it 'should be created with a lexeme and a terminal argument' do
22
+ expect { Token.new(lexeme, sample_terminal) }.not_to raise_error
23
+ end
21
24
 
22
- it 'should be created with a lexeme and a terminal argument' do
23
- expect { Token.new(lexeme, sample_terminal) }.not_to raise_error
24
- end
25
+ it 'should know its lexeme' do
26
+ expect(subject.lexeme).to eq(lexeme)
27
+ end
25
28
 
26
- it 'should know its lexeme' do
27
- expect(subject.lexeme).to eq(lexeme)
28
- end
29
-
30
- it 'should know its terminal' do
31
- expect(subject.terminal).to eq(sample_terminal)
32
- end
33
- end # context
34
-
35
- end # describe
29
+ it 'should know its terminal' do
30
+ expect(subject.terminal).to eq(sample_terminal)
31
+ end
32
+ end # context
36
33
 
34
+ end # describe
37
35
  end # module
38
36
  end # module
39
37
 
40
- # End of file
38
+ # End of file
@@ -9,141 +9,139 @@ require_relative '../../../lib/rley/syntax/grammar'
9
9
 
10
10
  module Rley # Open this namespace to avoid module qualifier prefixes
11
11
  module Syntax # Open this namespace to avoid module qualifier prefixes
12
+ describe Grammar do
12
13
 
13
- describe Grammar do
14
+ # Factory method. Builds a list of productions
15
+ # having same lhs and the symbols sequence
16
+ # in their rhs.
17
+ def alternate_prods(aNonTerminal, sequences)
18
+ prods = sequences.map do |symbs|
19
+ Production.new(aNonTerminal, symbs)
20
+ end
14
21
 
15
- # Factory method. Builds a list of productions
16
- # having same lhs and the symbols sequence
17
- # in their rhs.
18
- def alternate_prods(aNonTerminal, sequences)
19
- prods = sequences.map do |symbs|
20
- Production.new(aNonTerminal, symbs)
22
+ return prods
21
23
  end
22
24
 
23
- return prods
24
- end
25
-
26
- def build_verbatim_symbols(symbols)
27
- result = {}
28
- symbols.each { |symb| result[symb] = VerbatimSymbol.new(symb) }
29
- result
30
- end
31
-
32
- # Grammar 1: arithmetical expressions with integers
33
- let(:grm1_ops) do
34
- operators = %w[+ - * / ( ) ]
35
- build_verbatim_symbols(operators)
36
- end
37
-
38
- # Grammar symbols for integer arithmetic expressions
39
- let(:number) { Literal.new('number', /\d+/)} # Limited to positive integers
40
- let(:add_op) { NonTerminal.new('add_op') }
41
- let(:add_operators) { [grm1_ops['+'], grm1_ops['-']] }
42
- let(:mult_op) { NonTerminal.new('mult_op') }
43
- let(:mult_operators) { [grm1_ops['*'], grm1_ops['/']] }
44
- let(:factor) { NonTerminal.new('factor') }
45
- let(:term) { NonTerminal.new('term') }
46
- let(:expression) { NonTerminal.new('expression') }
47
-
48
-
49
- # Productions for grammar 1
50
- let(:add_op_prods) { alternate_prods(add_op, add_operators) }
51
- let(:mult_op_prods) { alternate_prods(mult_op, mult_operators) }
52
- let(:factor_prods) do
53
- alternatives = [
54
- [number],
55
- [grm1_ops['-'], factor],
56
- [grm1_ops['('], expression, grm1_ops[')']]
57
- ]
58
- alternate_prods(factor, alternatives)
59
- end
60
- let(:term_prods) do
61
- alternatives = [[factor], [term, mult_op, factor]]
62
- alternate_prods(term, alternatives)
63
- end
64
- let(:expression_prods) do
65
- alternatives = [ [term], [expression, add_op, term]]
66
- alternate_prods(expression, alternatives)
67
- end
68
-
69
- # Grammar 2: A very simple language
70
- # S ::= A.
71
- # A ::= "a" A "c".
72
- # A ::= "b".
73
- let(:nt_S) { NonTerminal.new('S') }
74
- let(:nt_A) { NonTerminal.new('A') }
75
- let(:a_) { VerbatimSymbol.new('a') }
76
- let(:b_) { VerbatimSymbol.new('b') }
77
- let(:c_) { VerbatimSymbol.new('c') }
78
- let(:prod_S) { Production.new(nt_S, [nt_A]) }
79
- let(:prod_A1) { Production.new(nt_A, [a_, nt_A, c_]) }
80
- let(:prod_A2) { Production.new(nt_A, [b_]) }
81
-
82
- =begin
83
- # Non-terminals that specify the lexicon of the language
84
- let(:noun) { NonTerminal.new('Noun') }
85
- let(:noun_list) { %w(flights breeze trip morning) }
86
- let(:verb) { NonTerminal.new('Verb') }
87
- let(:verb_list) { %w(is prefer like need want fly) }
88
- let(:adjective) { NonTerminal.new('Adjective') }
89
- let(:adjective_list) { %w(cheapest non-stop first latest other direct) }
90
- let(:pronoun) { NonTerminal.new('Pronoun') }
91
- let(:pronoun_list) { %w(me I you it) }
92
- let(:proper_noun) { NonTerminal.new('Proper_noun') }
93
- let(:proper_noun_list) do [ 'Alaska', 'Baltimore', 'Los Angeles',
94
- 'Chicago', 'United', 'American' ]
95
- end
96
- let(:determiner) { NonTerminal.new('Determiner') }
97
- let(:determiner_list) { %w(the a an this these that) }
98
- let(:preposition) { NonTerminal.new('Preposition') }
99
- let(:preposition_list) { %w(from to on near) }
100
- let(:conjunction) { NonTerminal.new('Conjunction') }
101
- let(:conjunction_list) { %w(and or but) }
102
-
103
-
104
-
105
-
106
- let(:noun_prods) { prods_for_list(noun, noun_list) }
107
- let(:verb_prods) { prods_for_list(verb, verb_list) }
108
- let(:adjective_prods) { prods_for_list(adjective, adjective_list) }
109
- let(:pronoun_prods) { prods_for_list(pronoun, pronoun_list) }
110
- let(:proper_pronoun_prods) do
111
- prods_for_list(proper_pronoun, proper_pronoun_list)
112
- end
113
- let(:determiner_prods) { prods_for_list(determiner, determiner_list) }
114
- let(:preposition_prods) { prods_for_list(preposition, preposition_list) }
115
- let(:conjunction_prods) { prods_for_list(conjunction, conjunction_list) }
116
-
117
- # Productions for the L0 language (from Jurafki & Martin)
118
- let(:nominal_prods) { Production}
119
- =end
120
-
121
- context 'Initialization:' do
122
- subject do
123
- productions = [prod_S, prod_A1, prod_A2]
124
- Grammar.new(productions)
25
+ def build_verbatim_symbols(symbols)
26
+ result = {}
27
+ symbols.each { |symb| result[symb] = VerbatimSymbol.new(symb) }
28
+ result
125
29
  end
126
30
 
127
- it 'should be created with a list of productions' do
128
- expect { Grammar.new([prod_S, prod_A1, prod_A2]) }.not_to raise_error
31
+ # Grammar 1: arithmetical expressions with integers
32
+ let(:grm1_ops) do
33
+ operators = %w[+ - * / ( ) ]
34
+ build_verbatim_symbols(operators)
129
35
  end
130
36
 
131
- it 'should know its productions' do
132
- expect(subject.rules).to eq([prod_S, prod_A1, prod_A2])
37
+ # Grammar symbols for integer arithmetic expressions
38
+ let(:number) { Literal.new('number', /\d+/) } # Positive integers only
39
+ let(:add_op) { NonTerminal.new('add_op') }
40
+ let(:add_operators) { [grm1_ops['+'], grm1_ops['-']] }
41
+ let(:mult_op) { NonTerminal.new('mult_op') }
42
+ let(:mult_operators) { [grm1_ops['*'], grm1_ops['/']] }
43
+ let(:factor) { NonTerminal.new('factor') }
44
+ let(:term) { NonTerminal.new('term') }
45
+ let(:expression) { NonTerminal.new('expression') }
46
+
47
+
48
+ # Productions for grammar 1
49
+ let(:add_op_prods) { alternate_prods(add_op, add_operators) }
50
+ let(:mult_op_prods) { alternate_prods(mult_op, mult_operators) }
51
+ let(:factor_prods) do
52
+ alternatives = [
53
+ [number],
54
+ [grm1_ops['-'], factor],
55
+ [grm1_ops['('], expression, grm1_ops[')']]
56
+ ]
57
+ alternate_prods(factor, alternatives)
58
+ end
59
+ let(:term_prods) do
60
+ alternatives = [[factor], [term, mult_op, factor]]
61
+ alternate_prods(term, alternatives)
133
62
  end
63
+ let(:expression_prods) do
64
+ alternatives = [ [term], [expression, add_op, term]]
65
+ alternate_prods(expression, alternatives)
66
+ end
67
+
68
+ # Grammar 2: A very simple language
69
+ # S ::= A.
70
+ # A ::= "a" A "c".
71
+ # A ::= "b".
72
+ let(:nt_S) { NonTerminal.new('S') }
73
+ let(:nt_A) { NonTerminal.new('A') }
74
+ let(:a_) { VerbatimSymbol.new('a') }
75
+ let(:b_) { VerbatimSymbol.new('b') }
76
+ let(:c_) { VerbatimSymbol.new('c') }
77
+ let(:prod_S) { Production.new(nt_S, [nt_A]) }
78
+ let(:prod_A1) { Production.new(nt_A, [a_, nt_A, c_]) }
79
+ let(:prod_A2) { Production.new(nt_A, [b_]) }
134
80
 
135
- it 'should know its start symbol' do
136
- expect(subject.start_symbol).to eq(nt_S)
81
+ =begin
82
+ # Non-terminals that specify the lexicon of the language
83
+ let(:noun) { NonTerminal.new('Noun') }
84
+ let(:noun_list) { %w(flights breeze trip morning) }
85
+ let(:verb) { NonTerminal.new('Verb') }
86
+ let(:verb_list) { %w(is prefer like need want fly) }
87
+ let(:adjective) { NonTerminal.new('Adjective') }
88
+ let(:adjective_list) { %w(cheapest non-stop first latest other direct) }
89
+ let(:pronoun) { NonTerminal.new('Pronoun') }
90
+ let(:pronoun_list) { %w(me I you it) }
91
+ let(:proper_noun) { NonTerminal.new('Proper_noun') }
92
+ let(:proper_noun_list) do [ 'Alaska', 'Baltimore', 'Los Angeles',
93
+ 'Chicago', 'United', 'American' ]
137
94
  end
95
+ let(:determiner) { NonTerminal.new('Determiner') }
96
+ let(:determiner_list) { %w(the a an this these that) }
97
+ let(:preposition) { NonTerminal.new('Preposition') }
98
+ let(:preposition_list) { %w(from to on near) }
99
+ let(:conjunction) { NonTerminal.new('Conjunction') }
100
+ let(:conjunction_list) { %w(and or but) }
101
+
138
102
 
139
- it 'should know all its symbols' do
140
- expect(subject.symbols).to eq([nt_S, nt_A, a_, c_, b_])
103
+
104
+
105
+ let(:noun_prods) { prods_for_list(noun, noun_list) }
106
+ let(:verb_prods) { prods_for_list(verb, verb_list) }
107
+ let(:adjective_prods) { prods_for_list(adjective, adjective_list) }
108
+ let(:pronoun_prods) { prods_for_list(pronoun, pronoun_list) }
109
+ let(:proper_pronoun_prods) do
110
+ prods_for_list(proper_pronoun, proper_pronoun_list)
141
111
  end
142
- end # context
112
+ let(:determiner_prods) { prods_for_list(determiner, determiner_list) }
113
+ let(:preposition_prods) { prods_for_list(preposition, preposition_list) }
114
+ let(:conjunction_prods) { prods_for_list(conjunction, conjunction_list) }
115
+
116
+ # Productions for the L0 language (from Jurafki & Martin)
117
+ let(:nominal_prods) { Production}
118
+ =end
119
+
120
+ context 'Initialization:' do
121
+ subject do
122
+ productions = [prod_S, prod_A1, prod_A2]
123
+ Grammar.new(productions)
124
+ end
125
+
126
+ it 'should be created with a list of productions' do
127
+ expect { Grammar.new([prod_S, prod_A1, prod_A2]) }.not_to raise_error
128
+ end
129
+
130
+ it 'should know its productions' do
131
+ expect(subject.rules).to eq([prod_S, prod_A1, prod_A2])
132
+ end
133
+
134
+ it 'should know its start symbol' do
135
+ expect(subject.start_symbol).to eq(nt_S)
136
+ end
143
137
 
144
- end # describe
138
+ it 'should know all its symbols' do
139
+ expect(subject.symbols).to eq([nt_S, nt_A, a_, c_, b_])
140
+ end
141
+ end # context
145
142
 
143
+ end # describe
146
144
  end # module
147
145
  end # module
148
146
 
149
- # End of file
147
+ # End of file
@@ -5,25 +5,22 @@ require_relative '../../../lib/rley/syntax/grm_symbol'
5
5
 
6
6
  module Rley # Open this namespace to avoid module qualifier prefixes
7
7
  module Syntax # Open this namespace to avoid module qualifier prefixes
8
+ describe GrmSymbol do
9
+ let(:sample_name) { 'NP' }
10
+ subject { GrmSymbol.new(sample_name) }
8
11
 
9
- describe GrmSymbol do
10
- let(:sample_name) { 'NP' }
11
- subject { GrmSymbol.new(sample_name) }
12
+ context 'Initialization:' do
13
+ it 'should be created with a name' do
14
+ expect { GrmSymbol.new('NP') }.not_to raise_error
15
+ end
12
16
 
13
- context 'Initialization:' do
14
- it 'should be created with a name' do
15
- expect { GrmSymbol.new('NP') }.not_to raise_error
16
- end
17
-
18
- it 'should know its name' do
19
- expect(subject.name).to eq(sample_name)
20
- end
21
- end # context
22
-
23
- end # describe
17
+ it 'should know its name' do
18
+ expect(subject.name).to eq(sample_name)
19
+ end
20
+ end # context
24
21
 
22
+ end # describe
25
23
  end # module
26
24
  end # module
27
25
 
28
26
  # End of file
29
-
@@ -5,28 +5,26 @@ require_relative '../../../lib/rley/syntax/literal'
5
5
 
6
6
  module Rley # Open this namespace to avoid module qualifier prefixes
7
7
  module Syntax # Open this namespace to avoid module qualifier prefixes
8
+ describe Literal do
9
+ let(:sample_name) { 'ordinal' }
10
+ subject { Literal.new(sample_name, /\d+/) }
8
11
 
9
- describe Literal do
10
- let(:sample_name) { 'ordinal' }
11
- subject { Literal.new(sample_name, /\d+/) }
12
+ context 'Initialization:' do
13
+ it 'should be created with a name and regexp' do
14
+ expect { Literal.new(sample_name, /\d+/) }.not_to raise_error
15
+ end
12
16
 
13
- context 'Initialization:' do
14
- it 'should be created with a name and regexp' do
15
- expect { Literal.new(sample_name, /\d+/) }.not_to raise_error
16
- end
17
+ it 'should know its name' do
18
+ expect(subject.name).to eq(sample_name)
19
+ end
17
20
 
18
- it 'should know its name' do
19
- expect(subject.name).to eq(sample_name)
20
- end
21
-
22
- it 'should know its pattern' do
23
- expect(subject.pattern).to eq(/\d+/)
24
- end
25
- end # context
26
-
27
- end # describe
21
+ it 'should know its pattern' do
22
+ expect(subject.pattern).to eq(/\d+/)
23
+ end
24
+ end # context
28
25
 
26
+ end # describe
29
27
  end # module
30
28
  end # module
31
29
 
32
- # End of file
30
+ # End of file
@@ -5,25 +5,22 @@ require_relative '../../../lib/rley/syntax/non_terminal'
5
5
 
6
6
  module Rley # Open this namespace to avoid module qualifier prefixes
7
7
  module Syntax # Open this namespace to avoid module qualifier prefixes
8
+ describe NonTerminal do
9
+ let(:sample_name) { 'noun' }
10
+ subject { NonTerminal.new(sample_name) }
8
11
 
9
- describe NonTerminal do
10
- let(:sample_name) { 'noun' }
11
- subject { NonTerminal.new(sample_name) }
12
+ context 'Initialization:' do
13
+ it 'should be created with a name' do
14
+ expect { NonTerminal.new('noun') }.not_to raise_error
15
+ end
12
16
 
13
- context 'Initialization:' do
14
- it 'should be created with a name' do
15
- expect { NonTerminal.new('noun') }.not_to raise_error
16
- end
17
-
18
- it 'should know its name' do
19
- expect(subject.name).to eq(sample_name)
20
- end
21
- end # context
22
-
23
- end # describe
17
+ it 'should know its name' do
18
+ expect(subject.name).to eq(sample_name)
19
+ end
20
+ end # context
24
21
 
22
+ end # describe
25
23
  end # module
26
24
  end # module
27
25
 
28
26
  # End of file
29
-
@@ -9,41 +9,39 @@ require_relative '../../../lib/rley/syntax/production'
9
9
 
10
10
  module Rley # Open this namespace to avoid module qualifier prefixes
11
11
  module Syntax # Open this namespace to avoid module qualifier prefixes
12
-
13
- describe Production do
14
- let(:sentence) { NonTerminal.new('Sentence') }
15
- let(:np) { NonTerminal.new('NP') }
16
- let(:vp) { NonTerminal.new('VP') }
17
- let(:sequence) { [np, vp] }
18
-
19
- # Default instantiation rule
20
- subject { Production.new(sentence, sequence) }
21
-
22
- context 'Initialization:' do
23
- it 'should be created with a non-terminal and a symbol sequence' do
24
- expect { Production.new(sentence, sequence) }.not_to raise_error
25
- end
26
-
27
- it 'should know its lhs' do
28
- expect(subject.lhs).to eq(sentence)
29
- expect(subject.head).to eq(sentence)
30
- end
31
-
32
- it 'should know its rhs' do
33
- expect(subject.rhs).to eq(sequence)
34
- expect(subject.body).to eq(sequence)
35
- end
36
-
37
- it 'should know whether its rhs is empty' do
38
- expect(subject).not_to be_empty
39
-
40
- instance = Production.new(sentence, [])
41
- expect(instance).to be_empty
42
- end
43
- end # context
44
-
45
- end # describe
46
-
12
+ describe Production do
13
+ let(:sentence) { NonTerminal.new('Sentence') }
14
+ let(:np) { NonTerminal.new('NP') }
15
+ let(:vp) { NonTerminal.new('VP') }
16
+ let(:sequence) { [np, vp] }
17
+
18
+ # Default instantiation rule
19
+ subject { Production.new(sentence, sequence) }
20
+
21
+ context 'Initialization:' do
22
+ it 'should be created with a non-terminal and a symbol sequence' do
23
+ expect { Production.new(sentence, sequence) }.not_to raise_error
24
+ end
25
+
26
+ it 'should know its lhs' do
27
+ expect(subject.lhs).to eq(sentence)
28
+ expect(subject.head).to eq(sentence)
29
+ end
30
+
31
+ it 'should know its rhs' do
32
+ expect(subject.rhs).to eq(sequence)
33
+ expect(subject.body).to eq(sequence)
34
+ end
35
+
36
+ it 'should know whether its rhs is empty' do
37
+ expect(subject).not_to be_empty
38
+
39
+ instance = Production.new(sentence, [])
40
+ expect(instance).to be_empty
41
+ end
42
+ end # context
43
+
44
+ end # describe
47
45
  end # module
48
46
  end # module
49
47