lex.c 9.0 KB

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