/usr/share/doc/ragel/examples/statechart.rl is in ragel 6.8-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | /*
* Demonstrate the use of labels, the epsilon operator, and the join operator
* for creating machines using the named state and transition list paradigm.
* This implementes the same machine as the atoi example.
*/
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
using namespace std;
struct StateChart
{
bool neg;
int val;
int cs;
int init( );
int execute( const char *data, int len );
int finish( );
};
%%{
machine StateChart;
action begin {
neg = false;
val = 0;
}
action see_neg {
neg = true;
}
action add_digit {
val = val * 10 + (fc - '0');
}
action finish {
if ( neg )
val = -1 * val;
}
atoi = (
start: (
'-' @see_neg ->om_num |
'+' ->om_num |
[0-9] @add_digit ->more_nums
),
# One or more nums.
om_num: (
[0-9] @add_digit ->more_nums
),
# Zero ore more nums.
more_nums: (
[0-9] @add_digit ->more_nums |
'' -> final
)
) >begin %finish;
main := ( atoi '\n' @{ cout << val << endl; } )*;
}%%
%% write data;
int StateChart::init( )
{
neg = false;
val = false;
%% write init;
return 1;
}
int StateChart::execute( const char *data, int len )
{
const char *p = data;
const char *pe = data + len;
%% write exec;
if ( cs == StateChart_error )
return -1;
if ( cs >= StateChart_first_final )
return 1;
return 0;
}
int StateChart::finish( )
{
if ( cs == StateChart_error )
return -1;
if ( cs >= StateChart_first_final )
return 1;
return 0;
}
#define BUFSIZE 1024
int main()
{
char buf[BUFSIZE];
StateChart atoi;
atoi.init();
while ( fgets( buf, sizeof(buf), stdin ) != 0 ) {
atoi.execute( buf, strlen(buf) );
}
if ( atoi.finish() <= 0 )
cerr << "statechart: error: parsing input" << endl;
return 0;
}
|