antelope 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +23 -0
  3. data/.rspec +3 -0
  4. data/.yardopts +4 -0
  5. data/Gemfile +7 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +29 -0
  8. data/Rakefile +2 -0
  9. data/antelope.gemspec +30 -0
  10. data/bin/antelope +24 -0
  11. data/examples/deterministic.ace +27 -0
  12. data/examples/deterministic.output +229 -0
  13. data/examples/example.ace +45 -0
  14. data/examples/example.output +610 -0
  15. data/examples/simple.ace +26 -0
  16. data/examples/simple.output +194 -0
  17. data/lib/antelope/ace/compiler.rb +290 -0
  18. data/lib/antelope/ace/errors.rb +27 -0
  19. data/lib/antelope/ace/grammar/generation.rb +47 -0
  20. data/lib/antelope/ace/grammar/loading.rb +51 -0
  21. data/lib/antelope/ace/grammar/presidence.rb +59 -0
  22. data/lib/antelope/ace/grammar/production.rb +47 -0
  23. data/lib/antelope/ace/grammar/productions.rb +119 -0
  24. data/lib/antelope/ace/grammar/terminals.rb +41 -0
  25. data/lib/antelope/ace/grammar.rb +59 -0
  26. data/lib/antelope/ace/presidence.rb +51 -0
  27. data/lib/antelope/ace/scanner/first.rb +61 -0
  28. data/lib/antelope/ace/scanner/second.rb +160 -0
  29. data/lib/antelope/ace/scanner/third.rb +25 -0
  30. data/lib/antelope/ace/scanner.rb +110 -0
  31. data/lib/antelope/ace/token/epsilon.rb +22 -0
  32. data/lib/antelope/ace/token/error.rb +24 -0
  33. data/lib/antelope/ace/token/nonterminal.rb +15 -0
  34. data/lib/antelope/ace/token/terminal.rb +15 -0
  35. data/lib/antelope/ace/token.rb +171 -0
  36. data/lib/antelope/ace.rb +50 -0
  37. data/lib/antelope/automaton.rb +36 -0
  38. data/lib/antelope/generation/conflictor/conflict.rb +7 -0
  39. data/lib/antelope/generation/conflictor.rb +45 -0
  40. data/lib/antelope/generation/constructor/first.rb +52 -0
  41. data/lib/antelope/generation/constructor/follow.rb +46 -0
  42. data/lib/antelope/generation/constructor/lookahead.rb +42 -0
  43. data/lib/antelope/generation/constructor/nullable.rb +40 -0
  44. data/lib/antelope/generation/constructor.rb +81 -0
  45. data/lib/antelope/generation/recognizer/rule.rb +93 -0
  46. data/lib/antelope/generation/recognizer/state.rb +56 -0
  47. data/lib/antelope/generation/recognizer.rb +152 -0
  48. data/lib/antelope/generation/tableizer.rb +80 -0
  49. data/lib/antelope/generation.rb +12 -0
  50. data/lib/antelope/generator/output.rb +30 -0
  51. data/lib/antelope/generator/ruby.rb +57 -0
  52. data/lib/antelope/generator/templates/output.erb +49 -0
  53. data/lib/antelope/generator/templates/ruby.erb +62 -0
  54. data/lib/antelope/generator.rb +84 -0
  55. data/lib/antelope/version.rb +4 -0
  56. data/lib/antelope.rb +9 -0
  57. data/spec/antelope/ace/compiler_spec.rb +50 -0
  58. data/spec/antelope/ace/scanner_spec.rb +27 -0
  59. data/spec/antelope/automaton_spec.rb +29 -0
  60. data/spec/spec_helper.rb +38 -0
  61. data/spec/support/benchmark_helper.rb +5 -0
  62. metadata +223 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b8be2515bbd6f23ce46428a4e16b2c6f768ba50b
4
+ data.tar.gz: e89dee7b835a49a2d2f06ae30a8e2bbf506a6f53
5
+ SHA512:
6
+ metadata.gz: 17a9544c4be3edbf56c002f892192e33400bb45c0592aebef58d130beaa743bacff7bd96e734397c7b327f7a45aabee7e5d99f7f5b86afd3c2dbb580fcce9d9f
7
+ data.tar.gz: 3213b6b2ca1756ad32075e6924098212d53ff00e8240be9726b71b5d7834647bc73a69c461b611a1c56b342dfdcab0b64d257e28cf17f18fa5d2c160f2accb7f
data/.gitignore ADDED
@@ -0,0 +1,23 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ examples/*.rb
18
+ tmp
19
+ *.bundle
20
+ *.so
21
+ *.o
22
+ *.a
23
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ -f d
3
+ --require spec_helper.rb
data/.yardopts ADDED
@@ -0,0 +1,4 @@
1
+ --protected
2
+ -m markdown
3
+ -
4
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in antelope.gemspec
4
+ gemspec
5
+
6
+ gem 'coveralls'
7
+ gem 'simplecov'
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Jeremy Rodi
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Antelope
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'antelope'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install antelope
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it (<https://github.com/medcat/antelope/fork>)
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
data/antelope.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'antelope/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "antelope"
8
+ spec.version = Antelope::VERSION
9
+ spec.authors = ["Jeremy Rodi"]
10
+ spec.email = ["redjazz96@gmail.com"]
11
+ spec.summary = %q{A compiler compiler, written in ruby.}
12
+ spec.description = %q{A compiler compiler, written in ruby.}
13
+ spec.homepage = "https://github.com/medcat/antelope"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "liquid", "~> 2.6"
22
+ spec.add_dependency "sourcify", "~> 0.5"
23
+ spec.add_dependency "hashie", "~> 3.0"
24
+ spec.add_dependency "thor", "~> 0.19"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.6"
27
+ spec.add_development_dependency "rake"
28
+ spec.add_development_dependency "rspec"
29
+ spec.add_development_dependency "rspec-its"
30
+ end
data/bin/antelope ADDED
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $: << File.expand_path("../../lib", __FILE__) << Dir.pwd
4
+ require "antelope"
5
+ require "pathname"
6
+
7
+ ARGV.each do |program|
8
+ grammar = Antelope::Ace::Grammar.from_file(program)
9
+
10
+ out = grammar.generate
11
+
12
+ #pass = Hash[[:recognizer,
13
+ # :constructor,
14
+ # :conflictor,
15
+ # :table].zip(out)]
16
+
17
+ #program = Pathname.new(program)
18
+
19
+ #file_name = program.parent + program.basename(".ace")
20
+
21
+ #File.open("#{file_name}.output", "w") do |f|
22
+ # f.write Antelope::Output.output(grammar, pass)
23
+ #end
24
+ end
@@ -0,0 +1,27 @@
1
+ %{
2
+ require "antelope"
3
+ %}
4
+
5
+ %require "0.0.1"
6
+ %type "ruby"
7
+
8
+ %terminal NUMBER
9
+ %terminal SEMICOLON ";"
10
+ %terminal ADD "+"
11
+ %terminal LPAREN "("
12
+ %terminal RPAREN ")"
13
+
14
+ %%
15
+
16
+ s: e
17
+ e: t SEMICOLON
18
+ | t ADD e
19
+
20
+ t: NUMBER
21
+ | LPAREN e RPAREN
22
+
23
+ %%
24
+
25
+ class DeterministicParser < Antelope::Parser
26
+ %{write}
27
+ end
@@ -0,0 +1,229 @@
1
+ Productions:
2
+ 0/n0: $start(0) → s(0:1) $
3
+ 1/n1: s(0:1) → e(0:2)
4
+ 2/n1: e(9:11) → t(0:3) ";"
5
+ 3/n1: e(9:11) → t(0:3) "+" e(9:11)
6
+ 4/n1: t(9:3) → NUMBER
7
+ 5/n1: t(9:3) → "(" e(6:10) ")"
8
+ 13/n1: e(9:11) → t(6:3) ";"
9
+ 14/n1: e(9:11) → t(6:3) "+" e(9:11)
10
+ 20/n1: e(9:11) → t(9:3) ";"
11
+ 21/n1: e(9:11) → t(9:3) "+" e(9:11)
12
+
13
+ Original Productions:
14
+ s → e
15
+
16
+ e → t ";"
17
+
18
+ e → t "+" e
19
+
20
+ t → NUMBER
21
+
22
+ t → "(" e ")"
23
+
24
+ $start → s $
25
+
26
+
27
+ Conflicts:
28
+
29
+ Presidence:
30
+ --- highest
31
+ nonassoc 1:
32
+ {_}
33
+ nonassoc 0:
34
+ {$}
35
+ --- lowest
36
+
37
+ Table:
38
+ {0=>
39
+ {:s=>[:state, 1],
40
+ :e=>[:state, 2],
41
+ :t=>[:state, 3],
42
+ :NUMBER=>[:state, 5],
43
+ :LPAREN=>[:state, 6]},
44
+ 1=>{:"$"=>[:state, 7]},
45
+ 2=>{:"$"=>[:reduce, 1]},
46
+ 3=>{:SEMICOLON=>[:state, 8], :ADD=>[:state, 9]},
47
+ 4=>{:ADD=>[:state, 9]},
48
+ 5=>{:SEMICOLON=>[:reduce, 4], :ADD=>[:reduce, 4]},
49
+ 6=>
50
+ {:e=>[:state, 10],
51
+ :t=>[:state, 3],
52
+ :NUMBER=>[:state, 5],
53
+ :LPAREN=>[:state, 6]},
54
+ 7=>{:"$"=>[:accept, 0]},
55
+ 8=>{:"$"=>[:reduce, 2], :RPAREN=>[:reduce, 2]},
56
+ 9=>
57
+ {:e=>[:state, 11],
58
+ :t=>[:state, 3],
59
+ :NUMBER=>[:state, 5],
60
+ :LPAREN=>[:state, 6]},
61
+ 10=>{:RPAREN=>[:state, 12]},
62
+ 11=>{:"$"=>[:reduce, 3], :RPAREN=>[:reduce, 3]},
63
+ 12=>{:SEMICOLON=>[:reduce, 5], :ADD=>[:reduce, 5]}}
64
+
65
+ [#<Antelope::Generation::Recognizer::Rule id=0 left=$start(0) right=[s(0:1) $] position=0>,
66
+ #<Antelope::Generation::Recognizer::Rule id=1 left=s(0:1) right=[e(0:2)] position=0>,
67
+ #<Antelope::Generation::Recognizer::Rule id=2 left=e(9:11) right=[t(0:3) ";"] position=0>,
68
+ #<Antelope::Generation::Recognizer::Rule id=3 left=e(9:11) right=[t(0:3) "+" e(9:11)] position=0>,
69
+ #<Antelope::Generation::Recognizer::Rule id=4 left=t(9:3) right=[NUMBER] position=0>,
70
+ #<Antelope::Generation::Recognizer::Rule id=5 left=t(9:3) right=["(" e(6:10) ")"] position=0>,
71
+ #<Antelope::Generation::Recognizer::Rule id=6 left=$start(0) right=[s $] position=1>,
72
+ #<Antelope::Generation::Recognizer::Rule id=7 left=s(0:1) right=[e] position=1>,
73
+ #<Antelope::Generation::Recognizer::Rule id=8 left=e(9:11) right=[t ";"] position=1>,
74
+ nil,
75
+ #<Antelope::Generation::Recognizer::Rule id=10 left=e(9:11) right=[t "+" e] position=1>,
76
+ #<Antelope::Generation::Recognizer::Rule id=11 left=t(9:3) right=[NUMBER] position=1>,
77
+ #<Antelope::Generation::Recognizer::Rule id=12 left=t(9:3) right=["(" e ")"] position=1>,
78
+ #<Antelope::Generation::Recognizer::Rule id=13 left=e(9:11) right=[t(6:3) ";"] position=0>,
79
+ #<Antelope::Generation::Recognizer::Rule id=14 left=e(9:11) right=[t(6:3) "+" e(9:11)] position=0>,
80
+ #<Antelope::Generation::Recognizer::Rule id=15 left=t(9:3) right=[NUMBER] position=0>,
81
+ #<Antelope::Generation::Recognizer::Rule id=16 left=t(9:3) right=["(" e(6:10) ")"] position=0>,
82
+ #<Antelope::Generation::Recognizer::Rule id=17 left=$start(0) right=[s $] position=2>,
83
+ #<Antelope::Generation::Recognizer::Rule id=18 left=e(9:11) right=[t ";"] position=2>,
84
+ #<Antelope::Generation::Recognizer::Rule id=19 left=e(9:11) right=[t "+" e] position=2>,
85
+ #<Antelope::Generation::Recognizer::Rule id=20 left=e(9:11) right=[t(9:3) ";"] position=0>,
86
+ #<Antelope::Generation::Recognizer::Rule id=21 left=e(9:11) right=[t(9:3) "+" e(9:11)] position=0>,
87
+ #<Antelope::Generation::Recognizer::Rule id=22 left=t(9:3) right=[NUMBER] position=0>,
88
+ #<Antelope::Generation::Recognizer::Rule id=23 left=t(9:3) right=["(" e(6:10) ")"] position=0>,
89
+ #<Antelope::Generation::Recognizer::Rule id=24 left=t(9:3) right=["(" e ")"] position=2>,
90
+ #<Antelope::Generation::Recognizer::Rule id=25 left=e(9:11) right=[t "+" e] position=3>,
91
+ #<Antelope::Generation::Recognizer::Rule id=26 left=t(9:3) right=["(" e ")"] position=3>]
92
+
93
+ State 0:
94
+ rules:
95
+ 0/n0: $start(0) → • s(0:1) $
96
+ {}
97
+ 1/n1: s(0:1) → • e(0:2)
98
+ {}
99
+ 2/n1: e(9:11) → • t(0:3) ";"
100
+ {}
101
+ 3/n1: e(9:11) → • t(0:3) "+" e(9:11)
102
+ {}
103
+ 4/n1: t(9:3) → • NUMBER
104
+ {}
105
+ 5/n1: t(9:3) → • "(" e(6:10) ")"
106
+ {}
107
+
108
+ transitions:
109
+ s : State 1
110
+ e : State 2
111
+ t : State 3
112
+ NUMBER: State 5
113
+ LPAREN: State 6
114
+
115
+ State 1:
116
+ rules:
117
+ 6/n0: $start(0) → s • $
118
+ {}
119
+
120
+ transitions:
121
+ $: State 7
122
+
123
+ State 2:
124
+ rules:
125
+ 7/n1: s(0:1) → e •
126
+ {$}
127
+
128
+ transitions:
129
+
130
+ State 3:
131
+ rules:
132
+ 8/n1: e(9:11) → t • ";"
133
+ {}
134
+ 10/n1: e(9:11) → t • "+" e
135
+ {}
136
+
137
+ transitions:
138
+ SEMICOLON: State 8
139
+ ADD : State 9
140
+
141
+ State 4:
142
+ rules:
143
+ 10/n1: e(9:11) → t • "+" e
144
+ {}
145
+
146
+ transitions:
147
+ ADD: State 9
148
+
149
+ State 5:
150
+ rules:
151
+ 11/n1: t(9:3) → NUMBER •
152
+ {";", "+"}
153
+
154
+ transitions:
155
+
156
+ State 6:
157
+ rules:
158
+ 12/n1: t(9:3) → "(" • e ")"
159
+ {}
160
+ 13/n1: e(9:11) → • t(6:3) ";"
161
+ {}
162
+ 14/n1: e(9:11) → • t(6:3) "+" e(9:11)
163
+ {}
164
+ 15/n1: t(9:3) → • NUMBER
165
+ {}
166
+ 16/n1: t(9:3) → • "(" e(6:10) ")"
167
+ {}
168
+
169
+ transitions:
170
+ e : State 10
171
+ t : State 3
172
+ NUMBER: State 5
173
+ LPAREN: State 6
174
+
175
+ State 7:
176
+ rules:
177
+ 17/n0: $start(0) → s $ •
178
+ {}
179
+
180
+ transitions:
181
+
182
+ State 8:
183
+ rules:
184
+ 18/n1: e(9:11) → t ";" •
185
+ {$, ")"}
186
+
187
+ transitions:
188
+
189
+ State 9:
190
+ rules:
191
+ 19/n1: e(9:11) → t "+" • e
192
+ {}
193
+ 20/n1: e(9:11) → • t(9:3) ";"
194
+ {}
195
+ 21/n1: e(9:11) → • t(9:3) "+" e(9:11)
196
+ {}
197
+ 22/n1: t(9:3) → • NUMBER
198
+ {}
199
+ 23/n1: t(9:3) → • "(" e(6:10) ")"
200
+ {}
201
+
202
+ transitions:
203
+ e : State 11
204
+ t : State 3
205
+ NUMBER: State 5
206
+ LPAREN: State 6
207
+
208
+ State 10:
209
+ rules:
210
+ 24/n1: t(9:3) → "(" e • ")"
211
+ {}
212
+
213
+ transitions:
214
+ RPAREN: State 12
215
+
216
+ State 11:
217
+ rules:
218
+ 25/n1: e(9:11) → t "+" e •
219
+ {$, ")"}
220
+
221
+ transitions:
222
+
223
+ State 12:
224
+ rules:
225
+ 26/n1: t(9:3) → "(" e ")" •
226
+ {";", "+"}
227
+
228
+ transitions:
229
+
@@ -0,0 +1,45 @@
1
+ %require "0.0.1"
2
+ %type "ruby"
3
+
4
+ %terminal NUMBER
5
+ %terminal MULTIPLY "*"
6
+ %terminal DIVIDE "/"
7
+ %terminal ADD "+"
8
+ %terminal SUBTRACT "-"
9
+ %terminal LPAREN "("
10
+ %terminal RPAREN ")"
11
+
12
+ %nonassoc LPAREN RPAREN
13
+ %left MULTIPLY DIVIDE
14
+ %left ADD SUBTRACT
15
+
16
+ %%
17
+
18
+ expression: NUMBER { |a| a[1] }
19
+ | expression ADD expression { |a, _, b| a + b }
20
+ | expression SUBTRACT expression { |a, _, b| a - b }
21
+ | expression MULTIPLY expression { |a, _, b| a * b }
22
+ | expression DIVIDE expression { |a, _, b| a / b }
23
+ | LPAREN expression RPAREN { |_, a, _| a }
24
+ | LPAREN error RPAREN
25
+
26
+ %%
27
+
28
+ class ExampleParser
29
+ %{write}
30
+
31
+ def type(token)
32
+ token[0]
33
+ end
34
+ end
35
+
36
+ input = [[:NUMBER, 2], [:ADD], [:NUMBER, 2]]
37
+ input = [
38
+ [:NUMBER, 2],
39
+ [:ADD],
40
+ [:NUMBER, 2],
41
+ [:MULTIPLY],
42
+ [:NUMBER, 3]
43
+ ]
44
+
45
+ p ExampleParser.new.parse(input)