Ah, you can process at the bit level. See attached code. You'll probably want to scrutinize the generated and compiled code to see if it's good enough for your needs. One thing to note about using the getchar directive is that the expression can be evaluated more than once per value of p and if the compiler doesn't optimize that duplicate work away then your code won't be optimal.

By the way, I'm not sure if I just figured this out now, or if somebody told me about this technique a while back and I forgot. (Bob maybe?)

-Adrian

Jan Sporbeck wrote:
Hello there!

I'd like to use ragel to design the state machine for a microcontroller
project (avr atmega8535) written in C.

I think I understand how to use it for streams of char, int, etc. But
what's the best practice to use for bit based decisions?

I could use union..struct to pack all bits in one datatype and process
that. But I doubt that'll yield a performant implementation.

I read messages on this list that ragel is used for uC projects, but no
details how.

So any hints or example code to get me started is appreciated.

Thanks in advance,
/Jan.


_______________________________________________
ragel-users mailing list
[email protected]
http://www.complang.org/mailman/listinfo/ragel-users
#include <stdio.h>
#include <string.h>

%%{
        machine foo;
        getkey ( ( data[p>>3] >> (p & 0x7) ) & 0x1 );

        main := 0* 1* 0*;
}%%

%% write data;

int main( int argc, char **argv )
{
        if ( argc < 2 ) {
                printf("expecting at least one argument\n");
                return 1;
        }

        char *data = argv[1];
        long length = strlen( data );

        int p = 0;
        int pe = length << 3;
        int eof = pe;

        int cs;

        %% write init;
        %% write exec;

        if ( cs == foo_error )
                printf("FAILED\n");
}
_______________________________________________
ragel-users mailing list
[email protected]
http://www.complang.org/mailman/listinfo/ragel-users

Reply via email to