Setevik
Setevik

Reputation: 21

Basic Ragel parser

With a machine like this:

main := (any+);

When I feed it a chunk of data, more than 1 byte, it seems to consume only 1 byte before coming out (normally) of %%write exec block. I expect it to be greedy and consume all of the provided input.

I can always check p < pe and goto to before %%write exec, but it seems to be hackish.

How do I make it "greedy" ?

Upvotes: 2

Views: 439

Answers (1)

Roman Khimov
Roman Khimov

Reputation: 4977

Maybe your question is missing some critical data, but the default behaviour is to consume everything up to pe if possible. Obviously with your machine it is possible and given Ragel 6.9 and this simple program:

#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;
}

You should get something like this in the output:

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

Upvotes: 2

Related Questions