Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
R

RLTK

> 编程语言
Open source

The Ruby Language Toolkit

451 stars0 likes1 views
WebsiteGitHub

About

The Ruby Language Toolkit

Welcome to the Ruby Language Toolkit

RLTK is a collection of classes and methods designed to help programmers work with languages in an easy to use and straightforward manner. This toolkit provides the following features:

  • Lexer generator
  • Parser generator
  • AST node baseclass
  • Class for representing context free grammars
  • Low Level Virtual Machine (LLVM) bindings for code generation

In addition, RLTK includes several ready-made lexers and parsers and a Turing-complete language called Kazoo for use in your code and as examples for how to use the toolkit.

Why Use RLTK

Here are some reasons to use RLTK to build your lexers, parsers, and abstract syntax trees, as well as generating LLVM IR and native object files:

  • Lexer and Parser Definitions in Ruby - Many tools require you to write your lexer/parser definitions in their own format, which is then processed and used to generate Ruby code. RLTK lexers/parsers are written entirely in Ruby and use syntax you are already familiar with.

  • Re-entrant Code - The lexers and parsers generated by RLTK are fully re-entrant.

  • Multiple Lexers and Parsers - You can define as many lexers and parses as you want, and instantiate as many of them as you need.

  • Token Positions - Detailed information about a token's position is available in the parser.

  • Feature Rich Lexing and Parsing - Often, lexer and parser generators will try and force you to do everything their way. RLTK gives you more flexibility with features such as states and flags for lexers, and argument arrays for parsers. What's more, these features actually work (I'm looking at you REX).

  • LALR(1)/GLR Parsing - RLTK parsers use the LALR(1)/GLR parsing algorithms, which means you get both speed and the ability to handle any context-free grammar.

  • Parser Serialization - RLTK parsers can be serialized and saved after they are generated for faster loading the next time they are required.

  • Error Productions - RLTK parsers can use error productions to recover from, and report on, errors.

  • Fast Prototyping - If you need to change your lexer/parser you don't have to re-run the lexer and parser generation tools, simply make the changes and be on your way.

  • Parse Tree Graphs - RLTK parsers can print parse trees (in the DOT language) of accepted strings.

  • LLVM Bindings - RLTK provides wrappers for most of the C LLVM bindings.

  • The Contractor - LLVM's method of building instructions is a bit cumbersome, and is very imperative in style. RLTK provides the Contractor class to make things easier.

  • Documentation - We have it!

  • I Eat My Own Dog Food - I'm using RLTK for my own projects so if there is a bug I'll most likely be the first one to know.

Lexers

To create your own lexer using RLTK you simply need to subclass the {RLTK::Lexer} class and define the rules that will be used for matching text and generating tokens. Here we see a simple lexer for a calculator:

…

Ruby class PrefixCalc then_val, new_else_bb => else_val}, 'iftmp' }

build(start_bb) { cond cond_val, then_bb, else_bb }

build(new_then_bb) { br merge_bb } build(new_else_bb) { br merge_bb }

returning(phi_inst) { target merge_bb } end


More extensive examples of how to use the Contractor class can be found in the Kazoo tutorial chapters.

### Execution Engines

Once you have generated your code you may want to run it.  RLTK provides bindings to both the LLVM interpreter and JIT compiler to help you do just that.  Creating a JIT compiler is pretty simple.

```Ruby
mod = RLTK::CG::Module.new('my module')
jit = RLTK::CG::JITCompiler(mod)

mod.functions.add('add', RLTK:CG::NativeIntType, [RLTK::CG::NativeIntType, RLTK::CG::NativeIntType]) do
  blocks.append('entry', nil, nil, self) do |fun|
    ret add(fun.params[0], fun.params[1])
  end
end

Now you can run your 'add' function like this:

ruby
jit.run(fun, 1, 2)

…


def fib(x)
  if x < 3 then
    1
  else
    fib(x-1) + fib(x-2)

The tutorial is organized as follows:

  • Chapter 1: The Lexer
  • Chapter 2: The AST Nodes
  • Chapter 3: The Parser
  • Chapter 4: AST Translation
  • Chapter 5: JIT Compilation
  • Chapter 6: Adding Control Flow
  • Chapter 7: Playtime
  • Chapter 8: Mutable Variables

Before starting this tutorial you should know about regular expressions, the basic ideas behind lexing and parsing, and be able to read context-free grammar (CFG) definitions. By the end of this tutorial we will have written 372 lines of source code and have a JIT compiler for a Turing complete language.

Provided Lexers and Parsers

The following lexer and parser classes are included as part of RLTK:

  • {RLTK::Lexers::Calculator}
  • {RLTK::Lexers::EBNF}
  • {RLTK::Parsers::PrefixCalc}
  • {RLTK::Parsers::InfixCalc}
  • {RLTK::Parsers::PostfixCalc}

Contributing

If you are interested in contributing to RLTK there are many aspects of the library that you can work on. A detailed TODO list can be found here. If you are looking for smaller units of work feel free to:

  • Help provide unit tests. Not all of RLTK is tested as well as it could be. Specifically, more tests for the RLTK::CFG and RLTK::Parser classes would be appreciated.
  • Write lexers or parsers that you think others might want to use. Possibilities include HTML, JSON/YAML, Javascript, and Ruby.
  • Extend the RLTK::CFG class with additional functionality.

Lastly, I love hearing back from users. If you find any part of the documentation unclear or incomplete let me know. It is also helpful to me to know how people are using the library, so if you are using RLTK in your project send me an email. This lets me know what features are being used and where I should focus my development efforts.

News

Aaaaand we're back. Development of RLTK has been on hold for a while as I worked on other projects. If you want to see what I've been up to, you can check out Clang's new -Wconsumed flag and the Filigree gem.

The next version of RLTK is going to be updated to require Ruby 2.0 as well as LLVM 3.4. Previous versions of RLTK required my LLVM-ECB libarary to expose extra LLVM features through the C bindings; this is no longer necessary as this functionality has been moved into LLVM proper. If anyone has any requests for new or improved features for RLTK version 3.0, let me know.

Issues· 12 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Ruby

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言