rley 0.7.08 → 0.8.03
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +29 -5
- data/CHANGELOG.md +28 -4
- data/README.md +4 -5
- data/examples/NLP/nano_eng/nano_en_demo.rb +7 -11
- data/examples/NLP/nano_eng/nano_grammar.rb +18 -18
- data/examples/data_formats/JSON/json_ast_builder.rb +9 -18
- data/examples/data_formats/JSON/json_demo.rb +1 -2
- data/examples/data_formats/JSON/json_grammar.rb +11 -11
- data/examples/general/calc_iter1/calc_grammar.rb +5 -4
- data/examples/general/calc_iter2/calc_grammar.rb +9 -9
- data/examples/general/left.rb +1 -1
- data/examples/general/right.rb +1 -1
- data/lib/rley/base/dotted_item.rb +5 -0
- data/lib/rley/base/grm_items_builder.rb +6 -0
- data/lib/rley/constants.rb +1 -1
- data/lib/rley/engine.rb +2 -2
- data/lib/rley/interface.rb +16 -0
- data/lib/rley/notation/all_notation_nodes.rb +4 -0
- data/lib/rley/notation/ast_builder.rb +185 -0
- data/lib/rley/notation/ast_node.rb +44 -0
- data/lib/rley/notation/ast_visitor.rb +115 -0
- data/lib/rley/notation/grammar.rb +49 -0
- data/lib/rley/notation/grammar_builder.rb +505 -0
- data/lib/rley/notation/grouping_node.rb +23 -0
- data/lib/rley/notation/parser.rb +56 -0
- data/lib/rley/notation/sequence_node.rb +35 -0
- data/lib/rley/notation/symbol_node.rb +29 -0
- data/lib/rley/notation/tokenizer.rb +180 -0
- data/lib/rley/parse_rep/ast_base_builder.rb +44 -0
- data/lib/rley/parser/gfg_chart.rb +101 -6
- data/lib/rley/parser/gfg_earley_parser.rb +1 -1
- data/lib/rley/parser/gfg_parsing.rb +5 -3
- data/lib/rley/parser/parse_entry_set.rb +1 -1
- data/lib/rley/syntax/{grammar_builder.rb → base_grammar_builder.rb} +53 -15
- data/lib/rley/syntax/grm_symbol.rb +1 -1
- data/lib/rley/syntax/match_closest.rb +43 -0
- data/lib/rley/syntax/production.rb +6 -0
- data/lib/rley.rb +1 -1
- data/spec/rley/engine_spec.rb +6 -6
- data/spec/rley/gfg/grm_flow_graph_spec.rb +2 -2
- data/spec/rley/notation/grammar_builder_spec.rb +302 -0
- data/spec/rley/notation/parser_spec.rb +183 -0
- data/spec/rley/notation/tokenizer_spec.rb +364 -0
- data/spec/rley/parse_rep/ast_builder_spec.rb +0 -1
- data/spec/rley/parse_rep/groucho_spec.rb +1 -1
- data/spec/rley/parse_rep/parse_forest_builder_spec.rb +1 -1
- data/spec/rley/parse_rep/parse_forest_factory_spec.rb +2 -2
- data/spec/rley/parse_rep/parse_tree_factory_spec.rb +1 -1
- data/spec/rley/parser/dangling_else_spec.rb +447 -0
- data/spec/rley/parser/gfg_earley_parser_spec.rb +118 -10
- data/spec/rley/parser/gfg_parsing_spec.rb +2 -1
- data/spec/rley/parser/parse_walker_factory_spec.rb +2 -2
- data/spec/rley/support/ambiguous_grammar_helper.rb +2 -2
- data/spec/rley/support/grammar_abc_helper.rb +2 -2
- data/spec/rley/support/grammar_ambig01_helper.rb +2 -2
- data/spec/rley/support/grammar_arr_int_helper.rb +2 -2
- data/spec/rley/support/grammar_b_expr_helper.rb +2 -2
- data/spec/rley/support/grammar_int_seq_helper.rb +51 -0
- data/spec/rley/support/grammar_l0_helper.rb +2 -2
- data/spec/rley/support/grammar_pb_helper.rb +2 -2
- data/spec/rley/support/grammar_sppf_helper.rb +2 -2
- data/spec/rley/syntax/{grammar_builder_spec.rb → base_grammar_builder_spec.rb} +29 -11
- data/spec/rley/syntax/match_closest_spec.rb +46 -0
- data/spec/rley/syntax/production_spec.rb +4 -0
- metadata +29 -14
- data/lib/rley/parser/parse_state.rb +0 -78
- data/lib/rley/parser/parse_state_tracker.rb +0 -59
- data/lib/rley/parser/state_set.rb +0 -100
- data/spec/rley/parser/parse_state_spec.rb +0 -125
- data/spec/rley/parser/parse_tracer_spec.rb +0 -200
- data/spec/rley/parser/state_set_spec.rb +0 -130
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3e3f429e3129dc66ab35ca8bba695b0788d710c6e61033a086d7b3c7c40af7cf
|
4
|
+
data.tar.gz: eea709ac4554918cc690e27ebb8540418234b6b0706fd98390c6f8f5d603f868
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bf936e0f984789b7c509b9bbd5078c787e79caef32120f4409b23225fe7589d1bdddf5e74e1467401acffb329461b0812c3bb46a9da4b651ae10c94fcd4a52e1
|
7
|
+
data.tar.gz: 606e0e80bfedcd9915eff6b776a826b9032cef201febc07bfcbc1a027622606584bd83e9874a5f448637dd46f66592490c92138c874a07ae630d678e17b5982f
|
data/.rubocop.yml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
AllCops:
|
2
2
|
Exclude:
|
3
|
-
- '
|
4
|
-
- '
|
3
|
+
- 'examples/**/*'
|
4
|
+
- 'exp/**/*'
|
5
5
|
|
6
6
|
Gemspec/DateAssignment:
|
7
7
|
Enabled: true
|
@@ -50,6 +50,9 @@ Layout/IndentationConsistency:
|
|
50
50
|
Layout/HeredocIndentation:
|
51
51
|
Enabled: false
|
52
52
|
|
53
|
+
Layout/LineEndStringConcatenationIndentation:
|
54
|
+
Enabled: true
|
55
|
+
|
53
56
|
Layout/MultilineHashBraceLayout:
|
54
57
|
Enabled: true
|
55
58
|
|
@@ -81,6 +84,9 @@ Layout/TrailingWhitespace:
|
|
81
84
|
Lint/AmbiguousAssignment:
|
82
85
|
Enabled: true
|
83
86
|
|
87
|
+
Lint/AmbiguousRange:
|
88
|
+
Enabled: true
|
89
|
+
|
84
90
|
Lint/DeprecatedConstants:
|
85
91
|
Enabled: true
|
86
92
|
|
@@ -96,6 +102,9 @@ Lint/EmptyBlock:
|
|
96
102
|
Lint/EmptyClass:
|
97
103
|
Enabled: false
|
98
104
|
|
105
|
+
Lint/EmptyInPattern:
|
106
|
+
Enabled: true
|
107
|
+
|
99
108
|
Lint/LambdaWithoutLiteralBlock:
|
100
109
|
Enabled: true
|
101
110
|
|
@@ -192,6 +201,9 @@ Naming/ClassAndModuleCamelCase:
|
|
192
201
|
Naming/BlockParameterName:
|
193
202
|
Enabled: true
|
194
203
|
|
204
|
+
Naming/InclusiveLanguage:
|
205
|
+
Enabled: true
|
206
|
+
|
195
207
|
Naming/MethodParameterName:
|
196
208
|
Enabled: false
|
197
209
|
|
@@ -201,9 +213,6 @@ Naming/MethodName:
|
|
201
213
|
Naming/VariableName:
|
202
214
|
Enabled: false
|
203
215
|
|
204
|
-
Style/AccessorGrouping:
|
205
|
-
Enabled: false
|
206
|
-
|
207
216
|
Style/Alias:
|
208
217
|
Enabled: true
|
209
218
|
|
@@ -276,11 +285,17 @@ Style/HashTransformValues:
|
|
276
285
|
Style/IfUnlessModifier:
|
277
286
|
Enabled: false
|
278
287
|
|
288
|
+
Style/InPatternThen:
|
289
|
+
Enabled: true
|
290
|
+
|
279
291
|
Style/InverseMethods:
|
280
292
|
Enabled: false
|
281
293
|
|
282
294
|
Style/MissingRespondToMissing:
|
283
295
|
Enabled: false
|
296
|
+
|
297
|
+
Style/MultilineInPatternThen:
|
298
|
+
Enabled: true
|
284
299
|
|
285
300
|
Style/NegatedIfElseCondition:
|
286
301
|
Enabled: true
|
@@ -293,6 +308,9 @@ Style/NilLambda:
|
|
293
308
|
|
294
309
|
Style/NumericLiterals:
|
295
310
|
Enabled: false
|
311
|
+
|
312
|
+
Style/QuotedSymbols:
|
313
|
+
Enabled: true
|
296
314
|
|
297
315
|
Style/RaiseArgs:
|
298
316
|
Enabled: true
|
@@ -305,6 +323,9 @@ Style/RedundantReturn:
|
|
305
323
|
|
306
324
|
Style/RedundantSelf:
|
307
325
|
Enabled: true
|
326
|
+
|
327
|
+
Style/RedundantSelfAssignmentBranch:
|
328
|
+
Enabled: true
|
308
329
|
|
309
330
|
Style/RegexpLiteral:
|
310
331
|
Enabled: false
|
@@ -367,6 +388,9 @@ Lint/TopLevelReturnWithArgument:
|
|
367
388
|
Lint/UnreachableLoop:
|
368
389
|
Enabled: true
|
369
390
|
|
391
|
+
Style/AccessorGrouping:
|
392
|
+
Enabled: true
|
393
|
+
|
370
394
|
Style/ArrayCoercion:
|
371
395
|
Enabled: true
|
372
396
|
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,27 @@
|
|
1
|
+
### 0.8.03 / 2021-08-27
|
2
|
+
- Added missing method in `ASTBaseBuilder` class
|
3
|
+
|
4
|
+
* [FIX] Added method `ParseRep::ASTBaseBuilder#reduce_children`.
|
5
|
+
|
6
|
+
### 0.8.02 / 2021-08-23
|
7
|
+
- Code restyling to please Rubocop 1.19.1
|
8
|
+
|
9
|
+
* [CHANGE] File `.rubocp.yml` updated to take into account new cops.
|
10
|
+
* [CHANGE] File `*.rb` files updated to fix Rubocop findings
|
11
|
+
|
12
|
+
|
13
|
+
### 0.8.01 / 2021-08-22
|
14
|
+
- Unused/redundant file removal. Fix in rule generation
|
15
|
+
|
16
|
+
* [CHANGE] Removal of files in repository that were redundant/useless.
|
17
|
+
* [FIX] The rule ordering was broken by the rules implicitly generated by Rlry
|
18
|
+
|
19
|
+
### 0.8.00 / 2021-08-15
|
20
|
+
- New grammar builder that accepts ? * + modifiers
|
21
|
+
|
22
|
+
### 0.7.08 / 2021-05-30
|
23
|
+
- Code restyling to please rubocop 1.15.0
|
24
|
+
|
1
25
|
### 0.7.07 / 2020-11-16
|
2
26
|
- Code restyling to please rubocop 0.93.1
|
3
27
|
* [CHANGE] File `.travis.yml`: updated Ruby versions, drop support for Ruby 2.3.x
|
@@ -9,10 +33,10 @@
|
|
9
33
|
- [FIX] Method `GFGParsing#nullable_rule`: issue with nullable productions having at least one member in their rhs.
|
10
34
|
|
11
35
|
### 0.7.04 / 2019-08-17
|
12
|
-
- Rley recognizer is about 25% faster than previous version. Kudos to the people
|
13
|
-
behind the *magic_frozen_string_literal* gem.
|
36
|
+
- Rley recognizer is about 25% faster than previous version. Kudos to the people
|
37
|
+
behind the *magic_frozen_string_literal* gem.
|
14
38
|
- Code refactoring to use string frozen magic comments (as a consequence, Rley runs only on Rubies 2.3 or newer).
|
15
|
-
- Code restyling to please rubocop 0.7.40.
|
39
|
+
- Code restyling to please rubocop 0.7.40.
|
16
40
|
- [CHANGE] Class `ParseEntrySet`: minor code optimization
|
17
41
|
- [CHANGE] File `README.md` removed allusion to Ruby 2.0.x up to 2.2.x.
|
18
42
|
- [CHANGE] File `README.md` added Ruby 2.6.x up as supported version.
|
@@ -97,7 +121,7 @@ behind the *magic_frozen_string_literal* gem.
|
|
97
121
|
* [FIX] Code re-styling to remove most style offenses found by Rubocop 0.52.1
|
98
122
|
|
99
123
|
### 0.6.00 / 2018-02-25
|
100
|
-
Version bump. Highlights: new programming interface through facade object, improved AST generation.
|
124
|
+
Version bump. Highlights: new programming interface through facade object, improved AST generation.
|
101
125
|
* [NEW] Class `Rley::Engine`: Implementation of Facade design pattern to reach more convenient interface.
|
102
126
|
* [NEW] Class `Rley::ParseRep::ASTBaseBuilder` Abstract class that simplifies the creation of custom AST (Abstract Syntax Tree)
|
103
127
|
* [NEW] Module `Rley::ParseRep` hosts the classes for building parse representations (parse trees and forests)
|
data/README.md
CHANGED
@@ -50,11 +50,10 @@ application range such as:
|
|
50
50
|
|
51
51
|
|
52
52
|
### Compatibility
|
53
|
-
Rley supports the following Ruby implementations:
|
54
|
-
- MRI 2.3
|
55
|
-
- MRI 2.4
|
53
|
+
Rley supports the following Ruby implementations:
|
56
54
|
- MRI 2.5
|
57
55
|
- MRI 2.6
|
56
|
+
- MRI 2.7
|
58
57
|
- JRuby 9.1+
|
59
58
|
|
60
59
|
---
|
@@ -106,9 +105,9 @@ The subset of English grammar is based on an example from the NLTK book.
|
|
106
105
|
# Here we define the productions (= grammar rules)
|
107
106
|
rule 'S' => 'NP VP'
|
108
107
|
rule 'NP' => 'Proper-Noun'
|
109
|
-
rule 'NP' => 'Determiner Noun'
|
108
|
+
rule 'NP' => 'Determiner Noun'
|
110
109
|
rule 'NP' => 'Determiner Noun PP'
|
111
|
-
rule 'VP' => 'Verb NP'
|
110
|
+
rule 'VP' => 'Verb NP'
|
112
111
|
rule 'VP' => 'Verb NP PP'
|
113
112
|
rule 'PP' => 'Preposition NP'
|
114
113
|
end
|
@@ -24,20 +24,16 @@ engine.build_grammar do
|
|
24
24
|
|
25
25
|
# Here we define the productions (= grammar rules)
|
26
26
|
rule 'Start' => 'S'
|
27
|
-
rule 'S' =>
|
28
|
-
rule 'S' => %w[Aux NP VP]
|
27
|
+
rule 'S' => 'Aux? NP VP'
|
29
28
|
rule 'S' => 'VP'
|
30
29
|
rule 'NP' => 'Pronoun'
|
31
30
|
rule 'NP' => 'Proper-Noun'
|
32
|
-
rule 'NP' =>
|
33
|
-
rule 'Nominal' =>
|
34
|
-
rule '
|
35
|
-
rule 'VP' => 'Verb'
|
36
|
-
rule 'VP' =>
|
37
|
-
rule '
|
38
|
-
rule 'VP' => %w[Verb PP]
|
39
|
-
rule 'VP' => %w[VP PP]
|
40
|
-
rule 'PP' => %w[Preposition NP]
|
31
|
+
rule 'NP' => 'Determiner Nominal'
|
32
|
+
rule 'Nominal' => 'Nominal? Noun'
|
33
|
+
rule 'VP' => 'Verb PP?'
|
34
|
+
rule 'VP' => 'Verb NP PP?'
|
35
|
+
rule 'VP' => 'VP PP'
|
36
|
+
rule 'PP' => 'Preposition NP'
|
41
37
|
end
|
42
38
|
|
43
39
|
########################################
|
@@ -12,7 +12,7 @@ require 'rley' # Load the gem
|
|
12
12
|
# Daniel Jurafsky, James H. Martin: "Speech and Language Processing";
|
13
13
|
# 2009, Pearson Education, Inc., ISBN 978-0135041963
|
14
14
|
# It defines the syntax of a sentence in a mini English-like language
|
15
|
-
builder = Rley::
|
15
|
+
builder = Rley::grammar_builder do
|
16
16
|
add_terminals('Pronoun', 'Proper-Noun')
|
17
17
|
add_terminals('Determiner', 'Noun')
|
18
18
|
add_terminals('Cardinal_number', 'Ordinal_number', 'Quant')
|
@@ -25,19 +25,19 @@ builder = Rley::Syntax::GrammarBuilder.new do
|
|
25
25
|
rule 'sentence' => 'yes_no_question'
|
26
26
|
rule 'sentence' => 'wh_subject_question'
|
27
27
|
rule 'sentence' => 'wh_non_subject_question'
|
28
|
-
rule 'declarative' =>
|
28
|
+
rule 'declarative' => 'NP VP'
|
29
29
|
rule 'imperative' => 'VP'
|
30
|
-
rule 'yes_no_question' =>
|
31
|
-
rule 'wh_subject_question' =>
|
32
|
-
rule 'wh_non_subject_question' =>
|
33
|
-
rule 'NP' =>
|
30
|
+
rule 'yes_no_question' => 'Aux NP VP'
|
31
|
+
rule 'wh_subject_question' => 'Wh_NP NP VP'
|
32
|
+
rule 'wh_non_subject_question' => 'Wh_NP Aux NP VP'
|
33
|
+
rule 'NP' => 'Predeterminer NP'
|
34
34
|
rule 'NP' => 'Pronoun'
|
35
35
|
rule 'NP' => 'Proper-Noun'
|
36
|
-
rule 'NP' =>
|
36
|
+
rule 'NP' => 'Det Card Ord Quant Nominal'
|
37
37
|
rule 'VP' => 'Verb'
|
38
|
-
rule 'VP' =>
|
39
|
-
rule 'VP' =>
|
40
|
-
rule 'VP' =>
|
38
|
+
rule 'VP' => 'Verb NP'
|
39
|
+
rule 'VP' => 'Verb NP PP'
|
40
|
+
rule 'VP' => 'Verb PP'
|
41
41
|
rule 'Det' => 'Determiner'
|
42
42
|
rule 'Det' => []
|
43
43
|
rule 'Card' => 'Cardinal_number'
|
@@ -45,15 +45,15 @@ builder = Rley::Syntax::GrammarBuilder.new do
|
|
45
45
|
rule 'Ord' => 'Ordinal_number'
|
46
46
|
rule 'Ord' => []
|
47
47
|
rule 'Nominal' => 'Noun'
|
48
|
-
rule 'Nominal' =>
|
49
|
-
rule 'Nominal' =>
|
50
|
-
rule 'Nominal' =>
|
51
|
-
rule 'PP' =>
|
48
|
+
rule 'Nominal' => 'Nominal Noun'
|
49
|
+
rule 'Nominal' => 'Nominal GerundVP'
|
50
|
+
rule 'Nominal' => 'Nominal RelClause'
|
51
|
+
rule 'PP' => 'Preposition NP'
|
52
52
|
rule 'GerundVP' => 'GerundV'
|
53
|
-
rule 'GerundVP' =>
|
54
|
-
rule 'GerundVP' =>
|
55
|
-
rule 'GerundVP' =>
|
56
|
-
rule 'RelClause' =>
|
53
|
+
rule 'GerundVP' => 'GerundV NP'
|
54
|
+
rule 'GerundVP' => 'GerundV NP PP'
|
55
|
+
rule 'GerundVP' => 'GerundV PP'
|
56
|
+
rule 'RelClause' => 'Relative_pronoun VP'
|
57
57
|
end
|
58
58
|
|
59
59
|
# And now build the grammar...
|
@@ -32,24 +32,20 @@ class JSONASTBuilder < Rley::ParseRep::ASTBaseBuilder
|
|
32
32
|
|
33
33
|
# rubocop: disable Naming/VariableNumber
|
34
34
|
|
35
|
+
# rule 'JSON_text' => 'value'
|
35
36
|
def reduce_JSON_text_0(_aProd, aRange, theTokens, theChildren)
|
36
37
|
return_first_child(aRange, theTokens, theChildren)
|
37
38
|
end
|
38
39
|
|
39
|
-
# rule 'object' =>
|
40
|
+
# rule 'object' => 'begin-object member-list? end-object'
|
40
41
|
def reduce_object_0(aProduction, _range, _tokens, theChildren)
|
41
42
|
second_child = theChildren[1]
|
42
43
|
second_child.symbol = aProduction.lhs
|
43
44
|
return second_child
|
44
45
|
end
|
45
46
|
|
46
|
-
# rule '
|
47
|
-
def
|
48
|
-
return JSONObjectNode.new(aProduction.lhs)
|
49
|
-
end
|
50
|
-
|
51
|
-
# rule 'member-list' => %w[member-list value-separator member]
|
52
|
-
def reduce_member_list_0(_range, _tokens, theChildren)
|
47
|
+
# rule 'member-list' => 'member-list value-separator member'
|
48
|
+
def reduce_member_list_0(_production, _range, _tokens, theChildren)
|
53
49
|
node = theChildren[0]
|
54
50
|
node.members << theChildren.last
|
55
51
|
return node
|
@@ -62,31 +58,26 @@ class JSONASTBuilder < Rley::ParseRep::ASTBaseBuilder
|
|
62
58
|
return node
|
63
59
|
end
|
64
60
|
|
65
|
-
# rule 'member' =>
|
61
|
+
# rule 'member' => 'string name-separator value'
|
66
62
|
def reduce_member_0(aProduction, _range, _tokens, theChildren)
|
67
63
|
return JSONPair.new(theChildren[0], theChildren[2], aProduction.lhs)
|
68
64
|
end
|
69
65
|
|
70
|
-
# rule 'array' =>
|
66
|
+
# rule 'array' => 'begin-array array-items? end-array'
|
71
67
|
def reduce_array_0(aProduction, _range, _tokens, theChildren)
|
72
68
|
second_child = theChildren[1]
|
73
69
|
second_child.symbol = aProduction.lhs
|
74
70
|
return second_child
|
75
71
|
end
|
76
72
|
|
77
|
-
# rule 'array' =>
|
78
|
-
def
|
79
|
-
return JSONArrayNode.new
|
80
|
-
end
|
81
|
-
|
82
|
-
# rule 'array-items' => %w[array-items value-separator value]
|
83
|
-
def reduce_array_items_0(_range, _tokens, theChildren)
|
73
|
+
# rule 'array-items' => 'array-items value-separator value'
|
74
|
+
def reduce_array_items_0(_production, _range, _tokens, theChildren)
|
84
75
|
node = theChildren[0]
|
85
76
|
node.children << theChildren[2]
|
86
77
|
return node
|
87
78
|
end
|
88
79
|
|
89
|
-
# rule 'array-items' =>
|
80
|
+
# rule 'array-items' => 'value'
|
90
81
|
def reduce_array_items_1(aProduction, _range, _tokens, theChildren)
|
91
82
|
node = JSONArrayNode.new(aProduction.lhs)
|
92
83
|
node.children << theChildren[0]
|
@@ -37,9 +37,8 @@ case cli_options[:format]
|
|
37
37
|
raise StandardError, msg if tree_rep == :cst
|
38
38
|
end
|
39
39
|
|
40
|
-
|
41
40
|
# Create a Rley facade object
|
42
|
-
# If
|
41
|
+
# If requested, select AST representation
|
43
42
|
engine = Rley::Engine.new do |cfg|
|
44
43
|
builder = tree_rep == :ast ? JSONASTBuilder : nil
|
45
44
|
cfg.repr_builder = builder
|
@@ -9,30 +9,30 @@ require 'rley' # Load the gem
|
|
9
9
|
# Original JSON grammar is available at: http://www.json.org/fatfree.html
|
10
10
|
# Official JSON grammar: http://rfc7159.net/rfc7159#rfc.section.2
|
11
11
|
# Names of grammar elements are based on the RFC 7159 documentation
|
12
|
-
builder = Rley::
|
12
|
+
builder = Rley::grammar_builder do
|
13
13
|
add_terminals('false', 'null', 'true') # Literal names
|
14
14
|
add_terminals('string', 'number')
|
15
15
|
add_terminals('begin-object', 'end-object') # For '{', '}' delimiters
|
16
16
|
add_terminals('begin-array', 'end-array') # For '[', ']' delimiters
|
17
17
|
add_terminals('name-separator', 'value-separator') # For ':', ',' separators
|
18
|
+
|
18
19
|
rule 'JSON_text' => 'value'
|
19
20
|
rule 'value' => 'false'
|
20
21
|
rule 'value' => 'null'
|
21
22
|
rule 'value' => 'true'
|
22
|
-
rule 'value' => 'object'
|
23
23
|
rule 'value' => 'array'
|
24
|
+
rule 'value' => 'object'
|
24
25
|
rule 'value' => 'number'
|
25
26
|
rule 'value' => 'string'
|
26
|
-
rule 'object' =>
|
27
|
-
|
27
|
+
rule 'object' => 'begin-object member_list? end-object'
|
28
|
+
|
28
29
|
# Next rule is an example of a left recursive rule
|
29
|
-
rule '
|
30
|
-
rule '
|
31
|
-
rule 'member' =>
|
32
|
-
rule 'array' =>
|
33
|
-
rule '
|
34
|
-
rule '
|
35
|
-
rule 'array-items' => %w[value]
|
30
|
+
rule 'member_list' => 'member_list value-separator member'
|
31
|
+
rule 'member_list' => 'member'
|
32
|
+
rule 'member' => 'string name-separator value'
|
33
|
+
rule 'array' => 'begin-array array_items? end-array'
|
34
|
+
rule 'array_items' => 'array_items value-separator value'
|
35
|
+
rule 'array_items' => 'value'
|
36
36
|
end
|
37
37
|
|
38
38
|
# And now build the JSON grammar...
|
@@ -5,18 +5,19 @@ require 'rley' # Load the gem
|
|
5
5
|
|
6
6
|
########################################
|
7
7
|
# Define a grammar for basic arithmetical expressions
|
8
|
-
builder = Rley::
|
8
|
+
builder = Rley::grammar_builder do
|
9
9
|
add_terminals('NUMBER')
|
10
10
|
add_terminals('LPAREN', 'RPAREN') # For '(', ')' delimiters
|
11
11
|
add_terminals('PLUS', 'MINUS') # For '+', '-' operators
|
12
12
|
add_terminals('STAR', 'DIVIDE') # For '*', '/' operators
|
13
|
+
|
13
14
|
rule 'expression' => 'simple_expression'
|
14
15
|
rule 'simple_expression' => 'term'
|
15
|
-
rule 'simple_expression' =>
|
16
|
+
rule 'simple_expression' => 'simple_expression add_operator term'
|
16
17
|
rule 'term' => 'factor'
|
17
|
-
rule 'term' =>
|
18
|
+
rule 'term' => 'term mul_operator factor'
|
18
19
|
rule 'factor' => 'NUMBER'
|
19
|
-
rule 'factor' =>
|
20
|
+
rule 'factor' => 'LPAREN expression RPAREN'
|
20
21
|
rule 'add_operator' => 'PLUS'
|
21
22
|
rule 'add_operator' => 'MINUS'
|
22
23
|
rule 'mul_operator' => 'STAR'
|
@@ -5,7 +5,7 @@ require 'rley' # Load the gem
|
|
5
5
|
|
6
6
|
########################################
|
7
7
|
# Define a grammar for basic arithmetical expressions
|
8
|
-
builder = Rley::
|
8
|
+
builder = Rley::grammar_builder do
|
9
9
|
add_terminals('NUMBER')
|
10
10
|
add_terminals('PLUS', 'MINUS') # For '+', '-' operators or sign
|
11
11
|
add_terminals('STAR', 'DIVIDE', 'POWER') # For '*', '/', '**' operators
|
@@ -13,16 +13,16 @@ builder = Rley::Syntax::GrammarBuilder.new do
|
|
13
13
|
add_terminals('PI', 'E') # For numeric constants
|
14
14
|
add_terminals('RESERVED') # Reserved identifiers
|
15
15
|
|
16
|
-
rule 'expression' =>
|
16
|
+
rule 'expression' => 'simple_expression'
|
17
17
|
rule 'simple_expression' => 'term'
|
18
|
-
rule 'simple_expression' =>
|
18
|
+
rule 'simple_expression' => 'simple_expression add_operator term'
|
19
19
|
rule 'term' => 'factor'
|
20
|
-
rule 'term' =>
|
20
|
+
rule 'term' => 'term mul_operator factor'
|
21
21
|
rule 'factor' => 'simple_factor'
|
22
|
-
rule 'factor' =>
|
23
|
-
rule 'simple_factor' =>
|
24
|
-
rule 'simple_factor' =>
|
25
|
-
rule 'simple_factor' =>
|
22
|
+
rule 'factor' => 'factor POWER simple_factor'
|
23
|
+
rule 'simple_factor' => 'sign scalar'
|
24
|
+
rule 'simple_factor' => 'unary_function in_parenthesis'
|
25
|
+
rule 'simple_factor' => 'MINUS in_parenthesis'
|
26
26
|
rule 'simple_factor' => 'in_parenthesis'
|
27
27
|
rule 'sign' => 'PLUS'
|
28
28
|
rule 'sign' => 'MINUS'
|
@@ -31,7 +31,7 @@ builder = Rley::Syntax::GrammarBuilder.new do
|
|
31
31
|
rule 'scalar' => 'PI'
|
32
32
|
rule 'scalar' => 'E'
|
33
33
|
rule 'unary_function' => 'RESERVED'
|
34
|
-
rule 'in_parenthesis' =>
|
34
|
+
rule 'in_parenthesis' => 'LPAREN expression RPAREN'
|
35
35
|
rule 'add_operator' => 'PLUS'
|
36
36
|
rule 'add_operator' => 'MINUS'
|
37
37
|
rule 'mul_operator' => 'STAR'
|