lex.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. /* Token-reader for Bison's input parser,
  2. Copyright (C) 1984, 1986, 1989, 1992 Free Software Foundation, Inc.
  3. Modified (1992) from bison-1.19 by
  4. Wilfred J. Hansen (wjh+@cmu.edu)
  5. Andrew Consortium, Carnegie Mellon University
  6. This file is part of Bison, the GNU Compiler Compiler.
  7. Bison is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11. Bison is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with Bison; see the file COPYING. If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  18. /*
  19. lex() is the entry point. It is called from reader.c.
  20. It returns one of the token-type codes defined in lex.h.
  21. When an identifier is seen, the code IDENTIFIER is returned
  22. and the name is looked up in the symbol table using symtab.c;
  23. symval is set to a pointer to the entry found. */
  24. #include <stdio.h>
  25. #include <ctype.h>
  26. #include "system.h"
  27. #include "files.h"
  28. #include "symtab.h"
  29. #include "lex.h"
  30. #include "new.h"
  31. extern int lineno;
  32. extern int translations;
  33. int parse_percent_token();
  34. /* functions from main.c */
  35. extern void fatal();
  36. extern void warni();
  37. extern void warn();
  38. /* Buffer for storing the current token. */
  39. char *token_buffer;
  40. /* Allocated size of token_buffer, not including space for terminator. */
  41. static int maxtoken;
  42. bucket *symval;
  43. int numval;
  44. static int unlexed; /* these two describe a token to be reread */
  45. static bucket *unlexed_symval; /* by the next call to lex */
  46. void
  47. init_lex()
  48. {
  49. maxtoken = 100;
  50. token_buffer = NEW2 (maxtoken + 1, char);
  51. unlexed = -1;
  52. }
  53. static char *
  54. grow_token_buffer (p)
  55. char *p;
  56. {
  57. int offset = p - token_buffer;
  58. maxtoken *= 2;
  59. token_buffer = (char *) xrealloc(token_buffer, maxtoken + 1);
  60. return token_buffer + offset;
  61. }
  62. int
  63. skip_white_space()
  64. {
  65. register int c;
  66. register int inside;
  67. c = getc(finput);
  68. for (;;)
  69. {
  70. int cplus_comment;
  71. switch (c)
  72. {
  73. case '/':
  74. c = getc(finput);
  75. if (c != '*' && c != '/')
  76. {
  77. warn("unexpected `/' found and ignored");
  78. break;
  79. }
  80. cplus_comment = (c == '/');
  81. c = getc(finput);
  82. inside = 1;
  83. while (inside)
  84. {
  85. if (!cplus_comment && c == '*')
  86. {
  87. while (c == '*')
  88. c = getc(finput);
  89. if (c == '/')
  90. {
  91. inside = 0;
  92. c = getc(finput);
  93. }
  94. }
  95. else if (c == '\n')
  96. {
  97. lineno++;
  98. if (cplus_comment)
  99. inside = 0;
  100. c = getc(finput);
  101. }
  102. else if (c == EOF)
  103. fatal("unterminated comment");
  104. else
  105. c = getc(finput);
  106. }
  107. break;
  108. case '\n':
  109. lineno++;
  110. case ' ':
  111. case '\t':
  112. case '\f':
  113. c = getc(finput);
  114. break;
  115. default:
  116. return (c);
  117. }
  118. }
  119. }
  120. /* do a getc, but give error message if EOF encountered */
  121. int
  122. safegetc(f)
  123. FILE *f;
  124. {
  125. register int c = getc(f);
  126. if (c == EOF)
  127. fatal("Unexpected EOF");
  128. return c;
  129. }
  130. /* read one literal character from finput. process \ escapes.
  131. append the normalized string version of the char to *pp.
  132. assign the character code to *pcode
  133. return 1 unless the character is an unescaped 'term' or \n
  134. report error for \n
  135. */
  136. int
  137. literalchar(pp, pcode, term)
  138. char **pp;
  139. int *pcode;
  140. char term;
  141. {
  142. register int c;
  143. register char *p;
  144. register int code;
  145. int wasquote = 0;
  146. c = safegetc(finput);
  147. if (c == '\n')
  148. {
  149. warn("unescaped newline in constant");
  150. ungetc(c, finput);
  151. code = '?';
  152. wasquote = 1;
  153. }
  154. else if (c != '\\')
  155. {
  156. code = c;
  157. if (c == term)
  158. wasquote = 1;
  159. }
  160. else
  161. {
  162. c = safegetc(finput);
  163. if (c == 't') code = '\t';
  164. else if (c == 'n') code = '\n';
  165. else if (c == 'a') code = '\007';
  166. else if (c == 'r') code = '\r';
  167. else if (c == 'f') code = '\f';
  168. else if (c == 'b') code = '\b';
  169. else if (c == 'v') code = 013;
  170. else if (c == '\\') code = '\\';
  171. else if (c == '\'') code = '\'';
  172. else if (c == '\"') code = '\"';
  173. else if (c <= '7' && c >= '0')
  174. {
  175. code = 0;
  176. while (c <= '7' && c >= '0')
  177. {
  178. code = (code * 8) + (c - '0');
  179. if (code >= 256 || code < 0)
  180. {
  181. warni("octal value outside range 0...255: `\\%o'", code);
  182. code &= 0xFF;
  183. break;
  184. }
  185. c = safegetc(finput);
  186. }
  187. ungetc(c, finput);
  188. }
  189. else if (c == 'x')
  190. {
  191. c = safegetc(finput);
  192. code = 0;
  193. while (1)
  194. {
  195. if (c >= '0' && c <= '9')
  196. code *= 16, code += c - '0';
  197. else if (c >= 'a' && c <= 'f')
  198. code *= 16, code += c - 'a' + 10;
  199. else if (c >= 'A' && c <= 'F')
  200. code *= 16, code += c - 'A' + 10;
  201. else
  202. break;
  203. if (code >= 256 || code<0)
  204. {
  205. warni("hexadecimal value above 255: `\\x%x'", code);
  206. code &= 0xFF;
  207. break;
  208. }
  209. c = safegetc(finput);
  210. }
  211. ungetc(c, finput);
  212. }
  213. else
  214. {
  215. if (c >= 040 && c <= 0177)
  216. warni ("unknown escape sequence `\\%c'", c);
  217. else
  218. warni ("unknown escape sequence: `\\' followed by char code 0x%x", c);
  219. code = '?';
  220. }
  221. } /* has \ */
  222. /* now fill token_buffer with the canonical name for this character
  223. as a literal token. Do not use what the user typed,
  224. so that '\012' and '\n' can be interchangeable. */
  225. p = *pp;
  226. if (code >= 040 && code < 0177)
  227. *p++ = code;
  228. else if (code == '\\') {*p++ = '\\'; *p++ = '\\';}
  229. else if (code == '\'') {*p++ = '\\'; *p++ = '\'';}
  230. else if (code == '\"') {*p++ = '\\'; *p++ = '\"';}
  231. else if (code == '\t') {*p++ = '\\'; *p++ = 't';}
  232. else if (code == '\n') {*p++ = '\\'; *p++ = 'n';}
  233. else if (code == '\r') {*p++ = '\\'; *p++ = 'r';}
  234. else if (code == '\v') {*p++ = '\\'; *p++ = 'v';}
  235. else if (code == '\b') {*p++ = '\\'; *p++ = 'b';}
  236. else if (code == '\f') {*p++ = '\\'; *p++ = 'f';}
  237. else
  238. {
  239. *p++ = '\\';
  240. *p++ = code / 0100 + '0';
  241. *p++ = ((code / 010) & 07) + '0';
  242. *p++ = (code & 07) + '0';
  243. }
  244. *pp = p;
  245. *pcode = code;
  246. return ! wasquote;
  247. }
  248. void
  249. unlex(token)
  250. int token;
  251. {
  252. unlexed = token;
  253. unlexed_symval = symval;
  254. }
  255. int
  256. lex()
  257. {
  258. register int c;
  259. char *p;
  260. if (unlexed >= 0)
  261. {
  262. symval = unlexed_symval;
  263. c = unlexed;
  264. unlexed = -1;
  265. return (c);
  266. }
  267. c = skip_white_space();
  268. *token_buffer = c; /* for error messages (token buffer always valid) */
  269. token_buffer[1] = 0;
  270. switch (c)
  271. {
  272. case EOF:
  273. strcpy(token_buffer, "EOF");
  274. return (ENDFILE);
  275. case 'A': case 'B': case 'C': case 'D': case 'E':
  276. case 'F': case 'G': case 'H': case 'I': case 'J':
  277. case 'K': case 'L': case 'M': case 'N': case 'O':
  278. case 'P': case 'Q': case 'R': case 'S': case 'T':
  279. case 'U': case 'V': case 'W': case 'X': case 'Y':
  280. case 'Z':
  281. case 'a': case 'b': case 'c': case 'd': case 'e':
  282. case 'f': case 'g': case 'h': case 'i': case 'j':
  283. case 'k': case 'l': case 'm': case 'n': case 'o':
  284. case 'p': case 'q': case 'r': case 's': case 't':
  285. case 'u': case 'v': case 'w': case 'x': case 'y':
  286. case 'z':
  287. case '.': case '_':
  288. p = token_buffer;
  289. while (isalnum(c) || c == '_' || c == '.')
  290. {
  291. if (p == token_buffer + maxtoken)
  292. p = grow_token_buffer(p);
  293. *p++ = c;
  294. c = getc(finput);
  295. }
  296. *p = 0;
  297. ungetc(c, finput);
  298. symval = getsym(token_buffer);
  299. return (IDENTIFIER);
  300. case '0': case '1': case '2': case '3': case '4':
  301. case '5': case '6': case '7': case '8': case '9':
  302. {
  303. numval = 0;
  304. p = token_buffer;
  305. while (isdigit(c))
  306. {
  307. if (p == token_buffer + maxtoken)
  308. p = grow_token_buffer(p);
  309. *p++ = c;
  310. numval = numval*10 + c - '0';
  311. c = getc(finput);
  312. }
  313. *p = 0;
  314. ungetc(c, finput);
  315. return (NUMBER);
  316. }
  317. case '\'':
  318. /* parse the literal token and compute character code in code */
  319. translations = -1;
  320. {
  321. int code, discode;
  322. char discard[10], *dp;
  323. p = token_buffer;
  324. *p++ = '\'';
  325. literalchar(&p, &code, '\'');
  326. c = getc(finput);
  327. if (c != '\'')
  328. {
  329. warn("use \"...\" for multicharacter literal tokens");
  330. dp = discard;
  331. while (literalchar(&dp, &discode, '\'')) {}
  332. }
  333. *p++ = '\'';
  334. *p = 0;
  335. symval = getsym(token_buffer);
  336. symval->class = STOKEN;
  337. if (! symval->user_token_number)
  338. symval->user_token_number = code;
  339. return (IDENTIFIER);
  340. }
  341. case '\"':
  342. /* parse the literal string token and treat as an identifier */
  343. translations = -1;
  344. {
  345. int code; /* ignored here */
  346. p = token_buffer;
  347. *p++ = '\"';
  348. while (literalchar(&p, &code, '\"')) /* read up to and including " */
  349. {
  350. if (p >= token_buffer + maxtoken - 4)
  351. p = grow_token_buffer(p);
  352. }
  353. *p = 0;
  354. symval = getsym(token_buffer);
  355. symval->class = STOKEN;
  356. return (IDENTIFIER);
  357. }
  358. case ',':
  359. return (COMMA);
  360. case ':':
  361. return (COLON);
  362. case ';':
  363. return (SEMICOLON);
  364. case '|':
  365. return (BAR);
  366. case '{':
  367. return (LEFT_CURLY);
  368. case '=':
  369. do
  370. {
  371. c = getc(finput);
  372. if (c == '\n') lineno++;
  373. }
  374. while(c==' ' || c=='\n' || c=='\t');
  375. if (c == '{')
  376. {
  377. strcpy(token_buffer, "={");
  378. return(LEFT_CURLY);
  379. }
  380. else
  381. {
  382. ungetc(c, finput);
  383. return(ILLEGAL);
  384. }
  385. case '<':
  386. p = token_buffer;
  387. c = getc(finput);
  388. while (c != '>')
  389. {
  390. if (c == EOF)
  391. fatal("unterminated type name at EOF");
  392. if (c == '\n')
  393. {
  394. warn("unterminated type name");
  395. ungetc(c, finput);
  396. break;
  397. }
  398. if (p == token_buffer + maxtoken)
  399. p = grow_token_buffer(p);
  400. *p++ = c;
  401. c = getc(finput);
  402. }
  403. *p = 0;
  404. return (TYPENAME);
  405. case '%':
  406. return (parse_percent_token());
  407. default:
  408. return (ILLEGAL);
  409. }
  410. }
  411. /* parse a token which starts with %. Assumes the % has already been read and discarded. */
  412. int
  413. parse_percent_token ()
  414. {
  415. register int c;
  416. register char *p;
  417. p = token_buffer;
  418. c = getc(finput);
  419. *p++ = '%';
  420. *p++ = c; /* for error msg */
  421. *p = 0;
  422. switch (c)
  423. {
  424. case '%':
  425. return (TWO_PERCENTS);
  426. case '{':
  427. return (PERCENT_LEFT_CURLY);
  428. case '<':
  429. return (LEFT);
  430. case '>':
  431. return (RIGHT);
  432. case '2':
  433. return (NONASSOC);
  434. case '0':
  435. return (TOKEN);
  436. case '=':
  437. return (PREC);
  438. }
  439. if (!isalpha(c))
  440. return (ILLEGAL);
  441. p = token_buffer;
  442. *p++ = '%';
  443. while (isalpha(c) || c == '_')
  444. {
  445. if (p == token_buffer + maxtoken)
  446. p = grow_token_buffer(p);
  447. *p++ = c;
  448. c = getc(finput);
  449. }
  450. ungetc(c, finput);
  451. *p = 0;
  452. if (strcmp(token_buffer, "%token") == 0
  453. ||
  454. strcmp(token_buffer, "%term") == 0)
  455. return (TOKEN);
  456. else if (strcmp(token_buffer, "%nterm") == 0)
  457. return (NTERM);
  458. else if (strcmp(token_buffer, "%type") == 0)
  459. return (TYPE);
  460. else if (strcmp(token_buffer, "%guard") == 0)
  461. return (GUARD);
  462. else if (strcmp(token_buffer, "%union") == 0)
  463. return (UNION);
  464. else if (strcmp(token_buffer, "%expect") == 0)
  465. return (EXPECT);
  466. else if (strcmp(token_buffer, "%thong") == 0)
  467. return (THONG);
  468. else if (strcmp(token_buffer, "%start") == 0)
  469. return (START);
  470. else if (strcmp(token_buffer, "%left") == 0)
  471. return (LEFT);
  472. else if (strcmp(token_buffer, "%right") == 0)
  473. return (RIGHT);
  474. else if (strcmp(token_buffer, "%nonassoc") == 0
  475. ||
  476. strcmp(token_buffer, "%binary") == 0)
  477. return (NONASSOC);
  478. else if (strcmp(token_buffer, "%semantic_parser") == 0)
  479. return (SEMANTIC_PARSER);
  480. else if (strcmp(token_buffer, "%pure_parser") == 0)
  481. return (PURE_PARSER);
  482. else if (strcmp(token_buffer, "%prec") == 0)
  483. return (PREC);
  484. else return (ILLEGAL);
  485. }