cycle 560 B

123456789101112131415161718192021222324252627282930313233
  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. N {
  9. if (stk[$]) {
  10. stk[$] = NULL;
  11. }
  12. else if ($tvedge == NULL) { /* current root */
  13. stk[$] = $;
  14. }
  15. else {
  16. stk[$] = $tvedge.tail;
  17. }
  18. }
  19. E {
  20. if (stk[$.head]) {
  21. tp = $.tail;
  22. hp = $.head;
  23. while (tp != $.head) {
  24. printf ("%s -> %s\n", tp.name, hp.name);
  25. hp = tp;
  26. tp = stk[tp];
  27. }
  28. printf ("%s -> %s\n", tp.name, hp.name);
  29. exit(0);
  30. }
  31. }