LLlex.c 13 KB

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