A lexer generator for Ruby
Write lexer rules once and run them directly or generate a Ruby scanner.
Quick start · Playground · Documentation · Product site
Install flexr on Ruby 3.4 or newer.
gem install flexrSave the rules as lexer.flexr.rb.
require "flexr"
class Lexer < Flexr::Lexer
emits :INTEGER, :PLUS
rule(/[ \t\r\n]+/, skip: true)
rule(/[0-9]+/) { emit :INTEGER, text.to_i }
rule(/\+/) { emit :PLUS }
endRun the lexer on a short input.
ruby -r ./lexer.flexr.rb -e 'p Lexer.new("12 + 3").tokens'[[:INTEGER, 12], [:PLUS, "+"], [:INTEGER, 3]]Generate a Ruby file from the same rules.
flexr lexer.flexr.rb -o lexer.rbLoad lexer.rb in your application with the flexr gem installed. Use
--standalone to embed the runtime
when you need to distribute a single file that runs without the gem.
Generation reads the specification statically by default. The --eval option
executes the Ruby source at build time. Use it only with trusted specifications.
By default, flexr chooses the longest match at each input position. For
example, == wins over = even if the = rule comes first. For matches of
equal length, higher priority: values win. Definition order breaks ties
between rules with the same priority.
Rules support a subset of Ruby regexp syntax designed for DFA matching.
Run a lexer in the browser playground to inspect its tokens, diagnostics, and match decisions.
Open the specifications from the demo.
- Calculator tutorial walks through building, checking, and generating a lexer.
- Documentation map links to how-to guides, including parser integration and migration.
- Reference covers the DSL, CLI, diagnostics, and API stability.
- Examples contains runnable lexers and parser integrations.
- Changelog lists release highlights and upgrade notes.
See CONTRIBUTING.md for local checks and RELEASING.md for the release process.
MIT. See LICENSE.txt.

