Ragel State Machine Compiler

What is Ragel?

Ragel compiles executable finite state machines from regular languages. Ragel targets C, C++, Obj-C, C#, D, Java, Go and Ruby Ragel state machines can not only recognize byte sequences as regular expression machines do, but can also execute code at arbitrary points in the recognition of a regular language. Code embedding is done using inline operators that do not disrupt the regular language syntax.

The core language consists of standard regular expression operators (such as union, concatenation and Kleene star) and action embedding operators. The user's regular expressions are compiled to a deterministic state machine and the embedded actions are associated with the transitions of the machine. Understanding the formal relationship between regular expressions and deterministic finite automata is key to using Ragel effectively.

Ragel also provides operators that let you control any non-determinism that you create, construct scanners, and build state machines using a statechart model. It is also possible to influence the execution of a state machine from inside an embedded action by jumping or calling to other parts of the machine, or reprocessing input.

Ragel provides a very flexible interface to the host language that attempts to place minimal restrictions on how the generated code is integrated into the application. The generated code has no dependencies.

action dgt      { printf("DGT: %c\n", fc); }
action dec      { printf("DEC: .\n"); }
action exp      { printf("EXP: %c\n", fc); }
action exp_sign { printf("SGN: %c\n", fc); }
action number   { /*NUMBER*/ }

number = (
    [0-9]+ $dgt ( '.' @dec [0-9]+ $dgt )?
    ( [eE] ( [+\-] $exp_sign )? [0-9]+ $exp )?
) %number;

main := ( number '\n' )*;
    if ( ++p == pe )
        goto out0;
    if ( 48 <= (*p) && (*p) <= 57 )
        goto tr0;
    goto st_err;
    { printf("DGT: %c\n", (*p)); }
    if ( ++p == pe )
        goto out1;
    switch ( (*p) ) {
        case 10: goto tr5;
        case 46: goto tr7;
        case 69: goto st4;
        case 101: goto st4;
    if ( 48 <= (*p) && (*p) <= 57 )
        goto tr0;
    goto st_err;

click for larger

What kind of task is Ragel good for?

  • Writing robust protocol implementations.
  • Parsing data formats.
  • Lexical analysis of programming languages.
  • Validating user input.


  • Construct finite state machines using:
    • regular language operators
    • state chart operators
    • a scanner operator
    • some mix of the above
  • Embed actions into machines in arbitrary places.
  • Control non-determinism using guarded operators.
  • Minimize state machines using Hopcroft's algorithm.
  • Visualize output with Graphviz.
  • Use byte, double byte or word-sized alphabets.
  • Generate C, C++, Obj-C, C#, D, Java, Go or Ruby code with no dependencies.
  • Choose from table or control flow driven state machines.


[1] Adrian D. Thurston. "Parsing Computer Languages with an Automaton Compiled from a Single Regular Expression." In 11th International Conference on Implementation and Application of Automata (CIAA 2006), Lecture Notes in Computer Science, volume 4094, pp. 285-286, Taipei, Taiwan, August 2006. pdf.

Documentation, Editors and Mailing List

Ragel has a user guide available in PDF format as well as a man page. Major version number releases contain language changes. See the ChangeLog and Release Notes for details.

If you use Vim, there is a syntax file ragel.vim for your editing pleasure. If you use TextMate there is a Ragel bundle Ragel.tmbundle.

The Ragel mailing list is now archived. The Archives are available here: ragel-users. There are ragel users answering questions on Stack Overflow.


  • CroMo: Morphological analysis of Croatian and other languages. link.
  • An ESI server derived from Mongrel. link.
  • A little assembler that uses Ragel for scanning and Lemon for parsing. link.
  • An article in Japanese on using Ragel. link.
  • Using Ragel for scanning wikitext. link.
  • Perian is a QuickTime component that adds native support for many popular video formats.
  • ABNF Generator is a tool which accepts grammars in ABNF and outputs Ragel definitions.
  • Qfsm is a graphical tool for designing state machines. It includes a Ragel export feature.
  • RaSPF is an SPF library in C.
  • appid: single-pass application protocol identification.
  • Utu: internet communication with cryptographically enforced identity, reputation and retribution.
  • Lib2geom: a computational geometry library for Inkscape.
  • A JSON parser for Pike. link
  • Zed Shaw on Ragel State Charts. link
  • Using Ragel and XCode. link


Clang: a scanner for a simple C like language. clang.rl

Mailbox: parses unix mailbox files. It breaks files into separate messages, the headers within messages and the bodies of messages. mailbox.rl

AwkEmu: performs the basic parsing that the awk program performs on input. awkemu.rl

Atoi: converts a string to an integer. atoi.rl

Concurrent: performs multiple independent tasks concurrently. concurrent.rl


StateChart: the Atoi machine built with the named state and transition list paradigm. statechart.rl

GotoCallRet: demonstrates the use of fgoto, fcall, fret and fhold. gotocallret.rl

Params: parses command line arguments. params.rl

RagelScan: scans ragel input files. rlscan.rl

CppScan: A c++ scanner that uses the longest-match method of scanning cppscan.rl


Source Code Repository:


Tar.Gz: The latest release is version is ragel-6.8.tar.gz (sig).

Older: Previous versions are available here.

Debian:  The homepage for the Debian package of Ragel is here. It is by Robert Lemmen.

OpenPKG: Ragel has been included in the OpenPKG project.

FreeBSD: A port for Ragel is available in the FreeBSD ports system.


NetBSD: There is a package for Ragel in the pkgsrc database.

Mac OS X: A port is available in the MacPorts repository.

Gentoo: A Gentoo port is available.

Suse: Packages for Suse can be found here.

Windows: Ragel can be compiled using Cygwin or MinGW. Binaries compiled with visual studio are here (6.8), provided by Joseph Goettgens. (landing)

Redhat/Fedora: A package for Ragel is available in Fedora Extras.

 Version:   6.8 
 Date:   Feb 11, 2013 
 Change Log:   ChangeLog 
 Tar.Gz:   ragel-6.8.tar.gz (sig
 Zip:   ragel-6.8.zip (sig
 User Guide PDF:   ragel-guide-6.8.pdf 

The public key for package signing is here.


Ragel is released under the GNU General Public License. A copy of the license is included in the distribution. It is also available from GNU.

Note: parts of Ragel output are copied from Ragel source covered by the GNU GPL. As a special exception to the GPL, you may use the parts of Ragel output copied from Ragel source without restriction. The remainder of Ragel output is derived from the input and inherits the copyright status of the input file. Use of Ragel makes no requirements about the license of generated code.


Ragel was written by Adrian Thurston. It was originally developed in early 2000 and was first released January 2002. Many people have contributed feedback, ideas and code. Please have a look at the CREDITS file.

Thai Yoga Massage Kitchener Waterloo