lex.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. /****************************************************************
  10. Copyright (C) Lucent Technologies 1997
  11. All Rights Reserved
  12. Permission to use, copy, modify, and distribute this software and
  13. its documentation for any purpose and without fee is hereby
  14. granted, provided that the above copyright notice appear in all
  15. copies and that both that the copyright notice and this
  16. permission notice and warranty disclaimer appear in supporting
  17. documentation, and that the name Lucent Technologies or any of
  18. its entities not be used in advertising or publicity pertaining
  19. to distribution of the software without specific, written prior
  20. permission.
  21. LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  22. INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
  23. IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
  24. SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  25. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  26. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  27. ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
  28. THIS SOFTWARE.
  29. ****************************************************************/
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <ctype.h>
  34. #include "awk.h"
  35. #include "y.tab.h"
  36. extern YYSTYPE yylval;
  37. extern int infunc;
  38. int lineno = 1;
  39. int bracecnt = 0;
  40. int brackcnt = 0;
  41. int parencnt = 0;
  42. typedef struct Keyword {
  43. char *word;
  44. int sub;
  45. int type;
  46. } Keyword;
  47. Keyword keywords[] ={ /* keep sorted: binary searched */
  48. { "BEGIN", XBEGIN, XBEGIN },
  49. { "END", XEND, XEND },
  50. { "NF", VARNF, VARNF },
  51. { "atan2", FATAN, BLTIN },
  52. { "break", BREAK, BREAK },
  53. { "close", CLOSE, CLOSE },
  54. { "continue", CONTINUE, CONTINUE },
  55. { "cos", FCOS, BLTIN },
  56. { "delete", DELETE, DELETE },
  57. { "do", DO, DO },
  58. { "else", ELSE, ELSE },
  59. { "exit", EXIT, EXIT },
  60. { "exp", FEXP, BLTIN },
  61. { "fflush", FFLUSH, BLTIN },
  62. { "for", FOR, FOR },
  63. { "func", FUNC, FUNC },
  64. { "function", FUNC, FUNC },
  65. { "getline", GETLINE, GETLINE },
  66. { "gsub", GSUB, GSUB },
  67. { "if", IF, IF },
  68. { "in", IN, IN },
  69. { "index", INDEX, INDEX },
  70. { "int", FINT, BLTIN },
  71. { "length", FLENGTH, BLTIN },
  72. { "log", FLOG, BLTIN },
  73. { "match", MATCHFCN, MATCHFCN },
  74. { "next", NEXT, NEXT },
  75. { "nextfile", NEXTFILE, NEXTFILE },
  76. { "print", PRINT, PRINT },
  77. { "printf", PRINTF, PRINTF },
  78. { "rand", FRAND, BLTIN },
  79. { "return", RETURN, RETURN },
  80. { "sin", FSIN, BLTIN },
  81. { "split", SPLIT, SPLIT },
  82. { "sprintf", SPRINTF, SPRINTF },
  83. { "sqrt", FSQRT, BLTIN },
  84. { "srand", FSRAND, BLTIN },
  85. { "sub", SUB, SUB },
  86. { "substr", SUBSTR, SUBSTR },
  87. { "system", FSYSTEM, BLTIN },
  88. { "tolower", FTOLOWER, BLTIN },
  89. { "toupper", FTOUPPER, BLTIN },
  90. { "utf", FUTF, BLTIN },
  91. { "while", WHILE, WHILE },
  92. };
  93. #define DEBUG
  94. #ifdef DEBUG
  95. #define RET(x) { if(dbg)printf("lex %s\n", tokname(x)); return(x); }
  96. #else
  97. #define RET(x) return(x)
  98. #endif
  99. int peek(void)
  100. {
  101. int c = input();
  102. unput(c);
  103. return c;
  104. }
  105. int gettok(char **pbuf, int *psz) /* get next input token */
  106. {
  107. int c;
  108. char *buf = *pbuf;
  109. int sz = *psz;
  110. char *bp = buf;
  111. c = input();
  112. if (c == 0)
  113. return 0;
  114. buf[0] = c;
  115. buf[1] = 0;
  116. if (!isalnum(c) && c != '.' && c != '_')
  117. return c;
  118. *bp++ = c;
  119. if (isalpha(c) || c == '_') { /* it's a varname */
  120. for ( ; (c = input()) != 0; ) {
  121. if (bp-buf >= sz)
  122. if (!adjbuf(&buf, &sz, bp-buf+2, 100, &bp, 0))
  123. FATAL( "out of space for name %.10s...", buf );
  124. if (isalnum(c) || c == '_')
  125. *bp++ = c;
  126. else {
  127. *bp = 0;
  128. unput(c);
  129. break;
  130. }
  131. }
  132. } else { /* it's a number */
  133. char *rem;
  134. /* read input until can't be a number */
  135. for ( ; (c = input()) != 0; ) {
  136. if (bp-buf >= sz)
  137. if (!adjbuf(&buf, &sz, bp-buf+2, 100, &bp, 0))
  138. FATAL( "out of space for number %.10s...", buf );
  139. if (isdigit(c) || c == 'e' || c == 'E'
  140. || c == '.' || c == '+' || c == '-')
  141. *bp++ = c;
  142. else {
  143. unput(c);
  144. break;
  145. }
  146. }
  147. *bp = 0;
  148. strtod(buf, &rem); /* parse the number */
  149. unputstr(rem); /* put rest back for later */
  150. rem[0] = 0;
  151. }
  152. *pbuf = buf;
  153. *psz = sz;
  154. return buf[0];
  155. }
  156. int word(char *);
  157. int string(void);
  158. int regexpr(void);
  159. int sc = 0; /* 1 => return a } right now */
  160. int reg = 0; /* 1 => return a REGEXPR now */
  161. int yylex(void)
  162. {
  163. int c;
  164. static char *buf = 0;
  165. static int bufsize = 500;
  166. if (buf == 0 && (buf = (char *) malloc(bufsize)) == NULL)
  167. FATAL( "out of space in yylex" );
  168. if (sc) {
  169. sc = 0;
  170. RET('}');
  171. }
  172. if (reg) {
  173. reg = 0;
  174. return regexpr();
  175. }
  176. for (;;) {
  177. c = gettok(&buf, &bufsize);
  178. if (c == 0)
  179. return 0;
  180. if (isalpha(c) || c == '_')
  181. return word(buf);
  182. if (isdigit(c) || c == '.') {
  183. yylval.cp = setsymtab(buf, tostring(buf), atof(buf), CON|NUM, symtab);
  184. /* should this also have STR set? */
  185. RET(NUMBER);
  186. }
  187. yylval.i = c;
  188. switch (c) {
  189. case '\n': /* {EOL} */
  190. RET(NL);
  191. case '\r': /* assume \n is coming */
  192. case ' ': /* {WS}+ */
  193. case '\t':
  194. break;
  195. case '#': /* #.* strip comments */
  196. while ((c = input()) != '\n' && c != 0)
  197. ;
  198. unput(c);
  199. break;
  200. case ';':
  201. RET(';');
  202. case '\\':
  203. if (peek() == '\n') {
  204. input();
  205. } else if (peek() == '\r') {
  206. input(); input(); /* \n */
  207. lineno++;
  208. } else {
  209. RET(c);
  210. }
  211. break;
  212. case '&':
  213. if (peek() == '&') {
  214. input(); RET(AND);
  215. } else
  216. RET('&');
  217. case '|':
  218. if (peek() == '|') {
  219. input(); RET(BOR);
  220. } else
  221. RET('|');
  222. case '!':
  223. if (peek() == '=') {
  224. input(); yylval.i = NE; RET(NE);
  225. } else if (peek() == '~') {
  226. input(); yylval.i = NOTMATCH; RET(MATCHOP);
  227. } else
  228. RET(NOT);
  229. case '~':
  230. yylval.i = MATCH;
  231. RET(MATCHOP);
  232. case '<':
  233. if (peek() == '=') {
  234. input(); yylval.i = LE; RET(LE);
  235. } else {
  236. yylval.i = LT; RET(LT);
  237. }
  238. case '=':
  239. if (peek() == '=') {
  240. input(); yylval.i = EQ; RET(EQ);
  241. } else {
  242. yylval.i = ASSIGN; RET(ASGNOP);
  243. }
  244. case '>':
  245. if (peek() == '=') {
  246. input(); yylval.i = GE; RET(GE);
  247. } else if (peek() == '>') {
  248. input(); yylval.i = APPEND; RET(APPEND);
  249. } else {
  250. yylval.i = GT; RET(GT);
  251. }
  252. case '+':
  253. if (peek() == '+') {
  254. input(); yylval.i = INCR; RET(INCR);
  255. } else if (peek() == '=') {
  256. input(); yylval.i = ADDEQ; RET(ASGNOP);
  257. } else
  258. RET('+');
  259. case '-':
  260. if (peek() == '-') {
  261. input(); yylval.i = DECR; RET(DECR);
  262. } else if (peek() == '=') {
  263. input(); yylval.i = SUBEQ; RET(ASGNOP);
  264. } else
  265. RET('-');
  266. case '*':
  267. if (peek() == '=') { /* *= */
  268. input(); yylval.i = MULTEQ; RET(ASGNOP);
  269. } else if (peek() == '*') { /* ** or **= */
  270. input(); /* eat 2nd * */
  271. if (peek() == '=') {
  272. input(); yylval.i = POWEQ; RET(ASGNOP);
  273. } else {
  274. RET(POWER);
  275. }
  276. } else
  277. RET('*');
  278. case '/':
  279. RET('/');
  280. case '%':
  281. if (peek() == '=') {
  282. input(); yylval.i = MODEQ; RET(ASGNOP);
  283. } else
  284. RET('%');
  285. case '^':
  286. if (peek() == '=') {
  287. input(); yylval.i = POWEQ; RET(ASGNOP);
  288. } else
  289. RET(POWER);
  290. case '$':
  291. /* BUG: awkward, if not wrong */
  292. c = gettok(&buf, &bufsize);
  293. if (c == '(' || c == '[' || (infunc && isarg(buf) >= 0)) {
  294. unputstr(buf);
  295. RET(INDIRECT);
  296. } else if (isalpha(c)) {
  297. if (strcmp(buf, "NF") == 0) { /* very special */
  298. unputstr("(NF)");
  299. RET(INDIRECT);
  300. }
  301. yylval.cp = setsymtab(buf, "", 0.0, STR|NUM, symtab);
  302. RET(IVAR);
  303. } else {
  304. unputstr(buf);
  305. RET(INDIRECT);
  306. }
  307. case '}':
  308. if (--bracecnt < 0)
  309. SYNTAX( "extra }" );
  310. sc = 1;
  311. RET(';');
  312. case ']':
  313. if (--brackcnt < 0)
  314. SYNTAX( "extra ]" );
  315. RET(']');
  316. case ')':
  317. if (--parencnt < 0)
  318. SYNTAX( "extra )" );
  319. RET(')');
  320. case '{':
  321. bracecnt++;
  322. RET('{');
  323. case '[':
  324. brackcnt++;
  325. RET('[');
  326. case '(':
  327. parencnt++;
  328. RET('(');
  329. case '"':
  330. return string(); /* BUG: should be like tran.c ? */
  331. default:
  332. RET(c);
  333. }
  334. }
  335. }
  336. int string(void)
  337. {
  338. int c, n;
  339. char *s, *bp;
  340. static char *buf = 0;
  341. static int bufsz = 500;
  342. if (buf == 0 && (buf = (char *) malloc(bufsz)) == NULL)
  343. FATAL("out of space for strings");
  344. for (bp = buf; (c = input()) != '"'; ) {
  345. if (!adjbuf(&buf, &bufsz, bp-buf+2, 500, &bp, 0))
  346. FATAL("out of space for string %.10s...", buf);
  347. switch (c) {
  348. case '\n':
  349. case '\r':
  350. case 0:
  351. SYNTAX( "non-terminated string %.10s...", buf );
  352. lineno++;
  353. break;
  354. case '\\':
  355. c = input();
  356. switch (c) {
  357. case '"': *bp++ = '"'; break;
  358. case 'n': *bp++ = '\n'; break;
  359. case 't': *bp++ = '\t'; break;
  360. case 'f': *bp++ = '\f'; break;
  361. case 'r': *bp++ = '\r'; break;
  362. case 'b': *bp++ = '\b'; break;
  363. case 'v': *bp++ = '\v'; break;
  364. case 'a': *bp++ = '\007'; break;
  365. case '\\': *bp++ = '\\'; break;
  366. case '0': case '1': case '2': /* octal: \d \dd \ddd */
  367. case '3': case '4': case '5': case '6': case '7':
  368. n = c - '0';
  369. if ((c = peek()) >= '0' && c < '8') {
  370. n = 8 * n + input() - '0';
  371. if ((c = peek()) >= '0' && c < '8')
  372. n = 8 * n + input() - '0';
  373. }
  374. *bp++ = n;
  375. break;
  376. case 'x': /* hex \x0-9a-fA-F + */
  377. { char xbuf[100], *px;
  378. for (px = xbuf; (c = input()) != 0 && px-xbuf < 100-2; ) {
  379. if (isdigit(c)
  380. || (c >= 'a' && c <= 'f')
  381. || (c >= 'A' && c <= 'F'))
  382. *px++ = c;
  383. else
  384. break;
  385. }
  386. *px = 0;
  387. unput(c);
  388. sscanf(xbuf, "%x", &n);
  389. *bp++ = n;
  390. break;
  391. }
  392. default:
  393. *bp++ = c;
  394. break;
  395. }
  396. break;
  397. default:
  398. *bp++ = c;
  399. break;
  400. }
  401. }
  402. *bp = 0;
  403. s = tostring(buf);
  404. *bp++ = ' '; *bp++ = 0;
  405. yylval.cp = setsymtab(buf, s, 0.0, CON|STR|DONTFREE, symtab);
  406. RET(STRING);
  407. }
  408. int binsearch(char *w, Keyword *kp, int n)
  409. {
  410. int cond, low, mid, high;
  411. low = 0;
  412. high = n - 1;
  413. while (low <= high) {
  414. mid = (low + high) / 2;
  415. if ((cond = strcmp(w, kp[mid].word)) < 0)
  416. high = mid - 1;
  417. else if (cond > 0)
  418. low = mid + 1;
  419. else
  420. return mid;
  421. }
  422. return -1;
  423. }
  424. int word(char *w)
  425. {
  426. Keyword *kp;
  427. int c, n;
  428. n = binsearch(w, keywords, sizeof(keywords)/sizeof(keywords[0]));
  429. kp = keywords + n;
  430. if (n != -1) { /* found in table */
  431. yylval.i = kp->sub;
  432. switch (kp->type) { /* special handling */
  433. case FSYSTEM:
  434. if (safe)
  435. SYNTAX( "system is unsafe" );
  436. RET(kp->type);
  437. case FUNC:
  438. if (infunc)
  439. SYNTAX( "illegal nested function" );
  440. RET(kp->type);
  441. case RETURN:
  442. if (!infunc)
  443. SYNTAX( "return not in function" );
  444. RET(kp->type);
  445. case VARNF:
  446. yylval.cp = setsymtab("NF", "", 0.0, NUM, symtab);
  447. RET(VARNF);
  448. default:
  449. RET(kp->type);
  450. }
  451. }
  452. c = peek(); /* look for '(' */
  453. if (c != '(' && infunc && (n=isarg(w)) >= 0) {
  454. yylval.i = n;
  455. RET(ARG);
  456. } else {
  457. yylval.cp = setsymtab(w, "", 0.0, STR|NUM|DONTFREE, symtab);
  458. if (c == '(') {
  459. RET(CALL);
  460. } else {
  461. RET(VAR);
  462. }
  463. }
  464. }
  465. void startreg(void) /* next call to yyles will return a regular expression */
  466. {
  467. reg = 1;
  468. }
  469. int regexpr(void)
  470. {
  471. int c;
  472. static char *buf = 0;
  473. static int bufsz = 500;
  474. char *bp;
  475. if (buf == 0 && (buf = (char *) malloc(bufsz)) == NULL)
  476. FATAL("out of space for rex expr");
  477. bp = buf;
  478. for ( ; (c = input()) != '/' && c != 0; ) {
  479. if (!adjbuf(&buf, &bufsz, bp-buf+3, 500, &bp, 0))
  480. FATAL("out of space for reg expr %.10s...", buf);
  481. if (c == '\n') {
  482. SYNTAX( "newline in regular expression %.10s...", buf );
  483. unput('\n');
  484. break;
  485. } else if (c == '\\') {
  486. *bp++ = '\\';
  487. *bp++ = input();
  488. } else {
  489. *bp++ = c;
  490. }
  491. }
  492. *bp = 0;
  493. yylval.s = tostring(buf);
  494. unput('/');
  495. RET(REGEXPR);
  496. }
  497. /* low-level lexical stuff, sort of inherited from lex */
  498. char ebuf[300];
  499. char *ep = ebuf;
  500. char yysbuf[100]; /* pushback buffer */
  501. char *yysptr = yysbuf;
  502. FILE *yyin = 0;
  503. int input(void) /* get next lexical input character */
  504. {
  505. int c;
  506. extern char *lexprog;
  507. if (yysptr > yysbuf)
  508. c = *--yysptr;
  509. else if (lexprog != NULL) { /* awk '...' */
  510. if ((c = *lexprog) != 0)
  511. lexprog++;
  512. } else /* awk -f ... */
  513. c = pgetc();
  514. if (c == '\n')
  515. lineno++;
  516. else if (c == EOF)
  517. c = 0;
  518. if (ep >= ebuf + sizeof ebuf)
  519. ep = ebuf;
  520. return *ep++ = c;
  521. }
  522. void unput(int c) /* put lexical character back on input */
  523. {
  524. if (c == '\n')
  525. lineno--;
  526. if (yysptr >= yysbuf + sizeof(yysbuf))
  527. FATAL("pushed back too much: %.20s...", yysbuf);
  528. *yysptr++ = c;
  529. if (--ep < ebuf)
  530. ep = ebuf + sizeof(ebuf) - 1;
  531. }
  532. void unputstr(char *s) /* put a string back on input */
  533. {
  534. int i;
  535. for (i = strlen(s)-1; i >= 0; i--)
  536. unput(s[i]);
  537. }