blob: ede9ed827c9c734fb20134081364cc6c8acc5419 (
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
|
/*
* @LANG: c++
*/
/* Balanced parenthesis with conditions. */
#include <iostream>
#include <string.h>
using std::cout;
using std::endl;
%%{
machine cond;
write data noerror;
}%%
void test( const char *str )
{
int cs = cond_start, n = 0;
const char *p = str;
const char *pe = str + strlen( str );
%%{
comment = '(' @{n=0;}
( '('@{n++;} | ')'@{n--;} | [^()] )*
:> ')' when{!n};
main := ' '* comment ' '* '\n' @{cout << "success";};
write exec;
}%%
if ( cs < cond_first_final )
cout << "failure";
cout << endl;
}
int main()
{
test( "( ( )\n" );
test( "()()\n" );
test( "(((\n" );
test( "((()\n" );
test( "((())\n" );
test( "()\n" );
test( "((()))\n" );
test( "(()())\n" );
test( "((())()(((()))))\n" );
return 0;
}
#ifdef _____OUTPUT_____
failure
failure
failure
failure
failure
success
success
success
success
#endif
|