lex.c 12 KB

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