Kanocc (Kanocc Ain’t NO Compiler-Compiler) 0.2.0 is released.
(Actually it was released a few months ago, but it only recently
occurred to
me that I could/should announce i here. Sorry…)
What is it: Kanocc is a syntax recognition framework for Ruby. It
emphasizes
seamless integration with Ruby, utilizing Ruby’s flexible syntax.
Kanocc is available as a gem.
Visit http://kanocc.rubyforge.org to find documentation and downloads.
To get a little taste of Kanocc, here’s an example: An interpreter for
simple arithmetic expressions could be done like this:
require “rubygems”
require “kanocc”
class Number < Kanocc::Token
attr_reader :val
pattern(/\d+/) { @val = @m[0].to_i}
end
class Expr < Kanocc::Nonterminal
attr_reader :val
rule(Expr, “+”, Expr) { @val = @rhs[0].val + @rhs[2].val}
rule(Expr, “-”, Expr) { @val = @rhs[0].val - @rhs[2].val}
rule(Expr, “*”, Expr) { @val = @rhs[0].val * @rhs[2].val}; precedence
-1
rule(Expr, “/”, Expr) { @val = @rhs[0].val / @rhs[2].val}; precedence
-1
rule(“(”, Expr, “)”) { @val = @rhs[1].val}
rule(Number) {@val = @rhs[0].val}
end
myParser = Kanocc::Kanocc.new(Expr)
puts myParser.parse(‘3 + 4 - 2’).val
puts myParser.parse(‘8 - 2 * 3’).val
puts myParser.parse(‘8 - (4 - 2)’).val
If this doesn’t make any sense at all, you may have to read the
documentation
Feedback is most welcome.
br. Christian S…