tl_lex.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /***** tl_spin: tl_lex.c *****/
  2. /* Copyright (c) 1995-2003 by Lucent Technologies, Bell Laboratories. */
  3. /* All Rights Reserved. This software is for educational purposes only. */
  4. /* No guarantee whatsoever is expressed or implied by the distribution of */
  5. /* this code. Permission is given to distribute this code provided that */
  6. /* this introductory message is not removed and no monies are exchanged. */
  7. /* Software written by Gerard J. Holzmann. For tool documentation see: */
  8. /* http://spinroot.com/ */
  9. /* Send all bug-reports and/or questions to: bugs@spinroot.com */
  10. /* Based on the translation algorithm by Gerth, Peled, Vardi, and Wolper, */
  11. /* presented at the PSTV Conference, held in 1995, Warsaw, Poland 1995. */
  12. #include <stdlib.h>
  13. #include <ctype.h>
  14. #include "tl.h"
  15. static Symbol *symtab[Nhash+1];
  16. static int tl_lex(void);
  17. extern YYSTYPE tl_yylval;
  18. extern char yytext[];
  19. #define Token(y) tl_yylval = tl_nn(y,ZN,ZN); return y
  20. static void
  21. tl_getword(int first, int (*tst)(int))
  22. { int i=0; char c;
  23. yytext[i++] = (char ) first;
  24. while (tst(c = tl_Getchar()))
  25. yytext[i++] = c;
  26. yytext[i] = '\0';
  27. tl_UnGetchar();
  28. }
  29. static int
  30. tl_follow(int tok, int ifyes, int ifno)
  31. { int c;
  32. char buf[32];
  33. extern int tl_yychar;
  34. if ((c = tl_Getchar()) == tok)
  35. return ifyes;
  36. tl_UnGetchar();
  37. tl_yychar = c;
  38. sprintf(buf, "expected '%c'", tok);
  39. tl_yyerror(buf); /* no return from here */
  40. return ifno;
  41. }
  42. int
  43. tl_yylex(void)
  44. { int c = tl_lex();
  45. #if 0
  46. printf("c = %d\n", c);
  47. #endif
  48. return c;
  49. }
  50. static int
  51. tl_lex(void)
  52. { int c;
  53. do {
  54. c = tl_Getchar();
  55. yytext[0] = (char ) c;
  56. yytext[1] = '\0';
  57. if (c <= 0)
  58. { Token(';');
  59. }
  60. } while (c == ' '); /* '\t' is removed in tl_main.c */
  61. if (islower(c))
  62. { tl_getword(c, isalnum_);
  63. if (strcmp("true", yytext) == 0)
  64. { Token(TRUE);
  65. }
  66. if (strcmp("false", yytext) == 0)
  67. { Token(FALSE);
  68. }
  69. tl_yylval = tl_nn(PREDICATE,ZN,ZN);
  70. tl_yylval->sym = tl_lookup(yytext);
  71. return PREDICATE;
  72. }
  73. if (c == '<')
  74. { c = tl_Getchar();
  75. if (c == '>')
  76. { Token(EVENTUALLY);
  77. }
  78. if (c != '-')
  79. { tl_UnGetchar();
  80. tl_yyerror("expected '<>' or '<->'");
  81. }
  82. c = tl_Getchar();
  83. if (c == '>')
  84. { Token(EQUIV);
  85. }
  86. tl_UnGetchar();
  87. tl_yyerror("expected '<->'");
  88. }
  89. switch (c) {
  90. case '/' : c = tl_follow('\\', AND, '/'); break;
  91. case '\\': c = tl_follow('/', OR, '\\'); break;
  92. case '&' : c = tl_follow('&', AND, '&'); break;
  93. case '|' : c = tl_follow('|', OR, '|'); break;
  94. case '[' : c = tl_follow(']', ALWAYS, '['); break;
  95. case '-' : c = tl_follow('>', IMPLIES, '-'); break;
  96. case '!' : c = NOT; break;
  97. case 'U' : c = U_OPER; break;
  98. case 'V' : c = V_OPER; break;
  99. #ifdef NXT
  100. case 'X' : c = NEXT; break;
  101. #endif
  102. default : break;
  103. }
  104. Token(c);
  105. }
  106. Symbol *
  107. tl_lookup(char *s)
  108. { Symbol *sp;
  109. int h = hash(s);
  110. for (sp = symtab[h]; sp; sp = sp->next)
  111. if (strcmp(sp->name, s) == 0)
  112. return sp;
  113. sp = (Symbol *) tl_emalloc(sizeof(Symbol));
  114. sp->name = (char *) tl_emalloc((int) strlen(s) + 1);
  115. strcpy(sp->name, s);
  116. sp->next = symtab[h];
  117. symtab[h] = sp;
  118. return sp;
  119. }
  120. Symbol *
  121. getsym(Symbol *s)
  122. { Symbol *n = (Symbol *) tl_emalloc(sizeof(Symbol));
  123. n->name = s->name;
  124. return n;
  125. }