1 /*-
2  * This code contains changes by
3  *      Gunnar Ritter, Freiburg i. Br., Germany, 2002. All rights reserved.
4  *
5  * Conditions 1, 2, and 4 and the no-warranty notice below apply
6  * to these changes.
7  *
8  *
9  * Copyright (c) 1991
10  * 	The Regents of the University of California.  All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  * 	This product includes software developed by the University of
23  * 	California, Berkeley and its contributors.
24  * 4. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  *
40  *
41  * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  *   Redistributions of source code and documentation must retain the
47  *    above copyright notice, this list of conditions and the following
48  *    disclaimer.
49  *   Redistributions in binary form must reproduce the above copyright
50  *    notice, this list of conditions and the following disclaimer in the
51  *    documentation and/or other materials provided with the distribution.
52  *   All advertising materials mentioning features or use of this software
53  *    must display the following acknowledgement:
54  *      This product includes software developed or owned by Caldera
55  *      International, Inc.
56  *   Neither the name of Caldera International, Inc. nor the names of
57  *    other contributors may be used to endorse or promote products
58  *    derived from this software without specific prior written permission.
59  *
60  * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
61  * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
62  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
63  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
64  * ARE DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE
65  * LIABLE FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR
66  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
67  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
68  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
69  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
70  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
71  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
72  */
73 
74 %{
75 #include <inttypes.h>
76 typedef	void	*YYSTYPE;
77 #define	YYSTYPE	YYSTYPE
78 %}
79 
80 %token	FIRSTTOKEN	/*must be first*/
81 %token	FINAL FATAL
82 %token	LT LE GT GE EQ NE
83 %token	MATCH NOTMATCH
84 %token	APPEND
85 %token	ADD MINUS MULT DIVIDE MOD UMINUS
86 %token	ASSIGN ADDEQ SUBEQ MULTEQ DIVEQ MODEQ
87 %token	JUMP
88 %token	XBEGIN XEND
89 %token	NL
90 %token	PRINT PRINTF SPRINTF SPLIT
91 %token	IF ELSE WHILE FOR IN NEXT EXIT BREAK CONTINUE
92 %token	PROGRAM PASTAT PASTAT2
93 %token	REGEXPR
94 
95 %right	ASGNOP
96 %left	BOR
97 %left	AND
98 %left	NOT
99 %left	NUMBER VAR ARRAY FNCN SUBSTR LSUBSTR INDEX
100 %left	GETLINE
101 %nonassoc RELOP MATCHOP
102 %left	OR
103 %left	STRING  DOT CCL NCCL CHAR
104 %left	'(' '^' '$'
105 %left	REGEXPR
106 %left	CAT
107 %left	'+' '-'
108 %left	'*' '/' '%'
109 %left	STAR PLUS QUEST
110 %left	POSTINCR PREINCR POSTDECR PREDECR INCR DECR
111 %left	FIELD INDIRECT
112 %token	LASTTOKEN	/* has to be last */
113 
114 %{
115 /*	from 4.4BSD /usr/src/old/awk/awk.g.y	4.4 (Berkeley) 4/27/91	*/
116 /*	Sccsid @(#)awk.g.y	1.5 (gritter) 7/24/03>	*/
117 
118 #include "awk.def"
119 #ifndef	DEBUG
120 #	define	PUTS(x)
121 #endif
122 %}
123 %%
124 
125 program:
126 	  begin pa_stats end	{ if (errorflag==0) winner = (node *)stat3(PROGRAM, $1, $2, $3); }
127 	| error			{ yyclearin; yyerror("bailing out"); }
128 	;
129 
130 begin:
131 	  XBEGIN '{' stat_list '}'	{ PUTS("XBEGIN list"); $$ = $3; }
132 	| begin NL
133 	| 	{ PUTS("empty XBEGIN"); $$ = nullstat; }
134 	;
135 
136 end:
137 	  XEND '{' stat_list '}'	{ PUTS("XEND list"); $$ = $3; }
138 	| end NL
139 	|	{ PUTS("empty END"); $$ = nullstat; }
140 	;
141 
142 compound_conditional:
143 	  conditional BOR conditional	{ PUTS("cond||cond"); $$ = op2(BOR, $1, $3); }
144 	| conditional AND conditional	{ PUTS("cond&&cond"); $$ = op2(AND, $1, $3); }
145 	| NOT conditional		{ PUTS("!cond"); $$ = op1(NOT, $2); }
146 	| '(' compound_conditional ')'	{ $$ = $2; }
147 	;
148 
149 compound_pattern:
150 	  pattern BOR pattern	{ PUTS("pat||pat"); $$ = op2(BOR, $1, $3); }
151 	| pattern AND pattern	{ PUTS("pat&&pat"); $$ = op2(AND, $1, $3); }
152 	| NOT pattern		{ PUTS("!pat"); $$ = op1(NOT, $2); }
153 	| '(' compound_pattern ')'	{ $$ = $2; }
154 	;
155 
156 conditional:
157 	  expr	{ PUTS("expr"); $$ = op2(NE, $1, valtonode(lookup("$zero&null", symtab, 0), CCON)); }
158 	| rel_expr		{ PUTS("relexpr"); }
159 	| lex_expr		{ PUTS("lexexpr"); }
160 	| compound_conditional	{ PUTS("compcond"); }
161 	;
162 
163 else:
164 	  ELSE optNL	{ PUTS("else"); }
165 	;
166 
167 field:
168 	  FIELD		{ PUTS("field"); $$ = valtonode($1, CFLD); }
169 	| INDIRECT term { PUTS("ind field"); $$ = op1(INDIRECT, $2); }
170 	;
171 
172 if:
173 	  IF '(' conditional ')' optNL	{ PUTS("if(cond)"); $$ = $3; }
174 	;
175 
176 lex_expr:
177 	  expr MATCHOP regular_expr	{ PUTS("expr~re"); $$ = op2((intptr_t)$2, $1, (void *)makedfa($3)); }
178 	| '(' lex_expr ')'	{ PUTS("(lex_expr)"); $$ = $2; }
179 	;
180 
181 var:
182 	  NUMBER	{PUTS("number"); $$ = valtonode($1, CCON); }
183 	| STRING 	{ PUTS("string"); $$ = valtonode($1, CCON); }
184 	| VAR		{ PUTS("var"); $$ = valtonode($1, CVAR); }
185 	| VAR '[' expr ']'	{ PUTS("array[]"); $$ = op2(ARRAY, $1, $3); }
186 	| field
187 	;
188 term:
189 	  var
190 	| GETLINE	{ PUTS("getline"); $$ = op1(GETLINE, 0); }
191 	| FNCN		{ PUTS("func");
192 			$$ = op2(FNCN, $1, valtonode(lookup("$record", symtab, 0), CFLD));
193 			}
194 	| FNCN '(' ')'	{ PUTS("func()");
195 			$$ = op2(FNCN, $1, valtonode(lookup("$record", symtab, 0), CFLD));
196 			}
197 	| FNCN '(' expr ')'	{ PUTS("func(expr)"); $$ = op2(FNCN, $1, $3); }
198 	| SPRINTF print_list	{ PUTS("sprintf"); $$ = op1((intptr_t)$1, $2); }
199 	| SUBSTR '(' expr ',' expr ',' expr ')'
200 			{ PUTS("substr(e,e,e)"); $$ = op3(SUBSTR, $3, $5, $7); }
201 	| SUBSTR '(' expr ',' expr ')'
202 			{ PUTS("substr(e,e,e)"); $$ = op3(SUBSTR, $3, $5, nullstat); }
203 	| SPLIT '(' expr ',' VAR ',' expr ')'
204 			{ PUTS("split(e,e,e)"); $$ = op3(SPLIT, $3, $5, $7); }
205 	| SPLIT '(' expr ',' VAR ')'
206 			{ PUTS("split(e,e,e)"); $$ = op3(SPLIT, $3, $5, nullstat); }
207 	| INDEX '(' expr ',' expr ')'
208 			{ PUTS("index(e,e)"); $$ = op2(INDEX, $3, $5); }
209 	| '(' expr ')'			{PUTS("(expr)");  $$ = $2; }
210 	| term '+' term			{ PUTS("t+t"); $$ = op2(ADD, $1, $3); }
211 	| term '-' term			{ PUTS("t-t"); $$ = op2(MINUS, $1, $3); }
212 	| term '*' term			{ PUTS("t*t"); $$ = op2(MULT, $1, $3); }
213 	| term '/' term			{ PUTS("t/t"); $$ = op2(DIVIDE, $1, $3); }
214 	| term '%' term			{ PUTS("t%t"); $$ = op2(MOD, $1, $3); }
215 	| '-' term %prec QUEST		{ PUTS("-term"); $$ = op1(UMINUS, $2); }
216 	| '+' term %prec QUEST		{ PUTS("+term"); $$ = $2; }
217 	| INCR var	{ PUTS("++var"); $$ = op1(PREINCR, $2); }
218 	| DECR var	{ PUTS("--var"); $$ = op1(PREDECR, $2); }
219 	| var INCR	{ PUTS("var++"); $$= op1(POSTINCR, $1); }
220 	| var DECR	{ PUTS("var--"); $$= op1(POSTDECR, $1); }
221 	;
222 
223 expr:
224 	  term		{ PUTS("term"); }
225 	| expr term	{ PUTS("expr term"); $$ = op2(CAT, $1, $2); }
226 	| var ASGNOP expr	{ PUTS("var=expr"); $$ = stat2((intptr_t)$2, $1, $3); }
227 	;
228 
229 optNL:
230 	  NL
231 	|
232 	;
233 
234 pa_stat:
235 	  pattern	{ PUTS("pattern"); $$ = stat2(PASTAT, $1, genprint()); }
236 	| pattern '{' stat_list '}'	{ PUTS("pattern {...}"); $$ = stat2(PASTAT, $1, $3); }
237 	| pattern ',' pattern		{ PUTS("srch,srch"); $$ = pa2stat($1, $3, genprint()); }
238 	| pattern ',' pattern '{' stat_list '}'
239 					{ PUTS("srch, srch {...}"); $$ = pa2stat($1, $3, $5); }
240 	| '{' stat_list '}'	{ PUTS("null pattern {...}"); $$ = stat2(PASTAT, nullstat, $2); }
241 	;
242 
243 pa_stats:
244 	  pa_stats pa_stat st	{ PUTS("pa_stats pa_stat"); $$ = linkum($1, $2); }
245 	|	{ PUTS("null pa_stat"); $$ = nullstat; }
246 	| pa_stats pa_stat	{PUTS("pa_stats pa_stat"); $$ = linkum($1, $2); }
247 	;
248 
249 pattern:
250 	  regular_expr	{ PUTS("regex");
251 		$$ = op2(MATCH, valtonode(lookup("$record", symtab, 0), CFLD), (void *)makedfa($1));
252 		}
253 	| rel_expr	{ PUTS("relexpr"); }
254 	| lex_expr	{ PUTS("lexexpr"); }
255 	| compound_pattern	{ PUTS("comp pat"); }
256 	;
257 
258 print_list:
259 	  expr	{ PUTS("expr"); }
260 	| pe_list	{ PUTS("pe_list"); }
261 	|		{ PUTS("null print_list"); $$ = valtonode(lookup("$record", symtab, 0), CFLD); }
262 	;
263 
264 pe_list:
265 	  expr ',' expr	{$$ = linkum($1, $3); }
266 	| pe_list ',' expr	{$$ = linkum($1, $3); }
267 	| '(' pe_list ')'		{$$ = $2; }
268 	;
269 
270 redir:
271 	  RELOP
272 	| '|'
273 	;
274 
275 regular_expr:
276 	  '/'	{ startreg(); }
277 	  REGEXPR '/'
278 		{ PUTS("/r/"); $$ = $3; }
279 	;
280 
281 rel_expr:
282 	  expr RELOP expr
283 		{ PUTS("expr relop expr"); $$ = op2((intptr_t)$2, $1, $3); }
284 	| '(' rel_expr ')'
285 		{ PUTS("(relexpr)"); $$ = $2; }
286 	;
287 
288 st:
289 	  NL
290 	| ';'
291 	;
292 
293 simple_stat:
294 	  PRINT print_list redir expr
295 		{ PUTS("print>stat"); $$ = stat3((intptr_t)$1, $2, $3, $4); }
296 	| PRINT print_list
297 		{ PUTS("print list"); $$ = stat3((intptr_t)$1, $2, nullstat, nullstat); }
298 	| PRINTF print_list redir expr
299 		{ PUTS("printf>stat"); $$ = stat3((intptr_t)$1, $2, $3, $4); }
300 	| PRINTF print_list
301 		{ PUTS("printf list"); $$ = stat3((intptr_t)$1, $2, nullstat, nullstat); }
302 	| expr	{ PUTS("expr"); $$ = exptostat($1); }
303 	|		{ PUTS("null simple statement"); $$ = nullstat; }
304 	| error		{ yyclearin; yyerror("illegal statement"); $$ = nullstat; }
305 	;
306 
307 statement:
308 	  simple_stat st	{ PUTS("simple stat"); }
309 	| if statement		{ PUTS("if stat"); $$ = stat3(IF, $1, $2, nullstat); }
310 	| if statement else statement
311 		{ PUTS("if-else stat"); $$ = stat3(IF, $1, $2, $4); }
312 	| while statement	{ PUTS("while stat"); $$ = stat2(WHILE, $1, $2); }
313 	| for			{ PUTS("for stat"); }
314 	| NEXT st		{ PUTS("next"); $$ = stat1(NEXT, 0); }
315 	| EXIT st		{ PUTS("exit"); $$ = stat1(EXIT, 0); }
316 	| EXIT expr st		{ PUTS("exit"); $$ = stat1(EXIT, $2); }
317 	| BREAK st		{ PUTS("break"); $$ = stat1(BREAK, 0); }
318 	| CONTINUE st		{ PUTS("continue"); $$ = stat1(CONTINUE, 0); }
319 	| '{' stat_list '}'	{ PUTS("{statlist}"); $$ = $2; }
320 	;
321 
322 stat_list:
323 	  stat_list statement	{ PUTS("stat_list stat"); $$ = linkum($1, $2); }
324 	|			{ PUTS("null stat list"); $$ = nullstat; }
325 	;
326 
327 while:
328 	  WHILE '(' conditional ')' optNL	{ PUTS("while(cond)"); $$ = $3; }
329 	;
330 
331 for:
332 	  FOR '(' simple_stat ';' conditional ';' simple_stat ')' optNL statement
333 		{ PUTS("for(e;e;e)"); $$ = stat4(FOR, $3, $5, $7, $10); }
334 	| FOR '(' simple_stat ';'  ';' simple_stat ')' optNL statement
335 		{ PUTS("for(e;e;e)"); $$ = stat4(FOR, $3, nullstat, $6, $9); }
336 	| FOR '(' VAR IN VAR ')' optNL statement
337 		{ PUTS("for(v in v)"); $$ = stat3(IN, $3, $5, $8); }
338 	;
339 
340 %%
341