racc 1.4.9-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (107) hide show
  1. data/.gemtest +0 -0
  2. data/COPYING +515 -0
  3. data/ChangeLog +846 -0
  4. data/DEPENDS +4 -0
  5. data/Manifest.txt +105 -0
  6. data/README.ja.rdoc +96 -0
  7. data/README.rdoc +86 -0
  8. data/Rakefile +51 -0
  9. data/TODO +5 -0
  10. data/bin/racc +308 -0
  11. data/bin/racc2y +195 -0
  12. data/bin/y2racc +339 -0
  13. data/ext/racc/MANIFEST +4 -0
  14. data/ext/racc/cparse.c +824 -0
  15. data/ext/racc/depend +1 -0
  16. data/ext/racc/extconf.rb +5 -0
  17. data/fastcache/extconf.rb +2 -0
  18. data/fastcache/fastcache.c +185 -0
  19. data/lib/racc.rb +6 -0
  20. data/lib/racc/compat.rb +40 -0
  21. data/lib/racc/debugflags.rb +59 -0
  22. data/lib/racc/exception.rb +15 -0
  23. data/lib/racc/grammar.rb +1115 -0
  24. data/lib/racc/grammarfileparser.rb +559 -0
  25. data/lib/racc/info.rb +16 -0
  26. data/lib/racc/iset.rb +91 -0
  27. data/lib/racc/logfilegenerator.rb +211 -0
  28. data/lib/racc/parser-text.rb +479 -0
  29. data/lib/racc/parser.rb +474 -0
  30. data/lib/racc/parserfilegenerator.rb +512 -0
  31. data/lib/racc/pre-setup +13 -0
  32. data/lib/racc/sourcetext.rb +34 -0
  33. data/lib/racc/state.rb +971 -0
  34. data/lib/racc/statetransitiontable.rb +316 -0
  35. data/lib/racc/static.rb +5 -0
  36. data/misc/dist.sh +31 -0
  37. data/rdoc/en/NEWS.en.rdoc +282 -0
  38. data/rdoc/en/command.en.html +78 -0
  39. data/rdoc/en/debug.en.rdoc +20 -0
  40. data/rdoc/en/grammar.en.rdoc +230 -0
  41. data/rdoc/en/index.en.html +10 -0
  42. data/rdoc/en/parser.en.rdoc +74 -0
  43. data/rdoc/en/usage.en.rdoc +83 -0
  44. data/rdoc/ja/NEWS.ja.rdoc +307 -0
  45. data/rdoc/ja/command.ja.html +94 -0
  46. data/rdoc/ja/debug.ja.rdoc +36 -0
  47. data/rdoc/ja/grammar.ja.rdoc +348 -0
  48. data/rdoc/ja/index.ja.html +10 -0
  49. data/rdoc/ja/parser.ja.rdoc +125 -0
  50. data/rdoc/ja/usage.ja.html +414 -0
  51. data/sample/array.y +67 -0
  52. data/sample/array2.y +59 -0
  53. data/sample/calc-ja.y +66 -0
  54. data/sample/calc.y +65 -0
  55. data/sample/conflict.y +15 -0
  56. data/sample/hash.y +60 -0
  57. data/sample/lalr.y +17 -0
  58. data/sample/lists.y +57 -0
  59. data/sample/syntax.y +46 -0
  60. data/sample/yyerr.y +46 -0
  61. data/setup.rb +1587 -0
  62. data/tasks/doc.rb +12 -0
  63. data/tasks/email.rb +55 -0
  64. data/test/assets/chk.y +126 -0
  65. data/test/assets/conf.y +16 -0
  66. data/test/assets/digraph.y +29 -0
  67. data/test/assets/echk.y +118 -0
  68. data/test/assets/err.y +60 -0
  69. data/test/assets/expect.y +7 -0
  70. data/test/assets/firstline.y +4 -0
  71. data/test/assets/ichk.y +102 -0
  72. data/test/assets/intp.y +546 -0
  73. data/test/assets/mailp.y +437 -0
  74. data/test/assets/newsyn.y +25 -0
  75. data/test/assets/noend.y +4 -0
  76. data/test/assets/nonass.y +41 -0
  77. data/test/assets/normal.y +27 -0
  78. data/test/assets/norule.y +4 -0
  79. data/test/assets/nullbug1.y +25 -0
  80. data/test/assets/nullbug2.y +15 -0
  81. data/test/assets/opt.y +123 -0
  82. data/test/assets/percent.y +35 -0
  83. data/test/assets/recv.y +97 -0
  84. data/test/assets/rrconf.y +14 -0
  85. data/test/assets/scan.y +72 -0
  86. data/test/assets/syntax.y +50 -0
  87. data/test/assets/unterm.y +5 -0
  88. data/test/assets/useless.y +12 -0
  89. data/test/assets/yyerr.y +46 -0
  90. data/test/bench.y +36 -0
  91. data/test/helper.rb +91 -0
  92. data/test/infini.y +8 -0
  93. data/test/scandata/brace +7 -0
  94. data/test/scandata/gvar +1 -0
  95. data/test/scandata/normal +4 -0
  96. data/test/scandata/percent +18 -0
  97. data/test/scandata/slash +10 -0
  98. data/test/src.intp +34 -0
  99. data/test/start.y +20 -0
  100. data/test/test_chk_y.rb +51 -0
  101. data/test/test_grammar_file_parser.rb +15 -0
  102. data/test/test_racc_command.rb +155 -0
  103. data/test/test_scan_y.rb +51 -0
  104. data/test/testscanner.rb +51 -0
  105. data/web/racc.en.rhtml +42 -0
  106. data/web/racc.ja.rhtml +51 -0
  107. metadata +233 -0
@@ -0,0 +1,474 @@
1
+ #
2
+ # $Id: 891566e6b68142589f193e8e7bd3372e1e258373 $
3
+ #
4
+ # Copyright (c) 1999-2006 Minero Aoki
5
+ #
6
+ # This program is free software.
7
+ # You can distribute/modify this program under the same terms of ruby.
8
+ #
9
+ # As a special exception, when this code is copied by Racc
10
+ # into a Racc output file, you may use that output file
11
+ # without restriction.
12
+ #
13
+
14
+ require 'racc/info'
15
+
16
+ unless defined?(NotImplementedError)
17
+ NotImplementedError = NotImplementError # :nodoc:
18
+ end
19
+
20
+ module Racc
21
+ class ParseError < StandardError; end
22
+ end
23
+ unless defined?(::ParseError)
24
+ ParseError = Racc::ParseError
25
+ end
26
+
27
+ # Racc is a LALR(1) parser generator.
28
+ # It is written in Ruby itself, and generates Ruby programs.
29
+ module Racc
30
+
31
+ unless defined?(Racc_No_Extentions)
32
+ Racc_No_Extentions = false # :nodoc:
33
+ end
34
+
35
+ class Parser
36
+
37
+ Racc_Runtime_Version = ::Racc::VERSION
38
+ Racc_Runtime_Revision = '$Id: 891566e6b68142589f193e8e7bd3372e1e258373 $'
39
+
40
+ Racc_Runtime_Core_Version_R = ::Racc::VERSION
41
+ Racc_Runtime_Core_Revision_R = '$Id: 891566e6b68142589f193e8e7bd3372e1e258373 $'.split[1]
42
+ begin
43
+ require 'racc/cparse'
44
+ # Racc_Runtime_Core_Version_C = (defined in extention)
45
+ Racc_Runtime_Core_Revision_C = Racc_Runtime_Core_Id_C.split[2]
46
+ unless new.respond_to?(:_racc_do_parse_c, true)
47
+ raise LoadError, 'old cparse.so'
48
+ end
49
+ if Racc_No_Extentions
50
+ raise LoadError, 'selecting ruby version of racc runtime core'
51
+ end
52
+
53
+ Racc_Main_Parsing_Routine = :_racc_do_parse_c # :nodoc:
54
+ Racc_YY_Parse_Method = :_racc_yyparse_c # :nodoc:
55
+ Racc_Runtime_Core_Version = Racc_Runtime_Core_Version_C # :nodoc:
56
+ Racc_Runtime_Core_Revision = Racc_Runtime_Core_Revision_C # :nodoc:
57
+ Racc_Runtime_Type = 'c' # :nodoc:
58
+ rescue LoadError
59
+ Racc_Main_Parsing_Routine = :_racc_do_parse_rb
60
+ Racc_YY_Parse_Method = :_racc_yyparse_rb
61
+ Racc_Runtime_Core_Version = Racc_Runtime_Core_Version_R
62
+ Racc_Runtime_Core_Revision = Racc_Runtime_Core_Revision_R
63
+ Racc_Runtime_Type = 'ruby'
64
+ end
65
+
66
+ def Parser.racc_runtime_type # :nodoc:
67
+ Racc_Runtime_Type
68
+ end
69
+
70
+ def _racc_setup
71
+ @yydebug = false unless self.class::Racc_debug_parser
72
+ @yydebug = false unless defined?(@yydebug)
73
+ if @yydebug
74
+ @racc_debug_out = $stderr unless defined?(@racc_debug_out)
75
+ @racc_debug_out ||= $stderr
76
+ end
77
+ arg = self.class::Racc_arg
78
+ arg[13] = true if arg.size < 14
79
+ arg
80
+ end
81
+
82
+ def _racc_init_sysvars
83
+ @racc_state = [0]
84
+ @racc_tstack = []
85
+ @racc_vstack = []
86
+
87
+ @racc_t = nil
88
+ @racc_val = nil
89
+
90
+ @racc_read_next = true
91
+
92
+ @racc_user_yyerror = false
93
+ @racc_error_status = 0
94
+ end
95
+
96
+ # The entry point of the parser. This method is used with #next_token.
97
+ # If Racc wants to get token (and its value), calls next_token.
98
+ #
99
+ # Example:
100
+ # def parse
101
+ # @q = [[1,1],
102
+ # [2,2],
103
+ # [3,3],
104
+ # [false, '$']]
105
+ # do_parse
106
+ # end
107
+ #
108
+ # def next_token
109
+ # @q.shift
110
+ # end
111
+ def do_parse
112
+ __send__(Racc_Main_Parsing_Routine, _racc_setup(), false)
113
+ end
114
+
115
+ # The method to fetch next token.
116
+ # If you use #do_parse method, you must implement #next_token.
117
+ #
118
+ # The format of return value is [TOKEN_SYMBOL, VALUE].
119
+ # +token-symbol+ is represented by Ruby's symbol by default, e.g. :IDENT
120
+ # for 'IDENT'. ";" (String) for ';'.
121
+ #
122
+ # The final symbol (End of file) must be false.
123
+ def next_token
124
+ raise NotImplementedError, "#{self.class}\#next_token is not defined"
125
+ end
126
+
127
+ def _racc_do_parse_rb(arg, in_debug)
128
+ action_table, action_check, action_default, action_pointer,
129
+ _, _, _, _,
130
+ _, _, token_table, * = arg
131
+
132
+ _racc_init_sysvars
133
+ tok = act = i = nil
134
+
135
+ catch(:racc_end_parse) {
136
+ while true
137
+ if i = action_pointer[@racc_state[-1]]
138
+ if @racc_read_next
139
+ if @racc_t != 0 # not EOF
140
+ tok, @racc_val = next_token()
141
+ unless tok # EOF
142
+ @racc_t = 0
143
+ else
144
+ @racc_t = (token_table[tok] or 1) # error token
145
+ end
146
+ racc_read_token(@racc_t, tok, @racc_val) if @yydebug
147
+ @racc_read_next = false
148
+ end
149
+ end
150
+ i += @racc_t
151
+ unless i >= 0 and
152
+ act = action_table[i] and
153
+ action_check[i] == @racc_state[-1]
154
+ act = action_default[@racc_state[-1]]
155
+ end
156
+ else
157
+ act = action_default[@racc_state[-1]]
158
+ end
159
+ while act = _racc_evalact(act, arg)
160
+ ;
161
+ end
162
+ end
163
+ }
164
+ end
165
+
166
+ # Another entry point for the parser.
167
+ # If you use this method, you must implement RECEIVER#METHOD_ID method.
168
+ #
169
+ # RECEIVER#METHOD_ID is a method to get next token.
170
+ # It must 'yield' the token, which format is [TOKEN-SYMBOL, VALUE].
171
+ def yyparse(recv, mid)
172
+ __send__(Racc_YY_Parse_Method, recv, mid, _racc_setup(), true)
173
+ end
174
+
175
+ def _racc_yyparse_rb(recv, mid, arg, c_debug)
176
+ action_table, action_check, action_default, action_pointer,
177
+ _, _, _, _,
178
+ _, _, token_table, * = arg
179
+
180
+ _racc_init_sysvars
181
+
182
+ catch(:racc_end_parse) {
183
+ until i = action_pointer[@racc_state[-1]]
184
+ while act = _racc_evalact(action_default[@racc_state[-1]], arg)
185
+ ;
186
+ end
187
+ end
188
+ recv.__send__(mid) do |tok, val|
189
+ unless tok
190
+ @racc_t = 0
191
+ else
192
+ @racc_t = (token_table[tok] or 1) # error token
193
+ end
194
+ @racc_val = val
195
+ @racc_read_next = false
196
+
197
+ i += @racc_t
198
+ unless i >= 0 and
199
+ act = action_table[i] and
200
+ action_check[i] == @racc_state[-1]
201
+ act = action_default[@racc_state[-1]]
202
+ end
203
+ while act = _racc_evalact(act, arg)
204
+ ;
205
+ end
206
+
207
+ while !(i = action_pointer[@racc_state[-1]]) ||
208
+ ! @racc_read_next ||
209
+ @racc_t == 0 # $
210
+ unless i and i += @racc_t and
211
+ i >= 0 and
212
+ act = action_table[i] and
213
+ action_check[i] == @racc_state[-1]
214
+ act = action_default[@racc_state[-1]]
215
+ end
216
+ while act = _racc_evalact(act, arg)
217
+ ;
218
+ end
219
+ end
220
+ end
221
+ }
222
+ end
223
+
224
+ ###
225
+ ### common
226
+ ###
227
+
228
+ def _racc_evalact(act, arg)
229
+ action_table, action_check, _, action_pointer,
230
+ _, _, _, _,
231
+ _, _, _, shift_n,
232
+ reduce_n, * = arg
233
+ nerr = 0 # tmp
234
+
235
+ if act > 0 and act < shift_n
236
+ #
237
+ # shift
238
+ #
239
+ if @racc_error_status > 0
240
+ @racc_error_status -= 1 unless @racc_t == 1 # error token
241
+ end
242
+ @racc_vstack.push @racc_val
243
+ @racc_state.push act
244
+ @racc_read_next = true
245
+ if @yydebug
246
+ @racc_tstack.push @racc_t
247
+ racc_shift @racc_t, @racc_tstack, @racc_vstack
248
+ end
249
+
250
+ elsif act < 0 and act > -reduce_n
251
+ #
252
+ # reduce
253
+ #
254
+ code = catch(:racc_jump) {
255
+ @racc_state.push _racc_do_reduce(arg, act)
256
+ false
257
+ }
258
+ if code
259
+ case code
260
+ when 1 # yyerror
261
+ @racc_user_yyerror = true # user_yyerror
262
+ return -reduce_n
263
+ when 2 # yyaccept
264
+ return shift_n
265
+ else
266
+ raise '[Racc Bug] unknown jump code'
267
+ end
268
+ end
269
+
270
+ elsif act == shift_n
271
+ #
272
+ # accept
273
+ #
274
+ racc_accept if @yydebug
275
+ throw :racc_end_parse, @racc_vstack[0]
276
+
277
+ elsif act == -reduce_n
278
+ #
279
+ # error
280
+ #
281
+ case @racc_error_status
282
+ when 0
283
+ unless arg[21] # user_yyerror
284
+ nerr += 1
285
+ on_error @racc_t, @racc_val, @racc_vstack
286
+ end
287
+ when 3
288
+ if @racc_t == 0 # is $
289
+ throw :racc_end_parse, nil
290
+ end
291
+ @racc_read_next = true
292
+ end
293
+ @racc_user_yyerror = false
294
+ @racc_error_status = 3
295
+ while true
296
+ if i = action_pointer[@racc_state[-1]]
297
+ i += 1 # error token
298
+ if i >= 0 and
299
+ (act = action_table[i]) and
300
+ action_check[i] == @racc_state[-1]
301
+ break
302
+ end
303
+ end
304
+ throw :racc_end_parse, nil if @racc_state.size <= 1
305
+ @racc_state.pop
306
+ @racc_vstack.pop
307
+ if @yydebug
308
+ @racc_tstack.pop
309
+ racc_e_pop @racc_state, @racc_tstack, @racc_vstack
310
+ end
311
+ end
312
+ return act
313
+
314
+ else
315
+ raise "[Racc Bug] unknown action #{act.inspect}"
316
+ end
317
+
318
+ racc_next_state(@racc_state[-1], @racc_state) if @yydebug
319
+
320
+ nil
321
+ end
322
+
323
+ def _racc_do_reduce(arg, act)
324
+ _, _, _, _,
325
+ goto_table, goto_check, goto_default, goto_pointer,
326
+ nt_base, reduce_table, _, _,
327
+ _, use_result, * = arg
328
+
329
+ state = @racc_state
330
+ vstack = @racc_vstack
331
+ tstack = @racc_tstack
332
+
333
+ i = act * -3
334
+ len = reduce_table[i]
335
+ reduce_to = reduce_table[i+1]
336
+ method_id = reduce_table[i+2]
337
+ void_array = []
338
+
339
+ tmp_t = tstack[-len, len] if @yydebug
340
+ tmp_v = vstack[-len, len]
341
+ tstack[-len, len] = void_array if @yydebug
342
+ vstack[-len, len] = void_array
343
+ state[-len, len] = void_array
344
+
345
+ # tstack must be updated AFTER method call
346
+ if use_result
347
+ vstack.push __send__(method_id, tmp_v, vstack, tmp_v[0])
348
+ else
349
+ vstack.push __send__(method_id, tmp_v, vstack)
350
+ end
351
+ tstack.push reduce_to
352
+
353
+ racc_reduce(tmp_t, reduce_to, tstack, vstack) if @yydebug
354
+
355
+ k1 = reduce_to - nt_base
356
+ if i = goto_pointer[k1]
357
+ i += state[-1]
358
+ if i >= 0 and (curstate = goto_table[i]) and goto_check[i] == k1
359
+ return curstate
360
+ end
361
+ end
362
+ goto_default[k1]
363
+ end
364
+
365
+ # This method is called when a parse error is found.
366
+ #
367
+ # ERROR_TOKEN_ID is an internal ID of token which caused error.
368
+ # You can get string representation of this ID by calling
369
+ # #token_to_str.
370
+ #
371
+ # ERROR_VALUE is a value of error token.
372
+ #
373
+ # value_stack is a stack of symbol values.
374
+ # DO NOT MODIFY this object.
375
+ #
376
+ # This method raises ParseError by default.
377
+ #
378
+ # If this method returns, parsers enter "error recovering mode".
379
+ def on_error(t, val, vstack)
380
+ raise ParseError, sprintf("\nparse error on value %s (%s)",
381
+ val.inspect, token_to_str(t) || '?')
382
+ end
383
+
384
+ # Enter error recovering mode.
385
+ # This method does not call #on_error.
386
+ def yyerror
387
+ throw :racc_jump, 1
388
+ end
389
+
390
+ # Exit parser.
391
+ # Return value is Symbol_Value_Stack[0].
392
+ def yyaccept
393
+ throw :racc_jump, 2
394
+ end
395
+
396
+ # Leave error recovering mode.
397
+ def yyerrok
398
+ @racc_error_status = 0
399
+ end
400
+
401
+ # For debugging output
402
+ def racc_read_token(t, tok, val)
403
+ @racc_debug_out.print 'read '
404
+ @racc_debug_out.print tok.inspect, '(', racc_token2str(t), ') '
405
+ @racc_debug_out.puts val.inspect
406
+ @racc_debug_out.puts
407
+ end
408
+
409
+ def racc_shift(tok, tstack, vstack)
410
+ @racc_debug_out.puts "shift #{racc_token2str tok}"
411
+ racc_print_stacks tstack, vstack
412
+ @racc_debug_out.puts
413
+ end
414
+
415
+ def racc_reduce(toks, sim, tstack, vstack)
416
+ out = @racc_debug_out
417
+ out.print 'reduce '
418
+ if toks.empty?
419
+ out.print ' <none>'
420
+ else
421
+ toks.each {|t| out.print ' ', racc_token2str(t) }
422
+ end
423
+ out.puts " --> #{racc_token2str(sim)}"
424
+ racc_print_stacks tstack, vstack
425
+ @racc_debug_out.puts
426
+ end
427
+
428
+ def racc_accept
429
+ @racc_debug_out.puts 'accept'
430
+ @racc_debug_out.puts
431
+ end
432
+
433
+ def racc_e_pop(state, tstack, vstack)
434
+ @racc_debug_out.puts 'error recovering mode: pop token'
435
+ racc_print_states state
436
+ racc_print_stacks tstack, vstack
437
+ @racc_debug_out.puts
438
+ end
439
+
440
+ def racc_next_state(curstate, state)
441
+ @racc_debug_out.puts "goto #{curstate}"
442
+ racc_print_states state
443
+ @racc_debug_out.puts
444
+ end
445
+
446
+ def racc_print_stacks(t, v)
447
+ out = @racc_debug_out
448
+ out.print ' ['
449
+ t.each_index do |i|
450
+ out.print ' (', racc_token2str(t[i]), ' ', v[i].inspect, ')'
451
+ end
452
+ out.puts ' ]'
453
+ end
454
+
455
+ def racc_print_states(s)
456
+ out = @racc_debug_out
457
+ out.print ' ['
458
+ s.each {|st| out.print ' ', st }
459
+ out.puts ' ]'
460
+ end
461
+
462
+ def racc_token2str(tok)
463
+ self.class::Racc_token_to_s_table[tok] or
464
+ raise "[Racc Bug] can't convert token #{tok} to string"
465
+ end
466
+
467
+ # Convert internal ID of token symbol to the string.
468
+ def token_to_str(t)
469
+ self.class::Racc_token_to_s_table[t]
470
+ end
471
+
472
+ end
473
+
474
+ end