rley 0.7.07 → 0.7.08
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +348 -54
- data/LICENSE.txt +1 -1
- data/README.md +3 -2
- data/examples/NLP/engtagger.rb +193 -190
- data/examples/NLP/nano_eng/nano_grammar.rb +5 -5
- data/examples/data_formats/JSON/cli_options.rb +1 -1
- data/examples/data_formats/JSON/json_ast_builder.rb +12 -9
- data/examples/data_formats/JSON/json_ast_nodes.rb +12 -21
- data/examples/data_formats/JSON/json_grammar.rb +2 -2
- data/examples/data_formats/JSON/json_lexer.rb +8 -8
- data/examples/data_formats/JSON/json_minifier.rb +1 -1
- data/examples/general/calc_iter1/calc_ast_builder.rb +13 -10
- data/examples/general/calc_iter1/calc_ast_nodes.rb +23 -37
- data/examples/general/calc_iter1/calc_grammar.rb +2 -2
- data/examples/general/calc_iter1/calc_lexer.rb +6 -4
- data/examples/general/calc_iter1/spec/calculator_spec.rb +5 -5
- data/examples/general/calc_iter2/calc_ast_builder.rb +5 -3
- data/examples/general/calc_iter2/calc_ast_nodes.rb +27 -43
- data/examples/general/calc_iter2/calc_grammar.rb +3 -3
- data/examples/general/calc_iter2/calc_lexer.rb +11 -10
- data/examples/general/calc_iter2/spec/calculator_spec.rb +26 -26
- data/examples/general/left.rb +2 -2
- data/examples/general/right.rb +2 -2
- data/lib/rley/base/dotted_item.rb +23 -31
- data/lib/rley/constants.rb +2 -2
- data/lib/rley/engine.rb +20 -23
- data/lib/rley/formatter/asciitree.rb +3 -3
- data/lib/rley/formatter/bracket_notation.rb +1 -8
- data/lib/rley/formatter/debug.rb +6 -6
- data/lib/rley/formatter/json.rb +2 -2
- data/lib/rley/gfg/call_edge.rb +1 -1
- data/lib/rley/gfg/edge.rb +5 -5
- data/lib/rley/gfg/end_vertex.rb +2 -6
- data/lib/rley/gfg/epsilon_edge.rb +1 -5
- data/lib/rley/gfg/grm_flow_graph.rb +27 -23
- data/lib/rley/gfg/item_vertex.rb +10 -10
- data/lib/rley/gfg/non_terminal_vertex.rb +4 -4
- data/lib/rley/gfg/scan_edge.rb +1 -1
- data/lib/rley/gfg/shortcut_edge.rb +2 -2
- data/lib/rley/gfg/start_vertex.rb +4 -8
- data/lib/rley/gfg/vertex.rb +43 -39
- data/lib/rley/lexical/token_range.rb +6 -6
- data/lib/rley/parse_forest_visitor.rb +5 -5
- data/lib/rley/parse_rep/ast_base_builder.rb +9 -11
- data/lib/rley/parse_rep/cst_builder.rb +5 -6
- data/lib/rley/parse_rep/parse_forest_builder.rb +20 -18
- data/lib/rley/parse_rep/parse_forest_factory.rb +3 -3
- data/lib/rley/parse_rep/parse_rep_creator.rb +11 -13
- data/lib/rley/parse_rep/parse_tree_builder.rb +4 -4
- data/lib/rley/parse_rep/parse_tree_factory.rb +27 -27
- data/lib/rley/parse_tree_visitor.rb +1 -1
- data/lib/rley/parser/error_reason.rb +4 -5
- data/lib/rley/parser/gfg_chart.rb +20 -22
- data/lib/rley/parser/gfg_parsing.rb +16 -30
- data/lib/rley/parser/parse_entry.rb +25 -31
- data/lib/rley/parser/parse_entry_set.rb +18 -15
- data/lib/rley/parser/parse_entry_tracker.rb +4 -4
- data/lib/rley/parser/parse_state.rb +16 -21
- data/lib/rley/parser/parse_state_tracker.rb +4 -4
- data/lib/rley/parser/parse_tracer.rb +13 -13
- data/lib/rley/parser/parse_walker_factory.rb +23 -28
- data/lib/rley/parser/state_set.rb +9 -10
- data/lib/rley/ptree/non_terminal_node.rb +7 -5
- data/lib/rley/ptree/parse_tree.rb +3 -3
- data/lib/rley/ptree/parse_tree_node.rb +5 -5
- data/lib/rley/ptree/terminal_node.rb +7 -7
- data/lib/rley/rley_error.rb +12 -12
- data/lib/rley/sppf/alternative_node.rb +6 -6
- data/lib/rley/sppf/composite_node.rb +7 -7
- data/lib/rley/sppf/epsilon_node.rb +3 -3
- data/lib/rley/sppf/leaf_node.rb +3 -3
- data/lib/rley/sppf/parse_forest.rb +16 -16
- data/lib/rley/sppf/sppf_node.rb +7 -8
- data/lib/rley/sppf/token_node.rb +3 -3
- data/lib/rley/syntax/grammar.rb +5 -5
- data/lib/rley/syntax/grammar_builder.rb +9 -9
- data/lib/rley/syntax/grm_symbol.rb +6 -6
- data/lib/rley/syntax/non_terminal.rb +9 -15
- data/lib/rley/syntax/production.rb +10 -10
- data/lib/rley/syntax/symbol_seq.rb +7 -9
- data/lib/rley/syntax/terminal.rb +4 -5
- data/lib/rley/syntax/verbatim_symbol.rb +3 -3
- data/lib/support/base_tokenizer.rb +19 -18
- data/spec/rley/base/dotted_item_spec.rb +2 -2
- data/spec/rley/engine_spec.rb +17 -15
- data/spec/rley/formatter/asciitree_spec.rb +7 -7
- data/spec/rley/formatter/bracket_notation_spec.rb +13 -13
- data/spec/rley/formatter/json_spec.rb +1 -1
- data/spec/rley/gfg/end_vertex_spec.rb +5 -5
- data/spec/rley/gfg/item_vertex_spec.rb +10 -10
- data/spec/rley/gfg/non_terminal_vertex_spec.rb +3 -3
- data/spec/rley/gfg/shortcut_edge_spec.rb +1 -1
- data/spec/rley/gfg/start_vertex_spec.rb +5 -5
- data/spec/rley/gfg/vertex_spec.rb +3 -3
- data/spec/rley/lexical/token_range_spec.rb +16 -16
- data/spec/rley/lexical/token_spec.rb +2 -2
- data/spec/rley/parse_forest_visitor_spec.rb +165 -163
- data/spec/rley/parse_rep/ambiguous_parse_spec.rb +44 -44
- data/spec/rley/parse_rep/ast_builder_spec.rb +6 -6
- data/spec/rley/parse_rep/cst_builder_spec.rb +5 -5
- data/spec/rley/parse_rep/groucho_spec.rb +21 -21
- data/spec/rley/parse_rep/parse_forest_builder_spec.rb +26 -26
- data/spec/rley/parse_rep/parse_forest_factory_spec.rb +6 -6
- data/spec/rley/parse_rep/parse_tree_factory_spec.rb +2 -2
- data/spec/rley/parse_tree_visitor_spec.rb +10 -8
- data/spec/rley/parser/error_reason_spec.rb +6 -6
- data/spec/rley/parser/gfg_earley_parser_spec.rb +4 -2
- data/spec/rley/parser/gfg_parsing_spec.rb +4 -8
- data/spec/rley/parser/parse_entry_spec.rb +19 -19
- data/spec/rley/parser/parse_state_spec.rb +5 -5
- data/spec/rley/parser/parse_walker_factory_spec.rb +1 -1
- data/spec/rley/parser/state_set_spec.rb +22 -22
- data/spec/rley/ptree/non_terminal_node_spec.rb +5 -3
- data/spec/rley/ptree/parse_tree_node_spec.rb +4 -4
- data/spec/rley/ptree/terminal_node_spec.rb +6 -6
- data/spec/rley/sppf/alternative_node_spec.rb +6 -6
- data/spec/rley/sppf/non_terminal_node_spec.rb +3 -3
- data/spec/rley/sppf/token_node_spec.rb +4 -4
- data/spec/rley/support/ambiguous_grammar_helper.rb +3 -4
- data/spec/rley/support/grammar_abc_helper.rb +2 -4
- data/spec/rley/support/grammar_ambig01_helper.rb +4 -5
- data/spec/rley/support/grammar_arr_int_helper.rb +4 -5
- data/spec/rley/support/grammar_b_expr_helper.rb +4 -5
- data/spec/rley/support/grammar_l0_helper.rb +10 -11
- data/spec/rley/support/grammar_pb_helper.rb +6 -5
- data/spec/rley/support/grammar_sppf_helper.rb +1 -1
- data/spec/rley/syntax/grammar_builder_spec.rb +5 -5
- data/spec/rley/syntax/grammar_spec.rb +6 -6
- data/spec/rley/syntax/grm_symbol_spec.rb +1 -1
- data/spec/rley/syntax/non_terminal_spec.rb +8 -8
- data/spec/rley/syntax/production_spec.rb +13 -13
- data/spec/rley/syntax/symbol_seq_spec.rb +2 -2
- data/spec/rley/syntax/terminal_spec.rb +5 -5
- data/spec/rley/syntax/verbatim_symbol_spec.rb +1 -1
- data/spec/spec_helper.rb +0 -12
- data/spec/support/base_tokenizer_spec.rb +7 -2
- metadata +21 -62
- data/.simplecov +0 -8
@@ -11,7 +11,7 @@ module Rley # This module is used as a namespace
|
|
11
11
|
# The set of parse states
|
12
12
|
attr_reader(:states)
|
13
13
|
|
14
|
-
def initialize
|
14
|
+
def initialize
|
15
15
|
@states = []
|
16
16
|
end
|
17
17
|
|
@@ -34,7 +34,7 @@ module Rley # This module is used as a namespace
|
|
34
34
|
# @param aSymbol [GrmSymbol] the expected symbol
|
35
35
|
# (=on the right of the dot)
|
36
36
|
def states_expecting(aSymbol)
|
37
|
-
|
37
|
+
states.select { |s| s.dotted_rule.next_symbol == aSymbol }
|
38
38
|
end
|
39
39
|
|
40
40
|
# The list of complete ParseState that have the given non-terminal
|
@@ -47,7 +47,7 @@ module Rley # This module is used as a namespace
|
|
47
47
|
|
48
48
|
# The list of ParseState that involve the given production
|
49
49
|
def states_for(aProduction)
|
50
|
-
|
50
|
+
states.select { |s| s.dotted_rule.production == aProduction }
|
51
51
|
end
|
52
52
|
|
53
53
|
# Retrieve the parse state that is the predecessor of the given one.
|
@@ -55,23 +55,22 @@ module Rley # This module is used as a namespace
|
|
55
55
|
dotted_rule = aPState.dotted_rule
|
56
56
|
raise StandardError, aPState.to_s unless dotted_rule.prev_position
|
57
57
|
|
58
|
-
|
59
|
-
return candidate
|
58
|
+
states.find { |s| s.precedes?(aPState) }
|
60
59
|
end
|
61
60
|
|
62
61
|
# The list of distinct expected terminal symbols. An expected symbol
|
63
62
|
# is on the left of a dot in a parse state of the parse set.
|
64
|
-
def expected_terminals
|
63
|
+
def expected_terminals
|
65
64
|
expecting_terminals = states.select do |s|
|
66
65
|
s.dotted_rule.next_symbol.kind_of?(Rley::Syntax::Terminal)
|
67
66
|
end
|
68
67
|
|
69
68
|
terminals = expecting_terminals.map { |s| s.dotted_rule.next_symbol }
|
70
|
-
|
69
|
+
terminals.uniq
|
71
70
|
end
|
72
71
|
|
73
72
|
# Return an Array of Arrays of ambiguous parse states.
|
74
|
-
def ambiguities
|
73
|
+
def ambiguities
|
75
74
|
complete_states = states.select(&:complete?)
|
76
75
|
return [] if complete_states.size <= 1
|
77
76
|
|
@@ -86,14 +85,14 @@ module Rley # This module is used as a namespace
|
|
86
85
|
ambiguous_groups << a_group if a_group.size > 1
|
87
86
|
end
|
88
87
|
|
89
|
-
|
88
|
+
ambiguous_groups
|
90
89
|
end
|
91
90
|
|
92
91
|
private
|
93
92
|
|
94
93
|
def include?(aState)
|
95
94
|
# TODO: make it better than linear search
|
96
|
-
|
95
|
+
states.include?(aState)
|
97
96
|
end
|
98
97
|
end # class
|
99
98
|
end # module
|
@@ -18,20 +18,22 @@ module Rley # This module is used as a namespace
|
|
18
18
|
def add_subnode(aSubnode)
|
19
19
|
subnodes.unshift(aSubnode)
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
# Emit a (formatted) string representation of the node.
|
23
23
|
# Mainly used for diagnosis/debugging purposes.
|
24
|
+
# rubocop: disable Style/StringConcatenation
|
24
25
|
def to_string(indentation)
|
25
26
|
connector = '+- '
|
26
27
|
selfie = super(indentation)
|
27
28
|
prefix = "\n" + (' ' * connector.size * indentation) + connector
|
28
29
|
subnodes_repr = subnodes.reduce(+'') do |sub_result, subnode|
|
29
|
-
sub_result << prefix + subnode.to_string(indentation + 1)
|
30
|
+
sub_result << prefix + subnode.to_string(indentation + 1)
|
30
31
|
end
|
31
|
-
|
32
|
-
|
32
|
+
|
33
|
+
selfie + subnodes_repr
|
33
34
|
end
|
34
|
-
|
35
|
+
# rubocop: enable Style/StringConcatenation
|
36
|
+
|
35
37
|
# Part of the 'visitee' role in Visitor design pattern.
|
36
38
|
# @param aVisitor[ParseTreeVisitor] the visitor
|
37
39
|
def accept(aVisitor)
|
@@ -6,12 +6,12 @@ require_relative 'non_terminal_node'
|
|
6
6
|
module Rley # This module is used as a namespace
|
7
7
|
module PTree # This module is used as a namespace
|
8
8
|
# A parse tree (a.k.a. concrete syntax tree) is a tree-based representation
|
9
|
-
# for the parse that corresponds to the input text. In a parse tree,
|
9
|
+
# for the parse that corresponds to the input text. In a parse tree,
|
10
10
|
# a node corresponds to a grammar symbol used during the parsing:
|
11
11
|
# - a leaf node maps to a terminal symbol occurring in
|
12
12
|
# the input, and
|
13
13
|
# - a intermediate node maps to a non-terminal node reduced
|
14
|
-
# during the parse.
|
14
|
+
# during the parse.
|
15
15
|
# The root node corresponds to the main/start symbol of the grammar.
|
16
16
|
class ParseTree
|
17
17
|
# @return [ParseTreeNode] The root node of the tree.
|
@@ -21,7 +21,7 @@ module Rley # This module is used as a namespace
|
|
21
21
|
def initialize(theRootNode)
|
22
22
|
@root = theRootNode
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
25
|
# Notification from the builder telling that the parse tree construction
|
26
26
|
# is over. This method can be overriden.
|
27
27
|
def done!
|
@@ -15,9 +15,9 @@ module Rley # This module is used as a namespace
|
|
15
15
|
@symbol = aSymbol
|
16
16
|
@range = Lexical::TokenRange.new(aRange)
|
17
17
|
end
|
18
|
-
|
18
|
+
|
19
19
|
# Notify the builder that the construction is over
|
20
|
-
def done!
|
20
|
+
def done!
|
21
21
|
# Do nothing
|
22
22
|
end
|
23
23
|
|
@@ -29,13 +29,13 @@ module Rley # This module is used as a namespace
|
|
29
29
|
# Emit a (formatted) string representation of the node.
|
30
30
|
# Mainly used for diagnosis/debugging purposes.
|
31
31
|
def to_string(indentation)
|
32
|
-
|
32
|
+
"#{symbol.name}#{range.to_string(indentation)}"
|
33
33
|
end
|
34
34
|
|
35
35
|
# Emit a short string representation of the node.
|
36
36
|
# Mainly used for diagnosis/debugging purposes.
|
37
|
-
def to_s
|
38
|
-
|
37
|
+
def to_s
|
38
|
+
"#{symbol.name}#{range.to_string(0)}"
|
39
39
|
end
|
40
40
|
end # class
|
41
41
|
end # module
|
@@ -9,10 +9,10 @@ module Rley # This module is used as a namespace
|
|
9
9
|
attr_reader(:token)
|
10
10
|
|
11
11
|
# @param aToken [Lexical::Token] Input Token object
|
12
|
-
# @param aPos [Integer] position of the token in the input stream.
|
12
|
+
# @param aPos [Integer] position of the token in the input stream.
|
13
13
|
def initialize(aToken, aPos)
|
14
|
-
# (major, minor) =
|
15
|
-
|
14
|
+
# (major, minor) =
|
15
|
+
|
16
16
|
# Use '1.class' trick to support both Integer and Fixnum classes
|
17
17
|
range = aPos.kind_of?(1.class) ? { low: aPos, high: aPos + 1 } : aPos
|
18
18
|
super(aToken.terminal, range)
|
@@ -24,18 +24,18 @@ module Rley # This module is used as a namespace
|
|
24
24
|
def to_string(indentation)
|
25
25
|
return super + ": '#{token.lexeme}'"
|
26
26
|
end
|
27
|
-
|
27
|
+
|
28
28
|
# Emit a short string representation of the node.
|
29
29
|
# Mainly used for diagnosis/debugging purposes.
|
30
|
-
def to_s
|
30
|
+
def to_s
|
31
31
|
return super + ": '#{token.lexeme}'"
|
32
|
-
end
|
32
|
+
end
|
33
33
|
|
34
34
|
# Part of the 'visitee' role in Visitor design pattern.
|
35
35
|
# @param aVisitor[ParseTreeVisitor] the visitor
|
36
36
|
def accept(aVisitor)
|
37
37
|
aVisitor.visit_terminal(self)
|
38
|
-
end
|
38
|
+
end
|
39
39
|
end # class
|
40
40
|
end # module
|
41
41
|
end # module
|
data/lib/rley/rley_error.rb
CHANGED
@@ -1,12 +1,12 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
# File: rley_error.rb
|
4
|
-
|
5
|
-
module Rley # Module used as a namespace
|
6
|
-
# @abstract
|
7
|
-
# Base class for any exception explicitly raised by Rley code.
|
8
|
-
class RleyError < StandardError
|
9
|
-
end # class
|
10
|
-
end # module
|
11
|
-
|
12
|
-
# End of file
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# File: rley_error.rb
|
4
|
+
|
5
|
+
module Rley # Module used as a namespace
|
6
|
+
# @abstract
|
7
|
+
# Base class for any exception explicitly raised by Rley code.
|
8
|
+
class RleyError < StandardError
|
9
|
+
end # class
|
10
|
+
end # module
|
11
|
+
|
12
|
+
# End of file
|
@@ -13,10 +13,10 @@ module Rley # This module is used as a namespace
|
|
13
13
|
# @return [Syntax::NonTerminal] Link to lhs symbol
|
14
14
|
attr_reader(:symbol)
|
15
15
|
|
16
|
-
# @param aVertex [GFG::ItemVertex]
|
17
|
-
# A GFG vertex that corresponds to a dotted item
|
16
|
+
# @param aVertex [GFG::ItemVertex]
|
17
|
+
# A GFG vertex that corresponds to a dotted item
|
18
18
|
# with the dot at the end) for the alternative under consideration.
|
19
|
-
# @param aRange [Lexical::TokenRange]
|
19
|
+
# @param aRange [Lexical::TokenRange]
|
20
20
|
# A range of token indices corresponding to this node.
|
21
21
|
def initialize(aVertex, aRange)
|
22
22
|
super(aRange)
|
@@ -28,14 +28,14 @@ module Rley # This module is used as a namespace
|
|
28
28
|
# Mainly used for diagnosis/debugging purposes.
|
29
29
|
# @return [String]
|
30
30
|
def to_string(indentation)
|
31
|
-
|
31
|
+
"Alt(#{label})#{range.to_string(indentation)}"
|
32
32
|
end
|
33
|
-
|
33
|
+
|
34
34
|
# Part of the 'visitee' role in Visitor design pattern.
|
35
35
|
# @param aVisitor[ParseTreeVisitor] the visitor
|
36
36
|
def accept(aVisitor)
|
37
37
|
aVisitor.visit_alternative(self)
|
38
|
-
end
|
38
|
+
end
|
39
39
|
end # class
|
40
40
|
end # module
|
41
41
|
end # module
|
@@ -9,11 +9,11 @@ module Rley # This module is used as a namespace
|
|
9
9
|
class CompositeNode < SPPFNode
|
10
10
|
# @return [Array<SPFFNode>] Sub-nodes (children).
|
11
11
|
attr_reader(:subnodes)
|
12
|
-
|
13
|
-
alias children subnodes
|
12
|
+
|
13
|
+
alias children subnodes
|
14
14
|
|
15
15
|
# Constructor
|
16
|
-
# @param aRange [Lexical::TokenRange]
|
16
|
+
# @param aRange [Lexical::TokenRange]
|
17
17
|
def initialize(aRange)
|
18
18
|
super(aRange)
|
19
19
|
@subnodes = []
|
@@ -24,14 +24,14 @@ module Rley # This module is used as a namespace
|
|
24
24
|
def add_subnode(aSubnode)
|
25
25
|
subnodes.unshift(aSubnode)
|
26
26
|
end
|
27
|
-
|
27
|
+
|
28
28
|
# @return [String] a text representation of the node.
|
29
|
-
def inspect
|
29
|
+
def inspect
|
30
30
|
key
|
31
|
-
end
|
31
|
+
end
|
32
32
|
|
33
33
|
# @return [String]
|
34
|
-
def key
|
34
|
+
def key
|
35
35
|
@key ||= to_string(0)
|
36
36
|
end
|
37
37
|
end # class
|
@@ -17,14 +17,14 @@ module Rley # This module is used as a namespace
|
|
17
17
|
# Mainly used for diagnosis/debugging purposes.
|
18
18
|
# @return [String]
|
19
19
|
def to_string(indentation)
|
20
|
-
|
20
|
+
"_#{range.to_string(indentation)}"
|
21
21
|
end
|
22
|
-
|
22
|
+
|
23
23
|
# Part of the 'visitee' role in Visitor design pattern.
|
24
24
|
# @param aVisitor[ParseTreeVisitor] the visitor
|
25
25
|
def accept(aVisitor)
|
26
26
|
aVisitor.visit_epsilon(self)
|
27
|
-
end
|
27
|
+
end
|
28
28
|
end # class
|
29
29
|
end # module
|
30
30
|
end # module
|
data/lib/rley/sppf/leaf_node.rb
CHANGED
@@ -8,14 +8,14 @@ module Rley # This module is used as a namespace
|
|
8
8
|
# child node.
|
9
9
|
class LeafNode < SPPFNode
|
10
10
|
# @return [String] a text representation of the node.
|
11
|
-
def inspect
|
11
|
+
def inspect
|
12
12
|
key
|
13
13
|
end
|
14
14
|
|
15
15
|
# @return [String]
|
16
|
-
def key
|
16
|
+
def key
|
17
17
|
@key ||= to_string(0)
|
18
|
-
end
|
18
|
+
end
|
19
19
|
end # class
|
20
20
|
end # module
|
21
21
|
end # module
|
@@ -7,19 +7,19 @@ require_relative 'alternative_node'
|
|
7
7
|
module Rley # This module is used as a namespace
|
8
8
|
module SPPF # This module is used as a namespace
|
9
9
|
# In an ambiguous grammar there are valid inputs that can result in multiple
|
10
|
-
# parse trees. A set of parse trees is commonly referred to as a parse
|
11
|
-
# forest. More specifically a parse forest is a graph data
|
10
|
+
# parse trees. A set of parse trees is commonly referred to as a parse
|
11
|
+
# forest. More specifically a parse forest is a graph data
|
12
12
|
# structure designed to represent a set of equally syntactically correct
|
13
13
|
# parse trees. Parse forests generated by Rley are so-called Shared Packed
|
14
|
-
# Parse Forests (SPPF). SPPFs allow very compact representation of parse
|
14
|
+
# Parse Forests (SPPF). SPPFs allow very compact representation of parse
|
15
15
|
# trees by sharing common sub-tree amongst the parse trees.
|
16
16
|
class ParseForest
|
17
17
|
# The root node of the forest
|
18
18
|
attr_reader(:root)
|
19
|
-
|
19
|
+
|
20
20
|
# A Hash with pairs of the kind node key => node
|
21
21
|
attr_reader(:key2node)
|
22
|
-
|
22
|
+
|
23
23
|
# A setter that tells that the parse is ambiguous.
|
24
24
|
attr_writer(:is_ambiguous)
|
25
25
|
|
@@ -30,34 +30,34 @@ module Rley # This module is used as a namespace
|
|
30
30
|
@key2node = {}
|
31
31
|
@is_ambiguous = false
|
32
32
|
end
|
33
|
-
|
33
|
+
|
34
34
|
# Notification that the SPPF construction is over
|
35
35
|
def done!
|
36
36
|
# Do nothing
|
37
37
|
end
|
38
|
-
|
38
|
+
|
39
39
|
# Returns true if the given node is present in the forest.
|
40
40
|
def include?(aNode)
|
41
|
-
|
41
|
+
key2node.include?(aNode)
|
42
42
|
end
|
43
|
-
|
43
|
+
|
44
44
|
# Returns true if the parse encountered a structural ambiguity
|
45
45
|
# (i.e. more than one parse tree for the given input)
|
46
|
-
def ambiguous?
|
47
|
-
|
46
|
+
def ambiguous?
|
47
|
+
@is_ambiguous
|
48
48
|
end
|
49
|
-
|
50
|
-
# Create an Enumerator that helps to iterate over the possible
|
51
|
-
# parse trees. That enumerator will generate a parse tree when
|
49
|
+
|
50
|
+
# Create an Enumerator that helps to iterate over the possible
|
51
|
+
# parse trees. That enumerator will generate a parse tree when
|
52
52
|
# called with `next` method.
|
53
53
|
# @return [Enumerator]
|
54
|
-
def to_ptree_enum
|
54
|
+
def to_ptree_enum
|
55
55
|
# How to implement?
|
56
56
|
# One visits the forest => beware of dependency
|
57
57
|
# At each visited item create a corresponding tree node.
|
58
58
|
# At end of visit & stack not empty
|
59
59
|
# Re-generate another ptree
|
60
|
-
end
|
60
|
+
end
|
61
61
|
|
62
62
|
# Part of the 'visitee' role in the Visitor design pattern.
|
63
63
|
# A visitee is expected to accept the visit from a visitor object
|
data/lib/rley/sppf/sppf_node.rb
CHANGED
@@ -7,23 +7,22 @@ module Rley # This module is used as a namespace
|
|
7
7
|
# Abstract class. The generalization for all kinds of nodes
|
8
8
|
# occurring in a shared packed parse forest (SPPF).
|
9
9
|
class SPPFNode
|
10
|
-
|
11
|
-
# @return [Lexical::TokenRange]
|
10
|
+
# @return [Lexical::TokenRange]
|
12
11
|
# A range of token indices corresponding to this node.
|
13
12
|
attr_reader(:range)
|
14
|
-
|
13
|
+
|
15
14
|
# Constructor
|
16
15
|
# @param aRange [Lexical::TokenRange]
|
17
16
|
def initialize(aRange)
|
18
17
|
@range = Lexical::TokenRange.new(aRange)
|
19
18
|
end
|
20
|
-
|
21
|
-
# Return the origin, that is, the index of the
|
19
|
+
|
20
|
+
# Return the origin, that is, the index of the
|
22
21
|
# first token matched by this node.
|
23
22
|
# @return [Integer]
|
24
|
-
def origin
|
25
|
-
|
26
|
-
end
|
23
|
+
def origin
|
24
|
+
range.low
|
25
|
+
end
|
27
26
|
end # class
|
28
27
|
end # module
|
29
28
|
end # module
|
data/lib/rley/sppf/token_node.rb
CHANGED
@@ -25,14 +25,14 @@ module Rley # This module is used as a namespace
|
|
25
25
|
# @param indentation [Integer]
|
26
26
|
# @return [String]
|
27
27
|
def to_string(indentation)
|
28
|
-
|
28
|
+
"#{token.terminal.name}#{range.to_string(indentation)}"
|
29
29
|
end
|
30
|
-
|
30
|
+
|
31
31
|
# Part of the 'visitee' role in Visitor design pattern.
|
32
32
|
# @param aVisitor[ParseTreeVisitor] the visitor
|
33
33
|
def accept(aVisitor)
|
34
34
|
aVisitor.visit_terminal(self)
|
35
|
-
end
|
35
|
+
end
|
36
36
|
end # class
|
37
37
|
end # module
|
38
38
|
end # module
|
data/lib/rley/syntax/grammar.rb
CHANGED
@@ -48,13 +48,13 @@ module Rley # This module is used as a namespace
|
|
48
48
|
end
|
49
49
|
|
50
50
|
# @return [Array] The list of non-terminals in the grammar.
|
51
|
-
def non_terminals
|
51
|
+
def non_terminals
|
52
52
|
@non_terminals ||= symbols.select { |s| s.kind_of?(NonTerminal) }
|
53
53
|
end
|
54
54
|
|
55
55
|
# @return [Production] The start production of the grammar (i.e.
|
56
56
|
# the rule that specifies the syntax for the start symbol.
|
57
|
-
def start_production
|
57
|
+
def start_production
|
58
58
|
return rules[0]
|
59
59
|
end
|
60
60
|
|
@@ -99,7 +99,7 @@ module Rley # This module is used as a namespace
|
|
99
99
|
end
|
100
100
|
|
101
101
|
# Perform some check of the grammar.
|
102
|
-
def diagnose
|
102
|
+
def diagnose
|
103
103
|
mark_undefined
|
104
104
|
mark_generative
|
105
105
|
compute_nullable
|
@@ -193,7 +193,7 @@ module Rley # This module is used as a namespace
|
|
193
193
|
|
194
194
|
# For each non-terminal determine whether it is nullable or not.
|
195
195
|
# A nullable nonterminal is a nonterminal that can match an empty string.
|
196
|
-
def compute_nullable
|
196
|
+
def compute_nullable
|
197
197
|
non_terminals.each { |nterm| nterm.nullable = false }
|
198
198
|
nullable_sets = [direct_nullable]
|
199
199
|
|
@@ -236,7 +236,7 @@ module Rley # This module is used as a namespace
|
|
236
236
|
nullables << prod.lhs
|
237
237
|
end
|
238
238
|
|
239
|
-
|
239
|
+
nullables
|
240
240
|
end
|
241
241
|
|
242
242
|
# For each prodction determine whether it is nullable or not.
|