%Start A str chc sc reg comment
%{
#include "awk.h"
#include "awk.def"
#undef input /* defeat lex */
extern int yylval;
extern int mustfld;
int lineno 1;
#ifdef DEBUG
# define RETURN(x) {if (dbg) ptoken(x); return(x); }
#else
# define RETURN(x) return(x)
#endif
#define CADD cbuf[clen++]=yytext[0]; if(clen>=CBUFLEN-1) {yyerror("string too long", cbuf); BEGIN A;}
#define CBUFLEN 150
char cbuf[CBUFLEN];
int clen, cflag;
%}
A [a-zA-Z]
B [a-zA-Z0-9]
D [0-9]
WS [ \t]
%%
switch (yybgin-yysvec-1) { /* witchcraft */
case 0:
BEGIN A;
break;
case sc:
BEGIN A;
RETURN('}');
}
^\n lineno++;
^{WS}*#.*\n lineno++; /* strip comment lines */
{WS} ;
"\\"\n lineno++;
"||" RETURN(BOR);
BEGIN RETURN(XBEGIN);
END RETURN(XEND);
PROGEND RETURN(EOF);
"&&" RETURN(AND);
"!" RETURN(NOT);
"!=" { yylval = NE; RETURN(RELOP); }
"~" { yylval = MATCH; RETURN(MATCHOP); }
"!~" { yylval = NOTMATCH; RETURN(MATCHOP); }
"<" { yylval = LT; RETURN(RELOP); }
"<=" { yylval = LE; RETURN(RELOP); }
"==" { yylval = EQ; RETURN(RELOP); }
">=" { yylval = GE; RETURN(RELOP); }
">" { yylval = GT; RETURN(RELOP); }
">>" { yylval = APPEND; RETURN(RELOP); }
"++" { yylval = INCR; RETURN(INCR); }
"--" { yylval = DECR; RETURN(DECR); }
"+=" { yylval = ADDEQ; RETURN(ASGNOP); }
"-=" { yylval = SUBEQ; RETURN(ASGNOP); }
"*=" { yylval = MULTEQ; RETURN(ASGNOP); }
"/=" { yylval = DIVEQ; RETURN(ASGNOP); }
"%=" { yylval = MODEQ; RETURN(ASGNOP); }
"=" { yylval = ASSIGN; RETURN(ASGNOP); }
"$"{D}+ { if (atoi(yytext+1)==0) {
yylval = lookup("$record", symtab);
RETURN(STRING);
} else {
yylval = fieldadr(atoi(yytext+1));
RETURN(FIELD);
}
}
"$"{WS}* { RETURN(INDIRECT); }
NF { mustfld=1; yylval = setsymtab(yytext, NULL, 0.0, NUM, symtab); RETURN(VAR); }
({D}+("."?){D}*|"."{D}+)((e|E)("+"|-)?{D}+)? {
yylval = setsymtab(yytext, NULL, atof(yytext), CON|NUM, symtab); RETURN(NUMBER); }
"}"{WS}*\n { BEGIN sc; lineno++; RETURN(';'); }
"}" { BEGIN sc; RETURN(';'); }
;\n { lineno++; RETURN(';'); }
\n { lineno++; RETURN(NL); }
while RETURN(WHILE);
for RETURN(FOR);
if RETURN(IF);
else RETURN(ELSE);
next RETURN(NEXT);
exit RETURN(EXIT);
break RETURN(BREAK);
continue RETURN(CONTINUE);
print { yylval = PRINT; RETURN(PRINT); }
printf { yylval = PRINTF; RETURN(PRINTF); }
sprintf { yylval = SPRINTF; RETURN(SPRINTF); }
split { yylval = SPLIT; RETURN(SPLIT); }
substr RETURN(SUBSTR);
index RETURN(INDEX);
in RETURN(IN);
length { yylval = FLENGTH; RETURN(FNCN); }
log { yylval = FLOG; RETURN(FNCN); }
int { yylval = FINT; RETURN(FNCN); }
exp { yylval = FEXP; RETURN(FNCN); }
sqrt { yylval = FSQRT; RETURN(FNCN); }
{A}{B}* { yylval = setsymtab(yytext, tostring(""), 0.0, STR, symtab); RETURN(VAR); }
\" { BEGIN str; clen=0; }
# { BEGIN comment; }
\n { BEGIN A; lineno++; RETURN(NL); }
. ;
. { yylval = yytext[0]; RETURN(yytext[0]); }
"[" { BEGIN chc; clen=0; cflag=0; }
"[^" { BEGIN chc; clen=0; cflag=1; }
"?" RETURN(QUEST);
"+" RETURN(PLUS);
"*" RETURN(STAR);
"|" RETURN(OR);
"." RETURN(DOT);
"(" RETURN('(');
")" RETURN(')');
"^" RETURN('^');
"$" RETURN('$');
\\. { if (yytext[1]=='n') yylval = '\n';
else if (yytext[1] == 't') yylval = '\t';
else yylval = yytext[1];
RETURN(CHAR);
}
"/" { BEGIN A; unput('/'); }
\n { yyerror("newline in regular expression"); lineno++; BEGIN A; }
. { yylval = yytext[0]; RETURN(CHAR); }
\" { BEGIN A; cbuf[clen]=0; yylval = setsymtab(cbuf, tostring(cbuf), 0.0, CON|STR, symtab); RETURN(STRING); }
\n { yyerror("newline in string"); lineno++; BEGIN A; }
"\\\"" { cbuf[clen++]='"'; }
"\\"n { cbuf[clen++]='\n'; }
"\\"t { cbuf[clen++]='\t'; }
"\\\\" { cbuf[clen++]='\\'; }
. { CADD; }
"\\""]" { cbuf[clen++]=']'; }
"]" { BEGIN reg; cbuf[clen]=0; yylval = tostring(cbuf);
if (cflag==0) { RETURN(CCL); }
else { RETURN(NCCL); } }
\n { yyerror("newline in character class"); lineno++; BEGIN A; }
. { CADD; }
%%
input()
{
register c;
extern char *lexprog;
if (yysptr > yysbuf)
c = U(*--yysptr);
else if (yyin == NULL)
c = *lexprog++;
else
c = getc(yyin);
if (c == '\n')
yylineno++;
else if (c == EOF)
c = 0;
return(c);
}
startreg()
{
BEGIN reg;
}