two state machines in single source - I cannot believe

pazzodalegare pazzodaleg... at gmail.com
Sat Jun 7 16:03:14 UTC 2008


Dear all,

I'm playing with two machines too. I code a dummy ragel-machine to
match a string:
A"12345FFF01"B. For exercise, I would like to match the string using
two machines (ragel code follows).

With the first machine (main, see below) I want to extract the string
between ' " ' which should match a set of strings defined by:

digit{2} (digit|'F')(3} 'F'{3}

then, with the second one (FSM1, see below) I match the string
12345FFF.
I found an unexpected behaviour: calling the FSM1 with fgoto it seems
it starts to process from p++ (where p is the pointer to the first
character for the called machine FSM1).

Uncommenting the line in action endpt1:
		//p--;

FSM1 machine behaves as expected ("OK pt2" is printed")..could you
help to find my mistake?

Tnx

pazzo

------------------ ragel code ------------------------

#include <string.h>
#include <stdio.h>
%%{
	# dummy machine to match A"12345FFF01"B

	machine test1;

	action cpch
	{
		printf("%c\n",fc);
		*fn = fc;
		fn++;
	}

	action _error
	{
		res = 0;
		printf("ERROR %d, %c\n",cs,fc);
		fbreak;
	}

	action endpt1
	{
		printf("OK\n%s\n", FN);
		p = FN;
		pe = p + strlen(p) + 1;
		//p--;
		fgoto FSM1;
	}

	action endsubstr
	{
		*fn = '\0';
	}

	action endpt2
	{
		printf("OK pt2\n");
	}

	FSM1 := '12345FFF' @!_error @endpt2 >~{printf("*** %d %c %s ***
\n",cs, fc, fpc);};

	main := ( 'A"' ( digit{2} (digit | 'F'){3} 'F'{3} ) $cpch @endsubstr
digit{2}  '"B' ) @!_error @endpt1 ;

}%%

%% write data;


int parse(char *buf)
{
	int res = 0;
	int cs;
	char *eof = 0;
	char *p = buf;
	char *pe = p + strlen(p) + 1;
	char FN[16];
	char *fn;
	int top, stack[32];

	fn = FN;
	%% write init;
	%% write exec;

	return res;
}

int main( int argc, char **argv )
{
	int res = 0;
	if ( argc > 1 ) {
		res = parse(argv[1]);
	}
	printf("result = %i\n", res );
	return 0;
}



More information about the ragel-users mailing list