| 1 |
/* Detect directed cycle and print one if found */ |
| 2 |
BEG_G{ |
| 3 |
node_t tp, hp; |
| 4 |
node_t stk[node_t]; |
| 5 |
$tvtype = TV_prepostfwd; |
| 6 |
$tvroot = fstnode($); |
| 7 |
} |
| 8 |
|
| 9 |
N { |
| 10 |
if (stk[$]) { |
| 11 |
stk[$] = NULL; |
| 12 |
} |
| 13 |
else if ($tvedge == NULL) { /* current root */ |
| 14 |
stk[$] = $; |
| 15 |
} |
| 16 |
else { |
| 17 |
stk[$] = $tvedge.tail; |
| 18 |
} |
| 19 |
} |
| 20 |
E { |
| 21 |
if (stk[$.head]) { |
| 22 |
tp = $.tail; |
| 23 |
hp = $.head; |
| 24 |
while (tp != $.head) { |
| 25 |
printf ("%s -> %s\n", tp.name, hp.name); |
| 26 |
hp = tp; |
| 27 |
tp = stk[tp]; |
| 28 |
} |
| 29 |
printf ("%s -> %s\n", tp.name, hp.name); |
| 30 |
exit(0); |
| 31 |
} |
| 32 |
} |
| 33 |
|