LEX.C 9.4 KB

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