rley 0.7.08 → 0.8.00

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +6 -0
  3. data/README.md +4 -5
  4. data/examples/NLP/nano_eng/nano_en_demo.rb +7 -11
  5. data/examples/NLP/nano_eng/nano_grammar.rb +18 -18
  6. data/examples/NLP/pico_en_demo.rb +2 -2
  7. data/examples/data_formats/JSON/json_ast_builder.rb +9 -18
  8. data/examples/data_formats/JSON/json_demo.rb +1 -2
  9. data/examples/data_formats/JSON/json_grammar.rb +11 -11
  10. data/examples/general/calc_iter1/calc_grammar.rb +5 -4
  11. data/examples/general/calc_iter2/calc_grammar.rb +9 -9
  12. data/examples/general/left.rb +1 -1
  13. data/examples/general/right.rb +1 -1
  14. data/lib/rley.rb +1 -1
  15. data/lib/rley/base/dotted_item.rb +5 -0
  16. data/lib/rley/base/grm_items_builder.rb +6 -0
  17. data/lib/rley/constants.rb +1 -1
  18. data/lib/rley/engine.rb +2 -2
  19. data/lib/rley/interface.rb +16 -0
  20. data/lib/rley/notation/all_notation_nodes.rb +2 -0
  21. data/lib/rley/notation/ast_builder.rb +191 -0
  22. data/lib/rley/notation/ast_node.rb +44 -0
  23. data/lib/rley/notation/ast_visitor.rb +113 -0
  24. data/lib/rley/notation/grammar.rb +49 -0
  25. data/lib/rley/notation/grammar_builder.rb +451 -0
  26. data/lib/rley/notation/grouping_node.rb +23 -0
  27. data/lib/rley/notation/parser.rb +56 -0
  28. data/lib/rley/notation/sequence_node.rb +35 -0
  29. data/lib/rley/notation/symbol_node.rb +29 -0
  30. data/lib/rley/notation/tokenizer.rb +192 -0
  31. data/lib/rley/parse_rep/ast_base_builder.rb +13 -0
  32. data/lib/rley/parser/gfg_chart.rb +100 -6
  33. data/lib/rley/parser/gfg_parsing.rb +5 -3
  34. data/lib/rley/parser/parse_entry_set.rb +1 -1
  35. data/lib/rley/syntax/{grammar_builder.rb → base_grammar_builder.rb} +45 -15
  36. data/lib/rley/syntax/grm_symbol.rb +1 -1
  37. data/lib/rley/syntax/match_closest.rb +43 -0
  38. data/lib/rley/syntax/production.rb +6 -0
  39. data/spec/rley/engine_spec.rb +6 -6
  40. data/spec/rley/gfg/grm_flow_graph_spec.rb +2 -2
  41. data/spec/rley/notation/grammar_builder_spec.rb +295 -0
  42. data/spec/rley/notation/parser_spec.rb +184 -0
  43. data/spec/rley/notation/tokenizer_spec.rb +370 -0
  44. data/spec/rley/parse_rep/ast_builder_spec.rb +0 -1
  45. data/spec/rley/parse_rep/groucho_spec.rb +1 -1
  46. data/spec/rley/parse_rep/parse_forest_builder_spec.rb +1 -1
  47. data/spec/rley/parse_rep/parse_forest_factory_spec.rb +2 -2
  48. data/spec/rley/parse_rep/parse_tree_factory_spec.rb +1 -1
  49. data/spec/rley/parser/dangling_else_spec.rb +445 -0
  50. data/spec/rley/parser/gfg_earley_parser_spec.rb +95 -9
  51. data/spec/rley/parser/gfg_parsing_spec.rb +1 -1
  52. data/spec/rley/parser/parse_walker_factory_spec.rb +2 -2
  53. data/spec/rley/support/ambiguous_grammar_helper.rb +2 -2
  54. data/spec/rley/support/grammar_abc_helper.rb +2 -2
  55. data/spec/rley/support/grammar_ambig01_helper.rb +2 -2
  56. data/spec/rley/support/grammar_arr_int_helper.rb +2 -2
  57. data/spec/rley/support/grammar_b_expr_helper.rb +2 -2
  58. data/spec/rley/support/grammar_int_seq_helper.rb +51 -0
  59. data/spec/rley/support/grammar_l0_helper.rb +2 -2
  60. data/spec/rley/support/grammar_pb_helper.rb +2 -2
  61. data/spec/rley/support/grammar_sppf_helper.rb +2 -2
  62. data/spec/rley/syntax/{grammar_builder_spec.rb → base_grammar_builder_spec.rb} +30 -11
  63. data/spec/rley/syntax/match_closest_spec.rb +46 -0
  64. data/spec/rley/syntax/production_spec.rb +4 -0
  65. metadata +29 -14
  66. data/lib/rley/parser/parse_state.rb +0 -78
  67. data/lib/rley/parser/parse_state_tracker.rb +0 -59
  68. data/lib/rley/parser/state_set.rb +0 -100
  69. data/spec/rley/parser/parse_state_spec.rb +0 -125
  70. data/spec/rley/parser/parse_tracer_spec.rb +0 -200
  71. 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: 7377b66e5bac6ee813506829f53a32d694e9a01eaee5f55deec4475a710305d7
4
- data.tar.gz: 7f972ad8f29be409a12cb61ae095177d8f9f2594fe9b7eb0ef1a931de0729c9c
3
+ metadata.gz: ee99e1b9803a447d18a3f82e91353659e211109a5fb99d48a834ddebf572998f
4
+ data.tar.gz: f4719d9fa301a0de2be143ca0aa5d0c261cf1562194a40b038c79b1faa1c04dc
5
5
  SHA512:
6
- metadata.gz: d9e888ca1df16c7421df180c551879e6d066f9f848db2135be637951c7edfe928d693be8b1e22753c10b546198351650cc6dfd1acf14837597979f2e0a589715
7
- data.tar.gz: 4ea2a4c50e66321a00d828264052df58e83beb69ccc13fe9cda7dd9b648eee58f4332510867fef291006e2378f2c25ce85fff95ac11a7ed367578cee567b3b34
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' => %w[NP VP]
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' => %w[Determiner Nominal]
33
- rule 'Nominal' => %w[Noun]
34
- rule 'Nominal' => %w[Nominal Noun]
35
- rule 'VP' => 'Verb'
36
- rule 'VP' => %w[Verb NP]
37
- rule 'VP' => %w[Verb NP PP]
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::Syntax::GrammarBuilder.new do
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' => %w[NP VP]
28
+ rule 'declarative' => 'NP VP'
29
29
  rule 'imperative' => 'VP'
30
- rule 'yes_no_question' => %w[Aux NP VP]
31
- rule 'wh_subject_question' => %w[Wh_NP NP VP]
32
- rule 'wh_non_subject_question' => %w[Wh_NP Aux NP VP]
33
- rule 'NP' => %w[Predeterminer 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' => %w[Det Card Ord Quant Nominal]
36
+ rule 'NP' => 'Det Card Ord Quant Nominal'
37
37
  rule 'VP' => 'Verb'
38
- rule 'VP' => %w[Verb NP]
39
- rule 'VP' => %w[Verb NP PP]
40
- rule 'VP' => %w[Verb PP]
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' => %w[Nominal Noun]
49
- rule 'Nominal' => %w[Nominal GerundVP]
50
- rule 'Nominal' => %w[Nominal RelClause]
51
- rule 'PP' => %w[Preposition NP]
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' => %w[GerundV NP]
54
- rule 'GerundVP' => %w[GerundV NP PP]
55
- rule 'GerundVP' => %w[GerundV PP]
56
- rule 'RelClause' => %w[Relative_pronoun VP]
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' => %w[begin-object member-list end-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 'object' => %w[begin-object end-object]
47
- def reduce_object_1(aProduction, _range, _tokens, _children)
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' => %w[string name-separator value]
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' => %w[begin-array array-items end-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' => %w[begin-array end-array]
78
- def reduce_array_1(_range, _tokens, _children)
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' => %w[value]
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 necessary, select AST representation
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::Syntax::GrammarBuilder.new do
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' => %w[begin-object member-list end-object]
27
- rule 'object' => %w[begin-object end-object]
27
+ rule 'object' => 'begin-object member_list? end-object'
28
+
28
29
  # Next rule is an example of a left recursive rule
29
- rule 'member-list' => %w[member-list value-separator member]
30
- rule 'member-list' => 'member'
31
- rule 'member' => %w[string name-separator value]
32
- rule 'array' => %w[begin-array array-items end-array]
33
- rule 'array' => %w[begin-array end-array]
34
- rule 'array-items' => %w[array-items value-separator value]
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::Syntax::GrammarBuilder.new do
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' => %w[simple_expression add_operator term]
16
+ rule 'simple_expression' => 'simple_expression add_operator term'
16
17
  rule 'term' => 'factor'
17
- rule 'term' => %w[term mul_operator factor]
18
+ rule 'term' => 'term mul_operator factor'
18
19
  rule 'factor' => 'NUMBER'
19
- rule 'factor' => %w[LPAREN expression RPAREN]
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::Syntax::GrammarBuilder.new do
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' => %w[simple_expression]
16
+ rule 'expression' => 'simple_expression'
17
17
  rule 'simple_expression' => 'term'
18
- rule 'simple_expression' => %w[simple_expression add_operator term]
18
+ rule 'simple_expression' => 'simple_expression add_operator term'
19
19
  rule 'term' => 'factor'
20
- rule 'term' => %w[term mul_operator factor]
20
+ rule 'term' => 'term mul_operator factor'
21
21
  rule 'factor' => 'simple_factor'
22
- rule 'factor' => %w[factor POWER simple_factor]
23
- rule 'simple_factor' => %w[sign scalar]
24
- rule 'simple_factor' => %w[unary_function in_parenthesis]
25
- rule 'simple_factor' => %w[MINUS in_parenthesis]
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' => %w[LPAREN expression RPAREN]
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'
@@ -11,7 +11,7 @@ builder = Rley::Syntax::GrammarBuilder.new do
11
11
 
12
12
  # Grammar with left recursive rule.
13
13
  rule 'l_dots' => []
14
- rule 'l_dots' => %w[l_dots DOT]
14
+ rule 'l_dots' => 'l_dots DOT'
15
15
  end
16
16
 
17
17
  # And now, let's build the grammar...
@@ -11,7 +11,7 @@ builder = Rley::Syntax::GrammarBuilder.new do
11
11
 
12
12
  # Grammar with right recursive rule.
13
13
  rule 'r_dots' => []
14
- rule 'r_dots' => %w[DOT r_dots]
14
+ rule 'r_dots' => 'DOT r_dots'
15
15
  end
16
16
 
17
17
  # And now, let's build the grammar...
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/syntax/grammar_builder'
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
@@ -5,7 +5,7 @@
5
5
 
6
6
  module Rley # Module used as a namespace
7
7
  # The version number of the gem.
8
- Version = '0.7.08'
8
+ Version = '0.8.00'
9
9
 
10
10
  # Brief description of the gem.
11
11
  Description = "Ruby implementation of the Earley's parsing algorithm"
data/lib/rley/engine.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative './syntax/grammar_builder'
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::Syntax::GrammarBuilder.new(&aBlock)
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