【问题标题】:Basic Ragel parser基本 Ragel 解析器
【发布时间】:2015-02-25 17:46:43
【问题描述】:

使用这样的机器:

main := (any+);

当我向它提供超过 1 个字节的数据块时,它似乎只消耗 1 个字节,然后(通常)输出 %%write exec 块。我希望它是贪婪的并消耗所有提供的输入。

我总是可以在 %%write exec 之前检查 p

如何让它“贪婪”?

【问题讨论】:

  • 你机器的生成图:i.imgur.com/hm7C2LM.png 表明它应该清楚地消耗所有输入:实际上没有办法退出机器。
  • 双圈不是可以接受的终止状态吗?
  • @ioquatix:是的,但它仍然应该上升到pe,有一个自引用转换。

标签: ragel


【解决方案1】:

也许您的问题缺少一些关键数据,但默认行为是尽可能使用直到 pe 的所有数据。显然,使用您的机器是可能的,并且给定 Ragel 6.9 和这个简单的程序:

#include <inttypes.h>
#include <stdint.h>
#include <stdio.h>

%%{
        machine main;
        alphtype char;

        main := (any+);
}%%

int main()
{
        static char data[] = "Some string!";
        int cs;
        char *p, *pe, *eof;

        %% write data;

        %% write init;
        p = data;
        pe = data + sizeof(data);
        eof = pe;

        printf("p: 0x%"PRIxPTR", pe: 0x%"PRIxPTR", eof: 0x%"PRIxPTR"\n",
               (uintptr_t) p, (uintptr_t) pe, (uintptr_t) eof);

        %% write exec;

        printf("p: 0x%"PRIxPTR", pe: 0x%"PRIxPTR", eof: 0x%"PRIxPTR"\n",
               (uintptr_t) p, (uintptr_t) pe, (uintptr_t) eof);
        return 0;
}

你应该在输出中得到类似的东西:

p: 0x601038, pe: 0x601045, eof: 0x601045
p: 0x601045, pe: 0x601045, eof: 0x601045

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-21
    • 2017-01-24
    相关资源
    最近更新 更多