tl_lex.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /***** tl_spin: tl_lex.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 <stdlib.h>
  12. #include <ctype.h>
  13. #include "tl.h"
  14. static Symbol *symtab[Nhash+1];
  15. static int tl_lex(void);
  16. extern YYSTYPE tl_yylval;
  17. extern char yytext[];
  18. #define Token(y) tl_yylval = tl_nn(y,ZN,ZN); return y
  19. static void
  20. getword(int first, int (*tst)(int))
  21. { int i=0; char c;
  22. yytext[i++]= (char ) first;
  23. while (tst(c = tl_Getchar()))
  24. yytext[i++] = c;
  25. yytext[i] = '\0';
  26. tl_UnGetchar();
  27. }
  28. static int
  29. follow(int tok, int ifyes, int ifno)
  30. { int c;
  31. char buf[32];
  32. extern int tl_yychar;
  33. if ((c = tl_Getchar()) == tok)
  34. return ifyes;
  35. tl_UnGetchar();
  36. tl_yychar = c;
  37. sprintf(buf, "expected '%c'", tok);
  38. tl_yyerror(buf); /* no return from here */
  39. return ifno;
  40. }
  41. int
  42. tl_yylex(void)
  43. { int c = tl_lex();
  44. #if 0
  45. printf("c = %d\n", c);
  46. #endif
  47. return c;
  48. }
  49. static int
  50. tl_lex(void)
  51. { int c;
  52. do {
  53. c = tl_Getchar();
  54. yytext[0] = (char ) c;
  55. yytext[1] = '\0';
  56. if (c <= 0)
  57. { Token(';');
  58. }
  59. } while (c == ' '); /* '\t' is removed in tl_main.c */
  60. if (islower(c))
  61. { getword(c, isalnum_);
  62. if (strcmp("true", yytext) == 0)
  63. { Token(TRUE);
  64. }
  65. if (strcmp("false", yytext) == 0)
  66. { Token(FALSE);
  67. }
  68. tl_yylval = tl_nn(PREDICATE,ZN,ZN);
  69. tl_yylval->sym = tl_lookup(yytext);
  70. return PREDICATE;
  71. }
  72. if (c == '<')
  73. { c = tl_Getchar();
  74. if (c == '>')
  75. { Token(EVENTUALLY);
  76. }
  77. if (c != '-')
  78. { tl_UnGetchar();
  79. tl_yyerror("expected '<>' or '<->'");
  80. }
  81. c = tl_Getchar();
  82. if (c == '>')
  83. { Token(EQUIV);
  84. }
  85. tl_UnGetchar();
  86. tl_yyerror("expected '<->'");
  87. }
  88. switch (c) {
  89. case '/' : c = follow('\\', AND, '/'); break;
  90. case '\\': c = follow('/', OR, '\\'); break;
  91. case '&' : c = follow('&', AND, '&'); break;
  92. case '|' : c = follow('|', OR, '|'); break;
  93. case '[' : c = follow(']', ALWAYS, '['); break;
  94. case '-' : c = follow('>', IMPLIES, '-'); break;
  95. case '!' : c = NOT; break;
  96. case 'U' : c = U_OPER; break;
  97. case 'V' : c = V_OPER; break;
  98. #ifdef NXT
  99. case 'X' : c = NEXT; break;
  100. #endif
  101. default : break;
  102. }
  103. Token(c);
  104. }
  105. Symbol *
  106. tl_lookup(char *s)
  107. { Symbol *sp;
  108. int h = hash(s);
  109. for (sp = symtab[h]; sp; sp = sp->next)
  110. if (strcmp(sp->name, s) == 0)
  111. return sp;
  112. sp = (Symbol *) tl_emalloc(sizeof(Symbol));
  113. sp->name = (char *) tl_emalloc(strlen(s) + 1);
  114. strcpy(sp->name, s);
  115. sp->next = symtab[h];
  116. symtab[h] = sp;
  117. return sp;
  118. }
  119. Symbol *
  120. getsym(Symbol *s)
  121. { Symbol *n = (Symbol *) tl_emalloc(sizeof(Symbol));
  122. n->name = s->name;
  123. return n;
  124. }