xref: /titanic_44/usr/src/cmd/oawk/awk.g.y (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
1 %{
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License, Version 1.0 only
7  * (the "License").  You may not use this file except in compliance
8  * with the License.
9  *
10  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11  * or http://www.opensolaris.org/os/licensing.
12  * See the License for the specific language governing permissions
13  * and limitations under the License.
14  *
15  * When distributing Covered Code, include this CDDL HEADER in each
16  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17  * If applicable, add the following below this CDDL HEADER, with the
18  * fields enclosed by brackets "[]" replaced with your own identifying
19  * information: Portions Copyright [yyyy] [name of copyright owner]
20  *
21  * CDDL HEADER END
22  */
23 %}
24 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
25 /*	  All Rights Reserved  	*/
26 
27 
28 
29 %{
30 #ident	"%Z%%M%	%I%	%E% SMI"
31 %}
32 %token	FIRSTTOKEN	/* must be first */
33 %token	FINAL FATAL
34 %token	LT LE GT GE EQ NE
35 %token	MATCH NOTMATCH
36 %token	APPEND
37 %token	ADD MINUS MULT DIVIDE MOD UMINUS
38 %token	ASSIGN ADDEQ SUBEQ MULTEQ DIVEQ MODEQ
39 %token	JUMP
40 %token	XBEGIN XEND
41 %token	NL
42 %token	PRINT PRINTF SPRINTF SPLIT
43 %token	IF ELSE WHILE FOR IN NEXT EXIT BREAK CONTINUE
44 %token	PROGRAM PASTAT PASTAT2
45 
46 
47 %right	ASGNOP
48 %left	BOR
49 %left	AND
50 %left	NOT
51 %left	NUMBER VAR ARRAY FNCN SUBSTR LSUBSTR INDEX
52 %left	GETLINE
53 %nonassoc RELOP MATCHOP
54 %left	OR
55 %left	STRING  DOT CCL NCCL CHAR
56 %left	'(' '^' '$'
57 %left	CAT
58 %left	'+' '-'
59 %left	'*' '/' '%'
60 %left	STAR PLUS QUEST
61 %left	POSTINCR PREINCR POSTDECR PREDECR INCR DECR
62 %left	FIELD INDIRECT
63 %token	JUMPTRUE JUMPFALSE PUSH GETREC
64 %token	NEWSTAT
65 %token	IN_INIT IN_EXIT
66 %token	LASTTOKEN	/* has to be last */
67 
68 
69 %{
70 #include "awk.def"
71 #ifndef	DEBUG
72 #	define	PUTS(x)
73 #endif
74 static wchar_t L_record[] = L"$record";
75 static wchar_t L_zeronull[] = L"$zero&null";
76 %}
77 %%
78 
79 
80 program:
81 		begin pa_stats end	{
82 			if (errorflag==0)
83 				winner = (NODE *)stat3(PROGRAM, $1, $2, $3); }
84 	| error			{ yyclearin; yyerror("bailing out"); }
85 	;
86 
87 
88 begin:
89 		XBEGIN '{' stat_list '}'	{ $$ = $3; }
90 	| begin NL
91 	| 	{ $$ = (int) 0; }
92 	;
93 
94 
95 end:
96 		XEND '{' stat_list '}'	{ $$ = $3; }
97 	| end NL
98 	|	{ $$ = (int) 0; }
99 	;
100 
101 
102 compound_conditional:
103 		conditional BOR conditional	{ $$ = op2(BOR, $1, $3); }
104 	| conditional AND conditional	{ $$ = op2(AND, $1, $3); }
105 	| NOT conditional		{ $$ = op1(NOT, $2); }
106 	| '(' compound_conditional ')'	{ $$ = $2; }
107 	;
108 
109 
110 compound_pattern:
111 		pattern BOR pattern	{ $$ = op2(BOR, $1, $3); }
112 	| pattern AND pattern	{ $$ = op2(AND, $1, $3); }
113 	| NOT pattern		{ $$ = op1(NOT, $2); }
114 	| '(' compound_pattern ')'	{ $$ = $2; }
115 	;
116 
117 
118 conditional:
119 		expr  {
120 		$$ = op2(NE, $1,
121 			valtonode(lookup(L_zeronull, symtab, 0), CCON));
122 		}
123 	| rel_expr
124 	| lex_expr
125 	| compound_conditional
126 	;
127 
128 
129 else:
130 		ELSE optNL
131 	;
132 
133 
134 field:
135 		FIELD		{ $$ = valtonode($1, CFLD); }
136 	| INDIRECT term { $$ = op1(INDIRECT, $2); }
137 	;
138 
139 
140 if:
141 		IF '(' conditional ')' optNL	{ $$ = $3; }
142 	;
143 
144 
145 lex_expr:
146 		expr MATCHOP regular_expr	{
147 			$$ = op2($2, $1, makedfa($3)); }
148 	| '(' lex_expr ')'		{ $$ = $2; }
149 	;
150 
151 
152 var:
153 		NUMBER	{ $$ = valtonode($1, CCON); }
154 	| STRING 	{ $$ = valtonode($1, CCON); }
155 	| VAR		{ $$ = valtonode($1, CVAR); }
156 	| VAR '[' expr ']'	{ $$ = op2(ARRAY, $1, $3); }
157 	| field
158 	;
159 term:
160 		var
161 	| GETLINE	{ $$ = op1(GETLINE, 0); }
162 	| FNCN		{
163 		$$ = op2(FNCN, $1,
164 			valtonode(lookup(L_record, symtab, 0), CFLD));
165 			}
166 	| FNCN '(' ')'	{
167 				$$ = op2(FNCN, $1,
168 				valtonode(lookup(L_record, symtab, 0), CFLD));
169 			}
170 	| FNCN '(' expr ')'	{ $$ = op2(FNCN, $1, $3); }
171 	| SPRINTF print_list	{ $$ = op1($1, $2); }
172 	| SUBSTR '(' expr ',' expr ',' expr ')'
173 			{ $$ = op3(SUBSTR, $3, $5, $7); }
174 	| SUBSTR '(' expr ',' expr ')'
175 			{ $$ = op3(SUBSTR, $3, $5, 0); }
176 	| SPLIT '(' expr ',' VAR ',' expr ')'
177 			{ $$ = op3(SPLIT, $3, $5, $7); }
178 	| SPLIT '(' expr ',' VAR ')'
179 			{ $$ = op3(SPLIT, $3, $5, 0); }
180 	| INDEX '(' expr ',' expr ')'
181 			{ $$ = op2(INDEX, $3, $5); }
182 	| '(' expr ')'			{$$ = $2; }
183 	| term '+' term			{ $$ = op2(ADD, $1, $3); }
184 	| term '-' term			{ $$ = op2(MINUS, $1, $3); }
185 	| term '*' term			{ $$ = op2(MULT, $1, $3); }
186 	| term '/' term			{ $$ = op2(DIVIDE, $1, $3); }
187 	| term '%' term			{ $$ = op2(MOD, $1, $3); }
188 	| '-' term %prec QUEST		{ $$ = op1(UMINUS, $2); }
189 	| '+' term %prec QUEST		{ $$ = $2; }
190 	| INCR var	{ $$ = op1(PREINCR, $2); }
191 	| DECR var	{ $$ = op1(PREDECR, $2); }
192 	| var INCR	{ $$= op1(POSTINCR, $1); }
193 	| var DECR	{ $$= op1(POSTDECR, $1); }
194 	;
195 
196 
197 expr:
198 		term
199 	| expr term	{ $$ = op2(CAT, $1, $2); }
200 	| var ASGNOP expr	{ $$ = op2($2, $1, $3); }
201 	;
202 
203 
204 optNL:
205 		NL
206 	|
207 	;
208 
209 
210 pa_stat:
211 		pattern	{ $$ = stat2(PASTAT, $1, genprint()); }
212 	| pattern '{' stat_list '}'	{ $$ = stat2(PASTAT, $1, $3); }
213 	| pattern ',' pattern		{ $$ = pa2stat($1, $3, genprint()); }
214 	| pattern ',' pattern '{' stat_list '}'
215 					{ $$ = pa2stat($1, $3, $5); }
216 	| '{' stat_list '}'	{ $$ = stat2(PASTAT, 0, $2); }
217 	;
218 
219 
220 pa_stats:
221 		pa_stats pa_stat st	{ $$ = linkum($1, $2); }
222 	|	{ $$ = (int)0; }
223 	| pa_stats pa_stat	{ $$ = linkum($1, $2); }
224 	;
225 
226 
227 pattern:
228 		regular_expr	{
229 		$$ = op2(MATCH,
230 		valtonode(lookup(L_record, symtab, 0), CFLD), makedfa($1));
231 		}
232 	| rel_expr
233 	| lex_expr
234 	| compound_pattern
235 	;
236 
237 
238 print_list:
239 	expr
240 	| pe_list
241 	|	{
242 			$$ = valtonode(lookup(L_record, symtab, 0), CFLD);
243 			}
244 	;
245 
246 
247 pe_list:
248 		expr ',' expr	{$$ = linkum($1, $3); }
249 	| pe_list ',' expr	{$$ = linkum($1, $3); }
250 	| '(' pe_list ')'		{$$ = $2; }
251 	;
252 
253 
254 redir:
255 		RELOP
256 	| '|'
257 	;
258 
259 
260 regular_expr:
261 		'/'	{ startreg(); }
262 		r '/'
263 		{ $$ = $3; }
264 	;
265 
266 
267 r:
268 		CHAR		{ $$ = op2(CHAR, (NODE *) 0, $1); }
269 	| DOT		{ $$ = op2(DOT, (NODE *) 0, (NODE *) 0); }
270 	| CCL		{ $$ = op2(CCL, (NODE *) 0, cclenter($1)); }
271 	| NCCL		{ $$ = op2(NCCL, (NODE *) 0, cclenter($1)); }
272 	| '^'		{ $$ = op2(CHAR, (NODE *) 0, HAT); }
273 	| '$'		{ $$ = op2(CHAR, (NODE *) 0, (NODE *) 0); }
274 	| r OR r	{ $$ = op2(OR, $1, $3); }
275 	| r r   %prec CAT
276 			{ $$ = op2(CAT, $1, $2); }
277 	| r STAR	{ $$ = op2(STAR, $1, (NODE *) 0); }
278 	| r PLUS	{ $$ = op2(PLUS, $1, (NODE *) 0); }
279 	| r QUEST	{ $$ = op2(QUEST, $1, (NODE *) 0); }
280 	| '(' r ')'	{ $$ = $2; }
281 	;
282 
283 
284 rel_expr:
285 		expr RELOP expr
286 		{ $$ = op2($2, $1, $3); }
287 	| '(' rel_expr ')'
288 		{ $$ = $2; }
289 	;
290 
291 
292 st:
293 		NL
294 	| ';'
295 	;
296 
297 
298 simple_stat:
299 		PRINT print_list redir expr
300 		{ $$ = stat3($1, $2, $3, $4); }
301 	| PRINT print_list
302 		{ $$ = stat3($1, $2, 0, 0); }
303 	| PRINTF print_list redir expr
304 		{ $$ = stat3($1, $2, $3, $4); }
305 	| PRINTF print_list
306 		{ $$ = stat3($1, $2, 0, 0); }
307 	| expr	{ $$ = exptostat($1); }
308 	|		{ $$ = (int)0; }
309 	| error		{ yyclearin; yyerror("illegal statement"); $$ = (int)0; }
310 	;
311 
312 
313 statement:
314 		simple_stat st
315 	| if statement		{ $$ = stat3(IF, $1, $2, 0); }
316 	| if statement else statement
317 		{ $$ = stat3(IF, $1, $2, $4); }
318 	| while statement	{ $$ = stat2(WHILE, $1, $2); }
319 	| for
320 	| NEXT st		{ $$ = stat1(NEXT, 0); }
321 	| EXIT st		{ $$ = stat1(EXIT, 0); }
322 	| EXIT expr st		{ $$ = stat1(EXIT, $2); }
323 	| BREAK st		{ $$ = stat1(BREAK, 0); }
324 	| CONTINUE st		{ $$ = stat1(CONTINUE, 0); }
325 	| '{' stat_list '}'	{ $$ = $2; }
326 	;
327 
328 
329 stat_list:
330 		stat_list statement	{ $$ = linkum($1, $2); }
331 	|			{ $$ = (int)0; }
332 	;
333 
334 
335 while:
336 		WHILE '(' conditional ')' optNL	{ $$ = $3; }
337 	;
338 
339 
340 for:
341 	FOR '(' simple_stat ';' conditional ';' simple_stat ')' optNL statement
342 		{ $$ = stat4(FOR, $3, $5, $7, $10); }
343 	| FOR '(' simple_stat ';'  ';' simple_stat ')' optNL statement
344 		{ $$ = stat4(FOR, $3, 0, $6, $9); }
345 	| FOR '(' VAR IN VAR ')' optNL statement
346 		{ $$ = stat3(IN, $3, $5, $8); }
347 	;
348 
349 
350 %%
351