lex.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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 <u.h>
  23. #include <lib9.h>
  24. #include <chartypes.h>
  25. #include <bio.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. { "utf", FUTF, BLTIN },
  83. { "while", WHILE, WHILE },
  84. };
  85. #ifdef DEBUG
  86. #define RET(x) { if(dbg)print("lex %s\n", tokname(x)); return(x); }
  87. #else
  88. #define RET(x) return(x)
  89. #endif
  90. int peek(void)
  91. {
  92. int c = input();
  93. unput(c);
  94. return c;
  95. }
  96. int gettok(char **pbuf, int *psz) /* get next input token */
  97. {
  98. int c;
  99. char *buf = *pbuf;
  100. int sz = *psz;
  101. char *bp = buf;
  102. c = input();
  103. if (c == 0)
  104. return 0;
  105. buf[0] = c;
  106. buf[1] = 0;
  107. if (!isalnum(c) && c != '.' && c != '_')
  108. return c;
  109. *bp++ = c;
  110. if (isalpha(c) || c == '_') { /* it's a varname */
  111. for ( ; (c = input()) != 0; ) {
  112. if (bp-buf >= sz)
  113. if (!adjbuf(&buf, &sz, bp-buf+2, 100, &bp, 0))
  114. FATAL( "out of space for name %.10s...", buf );
  115. if (isalnum(c) || c == '_')
  116. *bp++ = c;
  117. else {
  118. *bp = 0;
  119. unput(c);
  120. break;
  121. }
  122. }
  123. } else { /* it's a number */
  124. char *rem;
  125. /* read input until can't be a number */
  126. for ( ; (c = input()) != 0; ) {
  127. if (bp-buf >= sz)
  128. if (!adjbuf(&buf, &sz, bp-buf+2, 100, &bp, 0))
  129. FATAL( "out of space for number %.10s...", buf );
  130. if (isdigit(c) || c == 'e' || c == 'E'
  131. || c == '.' || c == '+' || c == '-')
  132. *bp++ = c;
  133. else {
  134. unput(c);
  135. break;
  136. }
  137. }
  138. *bp = 0;
  139. strtod(buf, (const char**)&rem); /* parse the number */
  140. unputstr(rem); /* put rest back for later */
  141. rem[0] = 0;
  142. }
  143. *pbuf = buf;
  144. *psz = sz;
  145. return buf[0];
  146. }
  147. int word(char *);
  148. int string(void);
  149. int regexpr(void);
  150. int sc = 0; /* 1 => return a } right now */
  151. int reg = 0; /* 1 => return a REGEXPR now */
  152. int yylex(void)
  153. {
  154. int c;
  155. static char *buf = 0;
  156. static int bufsize = 500;
  157. if (buf == 0 && (buf = (char *) malloc(bufsize)) == nil)
  158. FATAL( "out of space in yylex" );
  159. if (sc) {
  160. sc = 0;
  161. RET('}');
  162. }
  163. if (reg) {
  164. reg = 0;
  165. return regexpr();
  166. }
  167. for (;;) {
  168. c = gettok(&buf, &bufsize);
  169. if (c == 0)
  170. return 0;
  171. if (isalpha(c) || c == '_')
  172. return word(buf);
  173. if (isdigit(c) || c == '.') {
  174. yylval.cp = setsymtab(buf, tostring(buf), atof(buf), CON|NUM, symtab);
  175. /* should this also have STR set? */
  176. RET(NUMBER);
  177. }
  178. yylval.i = c;
  179. switch (c) {
  180. case '\n': /* {EOL} */
  181. RET(NL);
  182. case '\r': /* assume \n is coming */
  183. case ' ': /* {WS}+ */
  184. case '\t':
  185. break;
  186. case '#': /* #.* strip comments */
  187. while ((c = input()) != '\n' && c != 0)
  188. ;
  189. unput(c);
  190. break;
  191. case ';':
  192. RET(';');
  193. case '\\':
  194. if (peek() == '\n') {
  195. input();
  196. } else if (peek() == '\r') {
  197. input(); input(); /* \n */
  198. lineno++;
  199. } else {
  200. RET(c);
  201. }
  202. break;
  203. case '&':
  204. if (peek() == '&') {
  205. input(); RET(AND);
  206. } else
  207. RET('&');
  208. case '|':
  209. if (peek() == '|') {
  210. input(); RET(BOR);
  211. } else
  212. RET('|');
  213. case '!':
  214. if (peek() == '=') {
  215. input(); yylval.i = NE; RET(NE);
  216. } else if (peek() == '~') {
  217. input(); yylval.i = NOTMATCH; RET(MATCHOP);
  218. } else
  219. RET(NOT);
  220. case '~':
  221. yylval.i = MATCH;
  222. RET(MATCHOP);
  223. case '<':
  224. if (peek() == '=') {
  225. input(); yylval.i = LE; RET(LE);
  226. } else {
  227. yylval.i = LT; RET(LT);
  228. }
  229. case '=':
  230. if (peek() == '=') {
  231. input(); yylval.i = EQ; RET(EQ);
  232. } else {
  233. yylval.i = ASSIGN; RET(ASGNOP);
  234. }
  235. case '>':
  236. if (peek() == '=') {
  237. input(); yylval.i = GE; RET(GE);
  238. } else if (peek() == '>') {
  239. input(); yylval.i = APPEND; RET(APPEND);
  240. } else {
  241. yylval.i = GT; RET(GT);
  242. }
  243. case '+':
  244. if (peek() == '+') {
  245. input(); yylval.i = INCR; RET(INCR);
  246. } else if (peek() == '=') {
  247. input(); yylval.i = ADDEQ; RET(ASGNOP);
  248. } else
  249. RET('+');
  250. case '-':
  251. if (peek() == '-') {
  252. input(); yylval.i = DECR; RET(DECR);
  253. } else if (peek() == '=') {
  254. input(); yylval.i = SUBEQ; RET(ASGNOP);
  255. } else
  256. RET('-');
  257. case '*':
  258. if (peek() == '=') { /* *= */
  259. input(); yylval.i = MULTEQ; RET(ASGNOP);
  260. } else if (peek() == '*') { /* ** or **= */
  261. input(); /* eat 2nd * */
  262. if (peek() == '=') {
  263. input(); yylval.i = POWEQ; RET(ASGNOP);
  264. } else {
  265. RET(POWER);
  266. }
  267. } else
  268. RET('*');
  269. case '/':
  270. RET('/');
  271. case '%':
  272. if (peek() == '=') {
  273. input(); yylval.i = MODEQ; RET(ASGNOP);
  274. } else
  275. RET('%');
  276. case '^':
  277. if (peek() == '=') {
  278. input(); yylval.i = POWEQ; RET(ASGNOP);
  279. } else
  280. RET(POWER);
  281. case '$':
  282. /* BUG: awkward, if not wrong */
  283. c = gettok(&buf, &bufsize);
  284. if (c == '(' || c == '[' || (infunc && isarg(buf) >= 0)) {
  285. unputstr(buf);
  286. RET(INDIRECT);
  287. } else if (isalpha(c)) {
  288. if (strcmp(buf, "NF") == 0) { /* very special */
  289. unputstr("(NF)");
  290. RET(INDIRECT);
  291. }
  292. yylval.cp = setsymtab(buf, "", 0.0, STR|NUM, symtab);
  293. RET(IVAR);
  294. } else {
  295. unputstr(buf);
  296. RET(INDIRECT);
  297. }
  298. case '}':
  299. if (--bracecnt < 0)
  300. SYNTAX( "extra }" );
  301. sc = 1;
  302. RET(';');
  303. case ']':
  304. if (--brackcnt < 0)
  305. SYNTAX( "extra ]" );
  306. RET(']');
  307. case ')':
  308. if (--parencnt < 0)
  309. SYNTAX( "extra )" );
  310. RET(')');
  311. case '{':
  312. bracecnt++;
  313. RET('{');
  314. case '[':
  315. brackcnt++;
  316. RET('[');
  317. case '(':
  318. parencnt++;
  319. RET('(');
  320. case '"':
  321. return string(); /* BUG: should be like tran.c ? */
  322. default:
  323. RET(c);
  324. }
  325. }
  326. }
  327. int string(void)
  328. {
  329. int c, n;
  330. char *s, *bp;
  331. static char *buf = 0;
  332. static int bufsz = 500;
  333. if (buf == 0 && (buf = (char *) malloc(bufsz)) == nil)
  334. FATAL("out of space for strings");
  335. for (bp = buf; (c = input()) != '"'; ) {
  336. if (!adjbuf(&buf, &bufsz, bp-buf+2, 500, &bp, 0))
  337. FATAL("out of space for string %.10s...", buf);
  338. switch (c) {
  339. case '\n':
  340. case '\r':
  341. case 0:
  342. SYNTAX( "non-terminated string %.10s...", buf );
  343. lineno++;
  344. RET(0);
  345. case '\\':
  346. c = input();
  347. switch (c) {
  348. case '"': *bp++ = '"'; break;
  349. case 'n': *bp++ = '\n'; break;
  350. case 't': *bp++ = '\t'; break;
  351. case 'f': *bp++ = '\f'; break;
  352. case 'r': *bp++ = '\r'; break;
  353. case 'b': *bp++ = '\b'; break;
  354. case 'v': *bp++ = '\v'; break;
  355. case 'a': *bp++ = '\007'; break;
  356. case '\\': *bp++ = '\\'; break;
  357. case '0': case '1': case '2': /* octal: \d \dd \ddd */
  358. case '3': case '4': case '5': case '6': case '7':
  359. n = c - '0';
  360. if ((c = peek()) >= '0' && c < '8') {
  361. n = 8 * n + input() - '0';
  362. if ((c = peek()) >= '0' && c < '8')
  363. n = 8 * n + input() - '0';
  364. }
  365. *bp++ = n;
  366. break;
  367. case 'x': /* hex \x0-9a-fA-F + */
  368. { char xbuf[100], *px;
  369. for (px = xbuf; (c = input()) != 0 && px-xbuf < 100-2; ) {
  370. if (isdigit(c)
  371. || (c >= 'a' && c <= 'f')
  372. || (c >= 'A' && c <= 'F'))
  373. *px++ = c;
  374. else
  375. break;
  376. }
  377. *px = 0;
  378. unput(c);
  379. n = strtol(xbuf, nil, 16);
  380. *bp++ = n;
  381. break;
  382. }
  383. default:
  384. *bp++ = c;
  385. break;
  386. }
  387. break;
  388. default:
  389. *bp++ = c;
  390. break;
  391. }
  392. }
  393. *bp = 0;
  394. s = tostring(buf);
  395. *bp++ = ' '; *bp++ = 0;
  396. yylval.cp = setsymtab(buf, s, 0.0, CON|STR|DONTFREE, symtab);
  397. RET(STRING);
  398. }
  399. int binsearch(char *w, Keyword *kp, int n)
  400. {
  401. int cond, low, mid, high;
  402. low = 0;
  403. high = n - 1;
  404. while (low <= high) {
  405. mid = (low + high) / 2;
  406. if ((cond = strcmp(w, kp[mid].word)) < 0)
  407. high = mid - 1;
  408. else if (cond > 0)
  409. low = mid + 1;
  410. else
  411. return mid;
  412. }
  413. return -1;
  414. }
  415. int word(char *w)
  416. {
  417. Keyword *kp;
  418. int c, n;
  419. n = binsearch(w, keywords, sizeof(keywords)/sizeof(keywords[0]));
  420. kp = keywords + n;
  421. if (n != -1) { /* found in table */
  422. yylval.i = kp->sub;
  423. switch (kp->type) { /* special handling */
  424. case FSYSTEM:
  425. if (safe)
  426. SYNTAX( "system is unsafe" );
  427. RET(kp->type);
  428. case FUNC:
  429. if (infunc)
  430. SYNTAX( "illegal nested function" );
  431. RET(kp->type);
  432. case RETURN:
  433. if (!infunc)
  434. SYNTAX( "return not in function" );
  435. RET(kp->type);
  436. case VARNF:
  437. yylval.cp = setsymtab("NF", "", 0.0, NUM, symtab);
  438. RET(VARNF);
  439. default:
  440. RET(kp->type);
  441. }
  442. }
  443. c = peek(); /* look for '(' */
  444. if (c != '(' && infunc && (n=isarg(w)) >= 0) {
  445. yylval.i = n;
  446. RET(ARG);
  447. } else {
  448. yylval.cp = setsymtab(w, "", 0.0, STR|NUM|DONTFREE, symtab);
  449. if (c == '(') {
  450. RET(CALL);
  451. } else {
  452. RET(VAR);
  453. }
  454. }
  455. }
  456. void startreg(void) /* next call to yyles will return a regular expression */
  457. {
  458. reg = 1;
  459. }
  460. int regexpr(void)
  461. {
  462. int c;
  463. static char *buf = 0;
  464. static int bufsz = 500;
  465. char *bp;
  466. if (buf == 0 && (buf = (char *) malloc(bufsz)) == nil)
  467. FATAL("out of space for rex expr");
  468. bp = buf;
  469. for ( ; (c = input()) != '/' && c != 0; ) {
  470. if (!adjbuf(&buf, &bufsz, bp-buf+3, 500, &bp, 0))
  471. FATAL("out of space for reg expr %.10s...", buf);
  472. if (c == '\n') {
  473. SYNTAX( "newline in regular expression %.10s...", buf );
  474. unput('\n');
  475. break;
  476. } else if (c == '\\') {
  477. *bp++ = '\\';
  478. *bp++ = input();
  479. } else {
  480. *bp++ = c;
  481. }
  482. }
  483. *bp = 0;
  484. yylval.s = tostring(buf);
  485. unput('/');
  486. RET(REGEXPR);
  487. }
  488. /* low-level lexical stuff, sort of inherited from lex */
  489. char ebuf[300];
  490. char *ep = ebuf;
  491. char yysbuf[100]; /* pushback buffer */
  492. char *yysptr = yysbuf;
  493. Biobuf *yyin;
  494. int input(void) /* get next lexical input character */
  495. {
  496. int c;
  497. extern char *lexprog;
  498. if (yysptr > yysbuf)
  499. c = *--yysptr;
  500. else if (lexprog != nil) { /* awk '...' */
  501. if ((c = *lexprog) != 0)
  502. lexprog++;
  503. } else /* awk -f ... */
  504. c = pgetc();
  505. if (c == '\n')
  506. lineno++;
  507. else if (c == Beof)
  508. c = 0;
  509. if (ep >= ebuf + sizeof ebuf)
  510. ep = ebuf;
  511. return *ep++ = c;
  512. }
  513. void unput(int c) /* put lexical character back on input */
  514. {
  515. if (c == '\n')
  516. lineno--;
  517. if (yysptr >= yysbuf + sizeof(yysbuf))
  518. FATAL("pushed back too much: %.20s...", yysbuf);
  519. *yysptr++ = c;
  520. if (--ep < ebuf)
  521. ep = ebuf + sizeof(ebuf) - 1;
  522. }
  523. void unputstr(char *s) /* put a string back on input */
  524. {
  525. int i;
  526. for (i = strlen(s)-1; i >= 0; i--)
  527. unput(s[i]);
  528. }