LLlex.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  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. /* $Header$ */
  6. /* L E X I C A L A N A L Y Z E R */
  7. #include <alloc.h>
  8. #include "nofloat.h"
  9. #include "idfsize.h"
  10. #include "numsize.h"
  11. #include "debug.h"
  12. #include "strsize.h"
  13. #include "nopp.h"
  14. #include "input.h"
  15. #include "arith.h"
  16. #include "def.h"
  17. #include "idf.h"
  18. #include "LLlex.h"
  19. #include "Lpars.h"
  20. #include "class.h"
  21. #include "assert.h"
  22. #include "sizes.h"
  23. /* Data about the token yielded */
  24. struct token dot, ahead, aside;
  25. int ReplaceMacros = 1; /* replacing macros */
  26. int EoiForNewline = 0; /* return EOI upon encountering newline */
  27. int PreProcKeys = 0; /* return preprocessor key */
  28. int AccFileSpecifier = 0; /* return filespecifier <...> */
  29. int AccDefined = 0; /* accept "defined(...)" */
  30. int UnknownIdIsZero = 0; /* interpret unknown id as integer 0 */
  31. int SkipEscNewline = 0; /* how to interpret backslash-newline */
  32. int Unstacked = 0; /* an unstack is done */
  33. int File_Inserted = 0; /* a file has just been inserted */
  34. #define MAX_LL_DEPTH 2
  35. static struct token LexStack[MAX_LL_DEPTH];
  36. static LexSP = 0;
  37. /* In PushLex() the actions are taken in order to initialise or
  38. re-initialise the lexical scanner.
  39. E.g. at the invocation of a sub-parser that uses LLlex(), the
  40. state of the current parser should be saved.
  41. */
  42. PushLex()
  43. {
  44. ASSERT(LexSP < 2);
  45. ASSERT(ASIDE == 0); /* ASIDE = 0; */
  46. GetToken(&ahead);
  47. ahead.tk_line = LineNumber;
  48. ahead.tk_file = FileName;
  49. LexStack[LexSP++] = dot;
  50. }
  51. PopLex()
  52. {
  53. ASSERT(LexSP > 0);
  54. dot = LexStack[--LexSP];
  55. }
  56. int
  57. LLlex()
  58. {
  59. /* LLlex() plays the role of Lexical Analyzer for the C parser.
  60. The look-ahead and putting aside of tokens are taken into
  61. account.
  62. */
  63. if (ASIDE) { /* a token is put aside */
  64. dot = aside;
  65. ASIDE = 0;
  66. }
  67. else { /* read ahead and return the old one */
  68. dot = ahead;
  69. /* the following test is performed due to the dual
  70. task of LLlex(): it is also called for parsing the
  71. restricted constant expression following a #if or
  72. #elif. The newline character causes EOF to be
  73. returned in this case to stop the LLgen parsing task.
  74. */
  75. if (DOT != EOI)
  76. GetToken(&ahead);
  77. else
  78. DOT = EOF;
  79. }
  80. /* keep track of the place of the token in the file */
  81. ahead.tk_file = FileName;
  82. ahead.tk_line = LineNumber;
  83. return DOT;
  84. }
  85. char *string_token();
  86. int
  87. GetToken(ptok)
  88. register struct token *ptok;
  89. {
  90. /* GetToken() is the actual token recognizer. It calls the
  91. control line interpreter if it encounters a "\n#"
  92. combination. Macro replacement is also performed if it is
  93. needed.
  94. */
  95. char buf[(IDFSIZE > NUMSIZE ? IDFSIZE : NUMSIZE) + 1];
  96. register int ch, nch;
  97. if (File_Inserted) {
  98. File_Inserted = 0;
  99. goto firstline;
  100. }
  101. again: /* rescan the input after an error or replacement */
  102. #ifndef NOPP
  103. if (Unstacked) EnableMacros();
  104. #endif
  105. LoadChar(ch);
  106. go_on: /* rescan, the following character has been read */
  107. if ((ch & 0200) && ch != EOI) /* stop on non-ascii character */
  108. fatal("non-ascii '\\%03o' read", ch & 0377);
  109. switch (class(ch)) { /* detect character class */
  110. case STNL: /* newline, vertical space or formfeed */
  111. firstline:
  112. LineNumber++; /* also at vs and ff */
  113. if (EoiForNewline) /* called in control line */
  114. /* a newline in a control line indicates the
  115. end-of-information of the line.
  116. */
  117. return ptok->tk_symb = EOI;
  118. while (LoadChar(ch), ch == '#') { /* a control line follows */
  119. domacro();
  120. if (File_Inserted) {
  121. File_Inserted = 0;
  122. goto firstline;
  123. }
  124. }
  125. /* We have to loop here, because in
  126. `domacro' the nl, vt or ff is read. The
  127. character following it may again be a `#'.
  128. */
  129. goto go_on;
  130. case STSKIP: /* just skip the skip characters */
  131. goto again;
  132. case STGARB: /* garbage character */
  133. #ifndef NOPP
  134. if (SkipEscNewline && (ch == '\\')) {
  135. /* a '\\' is allowed in #if/#elif expression */
  136. LoadChar(ch);
  137. if (class(ch) == STNL) { /* vt , ff ? */
  138. ++LineNumber;
  139. goto again;
  140. }
  141. PushBack();
  142. ch = '\\';
  143. }
  144. #endif NOPP
  145. if (040 < ch && ch < 0177)
  146. lexerror("garbage char %c", ch);
  147. else
  148. lexerror("garbage char \\%03o", ch);
  149. goto again;
  150. case STSIMP: /* a simple character, no part of compound token*/
  151. if (ch == '/') { /* probably the start of comment */
  152. LoadChar(ch);
  153. if (ch == '*') { /* start of comment */
  154. skipcomment();
  155. goto again;
  156. }
  157. else {
  158. PushBack();
  159. ch = '/'; /* restore ch */
  160. }
  161. }
  162. return ptok->tk_symb = ch;
  163. case STCOMP: /* maybe the start of a compound token */
  164. LoadChar(nch); /* character lookahead */
  165. switch (ch) {
  166. case '!':
  167. if (nch == '=')
  168. return ptok->tk_symb = NOTEQUAL;
  169. PushBack();
  170. return ptok->tk_symb = ch;
  171. case '&':
  172. if (nch == '&')
  173. return ptok->tk_symb = AND;
  174. PushBack();
  175. return ptok->tk_symb = ch;
  176. case '+':
  177. if (nch == '+')
  178. return ptok->tk_symb = PLUSPLUS;
  179. PushBack();
  180. return ptok->tk_symb = ch;
  181. case '-':
  182. if (nch == '-')
  183. return ptok->tk_symb = MINMIN;
  184. if (nch == '>')
  185. return ptok->tk_symb = ARROW;
  186. PushBack();
  187. return ptok->tk_symb = ch;
  188. case '<':
  189. if (AccFileSpecifier) {
  190. PushBack(); /* pushback nch */
  191. ptok->tk_bts = string_token("file specifier",
  192. '>', &(ptok->tk_len));
  193. return ptok->tk_symb = FILESPECIFIER;
  194. }
  195. if (nch == '<')
  196. return ptok->tk_symb = LEFT;
  197. if (nch == '=')
  198. return ptok->tk_symb = LESSEQ;
  199. PushBack();
  200. return ptok->tk_symb = ch;
  201. case '=':
  202. if (nch == '=')
  203. return ptok->tk_symb = EQUAL;
  204. /* The following piece of code tries to recognise
  205. old-fashioned assignment operators `=op'
  206. */
  207. switch (nch) {
  208. case '+':
  209. return ptok->tk_symb = PLUSAB;
  210. case '-':
  211. return ptok->tk_symb = MINAB;
  212. case '*':
  213. return ptok->tk_symb = TIMESAB;
  214. case '/':
  215. return ptok->tk_symb = DIVAB;
  216. case '%':
  217. return ptok->tk_symb = MODAB;
  218. case '>':
  219. case '<':
  220. LoadChar(ch);
  221. if (ch != nch) {
  222. PushBack();
  223. lexerror("illegal combination '=%c'",
  224. nch);
  225. }
  226. return ptok->tk_symb =
  227. nch == '<' ? LEFTAB : RIGHTAB;
  228. case '&':
  229. return ptok->tk_symb = ANDAB;
  230. case '^':
  231. return ptok->tk_symb = XORAB;
  232. case '|':
  233. return ptok->tk_symb = ORAB;
  234. }
  235. PushBack();
  236. return ptok->tk_symb = ch;
  237. case '>':
  238. if (nch == '=')
  239. return ptok->tk_symb = GREATEREQ;
  240. if (nch == '>')
  241. return ptok->tk_symb = RIGHT;
  242. PushBack();
  243. return ptok->tk_symb = ch;
  244. case '|':
  245. if (nch == '|')
  246. return ptok->tk_symb = OR;
  247. PushBack();
  248. return ptok->tk_symb = ch;
  249. }
  250. case STIDF:
  251. {
  252. register char *tg = &buf[0];
  253. register int pos = -1;
  254. register int hash;
  255. register struct idf *idef;
  256. extern int idfsize; /* ??? */
  257. hash = STARTHASH();
  258. do { /* read the identifier */
  259. if (++pos < idfsize) {
  260. #ifndef NOPP
  261. if (Unstacked) EnableMacros();
  262. #endif
  263. *tg++ = ch;
  264. hash = ENHASH(hash, ch, pos);
  265. }
  266. LoadChar(ch);
  267. } while (in_idf(ch));
  268. hash = STOPHASH(hash);
  269. if (ch != EOI)
  270. PushBack();
  271. *tg++ = '\0'; /* mark the end of the identifier */
  272. idef = ptok->tk_idf = idf_hashed(buf, tg - buf, hash);
  273. #ifndef NOPP
  274. if (idef->id_macro && ReplaceMacros && replace(idef))
  275. /* macro replacement should be performed */
  276. goto again;
  277. if (UnknownIdIsZero && idef->id_reserved != SIZEOF) {
  278. ptok->tk_ival = (arith)0;
  279. ptok->tk_fund = INT;
  280. return ptok->tk_symb = INTEGER;
  281. }
  282. #endif NOPP
  283. ptok->tk_symb = (
  284. idef->id_reserved ? idef->id_reserved
  285. : idef->id_def && idef->id_def->df_sc == TYPEDEF ?
  286. TYPE_IDENTIFIER
  287. : IDENTIFIER
  288. );
  289. return IDENTIFIER;
  290. }
  291. case STCHAR: /* character constant */
  292. {
  293. register arith val = 0, size = 0;
  294. LoadChar(ch);
  295. if (ch == '\'')
  296. lexerror("character constant too short");
  297. else
  298. while (ch != '\'') {
  299. if (ch == '\n') {
  300. lexerror("newline in character constant");
  301. LineNumber++;
  302. break;
  303. }
  304. if (ch == '\\') {
  305. LoadChar(ch);
  306. if (ch == '\n')
  307. LineNumber++;
  308. ch = quoted(ch);
  309. }
  310. if (ch >= 128) ch -= 256;
  311. val = val*256 + ch;
  312. size++;
  313. LoadChar(ch);
  314. }
  315. if (size > int_size)
  316. lexerror("character constant too long");
  317. ptok->tk_ival = val;
  318. ptok->tk_fund = INT;
  319. return ptok->tk_symb = INTEGER;
  320. }
  321. case STSTR: /* string */
  322. ptok->tk_bts = string_token("string", '"', &(ptok->tk_len));
  323. return ptok->tk_symb = STRING;
  324. case STNUM: /* a numeric constant */
  325. {
  326. /* It should be noted that 099 means 81(decimal) and
  327. 099.5 means 99.5 . This severely limits the tricks
  328. we can use to scan a numeric value.
  329. */
  330. register char *np = &buf[1];
  331. register int base = 10;
  332. register int vch;
  333. register arith val = 0;
  334. if (ch == '.') { /* an embarrassing ambiguity */
  335. #ifndef NOFLOAT
  336. LoadChar(vch);
  337. PushBack();
  338. if (!is_dig(vch)) /* just a `.' */
  339. return ptok->tk_symb = ch;
  340. *np++ = '0';
  341. /* in the rest of the compiler, all floats
  342. have to start with a digit.
  343. */
  344. #else NOFLOAT
  345. return ptok->tk_symb = ch;
  346. #endif NOFLOAT
  347. }
  348. if (ch == '0') {
  349. *np++ = ch;
  350. LoadChar(ch);
  351. if (ch == 'x' || ch == 'X') {
  352. base = 16;
  353. LoadChar(ch);
  354. }
  355. else
  356. base = 8;
  357. }
  358. while (vch = val_in_base(ch, base), vch >= 0) {
  359. val = val*base + vch;
  360. if (np < &buf[NUMSIZE])
  361. *np++ = ch;
  362. LoadChar(ch);
  363. }
  364. if (ch == 'l' || ch == 'L') {
  365. ptok->tk_ival = val;
  366. ptok->tk_fund = LONG;
  367. return ptok->tk_symb = INTEGER;
  368. }
  369. #ifndef NOFLOAT
  370. if (base == 16 || !(ch == '.' || ch == 'e' || ch == 'E'))
  371. #endif NOFLOAT
  372. {
  373. PushBack();
  374. ptok->tk_ival = val;
  375. /* The semantic analyser must know if the
  376. integral constant is given in octal/hexa-
  377. decimal form, in which case its type is
  378. UNSIGNED, or in decimal form, in which case
  379. its type is signed, indicated by
  380. the fund INTEGER.
  381. */
  382. ptok->tk_fund =
  383. (base == 10 || (base == 8 && val == (arith)0))
  384. ? INTEGER : UNSIGNED;
  385. return ptok->tk_symb = INTEGER;
  386. }
  387. /* where's the test for the length of the integral ??? */
  388. #ifndef NOFLOAT
  389. if (ch == '.'){
  390. if (np < &buf[NUMSIZE])
  391. *np++ = ch;
  392. LoadChar(ch);
  393. }
  394. while (is_dig(ch)){
  395. if (np < &buf[NUMSIZE])
  396. *np++ = ch;
  397. LoadChar(ch);
  398. }
  399. if (ch == 'e' || ch == 'E') {
  400. if (np < &buf[NUMSIZE])
  401. *np++ = ch;
  402. LoadChar(ch);
  403. if (ch == '+' || ch == '-') {
  404. if (np < &buf[NUMSIZE])
  405. *np++ = ch;
  406. LoadChar(ch);
  407. }
  408. if (!is_dig(ch)) {
  409. lexerror("malformed floating constant");
  410. if (np < &buf[NUMSIZE])
  411. *np++ = ch;
  412. }
  413. while (is_dig(ch)) {
  414. if (np < &buf[NUMSIZE])
  415. *np++ = ch;
  416. LoadChar(ch);
  417. }
  418. }
  419. PushBack();
  420. *np++ = '\0';
  421. buf[0] = '-'; /* good heavens... */
  422. if (np == &buf[NUMSIZE+1]) {
  423. lexerror("floating constant too long");
  424. ptok->tk_fval = Salloc("0.0",(unsigned) 5) + 1;
  425. }
  426. else
  427. ptok->tk_fval = Salloc(buf,(unsigned) (np - buf)) + 1;
  428. return ptok->tk_symb = FLOATING;
  429. #endif NOFLOAT
  430. }
  431. case STEOI: /* end of text on source file */
  432. return ptok->tk_symb = EOI;
  433. default: /* this cannot happen */
  434. crash("bad class for char 0%o", ch);
  435. }
  436. /*NOTREACHED*/
  437. }
  438. skipcomment()
  439. {
  440. /* The last character read has been the '*' of '/_*'. The
  441. characters, except NL and EOI, between '/_*' and the first
  442. occurring '*_/' are not interpreted.
  443. NL only affects the LineNumber. EOI is not legal.
  444. Important note: it is not possible to stop skipping comment
  445. beyond the end-of-file of an included file.
  446. EOI is returned by LoadChar only on encountering EOF of the
  447. top-level file...
  448. */
  449. register int c;
  450. NoUnstack++;
  451. LoadChar(c);
  452. do {
  453. while (c != '*') {
  454. if (class(c) == STNL)
  455. ++LineNumber;
  456. else
  457. if (c == EOI) {
  458. NoUnstack--;
  459. return;
  460. }
  461. LoadChar(c);
  462. } /* last Character seen was '*' */
  463. LoadChar(c);
  464. } while (c != '/');
  465. NoUnstack--;
  466. }
  467. char *
  468. string_token(nm, stop_char, plen)
  469. char *nm;
  470. int *plen;
  471. {
  472. register int ch;
  473. register int str_size;
  474. register char *str = Malloc((unsigned) (str_size = ISTRSIZE));
  475. register int pos = 0;
  476. LoadChar(ch);
  477. while (ch != stop_char) {
  478. if (ch == '\n') {
  479. lexerror("newline in %s", nm);
  480. LineNumber++;
  481. break;
  482. }
  483. if (ch == EOI) {
  484. lexerror("end-of-file inside %s", nm);
  485. break;
  486. }
  487. if (ch == '\\') {
  488. LoadChar(ch);
  489. if (ch == '\n') {
  490. LineNumber++;
  491. LoadChar(ch);
  492. continue;
  493. }
  494. ch = quoted(ch);
  495. }
  496. str[pos++] = ch;
  497. if (pos == str_size)
  498. str = Srealloc(str, (unsigned) (str_size += RSTRSIZE));
  499. LoadChar(ch);
  500. }
  501. str[pos++] = '\0'; /* for filenames etc. */
  502. *plen = pos;
  503. return str;
  504. }
  505. int
  506. quoted(ch)
  507. register int ch;
  508. {
  509. /* quoted() replaces an escaped character sequence by the
  510. character meant.
  511. */
  512. /* first char after backslash already in ch */
  513. if (!is_oct(ch)) { /* a quoted char */
  514. switch (ch) {
  515. case 'n':
  516. ch = '\n';
  517. break;
  518. case 't':
  519. ch = '\t';
  520. break;
  521. case 'b':
  522. ch = '\b';
  523. break;
  524. case 'r':
  525. ch = '\r';
  526. break;
  527. case 'f':
  528. ch = '\f';
  529. break;
  530. }
  531. }
  532. else { /* a quoted octal */
  533. register int oct = 0, cnt = 0;
  534. do {
  535. oct = oct*8 + (ch-'0');
  536. LoadChar(ch);
  537. } while (is_oct(ch) && ++cnt < 3);
  538. PushBack();
  539. ch = oct;
  540. }
  541. return ch&0377;
  542. }
  543. /* provisional */
  544. int
  545. val_in_base(ch, base)
  546. register int ch;
  547. {
  548. return
  549. is_dig(ch) ? ch - '0'
  550. : base != 16 ? -1
  551. : is_hex(ch) ? (ch - 'a' + 10) & 017
  552. : -1;
  553. }