1
0

YYLEX.C 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include <lex.h>
  2. #define ERROR 256 /* yacc's value */
  3. static int llset(void);
  4. static int llinp(char **exp);
  5. static int lexgetc(char **exp)
  6. {
  7. char c= **exp;
  8. if (c) (*exp)++;
  9. return( c != 0 ? c : -1);
  10. }
  11. static int tst__b(register int c, char tab[])
  12. {
  13. return (tab[(c >> 3) & 037] & (1 << (c & 07)) );
  14. }
  15. static char *llsave[16]; /* Look ahead buffer */
  16. static char llbuf[100]; /* work buffer */
  17. static char *llp1 = &llbuf[0]; /* pointer to next avail. in token */
  18. static char *llp2 = &llbuf[0]; /* pointer to end of lookahead */
  19. static char *llend = &llbuf[0]; /* pointer to end of token */
  20. static char *llebuf = &llbuf[sizeof llbuf];
  21. static int lleof;
  22. static int yyline = 0;
  23. extern struct lextab lextab;
  24. int gettoken(char *lltb, int lltbsiz)
  25. {
  26. register char *lp, *tp, *ep;
  27. tp = lltb;
  28. ep = tp+lltbsiz-1;
  29. for (lp = llbuf; lp < llend && tp < ep;)
  30. *tp++ = *lp++;
  31. *tp = 0;
  32. return(tp-lltb);
  33. }
  34. int yylex(char **exp)
  35. {
  36. register int c, st;
  37. int final, l, llk, i;
  38. register struct lextab *lp;
  39. char *cp;
  40. while (1)
  41. {
  42. llk = 0;
  43. if (llset()) return(0);
  44. st = 0;
  45. final = -1;
  46. lp = &lextab;
  47. do {
  48. if (lp->lllook && (l = lp->lllook[st])) {
  49. for (c=0; c<NBPW; c++)
  50. if (l&(1<<c))
  51. llsave[c] = llp1;
  52. llk++;
  53. }
  54. if ((i = lp->llfinal[st]) != -1) {
  55. final = i;
  56. llend = llp1;
  57. }
  58. if ((c = llinp(exp)) < 0)
  59. break;
  60. if ((cp = lp->llbrk) && llk==0 && tst__b(c, cp)) {
  61. llp1--;
  62. break;
  63. }
  64. } while ((st = (*lp->llmove)(lp, c, st)) != -1);
  65. if (llp2 < llp1)
  66. llp2 = llp1;
  67. if (final == -1) {
  68. llend = llp1;
  69. if (st == 0 && c < 0)
  70. return(0);
  71. if ((cp = lp->llill) && tst__b(c, cp)) {
  72. continue;
  73. }
  74. return(ERROR);
  75. }
  76. if (c = (final >> 11) & 037)
  77. llend = llsave[c-1];
  78. if ((c = (*lp->llactr)(final&03777)) >= 0)
  79. return(c);
  80. }
  81. }
  82. void llinit(viud)
  83. {
  84. llp1 = llp2 = llend = llbuf;
  85. llebuf = llbuf + sizeof(llbuf);
  86. lleof = yyline = 0;
  87. }
  88. static int llinp(char **exp)
  89. {
  90. register c;
  91. register struct lextab *lp;
  92. register char *cp;
  93. lp = &lextab;
  94. cp = lp->llign; /* Ignore class */
  95. for (;;) {
  96. /*
  97. * Get the next character from the save buffer (if possible)
  98. * If the save buffer's empty, then return EOF or the next
  99. * input character. Ignore the character if it's in the
  100. * ignore class.
  101. */
  102. c = (llp1 < llp2) ? *llp1 & 0377 : (lleof) ? EOF : lexgetc(exp);
  103. if (c >= 0) { /* Got a character? */
  104. if (cp && tst__b(c, cp))
  105. continue; /* Ignore it */
  106. if (llp1 >= llebuf) { /* No, is there room? */
  107. return -1;
  108. }
  109. *llp1++ = c; /* Store in token buff */
  110. } else
  111. lleof = 1; /* Set EOF signal */
  112. return(c);
  113. }
  114. }
  115. static int llset(void)
  116. /*
  117. * Return TRUE if EOF and nothing was moved in the look-ahead buffer
  118. */
  119. {
  120. register char *lp1, *lp2;
  121. for (lp1 = llbuf, lp2 = llend; lp2 < llp2;)
  122. *lp1++ = *lp2++;
  123. llend = llp1 = llbuf;
  124. llp2 = lp1;
  125. return(lleof && lp1 == llbuf);
  126. }