How to make Ragel EOF actions work

I work with Ragel to evaluate the FSA, and I want to implement a user action that runs whenever my machine completes input testing. I need this action to be performed regardless of whether the machine has ended in a receiving state or not. I have this modified example, taken from the Ragel manual, which illustrates what I'm going to do:

#include <string.h> #include <stdio.h> %%{ machine foo; main := ( 'foo' | 'bar' ) 0 @{ res = 1; } $/{ finished = 1; }; }%% %% write data; int main( int argc, char **argv ) { int cs, res = 0, finished = 0; if ( argc > 1 ) { char *p = argv[1]; char *pe = p + strlen(p) + 1; char* eof = pe; %% write init; %% write exec; } printf("result = %i\n", res ); printf("finished = %i\n", finished); return 0; } 

My goal for this example is that res will be 1 when the input is either "foo" or "bar" and completed - 1 regardless of the input. I can't get this to work, though - it seems to be finished 1 when res is 1, and 0 when res is 0.

Any help would be awesome.

+6
source share
2 answers

The eof action will be executed when p == pe == eof . Another important thing: when your state machine cannot match the any state, the state goes into error and the match stops, which means that you can never reach the end.

See when you type foo1 . When parsing on o everything is fine. Howerver the next character 1 cannot correspond to the state you specified, so an error occurs. You can never meet the action eof. Thus, the variable res and finish is 0.

When you type foo , everything is fine. The state can go to the end. Thus, the action of eof occurs.

You can set the action with an error to see what happens:

 %%{ main := ( 'foo' | 'bar' ) 0 @{ res = 1; } $err{ printf("error : %c", fc);} $/{ finished = 1; }; }%% 

And you can try this code to meet your needs:

 %%{ main := (( 'foo' | 'bar' ) 0 @{ res = 1; } | any* ) $/{ finished = 1; }; }%% 
+3
source

Try the following:

 main := ( 'foo' 0 @2 @{ res = 1; } | 'bar' 0 @2 @{ res = 1; } | any* ) @{ finished = 1; }; 
+1
source

Source: https://habr.com/ru/post/943861/


All Articles