rley 0.7.08 → 0.8.00
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- 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/NLP/pico_en_demo.rb +2 -2
- 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.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 +2 -0
- data/lib/rley/notation/ast_builder.rb +191 -0
- data/lib/rley/notation/ast_node.rb +44 -0
- data/lib/rley/notation/ast_visitor.rb +113 -0
- data/lib/rley/notation/grammar.rb +49 -0
- data/lib/rley/notation/grammar_builder.rb +451 -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 +192 -0
- data/lib/rley/parse_rep/ast_base_builder.rb +13 -0
- data/lib/rley/parser/gfg_chart.rb +100 -6
- 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} +45 -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/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 +295 -0
- data/spec/rley/notation/parser_spec.rb +184 -0
- data/spec/rley/notation/tokenizer_spec.rb +370 -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 +445 -0
- data/spec/rley/parser/gfg_earley_parser_spec.rb +95 -9
- data/spec/rley/parser/gfg_parsing_spec.rb +1 -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} +30 -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: ee99e1b9803a447d18a3f82e91353659e211109a5fb99d48a834ddebf572998f
|
4
|
+
data.tar.gz: f4719d9fa301a0de2be143ca0aa5d0c261cf1562194a40b038c79b1faa1c04dc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9810a2d3884f86d2d437e1ba0911681f53895ff60ef500e4d19bd5d9429ff433f46c1dd57d6e185eb0bce3fcf869b1e4884acd09aaff72aee0323d00a781e763
|
7
|
+
data.tar.gz: e7d49393824bbbeaff70bc7d57137d1fe82f86a98bfda52fc9144dbafa3e5568e51ce7e9da8c15fc2f51438b29100801e8ea145f613165ba4369093f07bfad23
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
### 0.8.00 / 2021-08-15
|
2
|
+
- New grammar builder that accepts ? * + modifiers
|
3
|
+
|
4
|
+
### 0.7.08 / 2021-05-30
|
5
|
+
- Code restyling to please rubocop 1.15.0
|
6
|
+
|
1
7
|
### 0.7.07 / 2020-11-16
|
2
8
|
- Code restyling to please rubocop 0.93.1
|
3
9
|
* [CHANGE] File `.travis.yml`: updated Ruby versions, drop support for Ruby 2.3.x
|
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...
|
@@ -26,8 +26,8 @@ engine.build_grammar do
|
|
26
26
|
rule 'S' => 'NP VP'
|
27
27
|
rule 'NP' => 'Proper-Noun'
|
28
28
|
rule 'NP' => 'Determiner Noun'
|
29
|
-
rule 'NP' => 'Determiner Noun PP'
|
30
|
-
rule 'VP' => 'Verb NP'
|
29
|
+
rule 'NP' => 'Determiner Noun PP'
|
30
|
+
rule 'VP' => 'Verb NP'
|
31
31
|
rule 'VP' => 'Verb NP PP'
|
32
32
|
rule 'PP' => 'Preposition NP'
|
33
33
|
end
|
@@ -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'
|
data/examples/general/left.rb
CHANGED
data/examples/general/right.rb
CHANGED
data/lib/rley.rb
CHANGED
@@ -5,7 +5,7 @@
|
|
5
5
|
# for a Rley client.
|
6
6
|
|
7
7
|
require_relative './rley/constants'
|
8
|
-
require_relative './rley/
|
8
|
+
require_relative './rley/interface'
|
9
9
|
require_relative './rley/lexical/token'
|
10
10
|
require_relative './rley/parser/gfg_earley_parser'
|
11
11
|
require_relative './rley/parse_rep/ast_base_builder'
|
@@ -28,6 +28,11 @@ module Rley # This module is used as a namespace
|
|
28
28
|
# @return [Integer]
|
29
29
|
attr_reader :position
|
30
30
|
|
31
|
+
# A possible constraint between symbol on left of dot and
|
32
|
+
# the closest preceding given terminal
|
33
|
+
# @return [NilClass, Syntax::MatchClosest]
|
34
|
+
attr_accessor :constraint
|
35
|
+
|
31
36
|
# @param aProduction [Syntax::Production]
|
32
37
|
# @param aPosition [Integer] Position of the dot in rhs of production.
|
33
38
|
def initialize(aProduction, aPosition)
|
@@ -12,12 +12,18 @@ module Rley # This module is used as a namespace
|
|
12
12
|
def build_dotted_items(aGrammar)
|
13
13
|
items = []
|
14
14
|
aGrammar.rules.each do |prod|
|
15
|
+
index_prev = items.size
|
15
16
|
rhs_size = prod.rhs.size
|
16
17
|
if rhs_size.zero?
|
17
18
|
items << DottedItem.new(prod, 0)
|
18
19
|
else
|
19
20
|
items += (0..rhs_size).map { |i| DottedItem.new(prod, i) }
|
20
21
|
end
|
22
|
+
|
23
|
+
prod.constraints.each do |cs|
|
24
|
+
# Attach constraint to dotted item n + 1
|
25
|
+
items[index_prev + cs.idx_symbol + 1].constraint = cs
|
26
|
+
end
|
21
27
|
end
|
22
28
|
|
23
29
|
return items
|
data/lib/rley/constants.rb
CHANGED
data/lib/rley/engine.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_relative './
|
3
|
+
require_relative './notation/grammar_builder'
|
4
4
|
require_relative './parser/gfg_earley_parser'
|
5
5
|
require_relative './parse_tree_visitor'
|
6
6
|
require_relative './parse_forest_visitor'
|
@@ -61,7 +61,7 @@ module Rley # This module is used as a namespace
|
|
61
61
|
# add_production('elements' => 'INTEGER')
|
62
62
|
# end
|
63
63
|
def build_grammar(&aBlock)
|
64
|
-
builder = Rley::
|
64
|
+
builder = Rley::Notation::GrammarBuilder.new(&aBlock)
|
65
65
|
@grammar = builder.grammar
|
66
66
|
end
|
67
67
|
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative './notation/grammar_builder'
|
4
|
+
|
5
|
+
module Rley # Module used as a namespace
|
6
|
+
# Factory method.
|
7
|
+
# A grammar builder constructs a Rley grammar piece by piece
|
8
|
+
# from DSL instructions in a provided code block.
|
9
|
+
# @param aBlock [Proc] a code block
|
10
|
+
# @return [Rley::Notation::GrammarBuilder] An object that builds a grammar.
|
11
|
+
def self.grammar_builder(&aBlock)
|
12
|
+
Rley::Notation::GrammarBuilder.new(&aBlock)
|
13
|
+
end
|
14
|
+
end # module
|
15
|
+
|
16
|
+
# End of file
|