lex.c 9.7 KB

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