[ragel-users] Re: two state machines
massimiliano cialdi
cia... at gmail.com
Sat Jun 7 16:17:01 UTC 2008
On Sat, Jun 7, 2008 at 7:39 AM, mitchell <mforal.n... at gmail.com> wrote:
>
> Hi,
>
> Untested, but it's an idea I had:
I tried the same thing, and I have some doubt
here is my source (is only a simplified example of my really need):
----------------------------8<-------------------------------------
#include <string.h>
#include <stdio.h>
%%{
# test for dua FSM definition
# I want to match a pattern like A"12345FFF01"B ( or A"99FFFFF99"B or
A"321FFFFF14"B )
# once take the substring between quotes I want to pass it to FSM1
that match exacly
machine test1;
action copycar
{
printf("%c\n",fc);
// populating FN
*fn = fc;
fn++;
}
action error
{
printf("error %d, %c\n",cs,fc);
fbreak;
}
action endpt1
{
printf("OK\n%s\n", FN);
// setting new buffer margin for FSM1. It must operate on FN
p = FN;
pe = p + strlen(p) + 1;
fgoto FSM1;
}
action endsubstr
{
// placing null termination to substring in FN
*fn = '\0';
}
action endpt2
{
// substring matches
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} ) $copycar
@endsubstr digit{2} '"B' ) @!error @endpt1 ;
}%%
%% write data;
void parse(char *buf)
{
int cs;
char *eof = 0;
char *p = buf;
char *pe = p + strlen(p) + 1;
char FN[16];
char *fn;
// FN is the container fo the substring
fn = FN;
%% write init;
%% write exec;
}
int main( int argc, char **argv )
{
if ( argc > 1 ) {
parse(argv[1]);
}
return 0;
}
----------------------------8<-------------------------------------
if you try to execute it fails:
test A\"12345FFF01\"B
1
2
3
4
5
F
F
F
OK
12345FFF
*** 15 1 12345FFF ***
error 0, 2
but the string exactly match the pattern (as can be seen in line "***
15 1 12345FFF ***")
so now try to change the pattern of FSM1 as follow (we delete '1'):
FSM1 := '2345FFF' @!error @endpt2 >~{printf("*** %d %c %s ***\n",cs,
fc, fpc);};
and retry to execute:
test A\"12345FFF01\"B
1
2
3
4
5
F
F
F
OK
12345FFF
*** 15 1 12345FFF ***
OK pt2
now it report the string matching but we had changed the pattern.
It seems that it skip the first character after fgoto.
so I tried another way.
lets use originale FSM1 definition (pattern '12345FFF'), but change
action endpt1 to add an fhold statement as follow:
action endpt1
{
printf("OK\n%s\n", FN);
// setting new buffer margin for FSM1. It must operate on FN
p = FN;
pe = p + strlen(p) + 1;
// fhold is translated in C as p--;
fhold;
fgoto FSM1;
}
and let execute:
test A\"12345FFF01\"B
1
2
3
4
5
F
F
F
OK
12345FFF
*** 15 ***
OK pt2
it seems to work, but action to-state start of FSM1 fails, it cannot
print neither fc character nor pfc string...
is this the right way to do this?
thanks
--
Et nunc, auxilium solis, vincam!
Oppugnatio solaris!
VIS!
Massimiliano Cialdi
cia... at gmail.com
massimiliano.cia... at powersoft.it
More information about the ragel-users
mailing list