LLlex.c 13 KB

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