Skip to content
Browse files

improve grammar

played around with SyntaxNode monkey patching
  • Loading branch information...
1 parent d36aaff commit 82bcadffca7e095dccf5d3ee32655792b5b88bc3 @dsci dsci committed Nov 3, 2010
Showing with 34 additions and 5 deletions.
  1. +16 −4 product_test.treetop
  2. +18 −1 sample.rb
View
20 product_test.treetop
@@ -1,15 +1,27 @@
grammar ProductTest
include Number
- rule power
- multiple "^" number {
+ rule final
+ power {
def result
- multiple.text_value.to_i ** number.text_value.to_i
+ elements.each do |n|
+ puts n.text_value
+ end
+ pow(number)
end
}
end
+ rule power
+ multiple "^" number {
+ }
+ end
+
rule multiple
- number '*' number
+ number '*' number {
+ def merge
+ number1.text_value.to_i*number2.text_value.to_i
+ end
+ }
end
end
View
19 sample.rb
@@ -7,6 +7,23 @@
require "simple_test"
require "product_test"
+module IntegerExtension
+
+
+ def pow(k)
+ puts "#{self.text_value.to_i}/#{k.text_value}"
+ l = elements.map{|n| n.text_value.gsub("^", "**")}
+ return eval(l.to_s)
+ end
+
+ def debug
+ puts self.text_value
+ end
+
+end
+
+
+Treetop::Runtime::SyntaxNode.send(:include, IntegerExtension)
puts "grammar with [1-6]"
@@ -17,7 +34,7 @@
p e.value
-f = ProductTestParser.new.parse("4*3^9")
+f = ProductTestParser.new.parse("4*3^2")
pp f

0 comments on commit 82bcadf

Please sign in to comment.
Something went wrong with that request. Please try again.