LLlex.c 13 KB

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