rley 0.3.05 → 0.3.06

Sign up to get free protection for your applications and to get access to all the features.
@@ -194,60 +194,72 @@ module Rley # Open this namespace to avoid module qualifier prefixes
194
194
  event25 = walker.next
195
195
  expectations = [:revisit, '.S | 0', 0]
196
196
  event_expectations(event25, expectations)
197
-
197
+
198
198
  event26 = walker.next
199
- expectations = [:revisit, '.Phi | 0', 0]
199
+ expectations = [:revisit, 'Phi => . S | 0', 0]
200
200
  event_expectations(event26, expectations)
201
201
 
202
- # Backtracking is occurring
203
202
  event27 = walker.next
204
- expectations = [:backtrack, 'A. | 0', 1]
203
+ expectations = [:revisit, '.Phi | 0', 0]
205
204
  event_expectations(event27, expectations)
206
205
 
206
+ # Backtracking is occurring
207
207
  event28 = walker.next
208
- expectations = [:visit, 'A => B A . | 0', 1]
208
+ expectations = [:backtrack, 'A. | 0', 1]
209
209
  event_expectations(event28, expectations)
210
210
 
211
211
  event29 = walker.next
212
- expectations = [:revisit, 'A. | 0', 1]
212
+ expectations = [:visit, 'A => B A . | 0', 1]
213
213
  event_expectations(event29, expectations)
214
214
 
215
215
  event30 = walker.next
216
- expectations = [:visit, 'A => B . A | 0', 0]
216
+ expectations = [:revisit, 'A. | 0', 1]
217
217
  event_expectations(event30, expectations)
218
218
 
219
219
  event31 = walker.next
220
- expectations = [:visit, 'B. | 0', 0]
220
+ expectations = [:visit, 'A => B . A | 0', 0]
221
221
  event_expectations(event31, expectations)
222
222
 
223
223
  event32 = walker.next
224
- expectations = [:visit, 'B => . | 0', 0]
224
+ expectations = [:visit, 'B. | 0', 0]
225
225
  event_expectations(event32, expectations)
226
226
 
227
227
  event33 = walker.next
228
- expectations = [:visit, '.B | 0', 0]
228
+ expectations = [:visit, 'B => . | 0', 0]
229
229
  event_expectations(event33, expectations)
230
230
 
231
231
  event34 = walker.next
232
- expectations = [:visit, 'A => . B A | 0', 0]
232
+ expectations = [:visit, '.B | 0', 0]
233
233
  event_expectations(event34, expectations)
234
-
234
+
235
235
  event35 = walker.next
236
- expectations = [:revisit, '.A | 0', 0]
236
+ expectations = [:visit, 'A => . B A | 0', 0]
237
237
  event_expectations(event35, expectations)
238
-
238
+
239
239
  event36 = walker.next
240
- expectations = [:revisit, '.S | 0', 0]
240
+ expectations = [:revisit, '.A | 0', 0]
241
241
  event_expectations(event36, expectations)
242
242
 
243
243
  event37 = walker.next
244
- expectations = [:revisit, '.Phi | 0', 0]
244
+ expectations = [:revisit, 'S => . A T | 0', 0]
245
245
  event_expectations(event37, expectations)
246
+
247
+ event38 = walker.next
248
+ expectations = [:revisit, '.S | 0', 0]
249
+ event_expectations(event38, expectations)
250
+
251
+ event39 = walker.next
252
+ expectations = [:revisit, 'Phi => . S | 0', 0]
253
+ event_expectations(event39, expectations)
254
+
255
+ event40 = walker.next
256
+ expectations = [:revisit, '.Phi | 0', 0]
257
+ event_expectations(event40, expectations)
246
258
  end
247
259
 
248
260
  it 'should raise an exception at end of visit' do
249
261
  walker = subject.build_walker(accept_entry, accept_index)
250
- 37.times { walker.next }
262
+ 40.times { walker.next }
251
263
 
252
264
  expect { walker.next }.to raise_error(StopIteration)
253
265
  end
@@ -0,0 +1,41 @@
1
+ # Load the builder class
2
+ require_relative '../../../lib/rley/syntax/grammar_builder'
3
+ require_relative '../../../lib/rley/parser/token'
4
+
5
+
6
+ module GrammarAmbig01Helper
7
+ ########################################
8
+ # Factory method. Define a grammar for a very simple language
9
+ # Grammar 3: An ambiguous arithmetic expression language
10
+ # (based on example in article on Earley's algorithm in Wikipedia)
11
+ def grammar_ambig01_builder()
12
+ builder = Rley::Syntax::GrammarBuilder.new
13
+ builder.add_terminals('integer', '+', '*')
14
+ builder.add_production('P' => 'S')
15
+ builder.add_production('S' => %w(S + S))
16
+ builder.add_production('S' => %w(S * S))
17
+ builder.add_production('S' => 'L')
18
+ builder.add_production('L' => 'integer')
19
+ builder
20
+ end
21
+
22
+
23
+ # Highly simplified tokenizer implementation.
24
+ def tokenizer_ambig01(aText, aGrammar)
25
+ tokens = aText.scan(/\S+/).map do |lexeme|
26
+ case lexeme
27
+ when '+', '*'
28
+ terminal = aGrammar.name2symbol[lexeme]
29
+ when /^[-+]?\d+$/
30
+ terminal = aGrammar.name2symbol['integer']
31
+ else
32
+ msg = "Unknown input text '#{lexeme}'"
33
+ raise StandardError, msg
34
+ end
35
+ Rley::Parser::Token.new(lexeme, terminal)
36
+ end
37
+
38
+ return tokens
39
+ end
40
+ end # module
41
+ # End of file
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rley
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.05
4
+ version: 0.3.06
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dimitri Geshef
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-01 00:00:00.000000000 Z
11
+ date: 2016-11-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -225,6 +225,7 @@ files:
225
225
  - spec/rley/gfg/vertex_spec.rb
226
226
  - spec/rley/parse_forest_visitor_spec.rb
227
227
  - spec/rley/parse_tree_visitor_spec.rb
228
+ - spec/rley/parser/ambiguous_parse_spec.rb
228
229
  - spec/rley/parser/chart_spec.rb
229
230
  - spec/rley/parser/dotted_item_spec.rb
230
231
  - spec/rley/parser/earley_parser_spec.rb
@@ -253,6 +254,7 @@ files:
253
254
  - spec/rley/support/ambiguous_grammar_helper.rb
254
255
  - spec/rley/support/expectation_helper.rb
255
256
  - spec/rley/support/grammar_abc_helper.rb
257
+ - spec/rley/support/grammar_ambig01_helper.rb
256
258
  - spec/rley/support/grammar_b_expr_helper.rb
257
259
  - spec/rley/support/grammar_helper.rb
258
260
  - spec/rley/support/grammar_l0_helper.rb
@@ -310,6 +312,7 @@ test_files:
310
312
  - spec/rley/gfg/shortcut_edge_spec.rb
311
313
  - spec/rley/gfg/start_vertex_spec.rb
312
314
  - spec/rley/gfg/vertex_spec.rb
315
+ - spec/rley/parser/ambiguous_parse_spec.rb
313
316
  - spec/rley/parser/chart_spec.rb
314
317
  - spec/rley/parser/dotted_item_spec.rb
315
318
  - spec/rley/parser/earley_parser_spec.rb