lex.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /* Token-reader for Bison's input parser,
  2. Copyright (C) 1984, 1986 Bob Corbett and Free Software Foundation, Inc.
  3. BISON is distributed in the hope that it will be useful, but WITHOUT ANY
  4. WARRANTY. No author or distributor accepts responsibility to anyone
  5. for the consequences of using it or for whether it serves any
  6. particular purpose or works at all, unless he says so in writing.
  7. Refer to the BISON General Public License for full details.
  8. Everyone is granted permission to copy, modify and redistribute BISON,
  9. but only under the conditions described in the BISON General Public
  10. License. A copy of this license is supposed to have been given to you
  11. along with BISON so you can know your rights and responsibilities. It
  12. should be in a file named COPYING. Among other things, the copyright
  13. notice and this notice must be preserved on all copies.
  14. In other words, you are welcome to use, share and improve this program.
  15. You are forbidden to forbid anyone else to use, share and improve
  16. what you give them. Help stamp out software-hoarding! */
  17. /*
  18. lex() is the entry point. It is called from reader.c.
  19. It returns one of the token-type codes defined in lex.h.
  20. When an identifier is seen, the code IDENTIFIER is returned
  21. and the name is looked up in the symbol table using symtab.c;
  22. symval is set to a pointer to the entry found. */
  23. #include <stdio.h>
  24. #include <ctype.h>
  25. #include "files.h"
  26. #include "symtab.h"
  27. #include "lex.h"
  28. extern int lineno;
  29. extern int translations;
  30. char token_buffer[MAXTOKEN + 1];
  31. bucket *symval;
  32. int numval;
  33. static int unlexed; /* these two describe a token to be reread */
  34. static bucket *unlexed_symval; /* by the next call to lex */
  35. init_lex()
  36. {
  37. unlexed = -1;
  38. }
  39. int
  40. skip_white_space()
  41. {
  42. register int c;
  43. register int inside;
  44. c = getc(finput);
  45. for (;;)
  46. {
  47. switch (c)
  48. {
  49. case '/':
  50. c = getc(finput);
  51. if (c != '*')
  52. fatals("unexpected '/%c' found",c);
  53. c = getc(finput);
  54. inside = 1;
  55. while (inside)
  56. {
  57. if (c == '*')
  58. {
  59. while (c == '*')
  60. c = getc(finput);
  61. if (c == '/')
  62. {
  63. inside = 0;
  64. c = getc(finput);
  65. }
  66. }
  67. else if (c == '\n')
  68. {
  69. lineno++;
  70. c = getc(finput);
  71. }
  72. else if (c == EOF)
  73. fatal("unterminated comment");
  74. else
  75. c = getc(finput);
  76. }
  77. break;
  78. case '\n':
  79. lineno++;
  80. case ' ':
  81. case '\t':
  82. case '\f':
  83. c = getc(finput);
  84. break;
  85. default:
  86. return (c);
  87. }
  88. }
  89. }
  90. unlex(token)
  91. int token;
  92. {
  93. unlexed = token;
  94. unlexed_symval = symval;
  95. }
  96. int
  97. lex()
  98. {
  99. register int c;
  100. register char *p;
  101. if (unlexed >= 0)
  102. {
  103. symval = unlexed_symval;
  104. c = unlexed;
  105. unlexed = -1;
  106. return (c);
  107. }
  108. c = skip_white_space();
  109. switch (c)
  110. {
  111. case EOF:
  112. return (ENDFILE);
  113. case 'A': case 'B': case 'C': case 'D': case 'E':
  114. case 'F': case 'G': case 'H': case 'I': case 'J':
  115. case 'K': case 'L': case 'M': case 'N': case 'O':
  116. case 'P': case 'Q': case 'R': case 'S': case 'T':
  117. case 'U': case 'V': case 'W': case 'X': case 'Y':
  118. case 'Z':
  119. case 'a': case 'b': case 'c': case 'd': case 'e':
  120. case 'f': case 'g': case 'h': case 'i': case 'j':
  121. case 'k': case 'l': case 'm': case 'n': case 'o':
  122. case 'p': case 'q': case 'r': case 's': case 't':
  123. case 'u': case 'v': case 'w': case 'x': case 'y':
  124. case 'z':
  125. case '.': case '_':
  126. p = token_buffer;
  127. while (isalnum(c) || c == '_' || c == '.')
  128. {
  129. if (p < token_buffer + MAXTOKEN)
  130. *p++ = c;
  131. c = getc(finput);
  132. }
  133. *p = 0;
  134. ungetc(c, finput);
  135. symval = getsym(token_buffer);
  136. return (IDENTIFIER);
  137. case '0': case '1': case '2': case '3': case '4':
  138. case '5': case '6': case '7': case '8': case '9':
  139. {
  140. numval = 0;
  141. while (isdigit(c))
  142. {
  143. numval = numval*10 + c - '0';
  144. c = getc(finput);
  145. }
  146. ungetc(c, finput);
  147. return (NUMBER);
  148. }
  149. case '\'':
  150. translations = -1;
  151. /* parse the literal token and compute character code in code */
  152. c = getc(finput);
  153. {
  154. register int code = 0;
  155. if (c == '\\')
  156. {
  157. c = getc(finput);
  158. if (c <= '7' && c >= '0')
  159. {
  160. while (c <= '7' && c >= '0')
  161. {
  162. code = (code * 8) + (c - '0');
  163. c = getc(finput);
  164. }
  165. if (code >= 128 || code<0)/* JF this said if(c>=128) */
  166. fatals("malformatted literal token '\\%03o'",code);
  167. }
  168. else
  169. {
  170. if (c == 't')
  171. code = '\t';
  172. else if (c == 'n')
  173. code = '\n';
  174. else if (c == 'r')
  175. code = '\r';
  176. else if (c == 'f')
  177. code = '\f';
  178. else if (c == 'b')
  179. code = '\b';
  180. else if (c == '\\')
  181. code = '\\';
  182. else if (c == '\'')
  183. code = '\'';
  184. else if (c == '\"') /* JF this is a good idea */
  185. code = '\"';
  186. else fatals("invalid literal token '\\%c'",c);
  187. c = getc(finput);
  188. }
  189. }
  190. else
  191. {
  192. code = c;
  193. c = getc(finput);
  194. }
  195. if (c != '\'')
  196. fatal("multicharacter literal tokens NOT supported");
  197. /* now fill token_buffer with the canonical name for this character
  198. as a literal token. Do not use what the user typed,
  199. so that '\012' and '\n' can be interchangeable. */
  200. p = token_buffer;
  201. *p++ = '\'';
  202. if (code == '\\')
  203. {
  204. p = token_buffer + 1;
  205. *p++ = '\\';
  206. *p++ = '\\';
  207. }
  208. else if (code == '\'')
  209. {
  210. p = token_buffer + 1;
  211. *p++ = '\\';
  212. *p++ = '\'';
  213. }
  214. else if (code >= 040 && code != 0177)
  215. *p++ = code;
  216. else if (code == '\t')
  217. {
  218. p = token_buffer + 1;
  219. *p++ = '\\';
  220. *p++ = 't';
  221. }
  222. else if (code == '\n')
  223. {
  224. p = token_buffer + 1;
  225. *p++ = '\\';
  226. *p++ = 'n';
  227. }
  228. else if (code == '\r')
  229. {
  230. p = token_buffer + 1;
  231. *p++ = '\\';
  232. *p++ = 'r';
  233. }
  234. else if (code == '\b')
  235. {
  236. p = token_buffer + 1;
  237. *p++ = '\\';
  238. *p++ = 'b';
  239. }
  240. else if (code == '\f')
  241. {
  242. p = token_buffer + 1;
  243. *p++ = '\\';
  244. *p++ = 'f';
  245. }
  246. else
  247. {
  248. *p++ = code / 0100 + '0';
  249. *p++ = ((code / 010) & 07) + '0';
  250. *p++ = (code & 07) + '0';
  251. }
  252. *p++ = '\'';
  253. *p = 0;
  254. symval = getsym(token_buffer);
  255. symval->class = STOKEN;
  256. if (! symval->user_token_number)
  257. symval->user_token_number = code;
  258. return (IDENTIFIER);
  259. }
  260. case ',':
  261. return (COMMA);
  262. case ':':
  263. return (COLON);
  264. case ';':
  265. return (SEMICOLON);
  266. case '|':
  267. return (BAR);
  268. case '{':
  269. return (LEFT_CURLY);
  270. case '=':
  271. c = getc(finput);
  272. if (c == '{')
  273. return(LEFT_CURLY);
  274. else
  275. {
  276. ungetc(c, finput);
  277. return(ILLEGAL);
  278. }
  279. case '<':
  280. p = token_buffer;
  281. c = getc(finput);
  282. while (c != '>')
  283. {
  284. if (c == '\n' || c == EOF)
  285. fatal("unterminated type name");
  286. if (p >= token_buffer + MAXTOKEN - 1)
  287. fatals("type name too long (%d max)",MAXTOKEN-1);
  288. *p++ = c;
  289. c = getc(finput);
  290. }
  291. *p = 0;
  292. return (TYPENAME);
  293. case '%':
  294. return (parse_percent_token());
  295. default:
  296. return (ILLEGAL);
  297. }
  298. }
  299. /* parse a token which starts with %. Assumes the % has already been read and discarded. */
  300. int
  301. parse_percent_token ()
  302. {
  303. register int c;
  304. register char *p;
  305. p = token_buffer;
  306. c = getc(finput);
  307. switch (c)
  308. {
  309. case '%':
  310. return (TWO_PERCENTS);
  311. case '{':
  312. return (PERCENT_LEFT_CURLY);
  313. case '<':
  314. return (LEFT);
  315. case '>':
  316. return (RIGHT);
  317. case '2':
  318. return (NONASSOC);
  319. case '0':
  320. return (TOKEN);
  321. case '=':
  322. return (PREC);
  323. }
  324. if (!isalpha(c))
  325. return (ILLEGAL);
  326. while (isalpha(c) || c == '_')
  327. {
  328. if (p < token_buffer + MAXTOKEN)
  329. *p++ = c;
  330. c = getc(finput);
  331. }
  332. ungetc(c, finput);
  333. *p = 0;
  334. if (strcmp(token_buffer, "token") == 0
  335. ||
  336. strcmp(token_buffer, "term") == 0)
  337. return (TOKEN);
  338. else if (strcmp(token_buffer, "nterm") == 0)
  339. return (NTERM);
  340. else if (strcmp(token_buffer, "type") == 0)
  341. return (TYPE);
  342. else if (strcmp(token_buffer, "guard") == 0)
  343. return (GUARD);
  344. else if (strcmp(token_buffer, "union") == 0)
  345. return (UNION);
  346. else if (strcmp(token_buffer, "start") == 0)
  347. return (START);
  348. else if (strcmp(token_buffer, "left") == 0)
  349. return (LEFT);
  350. else if (strcmp(token_buffer, "right") == 0)
  351. return (RIGHT);
  352. else if (strcmp(token_buffer, "nonassoc") == 0
  353. ||
  354. strcmp(token_buffer, "binary") == 0)
  355. return (NONASSOC);
  356. else if (strcmp(token_buffer, "semantic_parser") == 0)
  357. return (SEMANTIC_PARSER);
  358. else if (strcmp(token_buffer, "pure_parser") == 0)
  359. return (PURE_PARSER);
  360. else if (strcmp(token_buffer, "prec") == 0)
  361. return (PREC);
  362. else return (ILLEGAL);
  363. }