* awk.y --- yacc/bison parser
* Copyright (C) 1986, 1988, 1989, 1991, 1992 the Free Software Foundation, Inc.
* This file is part of GAWK, the GNU implementation of the
* AWK Progamming Language.
* GAWK is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* GAWK is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with GAWK; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
static void yyerror (); /* va_alist */
static char *get_src_buf P((void));
static int yylex P((void));
static NODE *node_common P((NODETYPE op));
static NODE *snode P((NODE *subn, NODETYPE op, int sindex));
static NODE *mkrangenode P((NODE *cpair));
static NODE *make_for_loop P((NODE *init, NODE *cond, NODE *incr));
static NODE *append_right P((NODE *list, NODE *new));
static void func_install P((NODE *params, NODE *def));
static void pop_var P((NODE *np, int freeit));
static void pop_params P((NODE *params));
static NODE *make_param P((char *name));
static NODE *mk_rexp P((NODE *exp));
static int want_assign; /* lexical scanning kludge */
static int want_regexp; /* lexical scanning kludge */
static int can_return; /* lexical scanning kludge */
static int io_allowed = 1; /* lexical scanning kludge */
static char *lexptr; /* pointer to next char during parsing */
static char *lexptr_begin; /* keep track of where we were for error msgs */
static char *lexeme; /* beginning of lexeme for debugging */
static char *thisline = NULL;
#define YYDEBUG_LEXER_TEXT (lexeme)
static int param_counter;
static char *tokstart = NULL;
static char *token = NULL;
NODE *variables[HASHSIZE];
extern struct src *srcfiles;
extern NODE *begin_block;
%type <nodeval> function_prologue function_body
%type <nodeval> rexp exp start program rule simp_exp
%type <nodeval> non_post_simp_exp
%type <nodeval> action variable param_list
%type <nodeval> rexpression_list opt_rexpression_list
%type <nodeval> expression_list opt_expression_list
%type <nodeval> statements statement if_statement opt_param_list
%type <nodeval> opt_exp opt_variable regexp
%type <nodeval> input_redir output_redir
%type <nodetypeval> print
%token <sval> FUNC_CALL NAME REGEXP
%token <nodeval> YNUMBER YSTRING
%token <nodetypeval> RELOP APPEND_OP
%token <nodetypeval> ASSIGNOP MATCHOP NEWLINE CONCAT_OP
%token <nodetypeval> LEX_BEGIN LEX_END LEX_IF LEX_ELSE LEX_RETURN LEX_DELETE
%token <nodetypeval> LEX_WHILE LEX_DO LEX_FOR LEX_BREAK LEX_CONTINUE
%token <nodetypeval> LEX_PRINT LEX_PRINTF LEX_NEXT LEX_EXIT LEX_FUNCTION
%token <nodetypeval> LEX_GETLINE
%token <nodetypeval> LEX_IN
%token <lval> LEX_AND LEX_OR INCREMENT DECREMENT
%token <lval> LEX_BUILTIN LEX_LENGTH
/* these are just yylval numbers */
%left FUNC_CALL LEX_BUILTIN LEX_LENGTH
%nonassoc RELOP '<' '>' '|' APPEND_OP
%left INCREMENT DECREMENT
: opt_nls program opt_nls
{ expression_value = $2; }
/* add the rule to the tail of list */
if ($1->type != Node_rule_list)
$1 = node($1, Node_rule_list,
node($2, Node_rule_list,(NODE *) NULL));
| program error { $$ = NULL; }
: LEX_BEGIN { io_allowed = 0; }
if (begin_block->type != Node_rule_list)
begin_block = node(begin_block, Node_rule_list,
(void) append_right (begin_block, node(
node((NODE *)NULL, Node_rule_node, $3),
Node_rule_list, (NODE *)NULL) );
begin_block = node((NODE *)NULL, Node_rule_node, $3);
| LEX_END { io_allowed = 0; }
if (end_block->type != Node_rule_list)
end_block = node(end_block, Node_rule_list,
(void) append_right (end_block, node(
node((NODE *)NULL, Node_rule_node, $3),
Node_rule_list, (NODE *)NULL));
end_block = node((NODE *)NULL, Node_rule_node, $3);
| LEX_BEGIN statement_term
warning("BEGIN blocks must have an action part");
warning("END blocks must have an action part");
{ $$ = node ($1, Node_rule_node, $2); yyerrok; }
{ $$ = node ((NODE *)NULL, Node_rule_node, $1); yyerrok; }
node(node(node(make_number(0.0),
| function_prologue function_body
yyerror("%s() is a built-in function, it cannot be redefined",
func_name '(' opt_param_list r_paren opt_nls
$$ = append_right(make_param($3), $5);
: l_brace statements r_brace opt_semi
{ $$ = mkrangenode ( node($1, Node_cond_pair, $3) ); }
* In this rule, want_regexp tells yylex that the next thing
* is a regexp so it should read up to the closing slash.
n->re_exp = make_string($3, len);
n->re_reg = make_regexp($3, len, 0, 1);
: l_brace statements r_brace opt_semi opt_nls
| l_brace r_brace opt_semi opt_nls
if ($1 == NULL || $1->type != Node_statement_list)
$1 = node($1, Node_statement_list,(NODE *)NULL);
node( $2, Node_statement_list, (NODE *)NULL));
| l_brace statements r_brace
| LEX_WHILE '(' exp r_paren opt_nls statement
{ $$ = node ($3, Node_K_while, $6); }
| LEX_DO opt_nls statement LEX_WHILE '(' exp r_paren opt_nls
{ $$ = node ($6, Node_K_do, $3); }
| LEX_FOR '(' NAME LEX_IN NAME r_paren opt_nls statement
$$ = node ($8, Node_K_arrayfor, make_for_loop(variable($3,1),
(NODE *)NULL, variable($5,1)));
| LEX_FOR '(' opt_exp semi exp semi opt_exp r_paren opt_nls statement
$$ = node($10, Node_K_for, (NODE *)make_for_loop($3, $5, $7));
| LEX_FOR '(' opt_exp semi semi opt_exp r_paren opt_nls statement
$$ = node ($9, Node_K_for,
(NODE *)make_for_loop($3, (NODE *)NULL, $6));
| LEX_BREAK statement_term
/* for break, maybe we'll have to remember where to break to */
{ $$ = node ((NODE *)NULL, Node_K_break, (NODE *)NULL); }
| LEX_CONTINUE statement_term
{ $$ = node ((NODE *)NULL, Node_K_continue, (NODE *)NULL); }
| print '(' expression_list r_paren output_redir statement_term
{ $$ = node ($3, $1, $5); }
| print opt_rexpression_list output_redir statement_term
if ($1 == Node_K_print && $2 == NULL)
$2 = node(node(make_number(0.0),
| LEX_NEXT opt_exp statement_term
if ($2 && $2 == lookup("file")) {
warning("`next file' is a gawk extension");
else if (do_unix || do_posix)
yyerror("`next file' is a gawk extension");
yyerror("`next file' used in BEGIN or END action");
yyerror("next used in BEGIN or END action");
$$ = node ((NODE *)NULL, type, (NODE *)NULL);
| LEX_EXIT opt_exp statement_term
{ $$ = node ($2, Node_K_exit, (NODE *)NULL); }
{ if (! can_return) yyerror("return used outside function context"); }
{ $$ = node ($3, Node_K_return, (NODE *)NULL); }
| LEX_DELETE NAME '[' expression_list ']' statement_term
{ $$ = node (variable($2,1), Node_K_delete, $4); }
: LEX_IF '(' exp r_paren opt_nls statement
node($6, Node_if_branches, (NODE *)NULL));
| LEX_IF '(' exp r_paren opt_nls statement
LEX_ELSE opt_nls statement
{ $$ = node ($3, Node_K_if,
node ($6, Node_if_branches, $9)); }
{ $$ = node ($2, Node_redirect_input, (NODE *)NULL); }
{ $$ = node ($2, Node_redirect_output, (NODE *)NULL); }
{ $$ = node ($2, Node_redirect_append, (NODE *)NULL); }
{ $$ = node ($2, Node_redirect_pipe, (NODE *)NULL); }
{ $$ = append_right($1, make_param($3)); yyerrok; }
/* optional expression, as in for loop */
{ $$ = node ($1, Node_expression_list, (NODE *)NULL); }
| rexpression_list comma rexp
node( $3, Node_expression_list, (NODE *)NULL));
| rexpression_list error rexp
| rexpression_list comma error
{ $$ = node ($1, Node_expression_list, (NODE *)NULL); }
| expression_list comma exp
node( $3, Node_expression_list, (NODE *)NULL));
| expression_list error exp
| expression_list comma error
/* Expressions, not including the comma operator. */
if (do_lint && $4->type == Node_regex)
warning("Regular expression on left of assignment.");
| '(' expression_list r_paren LEX_IN NAME
{ $$ = node (variable($5,1), Node_in_array, $2); }
| exp '|' LEX_GETLINE opt_variable
$$ = node ($4, Node_K_getline,
node ($1, Node_redirect_pipein, (NODE *)NULL));
| LEX_GETLINE opt_variable input_redir
if (do_lint && ! io_allowed && $3 == NULL)
warning("non-redirected getline undefined inside BEGIN or END action");
$$ = node ($2, Node_K_getline, $3);
{ $$ = node ($1, Node_and, $3); }
{ $$ = node ($1, Node_or, $3); }
if ($1->type == Node_regex)
warning("Regular expression on left of MATCH operator.");
$$ = node ($1, $2, mk_rexp($3));
$$ = node(node(make_number(0.0),
{ $$ = node (variable($3,1), Node_in_array, $1); }
if (do_lint && $3->type == Node_regex)
warning("Regular expression on left of comparison.");
{ $$ = node ($1, Node_less, $3); }
{ $$ = node ($1, Node_greater, $3); }
{ $$ = node($1, Node_cond_exp, node($3, Node_if_branches, $5));}
| exp simp_exp %prec CONCAT_OP
{ $$ = node ($1, Node_concat, $2); }
{ $$ = node ($1, $2, $4); }
{ $$ = node ($1, Node_and, $3); }
{ $$ = node ($1, Node_or, $3); }
| LEX_GETLINE opt_variable input_redir
if (do_lint && ! io_allowed && $3 == NULL)
warning("non-redirected getline undefined inside BEGIN or END action");
$$ = node ($2, Node_K_getline, $3);
{ $$ = node((NODE *) NULL, Node_nomatch, $2); }
{ $$ = node ($1, $2, mk_rexp($3)); }
{ $$ = node (variable($3,1), Node_in_array, $1); }
{ $$ = node ($1, $2, $3); }
{ $$ = node($1, Node_cond_exp, node($3, Node_if_branches, $5));}
| rexp simp_exp %prec CONCAT_OP
{ $$ = node ($1, Node_concat, $2); }
/* Binary operators in order of decreasing precedence. */
{ $$ = node ($1, Node_exp, $3); }
{ $$ = node ($1, Node_times, $3); }
{ $$ = node ($1, Node_quotient, $3); }
{ $$ = node ($1, Node_mod, $3); }
{ $$ = node ($1, Node_plus, $3); }
{ $$ = node ($1, Node_minus, $3); }
{ $$ = node ($1, Node_postincrement, (NODE *)NULL); }
{ $$ = node ($1, Node_postdecrement, (NODE *)NULL); }
: '!' simp_exp %prec UNARY
{ $$ = node ($2, Node_not,(NODE *) NULL); }
'(' opt_expression_list r_paren
{ $$ = snode ($3, Node_builtin, (int) $1); }
| LEX_LENGTH '(' opt_expression_list r_paren
{ $$ = snode ($3, Node_builtin, (int) $1); }
warning("call of `length' without parentheses is not portable");
$$ = snode ((NODE *)NULL, Node_builtin, (int) $1);
warning( "call of `length' without parentheses is deprecated by POSIX");
| FUNC_CALL '(' opt_expression_list r_paren
$$ = node ($3, Node_func_call, make_string($1, strlen($1)));
{ $$ = node ($2, Node_preincrement, (NODE *)NULL); }
{ $$ = node ($2, Node_predecrement, (NODE *)NULL); }
| '-' simp_exp %prec UNARY
{ if ($2->type == Node_val) {
$2->numbr = -(force_number($2));
$$ = node ($2, Node_unary_minus, (NODE *)NULL);
| '+' simp_exp %prec UNARY
| NAME '[' expression_list ']'
$$ = node (variable($1,1), Node_subscript, $3->lnode);
$$ = node (variable($1,1), Node_subscript, $3);
{ $$ = node ($2, Node_field_spec, (NODE *)NULL); }
: '}' opt_nls { yyerrok; }
: ';' { yyerrok; want_assign = 0; }
comma : ',' opt_nls { yyerrok; }
char *operator; /* text to match */
NODETYPE value; /* node type */
int class; /* lexical class */
unsigned flags; /* # of args. allowed and compatability */
# define ARGS 0xFF /* 0, 1, 2, 3 args allowed (any combination */
# define VERSION 0xFF00 /* old awk is zero */
# define NOT_OLD 0x0100 /* feature not in old awk */
# define NOT_POSIX 0x0200 /* feature not in POSIX */
# define GAWKX 0x0400 /* gawk extension */
NODE *(*ptr) (); /* function that implements this keyword */
*do_exp(), *do_getline(), *do_index(), *do_length(),
*do_sqrt(), *do_log(), *do_sprintf(), *do_substr(),
*do_split(), *do_system(), *do_int(), *do_close(),
*do_atan2(), *do_sin(), *do_cos(), *do_rand(),
*do_srand(), *do_match(), *do_tolower(), *do_toupper(),
*do_sub(), *do_gsub(), *do_strftime(), *do_systime();
/* Tokentab is sorted ascii ascending order, so it can be binary searched. */
static struct token tokentab[] = {
{"BEGIN", Node_illegal, LEX_BEGIN, 0, 0},
{"END", Node_illegal, LEX_END, 0, 0},
{"atan2", Node_builtin, LEX_BUILTIN, NOT_OLD|A(2), do_atan2},
{"break", Node_K_break, LEX_BREAK, 0, 0},
{"close", Node_builtin, LEX_BUILTIN, NOT_OLD|A(1), do_close},
{"continue", Node_K_continue, LEX_CONTINUE, 0, 0},
{"cos", Node_builtin, LEX_BUILTIN, NOT_OLD|A(1), do_cos},
{"delete", Node_K_delete, LEX_DELETE, NOT_OLD, 0},
{"do", Node_K_do, LEX_DO, NOT_OLD, 0},
{"else", Node_illegal, LEX_ELSE, 0, 0},
{"exit", Node_K_exit, LEX_EXIT, 0, 0},
{"exp", Node_builtin, LEX_BUILTIN, A(1), do_exp},
{"for", Node_K_for, LEX_FOR, 0, 0},
{"func", Node_K_function, LEX_FUNCTION, NOT_POSIX|NOT_OLD, 0},
{"function", Node_K_function, LEX_FUNCTION, NOT_OLD, 0},
{"getline", Node_K_getline, LEX_GETLINE, NOT_OLD, 0},
{"gsub", Node_builtin, LEX_BUILTIN, NOT_OLD|A(2)|A(3), do_gsub},
{"if", Node_K_if, LEX_IF, 0, 0},
{"in", Node_illegal, LEX_IN, 0, 0},
{"index", Node_builtin, LEX_BUILTIN, A(2), do_index},
{"int", Node_builtin, LEX_BUILTIN, A(1), do_int},
{"length", Node_builtin, LEX_LENGTH, A(0)|A(1), do_length},
{"log", Node_builtin, LEX_BUILTIN, A(1), do_log},
{"match", Node_builtin, LEX_BUILTIN, NOT_OLD|A(2), do_match},
{"next", Node_K_next, LEX_NEXT, 0, 0},
{"print", Node_K_print, LEX_PRINT, 0, 0},
{"printf", Node_K_printf, LEX_PRINTF, 0, 0},
{"rand", Node_builtin, LEX_BUILTIN, NOT_OLD|A(0), do_rand},
{"return", Node_K_return, LEX_RETURN, NOT_OLD, 0},
{"sin", Node_builtin, LEX_BUILTIN, NOT_OLD|A(1), do_sin},
{"split", Node_builtin, LEX_BUILTIN, A(2)|A(3), do_split},
{"sprintf", Node_builtin, LEX_BUILTIN, 0, do_sprintf},
{"sqrt", Node_builtin, LEX_BUILTIN, A(1), do_sqrt},
{"srand", Node_builtin, LEX_BUILTIN, NOT_OLD|A(0)|A(1), do_srand},
{"strftime", Node_builtin, LEX_BUILTIN, GAWKX|A(1)|A(2), do_strftime},
{"sub", Node_builtin, LEX_BUILTIN, NOT_OLD|A(2)|A(3), do_sub},
{"substr", Node_builtin, LEX_BUILTIN, A(2)|A(3), do_substr},
{"system", Node_builtin, LEX_BUILTIN, NOT_OLD|A(1), do_system},
{"systime", Node_builtin, LEX_BUILTIN, GAWKX|A(0), do_systime},
{"tolower", Node_builtin, LEX_BUILTIN, NOT_OLD|A(1), do_tolower},
{"toupper", Node_builtin, LEX_BUILTIN, NOT_OLD|A(1), do_toupper},
{"while", Node_K_while, LEX_WHILE, 0, 0},
/* Find the current line in the input file */
mesg = "unexpected newline";
for ( ; cp != lexptr_begin && *cp != '\n'; --cp)
/* NL isn't guaranteed */
while (bp < lexend && *bp && *bp != '\n')
thisline = "(END OF FILE)";
msg("%.*s", (int) (bp - thisline), thisline);
cp = buf + sizeof(buf) - 24; /* 24 more than longest msg. input */
while (bp < cp && scan < lexeme)
mesg = va_arg(args, char *);
static int did_newline = 0;
# define SLOP 128 /* enough space to hold most source lines */
if (srcfiles[nextfile].stype == CMDLINE) {
len = strlen(srcfiles[nextfile].val);
lexptr = lexptr_begin = srcfiles[nextfile].val;
} else if (!did_newline && *(lexptr-1) != '\n') {
* The following goop is to ensure that the source
* ends with a newline and that the entire current
* line is available for error messages.
offset = lexptr - lexeme;
for (scan = lexeme; scan > lexptr_begin; scan--)
emalloc(buf, char *, len+1, "get_src_buf");
lexeme = lexptr - offset;
lexeme = lexptr = lexptr_begin = NULL;
if (lexptr == NULL && ++nextfile <= numfiles)
source = srcfiles[nextfile].val;
return lexeme = lexptr = lexptr_begin = NULL;
fatal("can't open source file \"%s\" for reading (%s)",
source, strerror(errno));
len = optimal_bufsize(fd);
emalloc(buf, char *, len + SLOP, "get_src_buf");
lexptr_begin = buf + SLOP;
* Here, we retain the current source line (up to length SLOP)
* in the beginning of the buffer that was overallocated above
offset = lexptr - lexeme;
for (scan = lexeme; scan > lexptr_begin; scan--)
thisline = buf + SLOP - linelen;
memcpy(thisline, scan, linelen);
lexeme = buf + SLOP - offset;
n = read(fd, buf + SLOP, len);
fatal("can't read sourcefile \"%s\" (%s)",
source, strerror(errno));
#define tokadd(x) (*token++ = (x), token == tokend ? tokexpand() : token)
tokoffset = token - tokstart;
erealloc(tokstart, char *, toksize, "tokexpand");
emalloc(tokstart, char *, toksize, "tokexpand");
tokend = tokstart + toksize;
token = tokstart + tokoffset;
if (lexptr && lexptr < lexend)
#define nextc() ((lexptr && lexptr < lexend) ? \
(get_src_buf() ? *lexptr++ : '\0') \
#define pushback() (lexptr && lexptr > lexptr_begin ? lexptr-- : lexptr)
* Read the input and turn it into tokens.
int seen_e = 0; /* These are for numbers */
int esc_seen; /* for literal strings */
static int did_newline = 0;
while ((c = nextc()) != 0) {
if ((c = nextc()) == '\0') {
yyerror("unterminated regexp ends with \\ at end of file");
case '/': /* end of the regexp */
yyerror("unterminated regexp");
yyerror("unterminated regexp at end of file");
while ((c = nextc()) == ' ' || c == '\t')
lexeme = lexptr ? lexptr - 1 : lexptr;
yylval.nodetypeval = Node_illegal;
case '#': /* it's a comment */
while ((c = nextc()) != '\n') {
#ifdef RELAXED_CONTINUATION
if (!do_unix) { /* strip trailing white-space and/or comment */
while ((c = nextc()) == ' ' || c == '\t') continue;
while ((c = nextc()) != '\n') if (!c) break;
#endif /*RELAXED_CONTINUATION*/
yyerror("inappropriate use of backslash");
if ((c = nextc()) == '=') {
yylval.nodetypeval = Node_assign_times;
/* make ** and **= aliases for ^ and ^= */
static int did_warn_op = 0, did_warn_assgn = 0;
if (do_lint && ! did_warn_assgn) {
warning("**= is not allowed by POSIX");
yylval.nodetypeval = Node_assign_exp;
if (do_lint && ! did_warn_op) {
warning("** is not allowed by POSIX");
yylval.nodetypeval = Node_assign_quotient;
yylval.nodetypeval = Node_assign_mod;
static int did_warn_op = 0, did_warn_assgn = 0;
if (do_lint && ! did_warn_assgn) {
warning("operator `^=' is not supported in old awk");
yylval.nodetypeval = Node_assign_exp;
if (do_lint && ! did_warn_op) {
warning("operator `^' is not supported in old awk");
if ((c = nextc()) == '=') {
yylval.nodetypeval = Node_assign_plus;
if ((c = nextc()) == '=') {
yylval.nodetypeval = Node_notequal;
yylval.nodetypeval = Node_nomatch;
yylval.nodetypeval = Node_leq;
yylval.nodetypeval = Node_less;
yylval.nodetypeval = Node_equal;
yylval.nodetypeval = Node_assign;
if ((c = nextc()) == '=') {
yylval.nodetypeval = Node_geq;
yylval.nodetypeval = Node_redirect_append;
yylval.nodetypeval = Node_greater;
yylval.nodetypeval = Node_match;
* Added did newline stuff. Easier than
--lexptr; /* pick up } next time */
while ((c = nextc()) != '"') {
yyerror("unterminated string");
yyerror("unterminated string");
yylval.nodeval = make_str_node(tokstart,
token - tokstart, esc_seen ? SCAN : 0);
yylval.nodeval->flags |= PERM;
if ((c = nextc()) == '=') {
yylval.nodetypeval = Node_assign_minus;
c = '.'; /* FALL THROUGH */
if ((c = nextc()) == '-' || c == '+')
yylval.nodeval = make_number(atof(tokstart));
yylval.nodeval->flags |= PERM;
if ((c = nextc()) == '&') {
yylval.nodetypeval = Node_and;
while ((c = nextc()) != '\n' && c != '\0')
if ((c = nextc()) == '|') {
yylval.nodetypeval = Node_or;
while ((c = nextc()) != '\n' && c != '\0')
if (c != '_' && ! isalpha(c))
yyerror("Invalid char '%c' in expression\n", c);
/* it's some type of name-type-thing. Find its length */
while (is_identchar(c)) {
emalloc(tokkey, char *, token - tokstart, "yylex");
memcpy(tokkey, tokstart, token - tokstart);
/* See if it is a special token. */
high = (sizeof (tokentab) / sizeof (tokentab[0])) - 1;
c = *tokstart - tokentab[mid].operator[0];
i = c ? c : strcmp (tokstart, tokentab[mid].operator);
if (i < 0) { /* token < mid */
} else if (i > 0) { /* token > mid */
if (tokentab[mid].flags & GAWKX)
warning("%s() is a gawk extension",
if (tokentab[mid].flags & NOT_POSIX)
warning("POSIX does not allow %s",
if (tokentab[mid].flags & NOT_OLD)
warning("%s is not supported in old awk",
if ((do_unix && (tokentab[mid].flags & GAWKX))
|| (do_posix && (tokentab[mid].flags & NOT_POSIX)))
if (tokentab[mid].class == LEX_BUILTIN
|| tokentab[mid].class == LEX_LENGTH
yylval.nodetypeval = tokentab[mid].value;
return tokentab[mid].class;
/* if lookahead is NL, lineno is 1 too high */
if (lexeme && *lexeme == '\n')
r->source_line = sourceline - 1;
r->source_line = sourceline;
* This allocates a node with defined lnode and rnode.
* This allocates a node with defined subnode and proc for builtin functions
* Checks for arg. count and supplies defaults where possible.
/* traverse expression list to see how many args. given */
for (n= subn; n; n= n->rnode) {
/* check against how many args. are allowed for this builtin */
args_allowed = tokentab[idx].flags & ARGS;
if (args_allowed && !(args_allowed & A(nexp)))
fatal("%s() cannot have %d argument%c",
tokentab[idx].operator, nexp, nexp == 1 ? ' ' : 's');
r->proc = tokentab[idx].ptr;
/* special case processing for a few builtins */
if (nexp == 0 && r->proc == do_length) {
subn = node(node(make_number(0.0),Node_field_spec,(NODE *)NULL),
} else if (r->proc == do_match) {
if (subn->rnode->lnode->type != Node_regex)
subn->rnode->lnode = mk_rexp(subn->rnode->lnode);
} else if (r->proc == do_sub || r->proc == do_gsub) {
if (subn->lnode->type != Node_regex)
subn->lnode = mk_rexp(subn->lnode);
append_right(subn, node(node(make_number(0.0),
else if (do_lint && subn->rnode->rnode->lnode->type == Node_val)
warning("string literal as last arg of substitute");
} else if (r->proc == do_split) {
node(FS_node, Node_expression_list, (NODE *) NULL));
n = subn->rnode->rnode->lnode;
if (n->type != Node_regex)
subn->rnode->rnode->lnode = mk_rexp(n);
subn->rnode->rnode->lnode->re_flags |= FS_DFLT;
* This allocates a Node_line_range node with defined condpair and
* zeroes the trigger word to avoid the temptation of assuming that calling
* 'node( foo, Node_line_range, 0)' will properly initialize 'triggered'.
/* Otherwise like node() */
r->type = Node_line_range;
make_for_loop(init, cond, incr)
NODE *init, *cond, *incr;
register FOR_LOOP_HEADER *r;
emalloc(r, FOR_LOOP_HEADER *, sizeof(FOR_LOOP_HEADER), "make_for_loop");
* Install a name in the symbol table, even if it is already there.
* Caller must check against redefinition if that is desired.
register int len, bucket;
bucket = hash(name, len);
hp->type = Node_hashnode;
hp->hnext = variables[bucket];
hp->hvalue->vname = name;
/* find the most recent hash node for name installed by install */
bucket = variables[hash(name, len)];
if (bucket->hlength == len && STREQN(bucket->hname, name, len))
* Add new to the rightmost branch of LIST. This uses n^2 time, so we make
* a simple attempt at optimizing it.
static NODE *savefront = NULL, *savetail = NULL;
if (savefront == oldlist) {
savetail = savetail->rnode = new;
while (list->rnode != NULL)
savetail = list->rnode = new;
* check if name is already installed; if so, it had better have Null value,
* in which case def is added as the value. Otherwise, install name with def
func_install(params, def)
pop_params(params->rnode);
r = lookup(params->param);
fatal("function name `%s' previously defined", params->param);
(void) install(params->param, node(params, Node_func, def));
register NODE *bucket, **save;
save = &(variables[hash(name, len)]);
for (bucket = *save; bucket; bucket = bucket->hnext) {
if (len == bucket->hlength && STREQN(bucket->hname, name, len)) {
for (np = params; np != NULL; np = np->rnode)
r->type = Node_param_list;
r->param_cnt = param_counter++;
return (install(name, r));
/* Name points to a variable name. Make sure its in the symbol table */
static int env_loaded = 0;
if (!env_loaded && STREQ(name, "ENVIRON")) {
if ((r = lookup(name)) == NULL)
r = install(name, node(Nnull_string, Node_var, (NODE *) NULL));
if (exp->type == Node_regex)