summaryrefslogtreecommitdiff
path: root/usr/src/cmd/oawk/awk.g.y
blob: e4660aa36ec23a3b55b8ed98f1a000b11cd4500e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
%{
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
%}
/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved  	*/



%{
#ident	"%Z%%M%	%I%	%E% SMI"
%}
%token	FIRSTTOKEN	/* must be first */
%token	FINAL FATAL
%token	LT LE GT GE EQ NE
%token	MATCH NOTMATCH
%token	APPEND
%token	ADD MINUS MULT DIVIDE MOD UMINUS
%token	ASSIGN ADDEQ SUBEQ MULTEQ DIVEQ MODEQ
%token	JUMP
%token	XBEGIN XEND
%token	NL
%token	PRINT PRINTF SPRINTF SPLIT
%token	IF ELSE WHILE FOR IN NEXT EXIT BREAK CONTINUE
%token	PROGRAM PASTAT PASTAT2


%right	ASGNOP
%left	BOR
%left	AND
%left	NOT
%left	NUMBER VAR ARRAY FNCN SUBSTR LSUBSTR INDEX
%left	GETLINE
%nonassoc RELOP MATCHOP
%left	OR
%left	STRING  DOT CCL NCCL CHAR
%left	'(' '^' '$'
%left	CAT
%left	'+' '-'
%left	'*' '/' '%'
%left	STAR PLUS QUEST
%left	POSTINCR PREINCR POSTDECR PREDECR INCR DECR
%left	FIELD INDIRECT
%token	JUMPTRUE JUMPFALSE PUSH GETREC
%token	NEWSTAT
%token	IN_INIT IN_EXIT
%token	LASTTOKEN	/* has to be last */


%{
#include "awk.def"
#ifndef	DEBUG
#	define	PUTS(x)
#endif
static wchar_t L_record[] = L"$record";
static wchar_t L_zeronull[] = L"$zero&null";
%}
%%


program:
		begin pa_stats end	{
			if (errorflag==0)
				winner = (NODE *)stat3(PROGRAM, $1, $2, $3); }
	| error			{ yyclearin; yyerror("bailing out"); }
	;


begin:
		XBEGIN '{' stat_list '}'	{ $$ = $3; }
	| begin NL
	| 	{ $$ = (int) 0; }
	;


end:
		XEND '{' stat_list '}'	{ $$ = $3; }
	| end NL
	|	{ $$ = (int) 0; }
	;


compound_conditional:
		conditional BOR conditional	{ $$ = op2(BOR, $1, $3); }
	| conditional AND conditional	{ $$ = op2(AND, $1, $3); }
	| NOT conditional		{ $$ = op1(NOT, $2); }
	| '(' compound_conditional ')'	{ $$ = $2; }
	;


compound_pattern:
		pattern BOR pattern	{ $$ = op2(BOR, $1, $3); }
	| pattern AND pattern	{ $$ = op2(AND, $1, $3); }
	| NOT pattern		{ $$ = op1(NOT, $2); }
	| '(' compound_pattern ')'	{ $$ = $2; }
	;


conditional:
		expr  {
		$$ = op2(NE, $1,
			valtonode(lookup(L_zeronull, symtab, 0), CCON));
		}
	| rel_expr
	| lex_expr
	| compound_conditional
	;


else:
		ELSE optNL
	;


field:
		FIELD		{ $$ = valtonode($1, CFLD); }
	| INDIRECT term { $$ = op1(INDIRECT, $2); }
	;


if:
		IF '(' conditional ')' optNL	{ $$ = $3; }
	;


lex_expr:
		expr MATCHOP regular_expr	{
			$$ = op2($2, $1, makedfa($3)); }
	| '(' lex_expr ')'		{ $$ = $2; }
	;


var:
		NUMBER	{ $$ = valtonode($1, CCON); }
	| STRING 	{ $$ = valtonode($1, CCON); }
	| VAR		{ $$ = valtonode($1, CVAR); }
	| VAR '[' expr ']'	{ $$ = op2(ARRAY, $1, $3); }
	| field
	;
term:
		var
	| GETLINE	{ $$ = op1(GETLINE, 0); }
	| FNCN		{
		$$ = op2(FNCN, $1,
			valtonode(lookup(L_record, symtab, 0), CFLD));
			}
	| FNCN '(' ')'	{
				$$ = op2(FNCN, $1,
				valtonode(lookup(L_record, symtab, 0), CFLD));
			}
	| FNCN '(' expr ')'	{ $$ = op2(FNCN, $1, $3); }
	| SPRINTF print_list	{ $$ = op1($1, $2); }
	| SUBSTR '(' expr ',' expr ',' expr ')'
			{ $$ = op3(SUBSTR, $3, $5, $7); }
	| SUBSTR '(' expr ',' expr ')'
			{ $$ = op3(SUBSTR, $3, $5, 0); }
	| SPLIT '(' expr ',' VAR ',' expr ')'
			{ $$ = op3(SPLIT, $3, $5, $7); }
	| SPLIT '(' expr ',' VAR ')'
			{ $$ = op3(SPLIT, $3, $5, 0); }
	| INDEX '(' expr ',' expr ')'
			{ $$ = op2(INDEX, $3, $5); }
	| '(' expr ')'			{$$ = $2; }
	| term '+' term			{ $$ = op2(ADD, $1, $3); }
	| term '-' term			{ $$ = op2(MINUS, $1, $3); }
	| term '*' term			{ $$ = op2(MULT, $1, $3); }
	| term '/' term			{ $$ = op2(DIVIDE, $1, $3); }
	| term '%' term			{ $$ = op2(MOD, $1, $3); }
	| '-' term %prec QUEST		{ $$ = op1(UMINUS, $2); }
	| '+' term %prec QUEST		{ $$ = $2; }
	| INCR var	{ $$ = op1(PREINCR, $2); }
	| DECR var	{ $$ = op1(PREDECR, $2); }
	| var INCR	{ $$= op1(POSTINCR, $1); }
	| var DECR	{ $$= op1(POSTDECR, $1); }
	;


expr:
		term
	| expr term	{ $$ = op2(CAT, $1, $2); }
	| var ASGNOP expr	{ $$ = op2($2, $1, $3); }
	;


optNL:
		NL
	|
	;


pa_stat:
		pattern	{ $$ = stat2(PASTAT, $1, genprint()); }
	| pattern '{' stat_list '}'	{ $$ = stat2(PASTAT, $1, $3); }
	| pattern ',' pattern		{ $$ = pa2stat($1, $3, genprint()); }
	| pattern ',' pattern '{' stat_list '}'
					{ $$ = pa2stat($1, $3, $5); }
	| '{' stat_list '}'	{ $$ = stat2(PASTAT, 0, $2); }
	;


pa_stats:
		pa_stats pa_stat st	{ $$ = linkum($1, $2); }
	|	{ $$ = (int)0; }
	| pa_stats pa_stat	{ $$ = linkum($1, $2); }
	;


pattern:
		regular_expr	{
		$$ = op2(MATCH,
		valtonode(lookup(L_record, symtab, 0), CFLD), makedfa($1));
		}
	| rel_expr
	| lex_expr
	| compound_pattern
	;


print_list:
	expr
	| pe_list
	|	{
			$$ = valtonode(lookup(L_record, symtab, 0), CFLD);
			}
	;


pe_list:
		expr ',' expr	{$$ = linkum($1, $3); }
	| pe_list ',' expr	{$$ = linkum($1, $3); }
	| '(' pe_list ')'		{$$ = $2; }
	;


redir:
		RELOP
	| '|'
	;


regular_expr:
		'/'	{ startreg(); }
		r '/'
		{ $$ = $3; }
	;


r:
		CHAR		{ $$ = op2(CHAR, (NODE *) 0, $1); }
	| DOT		{ $$ = op2(DOT, (NODE *) 0, (NODE *) 0); }
	| CCL		{ $$ = op2(CCL, (NODE *) 0, cclenter($1)); }
	| NCCL		{ $$ = op2(NCCL, (NODE *) 0, cclenter($1)); }
	| '^'		{ $$ = op2(CHAR, (NODE *) 0, HAT); }
	| '$'		{ $$ = op2(CHAR, (NODE *) 0, (NODE *) 0); }
	| r OR r	{ $$ = op2(OR, $1, $3); }
	| r r   %prec CAT
			{ $$ = op2(CAT, $1, $2); }
	| r STAR	{ $$ = op2(STAR, $1, (NODE *) 0); }
	| r PLUS	{ $$ = op2(PLUS, $1, (NODE *) 0); }
	| r QUEST	{ $$ = op2(QUEST, $1, (NODE *) 0); }
	| '(' r ')'	{ $$ = $2; }
	;


rel_expr:
		expr RELOP expr
		{ $$ = op2($2, $1, $3); }
	| '(' rel_expr ')'
		{ $$ = $2; }
	;


st:
		NL
	| ';'
	;


simple_stat:
		PRINT print_list redir expr
		{ $$ = stat3($1, $2, $3, $4); }
	| PRINT print_list
		{ $$ = stat3($1, $2, 0, 0); }
	| PRINTF print_list redir expr
		{ $$ = stat3($1, $2, $3, $4); }
	| PRINTF print_list
		{ $$ = stat3($1, $2, 0, 0); }
	| expr	{ $$ = exptostat($1); }
	|		{ $$ = (int)0; }
	| error		{ yyclearin; yyerror("illegal statement"); $$ = (int)0; }
	;


statement:
		simple_stat st
	| if statement		{ $$ = stat3(IF, $1, $2, 0); }
	| if statement else statement
		{ $$ = stat3(IF, $1, $2, $4); }
	| while statement	{ $$ = stat2(WHILE, $1, $2); }
	| for
	| NEXT st		{ $$ = stat1(NEXT, 0); }
	| EXIT st		{ $$ = stat1(EXIT, 0); }
	| EXIT expr st		{ $$ = stat1(EXIT, $2); }
	| BREAK st		{ $$ = stat1(BREAK, 0); }
	| CONTINUE st		{ $$ = stat1(CONTINUE, 0); }
	| '{' stat_list '}'	{ $$ = $2; }
	;


stat_list:
		stat_list statement	{ $$ = linkum($1, $2); }
	|			{ $$ = (int)0; }
	;


while:
		WHILE '(' conditional ')' optNL	{ $$ = $3; }
	;


for:
	FOR '(' simple_stat ';' conditional ';' simple_stat ')' optNL statement
		{ $$ = stat4(FOR, $3, $5, $7, $10); }
	| FOR '(' simple_stat ';'  ';' simple_stat ')' optNL statement
		{ $$ = stat4(FOR, $3, 0, $6, $9); }
	| FOR '(' VAR IN VAR ')' optNL statement
		{ $$ = stat3(IN, $3, $5, $8); }
	;


%%