tl_main.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /***** tl_spin: tl_main.c *****/
  2. /* Copyright (c) 1995-2000 by Lucent Technologies - Bell Laboratories */
  3. /* All Rights Reserved. This software is for educational purposes only. */
  4. /* Permission is given to distribute this code provided that this intro- */
  5. /* ductory message is not removed and no monies are exchanged. */
  6. /* No guarantee is expressed or implied by the distribution of this code. */
  7. /* Written by Gerard J. Holzmann, Bell Laboratories, U.S.A. */
  8. /* Based on the translation algorithm by Gerth, Peled, Vardi, and Wolper, */
  9. /* presented at the PSTV Conference, held in 1995, Warsaw, Poland 1995. */
  10. /* Send bug-reports and/or questions to: gerard@research.bell-labs.com */
  11. #include "tl.h"
  12. extern FILE *tl_out;
  13. int newstates = 0; /* debugging only */
  14. int tl_errs = 0;
  15. int tl_verbose = 0;
  16. int tl_terse = 0;
  17. int tl_clutter = 0;
  18. unsigned long All_Mem = 0;
  19. static char uform[4096];
  20. static int hasuform=0, cnt=0;
  21. static void tl_stats(void);
  22. static void non_fatal(char *, char *);
  23. int
  24. tl_Getchar(void)
  25. {
  26. if (cnt < hasuform)
  27. return uform[cnt++];
  28. cnt++;
  29. return -1;
  30. }
  31. void
  32. put_uform(void)
  33. {
  34. fprintf(tl_out, "%s", uform);
  35. }
  36. void
  37. tl_UnGetchar(void)
  38. {
  39. if (cnt > 0) cnt--;
  40. }
  41. int
  42. tl_main(int argc, char *argv[])
  43. { int i;
  44. extern int verbose, xspin;
  45. tl_verbose = verbose;
  46. tl_clutter = 1-xspin; /* use -X -f to turn off uncluttering */
  47. while (argc > 1 && argv[1][0] == '-')
  48. { switch (argv[1][1]) {
  49. case 'd': newstates = 1; /* debugging mode */
  50. break;
  51. case 'f': argc--; argv++;
  52. for (i = 0; i < argv[1][i]; i++)
  53. { if (argv[1][i] == '\t'
  54. || argv[1][i] == '\"'
  55. || argv[1][i] == '\n')
  56. argv[1][i] = ' ';
  57. }
  58. strcpy(uform, argv[1]);
  59. hasuform = strlen(uform);
  60. break;
  61. case 'v': tl_verbose++;
  62. break;
  63. case 'n': tl_terse = 1;
  64. break;
  65. default : printf("spin -f: saw '-%c'\n", argv[1][1]);
  66. goto nogood;
  67. }
  68. argc--; argv++;
  69. }
  70. if (hasuform == 0)
  71. {
  72. nogood: printf("usage:\tspin [-v] [-n] -f formula\n");
  73. printf(" -v verbose translation\n");
  74. printf(" -n normalize tl formula and exit\n");
  75. exit(1);
  76. }
  77. tl_parse();
  78. if (tl_verbose) tl_stats();
  79. return tl_errs;
  80. }
  81. static void
  82. tl_stats(void)
  83. { extern int Stack_mx;
  84. printf("total memory used: %9ld\n", All_Mem);
  85. printf("largest stack sze: %9d\n", Stack_mx);
  86. cache_stats();
  87. a_stats();
  88. }
  89. #define Binop(a) \
  90. fprintf(tl_out, "("); \
  91. dump(n->lft); \
  92. fprintf(tl_out, a); \
  93. dump(n->rgt); \
  94. fprintf(tl_out, ")")
  95. void
  96. dump(Node *n)
  97. {
  98. if (!n) return;
  99. switch(n->ntyp) {
  100. case OR: Binop(" || "); break;
  101. case AND: Binop(" && "); break;
  102. case U_OPER: Binop(" U "); break;
  103. case V_OPER: Binop(" V "); break;
  104. #ifdef NXT
  105. case NEXT:
  106. fprintf(tl_out, "X");
  107. fprintf(tl_out, " (");
  108. dump(n->lft);
  109. fprintf(tl_out, ")");
  110. break;
  111. #endif
  112. case NOT:
  113. fprintf(tl_out, "!");
  114. fprintf(tl_out, " (");
  115. dump(n->lft);
  116. fprintf(tl_out, ")");
  117. break;
  118. case FALSE:
  119. fprintf(tl_out, "false");
  120. break;
  121. case TRUE:
  122. fprintf(tl_out, "true");
  123. break;
  124. case PREDICATE:
  125. fprintf(tl_out, "(%s)", n->sym->name);
  126. break;
  127. case -1:
  128. fprintf(tl_out, " D ");
  129. break;
  130. default:
  131. printf("Unknown token: ");
  132. tl_explain(n->ntyp);
  133. break;
  134. }
  135. }
  136. void
  137. tl_explain(int n)
  138. {
  139. switch (n) {
  140. case ALWAYS: printf("[]"); break;
  141. case EVENTUALLY: printf("<>"); break;
  142. case IMPLIES: printf("->"); break;
  143. case EQUIV: printf("<->"); break;
  144. case PREDICATE: printf("predicate"); break;
  145. case OR: printf("||"); break;
  146. case AND: printf("&&"); break;
  147. case NOT: printf("!"); break;
  148. case U_OPER: printf("U"); break;
  149. case V_OPER: printf("V"); break;
  150. #ifdef NXT
  151. case NEXT: printf("X"); break;
  152. #endif
  153. case TRUE: printf("true"); break;
  154. case FALSE: printf("false"); break;
  155. case ';': printf("end of formula"); break;
  156. default: printf("%c", n); break;
  157. }
  158. }
  159. static void
  160. non_fatal(char *s1, char *s2)
  161. { extern int tl_yychar;
  162. int i;
  163. printf("tl_spin: ");
  164. if (s2)
  165. printf(s1, s2);
  166. else
  167. printf(s1);
  168. if (tl_yychar != -1 && tl_yychar != 0)
  169. { printf(", saw '");
  170. tl_explain(tl_yychar);
  171. printf("'");
  172. }
  173. printf("\ntl_spin: %s\n---------", uform);
  174. for (i = 0; i < cnt; i++)
  175. printf("-");
  176. printf("^\n");
  177. fflush(stdout);
  178. tl_errs++;
  179. }
  180. void
  181. tl_yyerror(char *s1)
  182. {
  183. Fatal(s1, (char *) 0);
  184. }
  185. void
  186. Fatal(char *s1, char *s2)
  187. {
  188. non_fatal(s1, s2);
  189. /* tl_stats(); */
  190. exit(1);
  191. }