compiler028: Ragel State Machine Compiler

Ragel是一种强大的状态机编译器,用于从正则语言创建可执行的有限状态机,支持代码嵌入、非确定性控制和多种编程模型。它适用于协议实现、数据解析、编程语言的词法分析等任务,并生成C/C++/ASM代码,提供灵活的接口和可视化工具。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Ragel State Machine Compiler

Ragel compiles executable finite state machines from regular languages. Ragel targets C, C++ and ASM. 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 unionconcatenation 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.

Ragel 状态机编译器

Ragel 从常规语言编译可执行的有限状态机。 Ragel 针对 C、C++ 和 ASM。 Ragel 状态机不仅可以像正则表达式机器那样识别字节序列,还可以在识别正则语言的任意点执行代码。代码嵌入是使用不破坏常规语言语法的内联运算符完成的。

核心语言由标准正则表达式运算符(例如联合、连接和 Kleene 星号)和动作嵌入运算符组成。用户的正则表达式被编译成一个确定的状态机,嵌入的动作与机器的转换相关联。理解正则表达式和确定性有限自动机之间的形式关系是有效使用 Ragel 的关键。

Ragel 还提供了操作符,让您可以控制您创建的任何非确定性、构建扫描仪以及使用状态图模型构建状态机。还可以通过跳转或调用机器的其他部分或重新处理输入来从嵌入式动作内部影响状态机的执行。

Ragel 为宿主语言提供了一个非常灵活的接口,它试图对生成的代码如何集成到应用程序中施加最小的限制。生成的代码没有依赖关系。

Ragel code looks like:

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' )*;

.. and it compiles to:

st0:
    if ( ++p == pe )
        goto out0;
    if ( 48 <= (*p) && (*p) <= 57 )
        goto tr0;
    goto st_err;
tr0:
    { printf("DGT: %c\n", (*p)); }
st1:
    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;

… and it visualizes as:

What kind of task is Ragel good for?

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

Features

  • 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++ or ASM (GNU, x86_64, System V ABI) code with no dependencies.
  • Choose from table or control flow driven state machines.

Download

Stable

March 24, 2017
ragel-6.10.tar.gz (sig) (key)
ragel-guide-6.10.pdf

Development

Feb 15, 2021
ragel-7.0.4.tar.gz 
(note that building ragel 7 requires colm)

Discussion and GIT Repos

Discussion

To ask questions about using Ragel, or to discuss development, please use github discussions.

The original ragel mailing list is not longer running. Managing spam and staying off blacklists became too much of a nuisance. For older messages, checkout the mailing list archives.

GIT Repos

git clone ssh://git@github.com/adrian-thurston/ragel.git

git clone https://github.com/adrian-thurston/ragel.git

License

Beginning with the next development release (> 7.0.0.9) Ragel is licensed under an MIT style license. Ragel 6 remains under GPL v2. Please see the file COPYING in the source.

Note: Part of the Ragel output is copied from Ragel source, covered by the MIT (or GPL v2) license. As an exception, 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 and license of the input file. Use of Ragel makes absolutely no requirement about the license of generated code.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值