summaryrefslogtreecommitdiff
path: root/examples/statechart.cpp
blob: afe3b9cae5443bb6d7f576968df2909708fc5505 (plain)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187

#line 1 "statechart.rl"
/*
 * 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( );
};


#line 66 "statechart.rl"



#line 33 "statechart.cpp"
static const int StateChart_start = 3;
static const int StateChart_first_final = 3;
static const int StateChart_error = 0;

static const int StateChart_en_main = 3;


#line 69 "statechart.rl"

int StateChart::init( )
{
	neg = false;
	val = false;
	
#line 48 "statechart.cpp"
	{
	cs = StateChart_start;
	}

#line 75 "statechart.rl"
	return 1;
}

int StateChart::execute( const char *data, int len )
{
	const char *p = data;
	const char *pe = data + len;

	
#line 63 "statechart.cpp"
	{
	if ( p == pe )
		goto _test_eof;
	switch ( cs )
	{
tr2:
#line 41 "statechart.rl"
	{
		if ( neg )
			val = -1 * val;
	}
#line 65 "statechart.rl"
	{ cout << val << endl; }
	goto st3;
st3:
	if ( ++p == pe )
		goto _test_eof3;
case 3:
#line 82 "statechart.cpp"
	switch( (*p) ) {
		case 43: goto tr3;
		case 45: goto tr4;
	}
	if ( 48 <= (*p) && (*p) <= 57 )
		goto tr5;
	goto st0;
st0:
cs = 0;
	goto _out;
tr3:
#line 28 "statechart.rl"
	{
		neg = false;
		val = 0;
	}
	goto st1;
tr4:
#line 28 "statechart.rl"
	{
		neg = false;
		val = 0;
	}
#line 33 "statechart.rl"
	{
		neg = true;
	}
	goto st1;
st1:
	if ( ++p == pe )
		goto _test_eof1;
case 1:
#line 115 "statechart.cpp"
	if ( 48 <= (*p) && (*p) <= 57 )
		goto tr0;
	goto st0;
tr0:
#line 37 "statechart.rl"
	{ 
		val = val * 10 + ((*p) - '0');
	}
	goto st2;
tr5:
#line 28 "statechart.rl"
	{
		neg = false;
		val = 0;
	}
#line 37 "statechart.rl"
	{ 
		val = val * 10 + ((*p) - '0');
	}
	goto st2;
st2:
	if ( ++p == pe )
		goto _test_eof2;
case 2:
#line 140 "statechart.cpp"
	if ( (*p) == 10 )
		goto tr2;
	if ( 48 <= (*p) && (*p) <= 57 )
		goto tr0;
	goto st0;
	}
	_test_eof3: cs = 3; goto _test_eof; 
	_test_eof1: cs = 1; goto _test_eof; 
	_test_eof2: cs = 2; goto _test_eof; 

	_test_eof: {}
	_out: {}
	}

#line 84 "statechart.rl"

	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;
}