Kanocc 0.2.0

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 :slight_smile:

Feedback is most welcome.

br. Christian S…

What is it: Kanocc is a syntax recognition framework for Ruby. It emphasizes
seamless integration with Ruby, utilizing Ruby’s flexible syntax.
How is Kanocc different from Treetop?
I saw a presentation by the creator showing how to implement a
parser/interpretor for arithmetic with the Treetop gem. Why use any one
over the other?

  • Ehsan

Well, actually I didn’t know about Treetop. It looks very nice. I’ll
have a
look at it.

br. Christian S.

2010/3/21 Tomo K. [email protected]

Kanocc uses Earley parsing algorithm, while Treetop uses PEGs.