racc 1.4.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (109) hide show
  1. data/.gitattributes +2 -0
  2. data/.gitignore +7 -0
  3. data/COPYING +515 -0
  4. data/ChangeLog +846 -0
  5. data/DEPENDS +4 -0
  6. data/README.en.rdoc +86 -0
  7. data/README.ja.rdoc +96 -0
  8. data/Rakefile +15 -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/doc/en/NEWS.en.rdoc +282 -0
  14. data/doc/en/command.en.html +78 -0
  15. data/doc/en/debug.en.rdoc +20 -0
  16. data/doc/en/grammar.en.rdoc +230 -0
  17. data/doc/en/index.en.html +10 -0
  18. data/doc/en/parser.en.rdoc +74 -0
  19. data/doc/en/usage.en.html +92 -0
  20. data/doc/ja/NEWS.ja.rdoc +307 -0
  21. data/doc/ja/command.ja.html +94 -0
  22. data/doc/ja/debug.ja.rdoc +36 -0
  23. data/doc/ja/grammar.ja.rdoc +348 -0
  24. data/doc/ja/index.ja.html +10 -0
  25. data/doc/ja/parser.ja.rdoc +125 -0
  26. data/doc/ja/usage.ja.html +414 -0
  27. data/ext/racc/cparse/MANIFEST +4 -0
  28. data/ext/racc/cparse/cparse.c +824 -0
  29. data/ext/racc/cparse/depend +1 -0
  30. data/ext/racc/cparse/extconf.rb +7 -0
  31. data/fastcache/extconf.rb +2 -0
  32. data/fastcache/fastcache.c +185 -0
  33. data/lib/racc.rb +6 -0
  34. data/lib/racc/compat.rb +40 -0
  35. data/lib/racc/debugflags.rb +59 -0
  36. data/lib/racc/exception.rb +15 -0
  37. data/lib/racc/grammar.rb +1115 -0
  38. data/lib/racc/grammarfileparser.rb +559 -0
  39. data/lib/racc/info.rb +16 -0
  40. data/lib/racc/iset.rb +91 -0
  41. data/lib/racc/logfilegenerator.rb +214 -0
  42. data/lib/racc/parser.rb +439 -0
  43. data/lib/racc/parserfilegenerator.rb +511 -0
  44. data/lib/racc/pre-setup +13 -0
  45. data/lib/racc/sourcetext.rb +34 -0
  46. data/lib/racc/state.rb +971 -0
  47. data/lib/racc/statetransitiontable.rb +316 -0
  48. data/lib/racc/static.rb +5 -0
  49. data/misc/dist.sh +31 -0
  50. data/sample/array.y +67 -0
  51. data/sample/array2.y +59 -0
  52. data/sample/calc-ja.y +66 -0
  53. data/sample/calc.y +65 -0
  54. data/sample/conflict.y +15 -0
  55. data/sample/hash.y +60 -0
  56. data/sample/lalr.y +17 -0
  57. data/sample/lists.y +57 -0
  58. data/sample/syntax.y +46 -0
  59. data/sample/yyerr.y +46 -0
  60. data/setup.rb +1587 -0
  61. data/tasks/doc.rb +12 -0
  62. data/tasks/email.rb +55 -0
  63. data/tasks/file.rb +37 -0
  64. data/tasks/gem.rb +37 -0
  65. data/tasks/test.rb +16 -0
  66. data/test/assets/chk.y +126 -0
  67. data/test/assets/conf.y +16 -0
  68. data/test/assets/digraph.y +29 -0
  69. data/test/assets/echk.y +118 -0
  70. data/test/assets/err.y +60 -0
  71. data/test/assets/expect.y +7 -0
  72. data/test/assets/firstline.y +4 -0
  73. data/test/assets/ichk.y +102 -0
  74. data/test/assets/intp.y +546 -0
  75. data/test/assets/mailp.y +437 -0
  76. data/test/assets/newsyn.y +25 -0
  77. data/test/assets/noend.y +4 -0
  78. data/test/assets/nonass.y +41 -0
  79. data/test/assets/normal.y +27 -0
  80. data/test/assets/norule.y +4 -0
  81. data/test/assets/nullbug1.y +25 -0
  82. data/test/assets/nullbug2.y +15 -0
  83. data/test/assets/opt.y +123 -0
  84. data/test/assets/percent.y +35 -0
  85. data/test/assets/recv.y +97 -0
  86. data/test/assets/rrconf.y +14 -0
  87. data/test/assets/scan.y +72 -0
  88. data/test/assets/syntax.y +50 -0
  89. data/test/assets/unterm.y +5 -0
  90. data/test/assets/useless.y +12 -0
  91. data/test/assets/yyerr.y +46 -0
  92. data/test/bench.y +36 -0
  93. data/test/helper.rb +88 -0
  94. data/test/infini.y +8 -0
  95. data/test/scandata/brace +7 -0
  96. data/test/scandata/gvar +1 -0
  97. data/test/scandata/normal +4 -0
  98. data/test/scandata/percent +18 -0
  99. data/test/scandata/slash +10 -0
  100. data/test/src.intp +34 -0
  101. data/test/start.y +20 -0
  102. data/test/test_chk_y.rb +51 -0
  103. data/test/test_grammar_file_parser.rb +15 -0
  104. data/test/test_racc_command.rb +155 -0
  105. data/test/test_scan_y.rb +51 -0
  106. data/test/testscanner.rb +51 -0
  107. data/web/racc.en.rhtml +42 -0
  108. data/web/racc.ja.rhtml +51 -0
  109. metadata +166 -0
@@ -0,0 +1,50 @@
1
+ #
2
+ # racc syntax checker
3
+ #
4
+
5
+ class M1::M2::ParserClass < S1::S2::SuperClass
6
+
7
+ token A
8
+ | B C
9
+
10
+ convert
11
+ A '5'
12
+ end
13
+
14
+ prechigh
15
+ left B
16
+ preclow
17
+
18
+ start target
19
+
20
+ expect 0
21
+
22
+ rule
23
+
24
+ target: A B C
25
+ {
26
+ print 'abc'
27
+ }
28
+ | B C A
29
+ | C B A
30
+ {
31
+ print 'cba'
32
+ }
33
+ | cont
34
+
35
+ cont : A c2 B c2 C
36
+
37
+ c2 : C C C C C
38
+
39
+ end
40
+
41
+ ---- inner
42
+
43
+ junk code !!!!
44
+
45
+ kjaljlajrlaolanbla /// %%% (*((( token rule
46
+ akiurtlajluealjflaj @@@@ end end end end __END__
47
+ laieu2o879urkq96ga(Q#*&%Q#
48
+ #&lkji END
49
+
50
+ q395q?/// liutjqlkr7
@@ -0,0 +1,5 @@
1
+ # unterminated action
2
+
3
+ class A
4
+ rule
5
+ a: A {
@@ -0,0 +1,12 @@
1
+
2
+
3
+ class A
4
+ token A B C X
5
+ rule
6
+
7
+ targ : A list B
8
+ | A B C
9
+
10
+ list: list X
11
+
12
+ end
@@ -0,0 +1,46 @@
1
+ #
2
+ # yyerror/yyerrok/yyaccept test
3
+ #
4
+
5
+ class A
6
+ rule
7
+
8
+ target: a b c
9
+
10
+ a:
11
+ {
12
+ yyerror
13
+ raise ArgumentError, "yyerror failed"
14
+ }
15
+ | error
16
+
17
+ b:
18
+ {
19
+ yyerrok
20
+ }
21
+
22
+ c:
23
+ {
24
+ yyaccept
25
+ raise ArgumentError, "yyaccept failed"
26
+ }
27
+
28
+ end
29
+
30
+ ---- inner
31
+
32
+ def parse
33
+ do_parse
34
+ end
35
+
36
+ def next_token
37
+ [false, '$end']
38
+ end
39
+
40
+ def on_error( *args )
41
+ $stderr.puts "on_error called: args=#{args.inspect}"
42
+ end
43
+
44
+ ---- footer
45
+
46
+ A.new.parse
@@ -0,0 +1,36 @@
1
+ class BenchmarkParser
2
+
3
+ rule
4
+
5
+ target: a a a a a a a a a a;
6
+ a: b b b b b b b b b b;
7
+ b: c c c c c c c c c c;
8
+ c: d d d d d d d d d d;
9
+ d: e e e e e e e e e e;
10
+
11
+ end
12
+
13
+ ---- inner
14
+
15
+ def initialize
16
+ @old = [ :e, 'e' ]
17
+ @i = 0
18
+ end
19
+
20
+ def next_token
21
+ return [false, '$'] if @i >= 10_0000
22
+ @i += 1
23
+ @old
24
+ end
25
+
26
+ def parse
27
+ do_parse
28
+ end
29
+
30
+ ---- footer
31
+
32
+ require 'benchmark'
33
+
34
+ Benchmark.bm do |x|
35
+ x.report { BenchmarkParser.new.parse }
36
+ end
@@ -0,0 +1,88 @@
1
+ $VERBOSE = true
2
+ require 'test/unit'
3
+ require 'racc/static'
4
+ require 'fileutils'
5
+
6
+ module Racc
7
+ class TestCase < Test::Unit::TestCase
8
+ PROJECT_DIR = File.expand_path(File.join(File.dirname(__FILE__), '..'))
9
+
10
+ TEST_DIR = File.join(PROJECT_DIR, 'test')
11
+
12
+ RACC = File.join(PROJECT_DIR, 'bin', 'racc')
13
+ OUT_DIR = File.join(TEST_DIR, 'out')
14
+ TAB_DIR = File.join(TEST_DIR, 'tab')
15
+ LOG_DIR = File.join(TEST_DIR, 'log')
16
+ ERR_DIR = File.join(TEST_DIR, 'err')
17
+ ASSET_DIR = File.join(TEST_DIR, 'assets')
18
+
19
+ INC = [
20
+ File.join(PROJECT_DIR, 'lib'),
21
+ File.join(PROJECT_DIR, 'ext'),
22
+ ].join(':')
23
+
24
+ unless RUBY_VERSION >= '1.9'
25
+ undef :default_test
26
+ end
27
+
28
+ def setup
29
+ [OUT_DIR, TAB_DIR, LOG_DIR, ERR_DIR].each do |dir|
30
+ FileUtils.mkdir_p(dir)
31
+ end
32
+ end
33
+
34
+ def teardown
35
+ [OUT_DIR, TAB_DIR, LOG_DIR, ERR_DIR].each do |dir|
36
+ FileUtils.rm_rf(dir)
37
+ end
38
+ end
39
+
40
+ def assert_compile asset, args = []
41
+ asset = File.basename(asset, '.y')
42
+ args = ([args].flatten) + [
43
+ "#{ASSET_DIR}/#{asset}.y",
44
+ '-Do',
45
+ "-O#{OUT_DIR}/#{asset}",
46
+ "-o#{TAB_DIR}/#{asset}",
47
+ ]
48
+ racc "#{args.join(' ')}"
49
+ end
50
+
51
+ def assert_debugfile asset, ok
52
+ name = File.basename(asset, '.y')
53
+ Dir.chdir(TEST_DIR) do
54
+ File.foreach("log/#{name}.y") do |line|
55
+ line.strip!
56
+ case line
57
+ when /sr/ then assert_equal "sr#{ok[0]}", line
58
+ when /rr/ then assert_equal "rr#{ok[1]}", line
59
+ when /un/ then assert_equal "un#{ok[2]}", line
60
+ when /ur/ then assert_equal "ur#{ok[3]}", line
61
+ when /ex/ then assert_equal "ex#{ok[4]}", line
62
+ else
63
+ raise TestFailed, 'racc outputs unknown debug report???'
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+ def assert_exec file
70
+ file = File.basename(file, '.y')
71
+ Dir.chdir(TEST_DIR) do
72
+ ruby("tab/#{file}")
73
+ end
74
+ end
75
+
76
+ def racc arg
77
+ ruby "-S #{RACC} #{arg}"
78
+ end
79
+
80
+ def ruby arg
81
+ Dir.chdir(TEST_DIR) do
82
+ cmd = "#{ENV['_']} -I #{INC} #{arg} 2>>/tmp/out"
83
+ result = system(cmd)
84
+ result ? assert(result) : raise(cmd)
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,8 @@
1
+
2
+ class I
3
+
4
+ rule
5
+
6
+ list: list X
7
+
8
+ end
@@ -0,0 +1,7 @@
1
+ { {
2
+ } { } {
3
+ { { { } } }
4
+ { { { {} } } }
5
+ {} {} {}
6
+ }
7
+ }
@@ -0,0 +1 @@
1
+ { $' $" $& $-a $/ $\ $( $1 $2 $3 $? $-i }
@@ -0,0 +1,4 @@
1
+ {
2
+ # comment
3
+ result = "string".match(/regexp/)[0]
4
+ }
@@ -0,0 +1,18 @@
1
+ {
2
+ 3 % 5 # mod
3
+ 3%5 # mod
4
+ 3% 5 # mod
5
+ i % 5 # mod
6
+ i%5 # mod
7
+ i% 5 # mod
8
+ call %{str} # string
9
+ call(%{str}) # string
10
+ %q{string} # string
11
+ %Q{string} # string
12
+ %r{string} # string
13
+ %w(array) # array
14
+ %x{array} # command string
15
+ %{string} # string
16
+ %_string_ # string
17
+ %/string/ # regexp
18
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ # here's many '/'s
3
+ i = 5/1 # div
4
+ re = /regex/ # regexp
5
+ i /= 5 # div
6
+ result = 5 / 1 # div
7
+ result = 5/ 1 # div
8
+ call(/regex/) # regexp
9
+ call /regex/ # regexp
10
+ }
@@ -0,0 +1,34 @@
1
+ def assert( no, cond )
2
+ if cond then
3
+ else
4
+ raise( 'assert ' + to_s(no) + ' failed' )
5
+ end
6
+ end
7
+
8
+ assert( 1, concat(concat(concat('str=', 'a'), "b"), 'c') == 'str=abc' )
9
+ assert( 2, 'operator' + ' ok' == 'operator ok' )
10
+ assert( 3, 1 + 1 == 2 )
11
+ assert( 4, 4 * 1 + 10 * 1 == 14 )
12
+
13
+ if true then
14
+ assert( 5, true )
15
+ else
16
+ assert( 6, false )
17
+ end
18
+
19
+ i = 1
20
+ while i == 1 do
21
+ i = false
22
+ end
23
+ assert( 7, i == false )
24
+ assert( 8, nil == nil )
25
+
26
+ def func
27
+ assert( 9, true )
28
+ end
29
+ func
30
+
31
+ def argfunc( str )
32
+ assert( 10, str == 'ok' )
33
+ end
34
+ argfunc 'ok'
@@ -0,0 +1,20 @@
1
+ class S
2
+
3
+ start st
4
+
5
+ rule
6
+
7
+ n: D { result = 'no' }
8
+ st : A B C n { result = 'ok' }
9
+
10
+ end
11
+
12
+ ---- inner
13
+
14
+ def parse
15
+ do_parse
16
+ end
17
+
18
+ ---- footer
19
+
20
+ S.new.parse == 'ok' or raise 'start stmt not worked'
@@ -0,0 +1,51 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
2
+
3
+ module Racc
4
+ class TestChkY < TestCase
5
+ def setup
6
+ file = File.join(ASSET_DIR, 'chk.y')
7
+ @debug_flags = Racc::DebugFlags.parse_option_string('o')
8
+ parser = Racc::GrammarFileParser.new(@debug_flags)
9
+ @result = parser.parse(File.read(file), File.basename(file))
10
+ @states = Racc::States.new(@result.grammar).nfa
11
+ @states.dfa
12
+ end
13
+
14
+ def test_compile_chk_y
15
+ generator = Racc::ParserFileGenerator.new(@states, @result.params.dup)
16
+
17
+ fork {
18
+ eval(generator.generate_parser)
19
+ }
20
+ Process.wait
21
+ assert_equal 0, $?.exitstatus
22
+
23
+ grammar = @states.grammar
24
+
25
+ assert_equal 0, @states.n_srconflicts
26
+ assert_equal 0, @states.n_rrconflicts
27
+ assert_equal 0, grammar.n_useless_nonterminals
28
+ assert_equal 0, grammar.n_useless_rules
29
+ assert_nil grammar.n_expected_srconflicts
30
+ end
31
+
32
+ def test_compile_chk_y_line_convert
33
+ params = @result.params.dup
34
+ params.convert_line_all = true
35
+
36
+ generator = Racc::ParserFileGenerator.new(@states, @result.params.dup)
37
+
38
+ fork { eval(generator.generate_parser) }
39
+ assert_equal 0, $?.exitstatus
40
+ Process.wait
41
+
42
+ grammar = @states.grammar
43
+
44
+ assert_equal 0, @states.n_srconflicts
45
+ assert_equal 0, @states.n_rrconflicts
46
+ assert_equal 0, grammar.n_useless_nonterminals
47
+ assert_equal 0, grammar.n_useless_rules
48
+ assert_nil grammar.n_expected_srconflicts
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,15 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
2
+
3
+ module Racc
4
+ class TestGrammarFileParser < TestCase
5
+ def test_parse
6
+ file = File.join(ASSET_DIR, 'yyerr.y')
7
+
8
+ debug_flags = Racc::DebugFlags.parse_option_string('o')
9
+ assert debug_flags.status_logging
10
+
11
+ parser = Racc::GrammarFileParser.new(debug_flags)
12
+ parser.parse(File.read(file), File.basename(file))
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,155 @@
1
+ require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))
2
+
3
+ module Racc
4
+ class TestRaccCommand < TestCase
5
+ def test_syntax_y
6
+ assert_compile 'syntax.y', '-v'
7
+ assert_debugfile 'syntax.y', [0,0,0,0,0]
8
+ end
9
+
10
+ def test_percent_y
11
+ assert_compile 'percent.y'
12
+ assert_debugfile 'percent.y', []
13
+ assert_exec 'percent.y'
14
+ end
15
+
16
+ def test_scan_y
17
+ assert_compile 'scan.y'
18
+ assert_debugfile 'scan.y', []
19
+ assert_exec 'scan.y'
20
+ end
21
+
22
+ def test_newsyn_y
23
+ assert_compile 'newsyn.y'
24
+ assert_debugfile 'newsyn.y', []
25
+ end
26
+
27
+ def test_normal_y
28
+ assert_compile 'normal.y'
29
+ assert_debugfile 'normal.y', []
30
+
31
+ assert_compile 'normal.y', '-vg'
32
+ assert_debugfile 'normal.y', []
33
+ end
34
+
35
+ def test_chk_y
36
+ assert_compile 'chk.y', '-vg'
37
+ assert_debugfile 'chk.y', []
38
+ assert_exec 'chk.y'
39
+
40
+ assert_compile 'chk.y', '--line-convert-all'
41
+ assert_debugfile 'chk.y', []
42
+ assert_exec 'chk.y'
43
+ end
44
+
45
+ def test_echk_y
46
+ assert_compile 'echk.y', '-E'
47
+ assert_debugfile 'echk.y', []
48
+ assert_exec 'echk.y'
49
+ end
50
+
51
+ def test_err_y
52
+ assert_compile 'err.y'
53
+ assert_debugfile 'err.y', []
54
+ assert_exec 'err.y'
55
+ end
56
+
57
+ def test_mailp_y
58
+ assert_compile 'mailp.y'
59
+ assert_debugfile 'mailp.y', []
60
+ end
61
+
62
+ def test_conf_y
63
+ assert_compile 'conf.y', '-v'
64
+ assert_debugfile 'conf.y', [4,1,1,2]
65
+ end
66
+
67
+ def test_rrconf_y
68
+ assert_compile 'rrconf.y'
69
+ assert_debugfile 'rrconf.y', [1,1,0,0]
70
+ end
71
+
72
+ def test_useless_y
73
+ assert_compile 'useless.y'
74
+ assert_debugfile 'useless.y', [0,0,1,2]
75
+ end
76
+
77
+ def test_opt_y
78
+ assert_compile 'opt.y'
79
+ assert_debugfile 'opt.y', []
80
+ assert_exec 'opt.y'
81
+ end
82
+
83
+ def test_yyerr_y
84
+ assert_compile 'yyerr.y'
85
+ assert_debugfile 'yyerr.y', []
86
+ assert_exec 'yyerr.y'
87
+ end
88
+
89
+ def test_recv_y
90
+ assert_compile 'recv.y'
91
+ assert_debugfile 'recv.y', [5,10,1,4]
92
+ end
93
+
94
+ def test_ichk_y
95
+ assert_compile 'ichk.y'
96
+ assert_debugfile 'ichk.y', []
97
+ assert_exec 'ichk.y'
98
+ end
99
+
100
+ def test_intp_y
101
+ assert_compile 'intp.y'
102
+ assert_debugfile 'intp.y', []
103
+ assert_exec 'intp.y'
104
+ end
105
+
106
+ def test_expect_y
107
+ assert_compile 'expect.y'
108
+ assert_debugfile 'expect.y', [1,0,0,0,1]
109
+ end
110
+
111
+ def test_nullbug1_y
112
+ assert_compile 'nullbug1.y'
113
+ assert_debugfile 'nullbug1.y', [0,0,0,0]
114
+ end
115
+
116
+ def test_nullbug2_y
117
+ assert_compile 'nullbug2.y'
118
+ assert_debugfile 'nullbug2.y', [0,0,0,0]
119
+ end
120
+
121
+ def test_firstline_y
122
+ assert_compile 'firstline.y'
123
+ assert_debugfile 'firstline.y', []
124
+ end
125
+
126
+ def test_nonass_y
127
+ assert_compile 'nonass.y'
128
+ assert_debugfile 'nonass.y', []
129
+ assert_exec 'nonass.y'
130
+ end
131
+
132
+ def test_digraph_y
133
+ assert_compile 'digraph.y'
134
+ assert_debugfile 'digraph.y', []
135
+ assert_exec 'digraph.y'
136
+ end
137
+
138
+ def test_noend_y
139
+ assert_compile 'noend.y'
140
+ assert_debugfile 'noend.y', []
141
+ end
142
+
143
+ def test_norule_y
144
+ assert_raises(RuntimeError) {
145
+ assert_compile 'norule.y'
146
+ }
147
+ end
148
+
149
+ def test_unterm_y
150
+ assert_raises(RuntimeError) {
151
+ assert_compile 'unterm.y'
152
+ }
153
+ end
154
+ end
155
+ end