lex.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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 2, 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. #include "new.h"
  28. extern int lineno;
  29. extern int translations;
  30. int parse_percent_token();
  31. extern void fatals();
  32. extern void fatal();
  33. /* Buffer for storing the current token. */
  34. char *token_buffer;
  35. /* Allocated size of token_buffer, not including space for terminator. */
  36. static int maxtoken;
  37. bucket *symval;
  38. int numval;
  39. static int unlexed; /* these two describe a token to be reread */
  40. static bucket *unlexed_symval; /* by the next call to lex */
  41. void
  42. init_lex()
  43. {
  44. maxtoken = 100;
  45. token_buffer = NEW2 (maxtoken + 1, char);
  46. unlexed = -1;
  47. }
  48. static char *
  49. grow_token_buffer (p)
  50. char *p;
  51. {
  52. int offset = p - token_buffer;
  53. maxtoken *= 2;
  54. token_buffer = (char *) xrealloc(token_buffer, maxtoken + 1);
  55. return token_buffer + offset;
  56. }
  57. int
  58. skip_white_space()
  59. {
  60. register int c;
  61. register int inside;
  62. c = getc(finput);
  63. for (;;)
  64. {
  65. int cplus_comment;
  66. switch (c)
  67. {
  68. case '/':
  69. c = getc(finput);
  70. if (c != '*' && c != '/')
  71. fatals("unexpected `/%c' found",c);
  72. cplus_comment = (c == '/');
  73. c = getc(finput);
  74. inside = 1;
  75. while (inside)
  76. {
  77. if (!cplus_comment && c == '*')
  78. {
  79. while (c == '*')
  80. c = getc(finput);
  81. if (c == '/')
  82. {
  83. inside = 0;
  84. c = getc(finput);
  85. }
  86. }
  87. else if (c == '\n')
  88. {
  89. lineno++;
  90. if (cplus_comment)
  91. inside = 0;
  92. c = getc(finput);
  93. }
  94. else if (c == EOF)
  95. fatal("unterminated comment");
  96. else
  97. c = getc(finput);
  98. }
  99. break;
  100. case '\n':
  101. lineno++;
  102. case ' ':
  103. case '\t':
  104. case '\f':
  105. c = getc(finput);
  106. break;
  107. default:
  108. return (c);
  109. }
  110. }
  111. }
  112. void
  113. unlex(token)
  114. int token;
  115. {
  116. unlexed = token;
  117. unlexed_symval = symval;
  118. }
  119. int
  120. lex()
  121. {
  122. register int c;
  123. register char *p;
  124. if (unlexed >= 0)
  125. {
  126. symval = unlexed_symval;
  127. c = unlexed;
  128. unlexed = -1;
  129. return (c);
  130. }
  131. c = skip_white_space();
  132. switch (c)
  133. {
  134. case EOF:
  135. return (ENDFILE);
  136. case 'A': case 'B': case 'C': case 'D': case 'E':
  137. case 'F': case 'G': case 'H': case 'I': case 'J':
  138. case 'K': case 'L': case 'M': case 'N': case 'O':
  139. case 'P': case 'Q': case 'R': case 'S': case 'T':
  140. case 'U': case 'V': case 'W': case 'X': case 'Y':
  141. case 'Z':
  142. case 'a': case 'b': case 'c': case 'd': case 'e':
  143. case 'f': case 'g': case 'h': case 'i': case 'j':
  144. case 'k': case 'l': case 'm': case 'n': case 'o':
  145. case 'p': case 'q': case 'r': case 's': case 't':
  146. case 'u': case 'v': case 'w': case 'x': case 'y':
  147. case 'z':
  148. case '.': case '_':
  149. p = token_buffer;
  150. while (isalnum(c) || c == '_' || c == '.')
  151. {
  152. if (p == token_buffer + maxtoken)
  153. p = grow_token_buffer(p);
  154. *p++ = c;
  155. c = getc(finput);
  156. }
  157. *p = 0;
  158. ungetc(c, finput);
  159. symval = getsym(token_buffer);
  160. return (IDENTIFIER);
  161. case '0': case '1': case '2': case '3': case '4':
  162. case '5': case '6': case '7': case '8': case '9':
  163. {
  164. numval = 0;
  165. while (isdigit(c))
  166. {
  167. numval = numval*10 + c - '0';
  168. c = getc(finput);
  169. }
  170. ungetc(c, finput);
  171. return (NUMBER);
  172. }
  173. case '\'':
  174. translations = -1;
  175. /* parse the literal token and compute character code in code */
  176. c = getc(finput);
  177. {
  178. register int code = 0;
  179. if (c == '\\')
  180. {
  181. c = getc(finput);
  182. if (c <= '7' && c >= '0')
  183. {
  184. while (c <= '7' && c >= '0')
  185. {
  186. code = (code * 8) + (c - '0');
  187. c = getc(finput);
  188. if (code >= 256 || code < 0)
  189. fatals("malformatted literal token `\\%03o'", code);
  190. }
  191. }
  192. else
  193. {
  194. if (c == 't')
  195. code = '\t';
  196. else if (c == 'n')
  197. code = '\n';
  198. else if (c == 'a')
  199. code = '\007';
  200. else if (c == 'r')
  201. code = '\r';
  202. else if (c == 'f')
  203. code = '\f';
  204. else if (c == 'b')
  205. code = '\b';
  206. else if (c == 'v')
  207. code = 013;
  208. else if (c == 'x')
  209. {
  210. c = getc(finput);
  211. while ((c <= '9' && c >= '0')
  212. || (c >= 'a' && c <= 'z')
  213. || (c >= 'A' && c <= 'Z'))
  214. {
  215. code *= 16;
  216. if (c <= '9' && c >= '0')
  217. code += c - '0';
  218. else if (c >= 'a' && c <= 'z')
  219. code += c - 'a' + 10;
  220. else if (c >= 'A' && c <= 'Z')
  221. code += c - 'A' + 10;
  222. if (code >= 256 || code<0)/* JF this said if(c>=128) */
  223. fatals("malformatted literal token `\\x%x'",code);
  224. c = getc(finput);
  225. }
  226. ungetc(c, finput);
  227. }
  228. else if (c == '\\')
  229. code = '\\';
  230. else if (c == '\'')
  231. code = '\'';
  232. else if (c == '\"') /* JF this is a good idea */
  233. code = '\"';
  234. else
  235. {
  236. if (c >= 040 && c <= 0177)
  237. fatals ("unknown escape sequence `\\%c'", c);
  238. else
  239. fatals ("unknown escape sequence: `\\' followed by char code 0x%x", c);
  240. }
  241. c = getc(finput);
  242. }
  243. }
  244. else
  245. {
  246. code = c;
  247. c = getc(finput);
  248. }
  249. if (c != '\'')
  250. fatal("multicharacter literal tokens not supported");
  251. /* now fill token_buffer with the canonical name for this character
  252. as a literal token. Do not use what the user typed,
  253. so that '\012' and '\n' can be interchangeable. */
  254. p = token_buffer;
  255. *p++ = '\'';
  256. if (code == '\\')
  257. {
  258. *p++ = '\\';
  259. *p++ = '\\';
  260. }
  261. else if (code == '\'')
  262. {
  263. *p++ = '\\';
  264. *p++ = '\'';
  265. }
  266. else if (code >= 040 && code != 0177)
  267. *p++ = code;
  268. else if (code == '\t')
  269. {
  270. *p++ = '\\';
  271. *p++ = 't';
  272. }
  273. else if (code == '\n')
  274. {
  275. *p++ = '\\';
  276. *p++ = 'n';
  277. }
  278. else if (code == '\r')
  279. {
  280. *p++ = '\\';
  281. *p++ = 'r';
  282. }
  283. else if (code == '\v')
  284. {
  285. *p++ = '\\';
  286. *p++ = 'v';
  287. }
  288. else if (code == '\b')
  289. {
  290. *p++ = '\\';
  291. *p++ = 'b';
  292. }
  293. else if (code == '\f')
  294. {
  295. *p++ = '\\';
  296. *p++ = 'f';
  297. }
  298. else
  299. {
  300. *p++ = code / 0100 + '0';
  301. *p++ = ((code / 010) & 07) + '0';
  302. *p++ = (code & 07) + '0';
  303. }
  304. *p++ = '\'';
  305. *p = 0;
  306. symval = getsym(token_buffer);
  307. symval->class = STOKEN;
  308. if (! symval->user_token_number)
  309. symval->user_token_number = code;
  310. return (IDENTIFIER);
  311. }
  312. case ',':
  313. return (COMMA);
  314. case ':':
  315. return (COLON);
  316. case ';':
  317. return (SEMICOLON);
  318. case '|':
  319. return (BAR);
  320. case '{':
  321. return (LEFT_CURLY);
  322. case '=':
  323. do
  324. {
  325. c = getc(finput);
  326. if (c == '\n') lineno++;
  327. }
  328. while(c==' ' || c=='\n' || c=='\t');
  329. if (c == '{')
  330. return(LEFT_CURLY);
  331. else
  332. {
  333. ungetc(c, finput);
  334. return(ILLEGAL);
  335. }
  336. case '<':
  337. p = token_buffer;
  338. c = getc(finput);
  339. while (c != '>')
  340. {
  341. if (c == '\n' || c == EOF)
  342. fatal("unterminated type name");
  343. if (p == token_buffer + maxtoken)
  344. p = grow_token_buffer(p);
  345. *p++ = c;
  346. c = getc(finput);
  347. }
  348. *p = 0;
  349. return (TYPENAME);
  350. case '%':
  351. return (parse_percent_token());
  352. default:
  353. return (ILLEGAL);
  354. }
  355. }
  356. /* parse a token which starts with %. Assumes the % has already been read and discarded. */
  357. int
  358. parse_percent_token ()
  359. {
  360. register int c;
  361. register char *p;
  362. p = token_buffer;
  363. c = getc(finput);
  364. switch (c)
  365. {
  366. case '%':
  367. return (TWO_PERCENTS);
  368. case '{':
  369. return (PERCENT_LEFT_CURLY);
  370. case '<':
  371. return (LEFT);
  372. case '>':
  373. return (RIGHT);
  374. case '2':
  375. return (NONASSOC);
  376. case '0':
  377. return (TOKEN);
  378. case '=':
  379. return (PREC);
  380. }
  381. if (!isalpha(c))
  382. return (ILLEGAL);
  383. while (isalpha(c) || c == '_')
  384. {
  385. if (p == token_buffer + maxtoken)
  386. p = grow_token_buffer(p);
  387. *p++ = c;
  388. c = getc(finput);
  389. }
  390. ungetc(c, finput);
  391. *p = 0;
  392. if (strcmp(token_buffer, "token") == 0
  393. ||
  394. strcmp(token_buffer, "term") == 0)
  395. return (TOKEN);
  396. else if (strcmp(token_buffer, "nterm") == 0)
  397. return (NTERM);
  398. else if (strcmp(token_buffer, "type") == 0)
  399. return (TYPE);
  400. else if (strcmp(token_buffer, "guard") == 0)
  401. return (GUARD);
  402. else if (strcmp(token_buffer, "union") == 0)
  403. return (UNION);
  404. else if (strcmp(token_buffer, "expect") == 0)
  405. return (EXPECT);
  406. else if (strcmp(token_buffer, "start") == 0)
  407. return (START);
  408. else if (strcmp(token_buffer, "left") == 0)
  409. return (LEFT);
  410. else if (strcmp(token_buffer, "right") == 0)
  411. return (RIGHT);
  412. else if (strcmp(token_buffer, "nonassoc") == 0
  413. ||
  414. strcmp(token_buffer, "binary") == 0)
  415. return (NONASSOC);
  416. else if (strcmp(token_buffer, "semantic_parser") == 0)
  417. return (SEMANTIC_PARSER);
  418. else if (strcmp(token_buffer, "pure_parser") == 0)
  419. return (PURE_PARSER);
  420. else if (strcmp(token_buffer, "prec") == 0)
  421. return (PREC);
  422. else return (ILLEGAL);
  423. }