xref: /inferno-os/utils/awk/awkgram.y (revision 74a4d8c26dd3c1e9febcb717cfd6cb6512991a7a)
1*74a4d8c2SCharles.Forsyth /****************************************************************
2*74a4d8c2SCharles.Forsyth Copyright (C) Lucent Technologies 1997
3*74a4d8c2SCharles.Forsyth All Rights Reserved
4*74a4d8c2SCharles.Forsyth 
5*74a4d8c2SCharles.Forsyth Permission to use, copy, modify, and distribute this software and
6*74a4d8c2SCharles.Forsyth its documentation for any purpose and without fee is hereby
7*74a4d8c2SCharles.Forsyth granted, provided that the above copyright notice appear in all
8*74a4d8c2SCharles.Forsyth copies and that both that the copyright notice and this
9*74a4d8c2SCharles.Forsyth permission notice and warranty disclaimer appear in supporting
10*74a4d8c2SCharles.Forsyth documentation, and that the name Lucent Technologies or any of
11*74a4d8c2SCharles.Forsyth its entities not be used in advertising or publicity pertaining
12*74a4d8c2SCharles.Forsyth to distribution of the software without specific, written prior
13*74a4d8c2SCharles.Forsyth permission.
14*74a4d8c2SCharles.Forsyth 
15*74a4d8c2SCharles.Forsyth LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16*74a4d8c2SCharles.Forsyth INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
17*74a4d8c2SCharles.Forsyth IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
18*74a4d8c2SCharles.Forsyth SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19*74a4d8c2SCharles.Forsyth WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
20*74a4d8c2SCharles.Forsyth IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
21*74a4d8c2SCharles.Forsyth ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
22*74a4d8c2SCharles.Forsyth THIS SOFTWARE.
23*74a4d8c2SCharles.Forsyth ****************************************************************/
24*74a4d8c2SCharles.Forsyth 
25*74a4d8c2SCharles.Forsyth %{
26*74a4d8c2SCharles.Forsyth #include <stdio.h>
27*74a4d8c2SCharles.Forsyth #include <string.h>
28*74a4d8c2SCharles.Forsyth #include "awk.h"
29*74a4d8c2SCharles.Forsyth 
30*74a4d8c2SCharles.Forsyth void checkdup(Node *list, Cell *item);
yywrap(void)31*74a4d8c2SCharles.Forsyth int yywrap(void) { return(1); }
32*74a4d8c2SCharles.Forsyth 
33*74a4d8c2SCharles.Forsyth Node	*beginloc = 0;
34*74a4d8c2SCharles.Forsyth Node	*endloc = 0;
35*74a4d8c2SCharles.Forsyth int	infunc	= 0;	/* = 1 if in arglist or body of func */
36*74a4d8c2SCharles.Forsyth int	inloop	= 0;	/* = 1 if in while, for, do */
37*74a4d8c2SCharles.Forsyth char	*curfname = 0;	/* current function name */
38*74a4d8c2SCharles.Forsyth Node	*arglist = 0;	/* list of args for current function */
39*74a4d8c2SCharles.Forsyth %}
40*74a4d8c2SCharles.Forsyth 
41*74a4d8c2SCharles.Forsyth %union {
42*74a4d8c2SCharles.Forsyth 	Node	*p;
43*74a4d8c2SCharles.Forsyth 	Cell	*cp;
44*74a4d8c2SCharles.Forsyth 	int	i;
45*74a4d8c2SCharles.Forsyth 	char	*s;
46*74a4d8c2SCharles.Forsyth }
47*74a4d8c2SCharles.Forsyth 
48*74a4d8c2SCharles.Forsyth %token	<i>	FIRSTTOKEN	/* must be first */
49*74a4d8c2SCharles.Forsyth %token	<p>	PROGRAM PASTAT PASTAT2 XBEGIN XEND
50*74a4d8c2SCharles.Forsyth %token	<i>	NL ',' '{' '(' '|' ';' '/' ')' '}' '[' ']'
51*74a4d8c2SCharles.Forsyth %token	<i>	ARRAY
52*74a4d8c2SCharles.Forsyth %token	<i>	MATCH NOTMATCH MATCHOP
53*74a4d8c2SCharles.Forsyth %token	<i>	FINAL DOT ALL CCL NCCL CHAR OR STAR QUEST PLUS
54*74a4d8c2SCharles.Forsyth %token	<i>	AND BOR APPEND EQ GE GT LE LT NE IN
55*74a4d8c2SCharles.Forsyth %token	<i>	ARG BLTIN BREAK CLOSE CONTINUE DELETE DO EXIT FOR FUNC
56*74a4d8c2SCharles.Forsyth %token	<i>	SUB GSUB IF INDEX LSUBSTR MATCHFCN NEXT NEXTFILE
57*74a4d8c2SCharles.Forsyth %token	<i>	ADD MINUS MULT DIVIDE MOD
58*74a4d8c2SCharles.Forsyth %token	<i>	ASSIGN ASGNOP ADDEQ SUBEQ MULTEQ DIVEQ MODEQ POWEQ
59*74a4d8c2SCharles.Forsyth %token	<i>	PRINT PRINTF SPRINTF
60*74a4d8c2SCharles.Forsyth %token	<p>	ELSE INTEST CONDEXPR
61*74a4d8c2SCharles.Forsyth %token	<i>	POSTINCR PREINCR POSTDECR PREDECR
62*74a4d8c2SCharles.Forsyth %token	<cp>	VAR IVAR VARNF CALL NUMBER STRING
63*74a4d8c2SCharles.Forsyth %token	<s>	REGEXPR
64*74a4d8c2SCharles.Forsyth 
65*74a4d8c2SCharles.Forsyth %type	<p>	pas pattern ppattern plist pplist patlist prarg term re
66*74a4d8c2SCharles.Forsyth %type	<p>	pa_pat pa_stat pa_stats
67*74a4d8c2SCharles.Forsyth %type	<s>	reg_expr
68*74a4d8c2SCharles.Forsyth %type	<p>	simple_stmt opt_simple_stmt stmt stmtlist
69*74a4d8c2SCharles.Forsyth %type	<p>	var varname funcname varlist
70*74a4d8c2SCharles.Forsyth %type	<p>	for if else while
71*74a4d8c2SCharles.Forsyth %type	<i>	do st
72*74a4d8c2SCharles.Forsyth %type	<i>	pst opt_pst lbrace rbrace rparen comma nl opt_nl and bor
73*74a4d8c2SCharles.Forsyth %type	<i>	subop print
74*74a4d8c2SCharles.Forsyth 
75*74a4d8c2SCharles.Forsyth %right	ASGNOP
76*74a4d8c2SCharles.Forsyth %right	'?'
77*74a4d8c2SCharles.Forsyth %right	':'
78*74a4d8c2SCharles.Forsyth %left	BOR
79*74a4d8c2SCharles.Forsyth %left	AND
80*74a4d8c2SCharles.Forsyth %left	GETLINE
81*74a4d8c2SCharles.Forsyth %nonassoc APPEND EQ GE GT LE LT NE MATCHOP IN '|'
82*74a4d8c2SCharles.Forsyth %left	ARG BLTIN BREAK CALL CLOSE CONTINUE DELETE DO EXIT FOR FUNC
83*74a4d8c2SCharles.Forsyth %left	GSUB IF INDEX LSUBSTR MATCHFCN NEXT NUMBER
84*74a4d8c2SCharles.Forsyth %left	PRINT PRINTF RETURN SPLIT SPRINTF STRING SUB SUBSTR
85*74a4d8c2SCharles.Forsyth %left	REGEXPR VAR VARNF IVAR WHILE '('
86*74a4d8c2SCharles.Forsyth %left	CAT
87*74a4d8c2SCharles.Forsyth %left	'+' '-'
88*74a4d8c2SCharles.Forsyth %left	'*' '/' '%'
89*74a4d8c2SCharles.Forsyth %left	NOT UMINUS
90*74a4d8c2SCharles.Forsyth %right	POWER
91*74a4d8c2SCharles.Forsyth %right	DECR INCR
92*74a4d8c2SCharles.Forsyth %left	INDIRECT
93*74a4d8c2SCharles.Forsyth %token	LASTTOKEN	/* must be last */
94*74a4d8c2SCharles.Forsyth 
95*74a4d8c2SCharles.Forsyth %%
96*74a4d8c2SCharles.Forsyth 
97*74a4d8c2SCharles.Forsyth program:
98*74a4d8c2SCharles.Forsyth 	  pas	{ if (errorflag==0)
99*74a4d8c2SCharles.Forsyth 			winner = (Node *)stat3(PROGRAM, beginloc, $1, endloc); }
100*74a4d8c2SCharles.Forsyth 	| error	{ yyclearin; bracecheck(); SYNTAX("bailing out"); }
101*74a4d8c2SCharles.Forsyth 	;
102*74a4d8c2SCharles.Forsyth 
103*74a4d8c2SCharles.Forsyth and:
104*74a4d8c2SCharles.Forsyth 	  AND | and NL
105*74a4d8c2SCharles.Forsyth 	;
106*74a4d8c2SCharles.Forsyth 
107*74a4d8c2SCharles.Forsyth bor:
108*74a4d8c2SCharles.Forsyth 	  BOR | bor NL
109*74a4d8c2SCharles.Forsyth 	;
110*74a4d8c2SCharles.Forsyth 
111*74a4d8c2SCharles.Forsyth comma:
112*74a4d8c2SCharles.Forsyth 	  ',' | comma NL
113*74a4d8c2SCharles.Forsyth 	;
114*74a4d8c2SCharles.Forsyth 
115*74a4d8c2SCharles.Forsyth do:
116*74a4d8c2SCharles.Forsyth 	  DO | do NL
117*74a4d8c2SCharles.Forsyth 	;
118*74a4d8c2SCharles.Forsyth 
119*74a4d8c2SCharles.Forsyth else:
120*74a4d8c2SCharles.Forsyth 	  ELSE | else NL
121*74a4d8c2SCharles.Forsyth 	;
122*74a4d8c2SCharles.Forsyth 
123*74a4d8c2SCharles.Forsyth for:
124*74a4d8c2SCharles.Forsyth 	  FOR '(' opt_simple_stmt ';' opt_nl pattern ';' opt_nl opt_simple_stmt rparen {inloop++;} stmt
125*74a4d8c2SCharles.Forsyth 		{ --inloop; $$ = stat4(FOR, $3, notnull($6), $9, $12); }
126*74a4d8c2SCharles.Forsyth 	| FOR '(' opt_simple_stmt ';'  ';' opt_nl opt_simple_stmt rparen {inloop++;} stmt
127*74a4d8c2SCharles.Forsyth 		{ --inloop; $$ = stat4(FOR, $3, NIL, $7, $10); }
128*74a4d8c2SCharles.Forsyth 	| FOR '(' varname IN varname rparen {inloop++;} stmt
129*74a4d8c2SCharles.Forsyth 		{ --inloop; $$ = stat3(IN, $3, makearr($5), $8); }
130*74a4d8c2SCharles.Forsyth 	;
131*74a4d8c2SCharles.Forsyth 
132*74a4d8c2SCharles.Forsyth funcname:
133*74a4d8c2SCharles.Forsyth 	  VAR	{ setfname($1); }
134*74a4d8c2SCharles.Forsyth 	| CALL	{ setfname($1); }
135*74a4d8c2SCharles.Forsyth 	;
136*74a4d8c2SCharles.Forsyth 
137*74a4d8c2SCharles.Forsyth if:
138*74a4d8c2SCharles.Forsyth 	  IF '(' pattern rparen		{ $$ = notnull($3); }
139*74a4d8c2SCharles.Forsyth 	;
140*74a4d8c2SCharles.Forsyth 
141*74a4d8c2SCharles.Forsyth lbrace:
142*74a4d8c2SCharles.Forsyth 	  '{' | lbrace NL
143*74a4d8c2SCharles.Forsyth 	;
144*74a4d8c2SCharles.Forsyth 
145*74a4d8c2SCharles.Forsyth nl:
146*74a4d8c2SCharles.Forsyth 	  NL | nl NL
147*74a4d8c2SCharles.Forsyth 	;
148*74a4d8c2SCharles.Forsyth 
149*74a4d8c2SCharles.Forsyth opt_nl:
150*74a4d8c2SCharles.Forsyth 	  /* empty */	{ $$ = 0; }
151*74a4d8c2SCharles.Forsyth 	| nl
152*74a4d8c2SCharles.Forsyth 	;
153*74a4d8c2SCharles.Forsyth 
154*74a4d8c2SCharles.Forsyth opt_pst:
155*74a4d8c2SCharles.Forsyth 	  /* empty */	{ $$ = 0; }
156*74a4d8c2SCharles.Forsyth 	| pst
157*74a4d8c2SCharles.Forsyth 	;
158*74a4d8c2SCharles.Forsyth 
159*74a4d8c2SCharles.Forsyth 
160*74a4d8c2SCharles.Forsyth opt_simple_stmt:
161*74a4d8c2SCharles.Forsyth 	  /* empty */			{ $$ = 0; }
162*74a4d8c2SCharles.Forsyth 	| simple_stmt
163*74a4d8c2SCharles.Forsyth 	;
164*74a4d8c2SCharles.Forsyth 
165*74a4d8c2SCharles.Forsyth pas:
166*74a4d8c2SCharles.Forsyth 	  opt_pst			{ $$ = 0; }
167*74a4d8c2SCharles.Forsyth 	| opt_pst pa_stats opt_pst	{ $$ = $2; }
168*74a4d8c2SCharles.Forsyth 	;
169*74a4d8c2SCharles.Forsyth 
170*74a4d8c2SCharles.Forsyth pa_pat:
171*74a4d8c2SCharles.Forsyth 	  pattern	{ $$ = notnull($1); }
172*74a4d8c2SCharles.Forsyth 	;
173*74a4d8c2SCharles.Forsyth 
174*74a4d8c2SCharles.Forsyth pa_stat:
175*74a4d8c2SCharles.Forsyth 	  pa_pat			{ $$ = stat2(PASTAT, $1, stat2(PRINT, rectonode(), NIL)); }
176*74a4d8c2SCharles.Forsyth 	| pa_pat lbrace stmtlist '}'	{ $$ = stat2(PASTAT, $1, $3); }
177*74a4d8c2SCharles.Forsyth 	| pa_pat ',' pa_pat		{ $$ = pa2stat($1, $3, stat2(PRINT, rectonode(), NIL)); }
178*74a4d8c2SCharles.Forsyth 	| pa_pat ',' pa_pat lbrace stmtlist '}'	{ $$ = pa2stat($1, $3, $5); }
179*74a4d8c2SCharles.Forsyth 	| lbrace stmtlist '}'		{ $$ = stat2(PASTAT, NIL, $2); }
180*74a4d8c2SCharles.Forsyth 	| XBEGIN lbrace stmtlist '}'
181*74a4d8c2SCharles.Forsyth 		{ beginloc = linkum(beginloc, $3); $$ = 0; }
182*74a4d8c2SCharles.Forsyth 	| XEND lbrace stmtlist '}'
183*74a4d8c2SCharles.Forsyth 		{ endloc = linkum(endloc, $3); $$ = 0; }
184*74a4d8c2SCharles.Forsyth 	| FUNC funcname '(' varlist rparen {infunc++;} lbrace stmtlist '}'
185*74a4d8c2SCharles.Forsyth 		{ infunc--; curfname=0; defn((Cell *)$2, $4, $8); $$ = 0; }
186*74a4d8c2SCharles.Forsyth 	;
187*74a4d8c2SCharles.Forsyth 
188*74a4d8c2SCharles.Forsyth pa_stats:
189*74a4d8c2SCharles.Forsyth 	  pa_stat
190*74a4d8c2SCharles.Forsyth 	| pa_stats opt_pst pa_stat	{ $$ = linkum($1, $3); }
191*74a4d8c2SCharles.Forsyth 	;
192*74a4d8c2SCharles.Forsyth 
193*74a4d8c2SCharles.Forsyth patlist:
194*74a4d8c2SCharles.Forsyth 	  pattern
195*74a4d8c2SCharles.Forsyth 	| patlist comma pattern		{ $$ = linkum($1, $3); }
196*74a4d8c2SCharles.Forsyth 	;
197*74a4d8c2SCharles.Forsyth 
198*74a4d8c2SCharles.Forsyth ppattern:
199*74a4d8c2SCharles.Forsyth 	  var ASGNOP ppattern		{ $$ = op2($2, $1, $3); }
200*74a4d8c2SCharles.Forsyth 	| ppattern '?' ppattern ':' ppattern %prec '?'
201*74a4d8c2SCharles.Forsyth 	 	{ $$ = op3(CONDEXPR, notnull($1), $3, $5); }
202*74a4d8c2SCharles.Forsyth 	| ppattern bor ppattern %prec BOR
203*74a4d8c2SCharles.Forsyth 		{ $$ = op2(BOR, notnull($1), notnull($3)); }
204*74a4d8c2SCharles.Forsyth 	| ppattern and ppattern %prec AND
205*74a4d8c2SCharles.Forsyth 		{ $$ = op2(AND, notnull($1), notnull($3)); }
206*74a4d8c2SCharles.Forsyth 	| ppattern MATCHOP reg_expr	{ $$ = op3($2, NIL, $1, (Node*)makedfa($3, 0)); }
207*74a4d8c2SCharles.Forsyth 	| ppattern MATCHOP ppattern
208*74a4d8c2SCharles.Forsyth 		{ if (constnode($3))
209*74a4d8c2SCharles.Forsyth 			$$ = op3($2, NIL, $1, (Node*)makedfa(strnode($3), 0));
210*74a4d8c2SCharles.Forsyth 		  else
211*74a4d8c2SCharles.Forsyth 			$$ = op3($2, (Node *)1, $1, $3); }
212*74a4d8c2SCharles.Forsyth 	| ppattern IN varname		{ $$ = op2(INTEST, $1, makearr($3)); }
213*74a4d8c2SCharles.Forsyth 	| '(' plist ')' IN varname	{ $$ = op2(INTEST, $2, makearr($5)); }
214*74a4d8c2SCharles.Forsyth 	| ppattern term %prec CAT	{ $$ = op2(CAT, $1, $2); }
215*74a4d8c2SCharles.Forsyth 	| re
216*74a4d8c2SCharles.Forsyth 	| term
217*74a4d8c2SCharles.Forsyth 	;
218*74a4d8c2SCharles.Forsyth 
219*74a4d8c2SCharles.Forsyth pattern:
220*74a4d8c2SCharles.Forsyth 	  var ASGNOP pattern		{ $$ = op2($2, $1, $3); }
221*74a4d8c2SCharles.Forsyth 	| pattern '?' pattern ':' pattern %prec '?'
222*74a4d8c2SCharles.Forsyth 	 	{ $$ = op3(CONDEXPR, notnull($1), $3, $5); }
223*74a4d8c2SCharles.Forsyth 	| pattern bor pattern %prec BOR
224*74a4d8c2SCharles.Forsyth 		{ $$ = op2(BOR, notnull($1), notnull($3)); }
225*74a4d8c2SCharles.Forsyth 	| pattern and pattern %prec AND
226*74a4d8c2SCharles.Forsyth 		{ $$ = op2(AND, notnull($1), notnull($3)); }
227*74a4d8c2SCharles.Forsyth 	| pattern EQ pattern		{ $$ = op2($2, $1, $3); }
228*74a4d8c2SCharles.Forsyth 	| pattern GE pattern		{ $$ = op2($2, $1, $3); }
229*74a4d8c2SCharles.Forsyth 	| pattern GT pattern		{ $$ = op2($2, $1, $3); }
230*74a4d8c2SCharles.Forsyth 	| pattern LE pattern		{ $$ = op2($2, $1, $3); }
231*74a4d8c2SCharles.Forsyth 	| pattern LT pattern		{ $$ = op2($2, $1, $3); }
232*74a4d8c2SCharles.Forsyth 	| pattern NE pattern		{ $$ = op2($2, $1, $3); }
233*74a4d8c2SCharles.Forsyth 	| pattern MATCHOP reg_expr	{ $$ = op3($2, NIL, $1, (Node*)makedfa($3, 0)); }
234*74a4d8c2SCharles.Forsyth 	| pattern MATCHOP pattern
235*74a4d8c2SCharles.Forsyth 		{ if (constnode($3))
236*74a4d8c2SCharles.Forsyth 			$$ = op3($2, NIL, $1, (Node*)makedfa(strnode($3), 0));
237*74a4d8c2SCharles.Forsyth 		  else
238*74a4d8c2SCharles.Forsyth 			$$ = op3($2, (Node *)1, $1, $3); }
239*74a4d8c2SCharles.Forsyth 	| pattern IN varname		{ $$ = op2(INTEST, $1, makearr($3)); }
240*74a4d8c2SCharles.Forsyth 	| '(' plist ')' IN varname	{ $$ = op2(INTEST, $2, makearr($5)); }
241*74a4d8c2SCharles.Forsyth 	| pattern '|' GETLINE var	{
242*74a4d8c2SCharles.Forsyth 			if (safe) SYNTAX("cmd | getline is unsafe");
243*74a4d8c2SCharles.Forsyth 			else $$ = op3(GETLINE, $4, itonp($2), $1); }
244*74a4d8c2SCharles.Forsyth 	| pattern '|' GETLINE		{
245*74a4d8c2SCharles.Forsyth 			if (safe) SYNTAX("cmd | getline is unsafe");
246*74a4d8c2SCharles.Forsyth 			else $$ = op3(GETLINE, (Node*)0, itonp($2), $1); }
247*74a4d8c2SCharles.Forsyth 	| pattern term %prec CAT	{ $$ = op2(CAT, $1, $2); }
248*74a4d8c2SCharles.Forsyth 	| re
249*74a4d8c2SCharles.Forsyth 	| term
250*74a4d8c2SCharles.Forsyth 	;
251*74a4d8c2SCharles.Forsyth 
252*74a4d8c2SCharles.Forsyth plist:
253*74a4d8c2SCharles.Forsyth 	  pattern comma pattern		{ $$ = linkum($1, $3); }
254*74a4d8c2SCharles.Forsyth 	| plist comma pattern		{ $$ = linkum($1, $3); }
255*74a4d8c2SCharles.Forsyth 	;
256*74a4d8c2SCharles.Forsyth 
257*74a4d8c2SCharles.Forsyth pplist:
258*74a4d8c2SCharles.Forsyth 	  ppattern
259*74a4d8c2SCharles.Forsyth 	| pplist comma ppattern		{ $$ = linkum($1, $3); }
260*74a4d8c2SCharles.Forsyth 	;
261*74a4d8c2SCharles.Forsyth 
262*74a4d8c2SCharles.Forsyth prarg:
263*74a4d8c2SCharles.Forsyth 	  /* empty */			{ $$ = rectonode(); }
264*74a4d8c2SCharles.Forsyth 	| pplist
265*74a4d8c2SCharles.Forsyth 	| '(' plist ')'			{ $$ = $2; }
266*74a4d8c2SCharles.Forsyth 	;
267*74a4d8c2SCharles.Forsyth 
268*74a4d8c2SCharles.Forsyth print:
269*74a4d8c2SCharles.Forsyth 	  PRINT | PRINTF
270*74a4d8c2SCharles.Forsyth 	;
271*74a4d8c2SCharles.Forsyth 
272*74a4d8c2SCharles.Forsyth pst:
273*74a4d8c2SCharles.Forsyth 	  NL | ';' | pst NL | pst ';'
274*74a4d8c2SCharles.Forsyth 	;
275*74a4d8c2SCharles.Forsyth 
276*74a4d8c2SCharles.Forsyth rbrace:
277*74a4d8c2SCharles.Forsyth 	  '}' | rbrace NL
278*74a4d8c2SCharles.Forsyth 	;
279*74a4d8c2SCharles.Forsyth 
280*74a4d8c2SCharles.Forsyth re:
281*74a4d8c2SCharles.Forsyth 	   reg_expr
282*74a4d8c2SCharles.Forsyth 		{ $$ = op3(MATCH, NIL, rectonode(), (Node*)makedfa($1, 0)); }
283*74a4d8c2SCharles.Forsyth 	| NOT re	{ $$ = op1(NOT, notnull($2)); }
284*74a4d8c2SCharles.Forsyth 	;
285*74a4d8c2SCharles.Forsyth 
286*74a4d8c2SCharles.Forsyth reg_expr:
287*74a4d8c2SCharles.Forsyth 	  '/' {startreg();} REGEXPR '/'		{ $$ = $3; }
288*74a4d8c2SCharles.Forsyth 	;
289*74a4d8c2SCharles.Forsyth 
290*74a4d8c2SCharles.Forsyth rparen:
291*74a4d8c2SCharles.Forsyth 	  ')' | rparen NL
292*74a4d8c2SCharles.Forsyth 	;
293*74a4d8c2SCharles.Forsyth 
294*74a4d8c2SCharles.Forsyth simple_stmt:
295*74a4d8c2SCharles.Forsyth 	  print prarg '|' term		{
296*74a4d8c2SCharles.Forsyth 			if (safe) SYNTAX("print | is unsafe");
297*74a4d8c2SCharles.Forsyth 			else $$ = stat3($1, $2, itonp($3), $4); }
298*74a4d8c2SCharles.Forsyth 	| print prarg APPEND term	{
299*74a4d8c2SCharles.Forsyth 			if (safe) SYNTAX("print >> is unsafe");
300*74a4d8c2SCharles.Forsyth 			else $$ = stat3($1, $2, itonp($3), $4); }
301*74a4d8c2SCharles.Forsyth 	| print prarg GT term		{
302*74a4d8c2SCharles.Forsyth 			if (safe) SYNTAX("print > is unsafe");
303*74a4d8c2SCharles.Forsyth 			else $$ = stat3($1, $2, itonp($3), $4); }
304*74a4d8c2SCharles.Forsyth 	| print prarg			{ $$ = stat3($1, $2, NIL, NIL); }
305*74a4d8c2SCharles.Forsyth 	| DELETE varname '[' patlist ']' { $$ = stat2(DELETE, makearr($2), $4); }
306*74a4d8c2SCharles.Forsyth 	| DELETE varname		 { $$ = stat2(DELETE, makearr($2), 0); }
307*74a4d8c2SCharles.Forsyth 	| pattern			{ $$ = exptostat($1); }
308*74a4d8c2SCharles.Forsyth 	| error				{ yyclearin; SYNTAX("illegal statement"); }
309*74a4d8c2SCharles.Forsyth 	;
310*74a4d8c2SCharles.Forsyth 
311*74a4d8c2SCharles.Forsyth st:
312*74a4d8c2SCharles.Forsyth 	  nl
313*74a4d8c2SCharles.Forsyth 	| ';' opt_nl
314*74a4d8c2SCharles.Forsyth 	;
315*74a4d8c2SCharles.Forsyth 
316*74a4d8c2SCharles.Forsyth stmt:
317*74a4d8c2SCharles.Forsyth 	  BREAK st		{ if (!inloop) SYNTAX("break illegal outside of loops");
318*74a4d8c2SCharles.Forsyth 				  $$ = stat1(BREAK, NIL); }
319*74a4d8c2SCharles.Forsyth 	| CONTINUE st		{  if (!inloop) SYNTAX("continue illegal outside of loops");
320*74a4d8c2SCharles.Forsyth 				  $$ = stat1(CONTINUE, NIL); }
321*74a4d8c2SCharles.Forsyth 	| do {inloop++;} stmt {--inloop;} WHILE '(' pattern ')' st
322*74a4d8c2SCharles.Forsyth 		{ $$ = stat2(DO, $3, notnull($7)); }
323*74a4d8c2SCharles.Forsyth 	| EXIT pattern st	{ $$ = stat1(EXIT, $2); }
324*74a4d8c2SCharles.Forsyth 	| EXIT st		{ $$ = stat1(EXIT, NIL); }
325*74a4d8c2SCharles.Forsyth 	| for
326*74a4d8c2SCharles.Forsyth 	| if stmt else stmt	{ $$ = stat3(IF, $1, $2, $4); }
327*74a4d8c2SCharles.Forsyth 	| if stmt		{ $$ = stat3(IF, $1, $2, NIL); }
328*74a4d8c2SCharles.Forsyth 	| lbrace stmtlist rbrace { $$ = $2; }
329*74a4d8c2SCharles.Forsyth 	| NEXT st	{ if (infunc)
330*74a4d8c2SCharles.Forsyth 				SYNTAX("next is illegal inside a function");
331*74a4d8c2SCharles.Forsyth 			  $$ = stat1(NEXT, NIL); }
332*74a4d8c2SCharles.Forsyth 	| NEXTFILE st	{ if (infunc)
333*74a4d8c2SCharles.Forsyth 				SYNTAX("nextfile is illegal inside a function");
334*74a4d8c2SCharles.Forsyth 			  $$ = stat1(NEXTFILE, NIL); }
335*74a4d8c2SCharles.Forsyth 	| RETURN pattern st	{ $$ = stat1(RETURN, $2); }
336*74a4d8c2SCharles.Forsyth 	| RETURN st		{ $$ = stat1(RETURN, NIL); }
337*74a4d8c2SCharles.Forsyth 	| simple_stmt st
338*74a4d8c2SCharles.Forsyth 	| while {inloop++;} stmt	{ --inloop; $$ = stat2(WHILE, $1, $3); }
339*74a4d8c2SCharles.Forsyth 	| ';' opt_nl		{ $$ = 0; }
340*74a4d8c2SCharles.Forsyth 	;
341*74a4d8c2SCharles.Forsyth 
342*74a4d8c2SCharles.Forsyth stmtlist:
343*74a4d8c2SCharles.Forsyth 	  stmt
344*74a4d8c2SCharles.Forsyth 	| stmtlist stmt		{ $$ = linkum($1, $2); }
345*74a4d8c2SCharles.Forsyth 	;
346*74a4d8c2SCharles.Forsyth 
347*74a4d8c2SCharles.Forsyth subop:
348*74a4d8c2SCharles.Forsyth 	  SUB | GSUB
349*74a4d8c2SCharles.Forsyth 	;
350*74a4d8c2SCharles.Forsyth 
351*74a4d8c2SCharles.Forsyth term:
352*74a4d8c2SCharles.Forsyth  	  term '/' ASGNOP term		{ $$ = op2(DIVEQ, $1, $4); }
353*74a4d8c2SCharles.Forsyth  	| term '+' term			{ $$ = op2(ADD, $1, $3); }
354*74a4d8c2SCharles.Forsyth 	| term '-' term			{ $$ = op2(MINUS, $1, $3); }
355*74a4d8c2SCharles.Forsyth 	| term '*' term			{ $$ = op2(MULT, $1, $3); }
356*74a4d8c2SCharles.Forsyth 	| term '/' term			{ $$ = op2(DIVIDE, $1, $3); }
357*74a4d8c2SCharles.Forsyth 	| term '%' term			{ $$ = op2(MOD, $1, $3); }
358*74a4d8c2SCharles.Forsyth 	| term POWER term		{ $$ = op2(POWER, $1, $3); }
359*74a4d8c2SCharles.Forsyth 	| '-' term %prec UMINUS		{ $$ = op1(UMINUS, $2); }
360*74a4d8c2SCharles.Forsyth 	| '+' term %prec UMINUS		{ $$ = $2; }
361*74a4d8c2SCharles.Forsyth 	| NOT term %prec UMINUS		{ $$ = op1(NOT, notnull($2)); }
362*74a4d8c2SCharles.Forsyth 	| BLTIN '(' ')'			{ $$ = op2(BLTIN, itonp($1), rectonode()); }
363*74a4d8c2SCharles.Forsyth 	| BLTIN '(' patlist ')'		{ $$ = op2(BLTIN, itonp($1), $3); }
364*74a4d8c2SCharles.Forsyth 	| BLTIN				{ $$ = op2(BLTIN, itonp($1), rectonode()); }
365*74a4d8c2SCharles.Forsyth 	| CALL '(' ')'			{ $$ = op2(CALL, celltonode($1,CVAR), NIL); }
366*74a4d8c2SCharles.Forsyth 	| CALL '(' patlist ')'		{ $$ = op2(CALL, celltonode($1,CVAR), $3); }
367*74a4d8c2SCharles.Forsyth 	| CLOSE term			{ $$ = op1(CLOSE, $2); }
368*74a4d8c2SCharles.Forsyth 	| DECR var			{ $$ = op1(PREDECR, $2); }
369*74a4d8c2SCharles.Forsyth 	| INCR var			{ $$ = op1(PREINCR, $2); }
370*74a4d8c2SCharles.Forsyth 	| var DECR			{ $$ = op1(POSTDECR, $1); }
371*74a4d8c2SCharles.Forsyth 	| var INCR			{ $$ = op1(POSTINCR, $1); }
372*74a4d8c2SCharles.Forsyth 	| GETLINE var LT term		{ $$ = op3(GETLINE, $2, itonp($3), $4); }
373*74a4d8c2SCharles.Forsyth 	| GETLINE LT term		{ $$ = op3(GETLINE, NIL, itonp($2), $3); }
374*74a4d8c2SCharles.Forsyth 	| GETLINE var			{ $$ = op3(GETLINE, $2, NIL, NIL); }
375*74a4d8c2SCharles.Forsyth 	| GETLINE			{ $$ = op3(GETLINE, NIL, NIL, NIL); }
376*74a4d8c2SCharles.Forsyth 	| INDEX '(' pattern comma pattern ')'
377*74a4d8c2SCharles.Forsyth 		{ $$ = op2(INDEX, $3, $5); }
378*74a4d8c2SCharles.Forsyth 	| INDEX '(' pattern comma reg_expr ')'
379*74a4d8c2SCharles.Forsyth 		{ SYNTAX("index() doesn't permit regular expressions");
380*74a4d8c2SCharles.Forsyth 		  $$ = op2(INDEX, $3, (Node*)$5); }
381*74a4d8c2SCharles.Forsyth 	| '(' pattern ')'		{ $$ = $2; }
382*74a4d8c2SCharles.Forsyth 	| MATCHFCN '(' pattern comma reg_expr ')'
383*74a4d8c2SCharles.Forsyth 		{ $$ = op3(MATCHFCN, NIL, $3, (Node*)makedfa($5, 1)); }
384*74a4d8c2SCharles.Forsyth 	| MATCHFCN '(' pattern comma pattern ')'
385*74a4d8c2SCharles.Forsyth 		{ if (constnode($5))
386*74a4d8c2SCharles.Forsyth 			$$ = op3(MATCHFCN, NIL, $3, (Node*)makedfa(strnode($5), 1));
387*74a4d8c2SCharles.Forsyth 		  else
388*74a4d8c2SCharles.Forsyth 			$$ = op3(MATCHFCN, (Node *)1, $3, $5); }
389*74a4d8c2SCharles.Forsyth 	| NUMBER			{ $$ = celltonode($1, CCON); }
390*74a4d8c2SCharles.Forsyth 	| SPLIT '(' pattern comma varname comma pattern ')'     /* string */
391*74a4d8c2SCharles.Forsyth 		{ $$ = op4(SPLIT, $3, makearr($5), $7, (Node*)STRING); }
392*74a4d8c2SCharles.Forsyth 	| SPLIT '(' pattern comma varname comma reg_expr ')'    /* const /regexp/ */
393*74a4d8c2SCharles.Forsyth 		{ $$ = op4(SPLIT, $3, makearr($5), (Node*)makedfa($7, 1), (Node *)REGEXPR); }
394*74a4d8c2SCharles.Forsyth 	| SPLIT '(' pattern comma varname ')'
395*74a4d8c2SCharles.Forsyth 		{ $$ = op4(SPLIT, $3, makearr($5), NIL, (Node*)STRING); }  /* default */
396*74a4d8c2SCharles.Forsyth 	| SPRINTF '(' patlist ')'	{ $$ = op1($1, $3); }
397*74a4d8c2SCharles.Forsyth 	| STRING	 		{ $$ = celltonode($1, CCON); }
398*74a4d8c2SCharles.Forsyth 	| subop '(' reg_expr comma pattern ')'
399*74a4d8c2SCharles.Forsyth 		{ $$ = op4($1, NIL, (Node*)makedfa($3, 1), $5, rectonode()); }
400*74a4d8c2SCharles.Forsyth 	| subop '(' pattern comma pattern ')'
401*74a4d8c2SCharles.Forsyth 		{ if (constnode($3))
402*74a4d8c2SCharles.Forsyth 			$$ = op4($1, NIL, (Node*)makedfa(strnode($3), 1), $5, rectonode());
403*74a4d8c2SCharles.Forsyth 		  else
404*74a4d8c2SCharles.Forsyth 			$$ = op4($1, (Node *)1, $3, $5, rectonode()); }
405*74a4d8c2SCharles.Forsyth 	| subop '(' reg_expr comma pattern comma var ')'
406*74a4d8c2SCharles.Forsyth 		{ $$ = op4($1, NIL, (Node*)makedfa($3, 1), $5, $7); }
407*74a4d8c2SCharles.Forsyth 	| subop '(' pattern comma pattern comma var ')'
408*74a4d8c2SCharles.Forsyth 		{ if (constnode($3))
409*74a4d8c2SCharles.Forsyth 			$$ = op4($1, NIL, (Node*)makedfa(strnode($3), 1), $5, $7);
410*74a4d8c2SCharles.Forsyth 		  else
411*74a4d8c2SCharles.Forsyth 			$$ = op4($1, (Node *)1, $3, $5, $7); }
412*74a4d8c2SCharles.Forsyth 	| SUBSTR '(' pattern comma pattern comma pattern ')'
413*74a4d8c2SCharles.Forsyth 		{ $$ = op3(SUBSTR, $3, $5, $7); }
414*74a4d8c2SCharles.Forsyth 	| SUBSTR '(' pattern comma pattern ')'
415*74a4d8c2SCharles.Forsyth 		{ $$ = op3(SUBSTR, $3, $5, NIL); }
416*74a4d8c2SCharles.Forsyth 	| var
417*74a4d8c2SCharles.Forsyth 	;
418*74a4d8c2SCharles.Forsyth 
419*74a4d8c2SCharles.Forsyth var:
420*74a4d8c2SCharles.Forsyth 	  varname
421*74a4d8c2SCharles.Forsyth 	| varname '[' patlist ']'	{ $$ = op2(ARRAY, makearr($1), $3); }
422*74a4d8c2SCharles.Forsyth 	| IVAR				{ $$ = op1(INDIRECT, celltonode($1, CVAR)); }
423*74a4d8c2SCharles.Forsyth 	| INDIRECT term	 		{ $$ = op1(INDIRECT, $2); }
424*74a4d8c2SCharles.Forsyth 	;
425*74a4d8c2SCharles.Forsyth 
426*74a4d8c2SCharles.Forsyth varlist:
427*74a4d8c2SCharles.Forsyth 	  /* nothing */		{ arglist = $$ = 0; }
428*74a4d8c2SCharles.Forsyth 	| VAR			{ arglist = $$ = celltonode($1,CVAR); }
429*74a4d8c2SCharles.Forsyth 	| varlist comma VAR	{
430*74a4d8c2SCharles.Forsyth 			checkdup($1, $3);
431*74a4d8c2SCharles.Forsyth 			arglist = $$ = linkum($1,celltonode($3,CVAR)); }
432*74a4d8c2SCharles.Forsyth 	;
433*74a4d8c2SCharles.Forsyth 
434*74a4d8c2SCharles.Forsyth varname:
435*74a4d8c2SCharles.Forsyth 	  VAR			{ $$ = celltonode($1, CVAR); }
436*74a4d8c2SCharles.Forsyth 	| ARG 			{ $$ = op1(ARG, itonp($1)); }
437*74a4d8c2SCharles.Forsyth 	| VARNF			{ $$ = op1(VARNF, (Node *) $1); }
438*74a4d8c2SCharles.Forsyth 	;
439*74a4d8c2SCharles.Forsyth 
440*74a4d8c2SCharles.Forsyth 
441*74a4d8c2SCharles.Forsyth while:
442*74a4d8c2SCharles.Forsyth 	  WHILE '(' pattern rparen	{ $$ = notnull($3); }
443*74a4d8c2SCharles.Forsyth 	;
444*74a4d8c2SCharles.Forsyth 
445*74a4d8c2SCharles.Forsyth %%
446*74a4d8c2SCharles.Forsyth 
447*74a4d8c2SCharles.Forsyth void setfname(Cell *p)
448*74a4d8c2SCharles.Forsyth {
449*74a4d8c2SCharles.Forsyth 	if (isarr(p))
450*74a4d8c2SCharles.Forsyth 		SYNTAX("%s is an array, not a function", p->nval);
451*74a4d8c2SCharles.Forsyth 	else if (isfcn(p))
452*74a4d8c2SCharles.Forsyth 		SYNTAX("you can't define function %s more than once", p->nval);
453*74a4d8c2SCharles.Forsyth 	curfname = p->nval;
454*74a4d8c2SCharles.Forsyth }
455*74a4d8c2SCharles.Forsyth 
456*74a4d8c2SCharles.Forsyth int constnode(Node *p)
457*74a4d8c2SCharles.Forsyth {
458*74a4d8c2SCharles.Forsyth 	return isvalue(p) && ((Cell *) (p->narg[0]))->csub == CCON;
459*74a4d8c2SCharles.Forsyth }
460*74a4d8c2SCharles.Forsyth 
461*74a4d8c2SCharles.Forsyth char *strnode(Node *p)
462*74a4d8c2SCharles.Forsyth {
463*74a4d8c2SCharles.Forsyth 	return ((Cell *)(p->narg[0]))->sval;
464*74a4d8c2SCharles.Forsyth }
465*74a4d8c2SCharles.Forsyth 
466*74a4d8c2SCharles.Forsyth Node *notnull(Node *n)
467*74a4d8c2SCharles.Forsyth {
468*74a4d8c2SCharles.Forsyth 	switch (n->nobj) {
469*74a4d8c2SCharles.Forsyth 	case LE: case LT: case EQ: case NE: case GT: case GE:
470*74a4d8c2SCharles.Forsyth 	case BOR: case AND: case NOT:
471*74a4d8c2SCharles.Forsyth 		return n;
472*74a4d8c2SCharles.Forsyth 	default:
473*74a4d8c2SCharles.Forsyth 		return op2(NE, n, nullnode);
474*74a4d8c2SCharles.Forsyth 	}
475*74a4d8c2SCharles.Forsyth }
476*74a4d8c2SCharles.Forsyth 
477*74a4d8c2SCharles.Forsyth void checkdup(Node *vl, Cell *cp)	/* check if name already in list */
478*74a4d8c2SCharles.Forsyth {
479*74a4d8c2SCharles.Forsyth 	char *s = cp->nval;
480*74a4d8c2SCharles.Forsyth 	for ( ; vl; vl = vl->nnext) {
481*74a4d8c2SCharles.Forsyth 		if (strcmp(s, ((Cell *)(vl->narg[0]))->nval) == 0) {
482*74a4d8c2SCharles.Forsyth 			SYNTAX("duplicate argument %s", s);
483*74a4d8c2SCharles.Forsyth 			break;
484*74a4d8c2SCharles.Forsyth 		}
485*74a4d8c2SCharles.Forsyth 	}
486*74a4d8c2SCharles.Forsyth }
487