LLlex.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. /*
  2. * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands.
  3. * See the copyright notice in the ACK home directory, in the file "Copyright".
  4. */
  5. /* $Id$ */
  6. /* L E X I C A L A N A L Y Z E R */
  7. #include <string.h>
  8. #include "idfsize.h"
  9. #include "numsize.h"
  10. #include "strsize.h"
  11. #include <alloc.h>
  12. #include "input.h"
  13. #include "arith.h"
  14. #include "macro.h"
  15. #include "idf.h"
  16. #include "LLlex.h"
  17. #include "Lpars.h"
  18. #include "class.h"
  19. #include "bits.h"
  20. #define BUFSIZ 1024
  21. struct token dot;
  22. char *string_token(char *nm, int stop_char);
  23. arith char_constant(char *nm);
  24. int val_in_base(int ch, int base);
  25. int quoted(int ch);
  26. int trigraph();
  27. int ReplaceMacros = 1; /* replacing macros */
  28. int AccDefined = 0; /* accept "defined(...)" */
  29. int UnknownIdIsZero = 0; /* interpret unknown id as integer 0 */
  30. int Unstacked = 0; /* an unstack is done */
  31. int AccFileSpecifier = 0; /* return filespecifier <...> */
  32. int LexSave = 0; /* last character read by GetChar */
  33. extern int InputLevel; /* # of current macro expansions */
  34. #define FLG_ESEEN 0x01 /* possibly a floating point number */
  35. #define FLG_DOTSEEN 0x02 /* certainly a floating point number */
  36. int LLlex()
  37. {
  38. return (DOT != EOF) ? GetToken(&dot) : EOF;
  39. }
  40. int GetToken(struct token *ptok)
  41. {
  42. /* GetToken() is the actual token recognizer. It calls the
  43. control line interpreter if it encounters a "\n{w}*#"
  44. combination. Macro replacement is also performed if it is
  45. needed.
  46. */
  47. char buf[BUFSIZ];
  48. register int ch, nch;
  49. again: /* rescan the input after an error or replacement */
  50. ch = GetChar();
  51. /* rescan, the following character has been read */
  52. if ((ch & 0200) && ch != EOI) /* stop on non-ascii character */
  53. fatal("non-ascii '\\%03o' read", ch & 0377);
  54. /* keep track of the place of the token in the file */
  55. switch (class(ch)) { /* detect character class */
  56. case STNL: /* newline, vertical space or formfeed */
  57. LineNumber++;
  58. return ptok->tk_symb = EOF;
  59. case STSKIP: /* just skip the skip characters */
  60. goto again;
  61. case STGARB: /* garbage character */
  62. garbage:
  63. if (040 < ch && ch < 0177)
  64. error("garbage char %c", ch);
  65. else
  66. error("garbage char \\%03o", ch);
  67. goto again;
  68. case STSIMP: /* a simple character, no part of compound token*/
  69. return ptok->tk_symb = ch;
  70. case STCOMP: /* maybe the start of a compound token */
  71. nch = GetChar(); /* character lookahead */
  72. switch (ch) {
  73. case '!':
  74. if (nch == '=')
  75. return ptok->tk_symb = NOTEQUAL;
  76. UnGetChar();
  77. return ptok->tk_symb = ch;
  78. case '&':
  79. if (nch == '&')
  80. return ptok->tk_symb = AND;
  81. else if (nch == '=')
  82. return ptok->tk_symb = ANDAB;
  83. UnGetChar();
  84. return ptok->tk_symb = ch;
  85. case '+':
  86. if (nch == '+')
  87. return ptok->tk_symb = PLUSPLUS;
  88. else if (nch == '=')
  89. return ptok->tk_symb = PLUSAB;
  90. UnGetChar();
  91. return ptok->tk_symb = ch;
  92. case '-':
  93. if (nch == '-')
  94. return ptok->tk_symb = MINMIN;
  95. else if (nch == '>')
  96. return ptok->tk_symb = ARROW;
  97. else if (nch == '=')
  98. return ptok->tk_symb = MINAB;
  99. UnGetChar();
  100. return ptok->tk_symb = ch;
  101. case '<':
  102. if (AccFileSpecifier) {
  103. UnGetChar(); /* pushback nch */
  104. ptok->tk_str =
  105. string_token("file specifier", '>');
  106. return ptok->tk_symb = FILESPECIFIER;
  107. } else if (nch == '<') {
  108. if ((nch = GetChar()) == '=')
  109. return ptok->tk_symb = LEFTAB;
  110. UnGetChar();
  111. return ptok->tk_symb = LEFT;
  112. } else if (nch == '=')
  113. return ptok->tk_symb = LESSEQ;
  114. UnGetChar();
  115. return ptok->tk_symb = ch;
  116. case '=':
  117. if (nch == '=')
  118. return ptok->tk_symb = EQUAL;
  119. UnGetChar();
  120. return ptok->tk_symb = ch;
  121. case '>':
  122. if (nch == '=')
  123. return ptok->tk_symb = GREATEREQ;
  124. else if (nch == '>') {
  125. if ((nch = GetChar()) == '=')
  126. return ptok->tk_symb = RIGHTAB;
  127. UnGetChar();
  128. return ptok->tk_symb = RIGHT;
  129. }
  130. UnGetChar();
  131. return ptok->tk_symb = ch;
  132. case '|':
  133. if (nch == '|')
  134. return ptok->tk_symb = OR;
  135. else if (nch == '=')
  136. return ptok->tk_symb = ORAB;
  137. UnGetChar();
  138. return ptok->tk_symb = ch;
  139. case '%':
  140. if (nch == '=')
  141. return ptok->tk_symb = MODAB;
  142. UnGetChar();
  143. return ptok->tk_symb = ch;
  144. case '*':
  145. if (nch == '=')
  146. return ptok->tk_symb = TIMESAB;
  147. UnGetChar();
  148. return ptok->tk_symb = ch;
  149. case '^':
  150. if (nch == '=')
  151. return ptok->tk_symb = XORAB;
  152. UnGetChar();
  153. return ptok->tk_symb = ch;
  154. case '/':
  155. if (nch == '*' && !InputLevel) {
  156. skipcomment();
  157. goto again;
  158. }
  159. else if (nch == '=')
  160. return ptok->tk_symb = DIVAB;
  161. UnGetChar();
  162. return ptok->tk_symb = ch;
  163. default:
  164. crash("bad class for char 0%o", ch);
  165. /* NOTREACHED */
  166. }
  167. case STCHAR: /* character constant */
  168. ptok->tk_val = char_constant("character");
  169. return ptok->tk_symb = INTEGER;
  170. case STSTR: /* string */
  171. ptok->tk_str = string_token("string", '"');
  172. return ptok->tk_symb = STRING;
  173. case STELL: /* wide character constant/string prefix */
  174. nch = GetChar();
  175. if (nch == '"') {
  176. ptok->tk_str =
  177. string_token("wide character string", '"');
  178. return ptok->tk_symb = STRING;
  179. } else if (nch == '\'') {
  180. ptok->tk_val = char_constant("wide character");
  181. return ptok->tk_symb = INTEGER;
  182. }
  183. UnGetChar();
  184. /* fallthrough */
  185. case STIDF:
  186. {
  187. extern int idfsize; /* ??? */
  188. register char *tg = &buf[0];
  189. register char *maxpos = &buf[idfsize];
  190. int NoExpandNext = 0;
  191. #define tstmac(bx) if (!(bits[ch] & bx)) goto nomac
  192. #define cpy *tg++ = ch
  193. #define load (ch = GetChar()); if (!in_idf(ch)) goto endidf
  194. if (Unstacked) EnableMacros(); /* unstack macro's when allowed. */
  195. if (ch == NOEXPM) {
  196. NoExpandNext = 1;
  197. ch = GetChar();
  198. }
  199. #ifdef DOBITS
  200. cpy; tstmac(bit0); load;
  201. cpy; tstmac(bit1); load;
  202. cpy; tstmac(bit2); load;
  203. cpy; tstmac(bit3); load;
  204. cpy; tstmac(bit4); load;
  205. cpy; tstmac(bit5); load;
  206. cpy; tstmac(bit6); load;
  207. cpy; tstmac(bit7); load;
  208. #endif
  209. for(;;) {
  210. if (tg < maxpos) {
  211. cpy;
  212. }
  213. load;
  214. }
  215. endidf:
  216. /*if (ch != EOI) UnGetChar();*/
  217. UnGetChar();
  218. *tg++ = '\0'; /* mark the end of the identifier */
  219. if (ReplaceMacros) {
  220. register struct idf *idef = findidf(buf);
  221. if (idef && idef->id_macro && !NoExpandNext) {
  222. if (replace(idef))
  223. goto again;
  224. }
  225. }
  226. nomac: /* buf can already be null-terminated. soit */
  227. ch = GetChar();
  228. while (in_idf(ch)) {
  229. if (tg < maxpos) *tg++ = ch;
  230. ch = GetChar();
  231. }
  232. UnGetChar();
  233. *tg++ = '\0'; /* mark the end of the identifier */
  234. NoExpandNext = 0;
  235. if (UnknownIdIsZero) {
  236. ptok->tk_val = (arith)0;
  237. return ptok->tk_symb = INTEGER;
  238. }
  239. ptok->tk_str = Malloc((unsigned)(tg - buf));
  240. strcpy(ptok->tk_str, buf);
  241. return IDENTIFIER;
  242. }
  243. case STNUM: /* a numeric constant */
  244. { /* it may only be an integer constant */
  245. int base = 10, vch;
  246. arith val = 0;
  247. int ovfl = 0;
  248. arith ubound = ~(1<<(sizeof(arith)*8-1))/(base/2);
  249. /* Since the preprocessor only knows integers and has
  250. * nothing to do with ellipsis we just return when the
  251. * pp-number starts with a '.'
  252. */
  253. if (ch == '.') {
  254. return ptok->tk_symb = ch;
  255. }
  256. if (ch == '0') {
  257. ch = GetChar();
  258. if (ch == 'x' || ch == 'X') {
  259. base = 16;
  260. ch = GetChar();
  261. } else {
  262. base = 8;
  263. }
  264. }
  265. while ((vch = val_in_base(ch, base)) >= 0) {
  266. if (val < 0 || val > ubound) ovfl++;
  267. val *= base;
  268. if (val < 0 && val + vch >= 0) ovfl++;
  269. val += vch;
  270. ch = GetChar();
  271. }
  272. ptok->tk_unsigned = 0;
  273. if (ch == 'u' || ch == 'U') {
  274. ptok->tk_unsigned = 1;
  275. ch = GetChar();
  276. if (ch == 'l' || ch == 'L') {
  277. ch = GetChar();
  278. }
  279. }
  280. else if (ch == 'l' || ch == 'L') {
  281. ch = GetChar();
  282. if (ch == 'u' || ch == 'U') {
  283. ptok->tk_unsigned = 1;
  284. ch = GetChar();
  285. }
  286. }
  287. if (ovfl) {
  288. warning("overflow in constant");
  289. ptok->tk_unsigned = 1;
  290. }
  291. else if (val < 0) {
  292. /* give warning??? */
  293. ptok->tk_unsigned = 1;
  294. }
  295. UnGetChar();
  296. ptok->tk_val = val;
  297. return ptok->tk_symb = INTEGER;
  298. }
  299. case STEOI: /* end of text on source file */
  300. return ptok->tk_symb = EOF;
  301. case STMSPEC:
  302. if (!InputLevel) goto garbage;
  303. if (ch == TOKSEP) goto again;
  304. /* fallthrough shouldn't happen */
  305. default: /* this cannot happen */
  306. crash("bad class for char 0%o", ch);
  307. }
  308. /*NOTREACHED*/
  309. return -1;
  310. }
  311. void skipcomment()
  312. {
  313. /* The last character read has been the '*' of '/_*'. The
  314. characters, except NL and EOI, between '/_*' and the first
  315. occurring '*_/' are not interpreted.
  316. NL only affects the LineNumber. EOI is not legal.
  317. Important note: it is not possible to stop skipping comment
  318. beyond the end-of-file of an included file.
  319. EOI is returned by LoadChar only on encountering EOF of the
  320. top-level file...
  321. */
  322. register int c;
  323. NoUnstack++;
  324. c = GetChar();
  325. do {
  326. while (c != '*') {
  327. if (class(c) == STNL) {
  328. ++LineNumber;
  329. } else if (c == EOI) {
  330. NoUnstack--;
  331. return;
  332. }
  333. c = GetChar();
  334. } /* last Character seen was '*' */
  335. c = GetChar();
  336. } while (c != '/');
  337. NoUnstack--;
  338. }
  339. arith char_constant(char *nm)
  340. {
  341. register arith val = 0;
  342. register int ch;
  343. int size = 0;
  344. ch = GetChar();
  345. if (ch == '\'')
  346. error("%s constant too short", nm);
  347. else
  348. while (ch != '\'') {
  349. if (ch == '\n') {
  350. error("newline in %s constant", nm);
  351. LineNumber++;
  352. break;
  353. }
  354. if (ch == '\\')
  355. ch = quoted(GetChar());
  356. if (ch >= 128) ch -= 256;
  357. if (size < sizeof(arith))
  358. val |= ch << (8 * size);
  359. size++;
  360. ch = GetChar();
  361. }
  362. if (size > sizeof(arith))
  363. error("%s constant too long", nm);
  364. else if (size > 1)
  365. strict("%s constant includes more than one character", nm);
  366. return val;
  367. }
  368. char *string_token(char *nm, int stop_char)
  369. {
  370. register int ch;
  371. register int str_size;
  372. register char *str = Malloc((unsigned) (str_size = ISTRSIZE));
  373. register int pos = 0;
  374. ch = GetChar();
  375. while (ch != stop_char) {
  376. if (ch == '\n') {
  377. error("newline in %s", nm);
  378. LineNumber++;
  379. break;
  380. }
  381. if (ch == EOI) {
  382. error("end-of-file inside %s", nm);
  383. break;
  384. }
  385. if (ch == '\\' && !AccFileSpecifier)
  386. ch = quoted(GetChar());
  387. str[pos++] = ch;
  388. if (pos == str_size)
  389. str = Realloc(str, (unsigned)(str_size <<= 1));
  390. ch = GetChar();
  391. }
  392. str[pos++] = '\0'; /* for filenames etc. */
  393. str = Realloc(str, (unsigned)pos);
  394. return str;
  395. }
  396. int quoted(int ch)
  397. {
  398. /* quoted() replaces an escaped character sequence by the
  399. character meant.
  400. */
  401. /* first char after backslash already in ch */
  402. if (!is_oct(ch)) { /* a quoted char */
  403. switch (ch) {
  404. case 'n':
  405. ch = '\n';
  406. break;
  407. case 't':
  408. ch = '\t';
  409. break;
  410. case 'b':
  411. ch = '\b';
  412. break;
  413. case 'r':
  414. ch = '\r';
  415. break;
  416. case 'f':
  417. ch = '\f';
  418. break;
  419. case 'a': /* alert */
  420. ch = '\007';
  421. break;
  422. case 'v': /* vertical tab */
  423. ch = '\013';
  424. break;
  425. case 'x': /* quoted hex */
  426. {
  427. register int hex = 0;
  428. register int vch;
  429. for (;;) {
  430. ch = GetChar();
  431. if (vch = val_in_base(ch, 16), vch == -1)
  432. break;
  433. hex = hex * 16 + vch;
  434. }
  435. UnGetChar();
  436. ch = hex;
  437. }
  438. }
  439. } else { /* a quoted octal */
  440. register int oct = 0, cnt = 0;
  441. do {
  442. oct = oct*8 + (ch-'0');
  443. ch = GetChar();
  444. } while (is_oct(ch) && ++cnt < 3);
  445. UnGetChar();
  446. ch = oct;
  447. }
  448. return ch&0377;
  449. }
  450. int val_in_base(int ch, int base)
  451. {
  452. switch (base) {
  453. case 8:
  454. return (is_dig(ch) && ch < '9') ? ch - '0' : -1;
  455. case 10:
  456. return is_dig(ch) ? ch - '0' : -1;
  457. case 16:
  458. return is_dig(ch) ? ch - '0'
  459. : is_hex(ch) ? (ch - 'a' + 10) & 017
  460. : -1;
  461. default:
  462. fatal("(val_in_base) illegal base value %d", base);
  463. /* NOTREACHED */
  464. }
  465. return -1;
  466. }
  467. int GetChar()
  468. {
  469. /* The routines GetChar and trigraph parses the trigraph
  470. sequences and removes occurences of \\\n.
  471. */
  472. register int ch;
  473. again:
  474. LoadChar(ch);
  475. /* possible trigraph sequence */
  476. if (ch == '?')
  477. ch = trigraph();
  478. /* \\\n are removed from the input stream */
  479. if (ch == '\\') {
  480. LoadChar(ch);
  481. if (ch == '\n') {
  482. ++LineNumber;
  483. goto again;
  484. }
  485. PushBack();
  486. ch = '\\';
  487. }
  488. return(LexSave = ch);
  489. }
  490. int trigraph()
  491. {
  492. register int ch;
  493. LoadChar(ch);
  494. if (ch == '?') {
  495. LoadChar(ch);
  496. switch (ch) { /* its a trigraph */
  497. case '=':
  498. ch = '#';
  499. return(ch);
  500. case '(':
  501. ch = '[';
  502. return(ch);
  503. case '/':
  504. ch = '\\';
  505. return(ch);
  506. case ')':
  507. ch = ']';
  508. return(ch);
  509. case '\'':
  510. ch = '^';
  511. return(ch);
  512. case '<':
  513. ch = '{';
  514. return(ch);
  515. case '!':
  516. ch = '|';
  517. return(ch);
  518. case '>':
  519. ch = '}';
  520. return(ch);
  521. case '-':
  522. ch = '~';
  523. return(ch);
  524. }
  525. PushBack();
  526. }
  527. PushBack();
  528. return('?');
  529. }