基本的Ragel解析器

时间:2015-02-25 17:46:43

标签: ragel

使用这样的机器:

main:=(any +);

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

我总能检查p< pe和goto在%%写exec之前,但它似乎是hackish。

如何让它“贪婪”?

1 个答案:

答案 0 :(得分:2)

也许您的问题缺少一些关键数据,但默认行为是尽可能消耗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
相关问题