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 "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_str =
  180. string_token("file specifier", '>');
  181. return ptok->tk_symb = FILESPECIFIER;
  182. }
  183. if (nch == '<')
  184. return ptok->tk_symb = LEFT;
  185. if (nch == '=')
  186. return ptok->tk_symb = LESSEQ;
  187. PushBack();
  188. return ptok->tk_symb = ch;
  189. case '=':
  190. if (nch == '=')
  191. return ptok->tk_symb = EQUAL;
  192. /* The following piece of code tries to recognise
  193. old-fashioned assignment operators `=op'
  194. */
  195. switch (nch) {
  196. case '+':
  197. return ptok->tk_symb = PLUSAB;
  198. case '-':
  199. return ptok->tk_symb = MINAB;
  200. case '*':
  201. return ptok->tk_symb = TIMESAB;
  202. case '/':
  203. return ptok->tk_symb = DIVAB;
  204. case '%':
  205. return ptok->tk_symb = MODAB;
  206. case '>':
  207. case '<':
  208. LoadChar(ch);
  209. if (ch != nch) {
  210. PushBack();
  211. lexerror("illegal combination '=%c'",
  212. nch);
  213. }
  214. return ptok->tk_symb =
  215. nch == '<' ? LEFTAB : RIGHTAB;
  216. case '&':
  217. return ptok->tk_symb = ANDAB;
  218. case '^':
  219. return ptok->tk_symb = XORAB;
  220. case '|':
  221. return ptok->tk_symb = ORAB;
  222. }
  223. PushBack();
  224. return ptok->tk_symb = ch;
  225. case '>':
  226. if (nch == '=')
  227. return ptok->tk_symb = GREATEREQ;
  228. if (nch == '>')
  229. return ptok->tk_symb = RIGHT;
  230. PushBack();
  231. return ptok->tk_symb = ch;
  232. case '|':
  233. if (nch == '|')
  234. return ptok->tk_symb = OR;
  235. PushBack();
  236. return ptok->tk_symb = ch;
  237. }
  238. case STIDF:
  239. {
  240. register char *tg = &buf[0];
  241. register int pos = -1;
  242. register int hash;
  243. register struct idf *idef;
  244. extern int idfsize; /* ??? */
  245. hash = STARTHASH();
  246. do { /* read the identifier */
  247. if (++pos < idfsize) {
  248. *tg++ = ch;
  249. hash = ENHASH(hash, ch, pos);
  250. }
  251. LoadChar(ch);
  252. } while (in_idf(ch));
  253. hash = STOPHASH(hash);
  254. if (ch != EOI)
  255. PushBack();
  256. *tg++ = '\0'; /* mark the end of the identifier */
  257. idef = ptok->tk_idf = idf_hashed(buf, tg - buf, hash);
  258. #ifndef NOPP
  259. if (idef->id_macro && ReplaceMacros) {
  260. /* macro replacement should be performed */
  261. if (replace(idef))
  262. goto again;
  263. /* arrived here: something went wrong in
  264. replace, don't substitute in this case
  265. */
  266. }
  267. else
  268. if (UnknownIdIsZero) {
  269. ptok->tk_ival = (arith)0;
  270. ptok->tk_fund = INT;
  271. return ptok->tk_symb = INTEGER;
  272. }
  273. #endif NOPP
  274. ptok->tk_symb = (
  275. idef->id_reserved ?
  276. idef->id_reserved :
  277. idef->id_def && idef->id_def->df_sc == TYPEDEF ?
  278. TYPE_IDENTIFIER :
  279. IDENTIFIER
  280. );
  281. return IDENTIFIER;
  282. }
  283. case STCHAR: /* character constant */
  284. {
  285. register arith val = 0, size = 0;
  286. LoadChar(ch);
  287. if (ch == '\'')
  288. lexerror("character constant too short");
  289. else
  290. while (ch != '\'') {
  291. if (ch == '\n') {
  292. lexerror("newline in character constant");
  293. LineNumber++;
  294. break;
  295. }
  296. if (ch == '\\') {
  297. LoadChar(ch);
  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_str = string_token("string", '"');
  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. 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. }
  333. if (ch == '0') {
  334. *np++ = ch;
  335. LoadChar(ch);
  336. if (ch == 'x' || ch == 'X') {
  337. base = 16;
  338. LoadChar(ch);
  339. }
  340. else
  341. base = 8;
  342. }
  343. while (vch = val_in_base(ch, base), vch >= 0) {
  344. val = val*base + vch;
  345. if (np < &buf[NUMSIZE])
  346. *np++ = ch;
  347. LoadChar(ch);
  348. }
  349. if (ch == 'l' || ch == 'L') {
  350. ptok->tk_ival = val;
  351. ptok->tk_fund = LONG;
  352. return ptok->tk_symb = INTEGER;
  353. }
  354. if (base == 16 || !(ch == '.' || ch == 'e' || ch == 'E')) {
  355. PushBack();
  356. ptok->tk_ival = val;
  357. /* The semantic analyser must know if the
  358. integral constant is given in octal/hexa-
  359. decimal form, in which case its type is
  360. UNSIGNED, or in decimal form, in which case
  361. its type is signed, indicated by
  362. the fund INTEGER.
  363. */
  364. ptok->tk_fund =
  365. (base == 10 || (base == 8 && val == (arith)0))
  366. ? INTEGER : UNSIGNED;
  367. return ptok->tk_symb = INTEGER;
  368. }
  369. /* where's the test for the length of the integral ??? */
  370. if (ch == '.'){
  371. if (np < &buf[NUMSIZE])
  372. *np++ = ch;
  373. LoadChar(ch);
  374. }
  375. while (is_dig(ch)){
  376. if (np < &buf[NUMSIZE])
  377. *np++ = ch;
  378. LoadChar(ch);
  379. }
  380. if (ch == 'e' || ch == 'E') {
  381. if (np < &buf[NUMSIZE])
  382. *np++ = ch;
  383. LoadChar(ch);
  384. if (ch == '+' || ch == '-') {
  385. if (np < &buf[NUMSIZE])
  386. *np++ = ch;
  387. LoadChar(ch);
  388. }
  389. if (!is_dig(ch)) {
  390. lexerror("malformed floating constant");
  391. if (np < &buf[NUMSIZE])
  392. *np++ = ch;
  393. }
  394. while (is_dig(ch)) {
  395. if (np < &buf[NUMSIZE])
  396. *np++ = ch;
  397. LoadChar(ch);
  398. }
  399. }
  400. PushBack();
  401. *np++ = '\0';
  402. buf[0] = '-'; /* good heavens... */
  403. if (np == &buf[NUMSIZE+1]) {
  404. lexerror("floating constant too long");
  405. ptok->tk_fval = Salloc("0.0", 5) + 1;
  406. }
  407. else
  408. ptok->tk_fval = Salloc(buf, np - buf) + 1;
  409. return ptok->tk_symb = FLOATING;
  410. }
  411. case STEOI: /* end of text on source file */
  412. return ptok->tk_symb = EOI;
  413. default: /* this cannot happen */
  414. crash("bad class for char 0%o", ch);
  415. }
  416. /*NOTREACHED*/
  417. }
  418. skipcomment()
  419. {
  420. /* The last character read has been the '*' of '/_*'. The
  421. characters, except NL and EOI, between '/_*' and the first
  422. occurring '*_/' are not interpreted.
  423. NL only affects the LineNumber. EOI is not legal.
  424. Important note: it is not possible to stop skipping comment
  425. beyond the end-of-file of an included file.
  426. EOI is returned by LoadChar only on encountering EOF of the
  427. top-level file...
  428. */
  429. register int c;
  430. NoUnstack++;
  431. LoadChar(c);
  432. do {
  433. while (c != '*') {
  434. if (class(c) == STNL)
  435. ++LineNumber;
  436. else
  437. if (c == EOI) {
  438. NoUnstack--;
  439. return;
  440. }
  441. LoadChar(c);
  442. }
  443. /* Last Character seen was '*' */
  444. LoadChar(c);
  445. } while (c != '/');
  446. NoUnstack--;
  447. }
  448. char *
  449. string_token(nm, stop_char)
  450. char *nm;
  451. {
  452. register int ch;
  453. register int str_size;
  454. register char *str = Malloc(str_size = ISTRSIZE);
  455. register int pos = 0;
  456. LoadChar(ch);
  457. while (ch != stop_char) {
  458. if (ch == '\n') {
  459. lexerror("newline in %s", nm);
  460. LineNumber++;
  461. break;
  462. }
  463. if (ch == EOI) {
  464. lexerror("end-of-file inside %s", nm);
  465. break;
  466. }
  467. if (ch == '\\') {
  468. register int nch;
  469. LoadChar(nch);
  470. if (nch == '\n') {
  471. LineNumber++;
  472. LoadChar(ch);
  473. continue;
  474. }
  475. else {
  476. str[pos++] = '\\';
  477. if (pos == str_size)
  478. str = Srealloc(str, str_size += RSTRSIZE);
  479. ch = nch;
  480. }
  481. }
  482. str[pos++] = ch;
  483. if (pos == str_size)
  484. str = Srealloc(str, str_size += RSTRSIZE);
  485. LoadChar(ch);
  486. }
  487. str[pos++] = '\0';
  488. return str;
  489. }
  490. int
  491. quoted(ch)
  492. register int ch;
  493. {
  494. /* quoted() replaces an escaped character sequence by the
  495. character meant.
  496. */
  497. /* first char after backslash already in ch */
  498. if (!is_oct(ch)) { /* a quoted char */
  499. switch (ch) {
  500. case 'n':
  501. ch = '\n';
  502. break;
  503. case 't':
  504. ch = '\t';
  505. break;
  506. case 'b':
  507. ch = '\b';
  508. break;
  509. case 'r':
  510. ch = '\r';
  511. break;
  512. case 'f':
  513. ch = '\f';
  514. break;
  515. }
  516. }
  517. else { /* a quoted octal */
  518. register int oct = 0, cnt = 0;
  519. do {
  520. oct = oct*8 + (ch-'0');
  521. LoadChar(ch);
  522. } while (is_oct(ch) && ++cnt < 3);
  523. PushBack();
  524. ch = oct;
  525. }
  526. return ch&0377;
  527. }
  528. /* provisional */
  529. int
  530. val_in_base(ch, base)
  531. register int ch;
  532. {
  533. return
  534. is_dig(ch) ? ch - '0' :
  535. base != 16 ? -1 :
  536. is_hex(ch) ? (ch - 'a' + 10) & 017 :
  537. -1;
  538. }