rley 0.4.06 → 0.4.07
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.
- checksums.yaml +4 -4
- data/.rubocop.yml +22 -13
- data/.travis.yml +8 -7
- data/CHANGELOG.md +9 -0
- data/README.md +9 -10
- data/examples/NLP/mini_en_demo.rb +7 -7
- data/examples/data_formats/JSON/cli_options.rb +5 -6
- data/examples/data_formats/JSON/{JSON_demo.rb → json_demo.rb} +2 -2
- data/examples/data_formats/JSON/{JSON_grammar.rb → json_grammar.rb} +10 -10
- data/examples/data_formats/JSON/{JSON_lexer.rb → json_lexer.rb} +17 -22
- data/examples/data_formats/JSON/{JSON_parser.rb → json_parser.rb} +2 -2
- data/examples/general/calc/calc_demo.rb +1 -1
- data/examples/general/calc/calc_grammar.rb +5 -5
- data/examples/general/calc/calc_lexer.rb +14 -17
- data/examples/general/calc/calc_parser.rb +2 -2
- data/lib/rley/constants.rb +1 -1
- data/lib/rley/formatter/asciitree.rb +15 -16
- data/lib/rley/formatter/bracket_notation.rb +3 -6
- data/lib/rley/formatter/debug.rb +0 -1
- data/lib/rley/formatter/json.rb +0 -1
- data/lib/rley/gfg/grm_flow_graph.rb +11 -10
- data/lib/rley/parse_forest_visitor.rb +0 -3
- data/lib/rley/parse_tree_visitor.rb +0 -3
- data/lib/rley/parser/error_reason.rb +1 -5
- data/lib/rley/parser/gfg_chart.rb +2 -2
- data/lib/rley/parser/gfg_earley_parser.rb +1 -2
- data/lib/rley/parser/gfg_parsing.rb +3 -7
- data/lib/rley/parser/parse_entry.rb +0 -1
- data/lib/rley/parser/parse_entry_set.rb +15 -16
- data/lib/rley/parser/parse_forest_builder.rb +8 -23
- data/lib/rley/parser/parse_state.rb +1 -1
- data/lib/rley/parser/parse_tree_builder.rb +2 -30
- data/lib/rley/parser/parse_tree_factory.rb +1 -1
- data/lib/rley/parser/parse_walker_factory.rb +3 -6
- data/lib/rley/parser/state_set.rb +0 -1
- data/lib/rley/ptree/parse_tree.rb +0 -1
- data/lib/rley/ptree/terminal_node.rb +4 -1
- data/lib/rley/rley_error.rb +1 -1
- data/lib/rley/sppf/composite_node.rb +0 -1
- data/lib/rley/sppf/parse_forest.rb +0 -1
- data/lib/rley/syntax/grammar.rb +5 -9
- data/lib/rley/syntax/grammar_builder.rb +8 -11
- data/lib/rley/syntax/grm_symbol.rb +0 -1
- data/lib/rley/syntax/production.rb +5 -4
- data/lib/rley/tokens/token_range.rb +0 -1
- data/spec/rley/formatter/bracket_notation_spec.rb +3 -1
- data/spec/rley/gfg/grm_flow_graph_spec.rb +15 -46
- data/spec/rley/gfg/item_vertex_spec.rb +1 -1
- data/spec/rley/parse_forest_visitor_spec.rb +1 -1
- data/spec/rley/parse_tree_visitor_spec.rb +2 -2
- data/spec/rley/parser/error_reason_spec.rb +19 -14
- data/spec/rley/parser/gfg_chart_spec.rb +1 -1
- data/spec/rley/parser/gfg_earley_parser_spec.rb +15 -15
- data/spec/rley/parser/gfg_parsing_spec.rb +3 -3
- data/spec/rley/parser/groucho_spec.rb +6 -7
- data/spec/rley/parser/parse_forest_builder_spec.rb +5 -5
- data/spec/rley/parser/parse_forest_factory_spec.rb +5 -5
- data/spec/rley/parser/parse_state_spec.rb +8 -0
- data/spec/rley/parser/parse_tracer_spec.rb +1 -1
- data/spec/rley/parser/parse_tree_builder_spec.rb +26 -29
- data/spec/rley/parser/parse_tree_factory_spec.rb +5 -5
- data/spec/rley/parser/parse_walker_factory_spec.rb +5 -5
- data/spec/rley/ptree/parse_tree_node_spec.rb +2 -2
- data/spec/rley/ptree/terminal_node_spec.rb +1 -1
- data/spec/rley/support/ambiguous_grammar_helper.rb +1 -1
- data/spec/rley/support/expectation_helper.rb +0 -1
- data/spec/rley/support/grammar_abc_helper.rb +1 -1
- data/spec/rley/support/grammar_ambig01_helper.rb +2 -3
- data/spec/rley/support/grammar_b_expr_helper.rb +2 -2
- data/spec/rley/support/grammar_l0_helper.rb +7 -8
- data/spec/rley/support/grammar_pb_helper.rb +3 -4
- data/spec/rley/support/grammar_sppf_helper.rb +4 -4
- data/spec/rley/syntax/grammar_builder_spec.rb +5 -4
- data/spec/rley/syntax/grammar_spec.rb +10 -11
- data/spec/rley/syntax/symbol_seq_spec.rb +2 -2
- data/spec/rley/syntax/terminal_spec.rb +1 -1
- metadata +31 -31
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3ddc536143d5391aac2c513060d9f0afa43656f7
|
4
|
+
data.tar.gz: 69e5069295d886ea1287b17cdb7453506d589552
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 18f44cd59717551570814e180c735e62badf109c291cb1b41cbaf709bfc2af6cc78864ceb34060b9eb3ca017ab7cfd2e642f1848770b87cced6b1752d27896c0
|
7
|
+
data.tar.gz: 668c3f7737921db9ad35301034be75b91faa584d1ede14fabb253a0dffa6d5a43dc09c94289df714236f0e184a59eb3f10775a2bbd16a6f5a39e8c746969871e
|
data/.rubocop.yml
CHANGED
@@ -1,13 +1,9 @@
|
|
1
1
|
AllCops:
|
2
2
|
Exclude:
|
3
|
-
- 'examples/**/*'
|
4
3
|
- 'features/**/*'
|
5
4
|
- 'exp/**/*'
|
6
5
|
- 'gems/**/*'
|
7
6
|
- 'refs/**/*'
|
8
|
-
|
9
|
-
AbcSize:
|
10
|
-
Max: 45
|
11
7
|
|
12
8
|
# This is disabled because some demos use UTF-8
|
13
9
|
AsciiComments:
|
@@ -20,7 +16,7 @@ BlockComments:
|
|
20
16
|
Enabled: false
|
21
17
|
|
22
18
|
CaseIndentation:
|
23
|
-
|
19
|
+
EnforcedStyle: end
|
24
20
|
IndentOneStep: true
|
25
21
|
|
26
22
|
# Rubocop enforces the use of is_a? instead of kind_of?
|
@@ -50,18 +46,34 @@ EmptyLines:
|
|
50
46
|
Encoding:
|
51
47
|
Enabled: false
|
52
48
|
|
49
|
+
EndOfLine:
|
50
|
+
Enabled: false
|
51
|
+
# SupportedStyles: lf
|
52
|
+
|
53
|
+
|
53
54
|
IndentationWidth :
|
54
55
|
Enabled: false
|
55
56
|
|
57
|
+
# Enabled after end of support of Rubies < 2.3
|
58
|
+
Layout/IndentHeredoc:
|
59
|
+
Enabled: false
|
60
|
+
|
61
|
+
Metrics/AbcSize:
|
62
|
+
Max: 50
|
56
63
|
|
57
64
|
# Avoid methods longer than 50 lines of code
|
58
|
-
MethodLength:
|
65
|
+
Metrics/MethodLength:
|
59
66
|
Max: 50
|
60
67
|
CountComments: false
|
61
|
-
|
62
|
-
# Avoid modules longer than
|
63
|
-
ModuleLength:
|
64
|
-
|
68
|
+
|
69
|
+
# Avoid modules longer than 200 lines of code
|
70
|
+
Metrics/ModuleLength:
|
71
|
+
CountComments: false
|
72
|
+
Max: 200
|
73
|
+
|
74
|
+
Metrics/PerceivedComplexity:
|
75
|
+
Enabled: true
|
76
|
+
Max: 50
|
65
77
|
|
66
78
|
NonNilCheck:
|
67
79
|
Enabled: false
|
@@ -69,9 +81,6 @@ NonNilCheck:
|
|
69
81
|
NumericLiterals:
|
70
82
|
Enabled: false
|
71
83
|
|
72
|
-
PerceivedComplexity:
|
73
|
-
Max: 10
|
74
|
-
|
75
84
|
RaiseArgs:
|
76
85
|
Enabled: false
|
77
86
|
|
data/.travis.yml
CHANGED
@@ -1,17 +1,18 @@
|
|
1
1
|
language: ruby
|
2
2
|
rvm:
|
3
|
-
- 2.0.0
|
4
|
-
- 2.1.
|
5
|
-
- 2.2.
|
6
|
-
- 2.3.
|
3
|
+
- 2.0.0-p648
|
4
|
+
- 2.1.10
|
5
|
+
- 2.2.7
|
6
|
+
- 2.3.4
|
7
|
+
- 2.4.1
|
7
8
|
- ruby-head
|
8
|
-
- rbx-2.
|
9
|
-
- jruby-9.1.
|
9
|
+
- rbx-2.7
|
10
|
+
- jruby-9.1.9.0
|
10
11
|
- jruby-head
|
11
12
|
matrix:
|
12
13
|
allow_failures:
|
13
14
|
- rvm: ruby-head
|
14
|
-
- rvm: rbx-2.
|
15
|
+
- rvm: rbx-2.7
|
15
16
|
- rvm: jruby-head
|
16
17
|
|
17
18
|
gemfile:
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,12 @@
|
|
1
|
+
### 0.4.07 / 2017-05-25
|
2
|
+
* [FIX] To avoid Fixnum deprecation error in Ruby 2.4 abd higher, all explicit references to Fixnum has been removed.
|
3
|
+
* [FIX] File `.rubocop.yml`: folder `examples` was excluded from Rubocop control. Now it is in code analysis scope.
|
4
|
+
* [NEW] File `spec\.rubocop.yml` to tune the code analysis for Rspec files
|
5
|
+
* [NEW] File `examples\.rubocop.yml` to tune the code analysis for example files
|
6
|
+
* [CHANGE] Code re-styling to please Rubocop 0.49.0: less than 10 offences remain (from above 200 count!)
|
7
|
+
* [CHANGE] Added support for Ruby 2.4.x. Files `.travis.yml` and `README.md` updated.
|
8
|
+
|
9
|
+
|
1
10
|
### 0.4.06 / 2017-05-25
|
2
11
|
* [FIX] File `formatter\asciitree.rb` fixed inconsistency in comments that caused Yard warnings.
|
3
12
|
* [FIX] File `formatter\bracket_notation.rb` fixed inconsistency in comments that caused Yard warnings.
|
data/README.md
CHANGED
@@ -55,8 +55,9 @@ Rley supports the following Ruby implementations:
|
|
55
55
|
- MRI 2.0
|
56
56
|
- MRI 2.1
|
57
57
|
- MRI 2.2
|
58
|
-
- MRI 2.3
|
59
|
-
-
|
58
|
+
- MRI 2.3
|
59
|
+
- MRI 2.4
|
60
|
+
- JRuby 9.1+
|
60
61
|
|
61
62
|
---
|
62
63
|
|
@@ -88,7 +89,7 @@ directory
|
|
88
89
|
The subset of English grammar is based on an example from the NLTK book.
|
89
90
|
|
90
91
|
```ruby
|
91
|
-
require 'rley'
|
92
|
+
require 'rley' # Load Rley library
|
92
93
|
|
93
94
|
# Instantiate a builder object that will build the grammar for us
|
94
95
|
builder = Rley::Syntax::GrammarBuilder.new do
|
@@ -134,7 +135,7 @@ The subset of English grammar is based on an example from the NLTK book.
|
|
134
135
|
'on' => 'Preposition',
|
135
136
|
'by' => 'Preposition',
|
136
137
|
'with' => 'Preposition'
|
137
|
-
}
|
138
|
+
}.freeze
|
138
139
|
```
|
139
140
|
|
140
141
|
|
@@ -142,12 +143,10 @@ The subset of English grammar is based on an example from the NLTK book.
|
|
142
143
|
```ruby
|
143
144
|
# A tokenizer reads the input string and converts it into a sequence of tokens
|
144
145
|
# Highly simplified tokenizer implementation.
|
145
|
-
def tokenizer(
|
146
|
-
tokens =
|
146
|
+
def tokenizer(aTextToParse, aGrammar)
|
147
|
+
tokens = aTextToParse.scan(/\S+/).map do |word|
|
147
148
|
term_name = Lexicon[word]
|
148
|
-
if term_name.nil?
|
149
|
-
raise StandardError, "Word '#{word}' not found in lexicon"
|
150
|
-
end
|
149
|
+
raise StandardError, "Word '#{word}' not found in lexicon" if term_name.nil?
|
151
150
|
terminal = aGrammar.name2symbol[term_name]
|
152
151
|
Rley::Tokens::Token.new(word, terminal)
|
153
152
|
end
|
@@ -441,7 +440,7 @@ The extensive resource list not to miss: [Awesome NLP with Ruby](https://github.
|
|
441
440
|
actively curated by Andrei Beliankou (aka arbox).
|
442
441
|
|
443
442
|
## Thanks to:
|
444
|
-
* Professor Keshav Pingali, one of the creators of the Grammar Flow Graph parsing approach for his encouraging e-mail
|
443
|
+
* Professor Keshav Pingali, one of the creators of the Grammar Flow Graph parsing approach for his encouraging e-mail exchange.
|
445
444
|
|
446
445
|
## Grammar Flow Graph
|
447
446
|
Since the Grammar Flow Graph parsing approach is quite new, it has not yet taken a place in
|
@@ -1,4 +1,4 @@
|
|
1
|
-
require 'rley'
|
1
|
+
require 'rley' # Load Rley library
|
2
2
|
|
3
3
|
########################################
|
4
4
|
# Step 1. Define a grammar for a micro English-like language
|
@@ -10,7 +10,8 @@ require 'rley' # Load Rley library
|
|
10
10
|
|
11
11
|
# Instantiate a builder object that will build the grammar for us
|
12
12
|
builder = Rley::Syntax::GrammarBuilder.new do
|
13
|
-
# Next 2 lines we define the terminal symbols
|
13
|
+
# Next 2 lines we define the terminal symbols
|
14
|
+
# (= word categories in the lexicon)
|
14
15
|
add_terminals('Noun', 'Proper-Noun', 'Verb')
|
15
16
|
add_terminals('Determiner', 'Preposition')
|
16
17
|
|
@@ -51,7 +52,7 @@ Lexicon = {
|
|
51
52
|
'on' => 'Preposition',
|
52
53
|
'by' => 'Preposition',
|
53
54
|
'with' => 'Preposition'
|
54
|
-
}
|
55
|
+
}.freeze
|
55
56
|
|
56
57
|
########################################
|
57
58
|
# Step 3. Creating a tokenizer
|
@@ -60,9 +61,7 @@ Lexicon = {
|
|
60
61
|
def tokenizer(aTextToParse, aGrammar)
|
61
62
|
tokens = aTextToParse.scan(/\S+/).map do |word|
|
62
63
|
term_name = Lexicon[word]
|
63
|
-
if term_name.nil?
|
64
|
-
raise StandardError, "Word '#{word}' not found in lexicon"
|
65
|
-
end
|
64
|
+
raise StandardError, "Word '#{word}' not found in lexicon" if term_name.nil?
|
66
65
|
terminal = aGrammar.name2symbol[term_name]
|
67
66
|
Rley::Tokens::Token.new(word, terminal)
|
68
67
|
end
|
@@ -102,7 +101,8 @@ visitor = Rley::ParseTreeVisitor.new(ptree)
|
|
102
101
|
# Let's create a formatter that will render the parse tree with characters
|
103
102
|
renderer = Rley::Formatter::Asciitree.new($stdout)
|
104
103
|
|
105
|
-
# Let's create a formatter that will render the parse tree in labelled
|
104
|
+
# Let's create a formatter that will render the parse tree in labelled
|
105
|
+
# bracket notation
|
106
106
|
# renderer = Rley::Formatter::BracketNotation.new($stdout)
|
107
107
|
|
108
108
|
# Subscribe the formatter to the visitor's event and launch the visit
|
@@ -2,10 +2,9 @@ require 'optparse'
|
|
2
2
|
|
3
3
|
# A Hash specialization that collects the command-line options
|
4
4
|
class CLIOptions < Hash
|
5
|
-
#labelled square notation (LBN).
|
6
|
-
#Use online tools (e.g. http://yohasebe.com/rsyntaxtree/) to visualize
|
7
|
-
#parse trees from LBN output.
|
8
|
-
|
5
|
+
# labelled square notation (LBN).
|
6
|
+
# Use online tools (e.g. http://yohasebe.com/rsyntaxtree/) to visualize
|
7
|
+
# parse trees from LBN output.
|
9
8
|
def initialize(progName, progVersion, args)
|
10
9
|
super()
|
11
10
|
|
@@ -42,7 +41,7 @@ Select the output format (default: ascii_tree). Available formats:
|
|
42
41
|
Use online tools (e.g. http://yohasebe.com/rsyntaxtree/)
|
43
42
|
to visualize parse trees from LBN output.
|
44
43
|
END_TEXT
|
45
|
-
formats = %i
|
44
|
+
formats = %i[ascii_tree labelled]
|
46
45
|
opts.on('-f', '--format FORMAT', formats, format_help) do |frm|
|
47
46
|
self[:format] = frm
|
48
47
|
end
|
@@ -62,4 +61,4 @@ END_TEXT
|
|
62
61
|
end
|
63
62
|
end
|
64
63
|
end
|
65
|
-
end # class
|
64
|
+
end # class
|
@@ -1,5 +1,5 @@
|
|
1
1
|
# Grammar for JSON data representation
|
2
|
-
require 'rley'
|
2
|
+
require 'rley' # Load the gem
|
3
3
|
|
4
4
|
|
5
5
|
########################################
|
@@ -21,17 +21,17 @@ builder = Rley::Syntax::GrammarBuilder.new do
|
|
21
21
|
rule 'value' => 'array'
|
22
22
|
rule 'value' => 'number'
|
23
23
|
rule 'value' => 'string'
|
24
|
-
rule 'object' => %w
|
25
|
-
rule 'object' => %w
|
24
|
+
rule 'object' => %w[begin-object member-list end-object]
|
25
|
+
rule 'object' => %w[begin-object end-object]
|
26
26
|
# Next rule is an example of a left recursive rule
|
27
|
-
rule 'member-list' => %w
|
27
|
+
rule 'member-list' => %w[member-list value-separator member]
|
28
28
|
rule 'member-list' => 'member'
|
29
|
-
rule 'member' => %w
|
30
|
-
rule 'array' => %w
|
31
|
-
rule 'array' => %w
|
32
|
-
rule 'array-items' => %w
|
33
|
-
rule 'array-items' => %w
|
29
|
+
rule 'member' => %w[string name-separator value]
|
30
|
+
rule 'array' => %w[begin-array array-items end-array]
|
31
|
+
rule 'array' => %w[begin-array end-array]
|
32
|
+
rule 'array-items' => %w[array-items value-separator value]
|
33
|
+
rule 'array-items' => %w[value]
|
34
34
|
end
|
35
35
|
|
36
36
|
# And now build the JSON grammar...
|
37
|
-
GrammarJSON = builder.grammar
|
37
|
+
GrammarJSON = builder.grammar
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# File: JSON_lexer.rb
|
2
2
|
# Lexer for the JSON data format
|
3
|
-
require 'rley'
|
3
|
+
require 'rley' # Load the gem
|
4
4
|
require 'strscan'
|
5
5
|
|
6
6
|
# Lexer for JSON.
|
@@ -17,20 +17,19 @@ class JSONLexer
|
|
17
17
|
']' => 'end-array',
|
18
18
|
',' => 'value-separator',
|
19
19
|
':' => 'name-separator'
|
20
|
-
}
|
20
|
+
}.freeze
|
21
21
|
|
22
|
-
class ScanError < StandardError
|
22
|
+
class ScanError < StandardError; end
|
23
23
|
|
24
|
-
public
|
25
24
|
def initialize(source, aGrammar)
|
26
25
|
@scanner = StringScanner.new(source)
|
27
26
|
@name2symbol = aGrammar.name2symbol
|
28
|
-
@lineno =
|
27
|
+
@lineno = 1
|
29
28
|
end
|
30
29
|
|
31
30
|
def tokens()
|
32
31
|
tok_sequence = []
|
33
|
-
until @scanner.eos?
|
32
|
+
until @scanner.eos?
|
34
33
|
token = _next_token
|
35
34
|
tok_sequence << token unless token.nil?
|
36
35
|
end
|
@@ -38,13 +37,14 @@ public
|
|
38
37
|
return tok_sequence
|
39
38
|
end
|
40
39
|
|
41
|
-
private
|
40
|
+
private
|
41
|
+
|
42
42
|
def _next_token()
|
43
43
|
token = nil
|
44
44
|
skip_whitespaces
|
45
45
|
curr_ch = scanner.getch # curr_ch is at start of token or eof reached...
|
46
46
|
|
47
|
-
|
47
|
+
loop do
|
48
48
|
break if curr_ch.nil?
|
49
49
|
|
50
50
|
case curr_ch
|
@@ -53,7 +53,7 @@ private
|
|
53
53
|
token_type = name2symbol[type_name]
|
54
54
|
token = Rley::Tokens::Token.new(curr_ch, token_type)
|
55
55
|
|
56
|
-
when /[ftn]/
|
56
|
+
when /[ftn]/ # First letter of keywords
|
57
57
|
@scanner.pos = scanner.pos - 1 # Simulate putback
|
58
58
|
keyw = scanner.scan(/false|true|null/)
|
59
59
|
if keyw.nil?
|
@@ -65,10 +65,11 @@ private
|
|
65
65
|
end
|
66
66
|
|
67
67
|
# LITERALS
|
68
|
-
when '"'
|
68
|
+
when '"' # Start string delimiter found
|
69
69
|
value = scanner.scan(/([^"\\]|\\.)*/)
|
70
|
-
end_delimiter = scanner.getch
|
71
|
-
|
70
|
+
end_delimiter = scanner.getch
|
71
|
+
err_msg = 'No closing quotes (") found'
|
72
|
+
raise ScanError.new(err_msg) if end_delimiter.nil?
|
72
73
|
token_type = name2symbol['string']
|
73
74
|
token = Rley::Tokens::Token.new(value, token_type)
|
74
75
|
|
@@ -83,15 +84,13 @@ private
|
|
83
84
|
sequel = scanner.scan(/.{1,20}/)
|
84
85
|
erroneous += sequel unless sequel.nil?
|
85
86
|
raise ScanError.new("Unknown token #{erroneous}")
|
86
|
-
end #case
|
87
|
-
|
88
|
-
|
89
|
-
end while (token.nil? && curr_ch = scanner.getch())
|
87
|
+
end # case
|
88
|
+
break unless token.nil? && (curr_ch = scanner.getch)
|
89
|
+
end
|
90
90
|
|
91
91
|
return token
|
92
92
|
end
|
93
93
|
|
94
|
-
|
95
94
|
def skip_whitespaces()
|
96
95
|
matched = scanner.scan(/[ \t\f\n\r]+/)
|
97
96
|
return if matched.nil?
|
@@ -101,12 +100,8 @@ private
|
|
101
100
|
newline_detected(newline_count)
|
102
101
|
end
|
103
102
|
|
104
|
-
|
105
103
|
def newline_detected(count)
|
106
104
|
@lineno += count
|
107
|
-
@line_start = scanner.pos
|
105
|
+
@line_start = scanner.pos
|
108
106
|
end
|
109
|
-
|
110
107
|
end # class
|
111
|
-
|
112
|
-
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# Purpose: to demonstrate how to build and render a parse tree for JSON
|
2
2
|
# language
|
3
|
-
require 'rley'
|
3
|
+
require 'rley' # Load the Rley gem
|
4
4
|
require_relative 'json_lexer'
|
5
5
|
|
6
6
|
# Steps to render a parse tree (of a valid parsed input):
|
@@ -13,7 +13,7 @@ require_relative 'json_lexer'
|
|
13
13
|
|
14
14
|
########################################
|
15
15
|
# Step 1. Load a grammar for JSON
|
16
|
-
require_relative '
|
16
|
+
require_relative 'json_grammar'
|
17
17
|
|
18
18
|
# A JSON parser derived from our general Earley parser.
|
19
19
|
class JSONParser < Rley::Parser::GFGEarleyParser
|
@@ -1,5 +1,5 @@
|
|
1
1
|
# Grammar for simple arithmetical expressions
|
2
|
-
require 'rley'
|
2
|
+
require 'rley' # Load the gem
|
3
3
|
|
4
4
|
########################################
|
5
5
|
# Define a grammar for basic arithmetical expressions
|
@@ -8,13 +8,13 @@ builder = Rley::Syntax::GrammarBuilder.new do
|
|
8
8
|
add_terminals('LPAREN', 'RPAREN') # For '(', ')' delimiters
|
9
9
|
add_terminals('PLUS', 'MINUS') # For '+', '-' operators or sign
|
10
10
|
add_terminals('STAR', 'DIVIDE') # For '*', '/' operators
|
11
|
-
rule 'expression' => %w
|
11
|
+
rule 'expression' => %w[sign simple_expression]
|
12
12
|
rule 'simple_expression' => 'term'
|
13
|
-
rule 'simple_expression' => %w
|
13
|
+
rule 'simple_expression' => %w[simple_expression add_operator term]
|
14
14
|
rule 'term' => 'factor'
|
15
|
-
rule 'term' => %w
|
15
|
+
rule 'term' => %w[term mul_operator factor]
|
16
16
|
rule 'factor' => 'NUMBER'
|
17
|
-
rule 'factor' => %w
|
17
|
+
rule 'factor' => %w[LPAREN expression RPAREN]
|
18
18
|
rule 'sign' => 'PLUS'
|
19
19
|
rule 'sign' => 'MINUS'
|
20
20
|
rule 'sign' => []
|