xref: /titanic_41/usr/src/lib/libdtrace/common/dt_lex.l (revision 2b24ab6b3865caeede9eeb9db6b83e1d89dcd1ea)
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 (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  *
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <string.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <assert.h>
32 #include <ctype.h>
33 #include <errno.h>
34 
35 #include <dt_impl.h>
36 #include <dt_grammar.h>
37 #include <dt_parser.h>
38 #include <dt_string.h>
39 
40 /*
41  * We need to undefine lex's input and unput macros so that references to these
42  * call the functions provided at the end of this source file.
43  */
44 #undef input
45 #undef unput
46 
47 static int id_or_type(const char *);
48 static int input(void);
49 static void unput(int);
50 
51 /*
52  * We first define a set of labeled states for use in the D lexer and then a
53  * set of regular expressions to simplify things below. The lexer states are:
54  *
55  * S0 - D program clause and expression lexing
56  * S1 - D comments (i.e. skip everything until end of comment)
57  * S2 - D program outer scope (probe specifiers and declarations)
58  * S3 - D control line parsing (i.e. after ^# is seen but before \n)
59  * S4 - D control line scan (locate control directives only and invoke S3)
60  */
61 %}
62 
63 %e 1500		/* maximum nodes */
64 %p 3700		/* maximum positions */
65 %n 600		/* maximum states */
66 
67 %s S0 S1 S2 S3 S4
68 
69 RGX_AGG		"@"[a-zA-Z_][0-9a-zA-Z_]*
70 RGX_PSPEC	[-$:a-zA-Z_.?*\\\[\]!][-$:0-9a-zA-Z_.`?*\\\[\]!]*
71 RGX_IDENT	[a-zA-Z_`][0-9a-zA-Z_`]*
72 RGX_INT		([0-9]+|0[xX][0-9A-Fa-f]+)[uU]?[lL]?[lL]?
73 RGX_FP		([0-9]+("."?)[0-9]*|"."[0-9]+)((e|E)("+"|-)?[0-9]+)?[fFlL]?
74 RGX_WS		[\f\n\r\t\v ]
75 RGX_STR		([^"\\\n]|\\[^"\n]|\\\")*
76 RGX_CHR		([^'\\\n]|\\[^'\n]|\\')*
77 RGX_INTERP	^[\f\t\v ]*#!.*
78 RGX_CTL		^[\f\t\v ]*#
79 
80 %%
81 
82 %{
83 
84 /*
85  * We insert a special prologue into yylex() itself: if the pcb contains a
86  * context token, we return that prior to running the normal lexer.  This
87  * allows libdtrace to force yacc into one of our three parsing contexts: D
88  * expression (DT_CTX_DEXPR), D program (DT_CTX_DPROG) or D type (DT_CTX_DTYPE).
89  * Once the token is returned, we clear it so this only happens once.
90  */
91 if (yypcb->pcb_token != 0) {
92 	int tok = yypcb->pcb_token;
93 	yypcb->pcb_token = 0;
94 	return (tok);
95 }
96 
97 %}
98 
99 <S0>auto	return (DT_KEY_AUTO);
100 <S0>break	return (DT_KEY_BREAK);
101 <S0>case	return (DT_KEY_CASE);
102 <S0>char	return (DT_KEY_CHAR);
103 <S0>const	return (DT_KEY_CONST);
104 <S0>continue	return (DT_KEY_CONTINUE);
105 <S0>counter	return (DT_KEY_COUNTER);
106 <S0>default	return (DT_KEY_DEFAULT);
107 <S0>do		return (DT_KEY_DO);
108 <S0>double	return (DT_KEY_DOUBLE);
109 <S0>else	return (DT_KEY_ELSE);
110 <S0>enum	return (DT_KEY_ENUM);
111 <S0>extern	return (DT_KEY_EXTERN);
112 <S0>float	return (DT_KEY_FLOAT);
113 <S0>for		return (DT_KEY_FOR);
114 <S0>goto	return (DT_KEY_GOTO);
115 <S0>if		return (DT_KEY_IF);
116 <S0>import	return (DT_KEY_IMPORT);
117 <S0>inline	return (DT_KEY_INLINE);
118 <S0>int		return (DT_KEY_INT);
119 <S0>long	return (DT_KEY_LONG);
120 <S0>offsetof	return (DT_TOK_OFFSETOF);
121 <S0>probe	return (DT_KEY_PROBE);
122 <S0>provider	return (DT_KEY_PROVIDER);
123 <S0>register	return (DT_KEY_REGISTER);
124 <S0>restrict	return (DT_KEY_RESTRICT);
125 <S0>return	return (DT_KEY_RETURN);
126 <S0>self	return (DT_KEY_SELF);
127 <S0>short	return (DT_KEY_SHORT);
128 <S0>signed	return (DT_KEY_SIGNED);
129 <S0>sizeof	return (DT_TOK_SIZEOF);
130 <S0>static	return (DT_KEY_STATIC);
131 <S0>string	return (DT_KEY_STRING);
132 <S0>stringof	return (DT_TOK_STRINGOF);
133 <S0>struct	return (DT_KEY_STRUCT);
134 <S0>switch	return (DT_KEY_SWITCH);
135 <S0>this	return (DT_KEY_THIS);
136 <S0>translator	return (DT_KEY_XLATOR);
137 <S0>typedef	return (DT_KEY_TYPEDEF);
138 <S0>union	return (DT_KEY_UNION);
139 <S0>unsigned	return (DT_KEY_UNSIGNED);
140 <S0>void	return (DT_KEY_VOID);
141 <S0>volatile	return (DT_KEY_VOLATILE);
142 <S0>while	return (DT_KEY_WHILE);
143 <S0>xlate	return (DT_TOK_XLATE);
144 
145 <S2>auto	{ yybegin(YYS_EXPR);	return (DT_KEY_AUTO); }
146 <S2>char	{ yybegin(YYS_EXPR);	return (DT_KEY_CHAR); }
147 <S2>const	{ yybegin(YYS_EXPR);	return (DT_KEY_CONST); }
148 <S2>counter	{ yybegin(YYS_DEFINE);	return (DT_KEY_COUNTER); }
149 <S2>double	{ yybegin(YYS_EXPR);	return (DT_KEY_DOUBLE); }
150 <S2>enum	{ yybegin(YYS_EXPR);	return (DT_KEY_ENUM); }
151 <S2>extern	{ yybegin(YYS_EXPR);	return (DT_KEY_EXTERN); }
152 <S2>float	{ yybegin(YYS_EXPR);	return (DT_KEY_FLOAT); }
153 <S2>import	{ yybegin(YYS_EXPR);	return (DT_KEY_IMPORT); }
154 <S2>inline	{ yybegin(YYS_DEFINE);	return (DT_KEY_INLINE); }
155 <S2>int		{ yybegin(YYS_EXPR);	return (DT_KEY_INT); }
156 <S2>long	{ yybegin(YYS_EXPR);	return (DT_KEY_LONG); }
157 <S2>provider	{ yybegin(YYS_DEFINE);	return (DT_KEY_PROVIDER); }
158 <S2>register	{ yybegin(YYS_EXPR);	return (DT_KEY_REGISTER); }
159 <S2>restrict	{ yybegin(YYS_EXPR);	return (DT_KEY_RESTRICT); }
160 <S2>self	{ yybegin(YYS_EXPR);	return (DT_KEY_SELF); }
161 <S2>short	{ yybegin(YYS_EXPR);	return (DT_KEY_SHORT); }
162 <S2>signed	{ yybegin(YYS_EXPR);	return (DT_KEY_SIGNED); }
163 <S2>static	{ yybegin(YYS_EXPR);	return (DT_KEY_STATIC); }
164 <S2>string	{ yybegin(YYS_EXPR);	return (DT_KEY_STRING); }
165 <S2>struct	{ yybegin(YYS_EXPR);	return (DT_KEY_STRUCT); }
166 <S2>this	{ yybegin(YYS_EXPR);	return (DT_KEY_THIS); }
167 <S2>translator	{ yybegin(YYS_DEFINE);	return (DT_KEY_XLATOR); }
168 <S2>typedef	{ yybegin(YYS_EXPR);	return (DT_KEY_TYPEDEF); }
169 <S2>union	{ yybegin(YYS_EXPR);	return (DT_KEY_UNION); }
170 <S2>unsigned	{ yybegin(YYS_EXPR);	return (DT_KEY_UNSIGNED); }
171 <S2>void	{ yybegin(YYS_EXPR);	return (DT_KEY_VOID); }
172 <S2>volatile	{ yybegin(YYS_EXPR);	return (DT_KEY_VOLATILE); }
173 
174 <S0>"$$"[0-9]+	{
175 			int i = atoi(yytext + 2);
176 			char *v = "";
177 
178 			/*
179 			 * A macro argument reference substitutes the text of
180 			 * an argument in place of the current token.  When we
181 			 * see $$<d> we fetch the saved string from pcb_sargv
182 			 * (or use the default argument if the option has been
183 			 * set and the argument hasn't been specified) and
184 			 * return a token corresponding to this string.
185 			 */
186 			if (i < 0 || (i >= yypcb->pcb_sargc &&
187 			    !(yypcb->pcb_cflags & DTRACE_C_DEFARG))) {
188 				xyerror(D_MACRO_UNDEF, "macro argument %s is "
189 				    "not defined\n", yytext);
190 			}
191 
192 			if (i < yypcb->pcb_sargc) {
193 				v = yypcb->pcb_sargv[i]; /* get val from pcb */
194 				yypcb->pcb_sflagv[i] |= DT_IDFLG_REF;
195 			}
196 
197 			if ((yylval.l_str = strdup(v)) == NULL)
198 				longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
199 
200 			(void) stresc2chr(yylval.l_str);
201 			return (DT_TOK_STRING);
202 		}
203 
204 <S0>"$"[0-9]+	{
205 			int i = atoi(yytext + 1);
206 			char *p, *v = "0";
207 
208 			/*
209 			 * A macro argument reference substitutes the text of
210 			 * one identifier or integer pattern for another.  When
211 			 * we see $<d> we fetch the saved string from pcb_sargv
212 			 * (or use the default argument if the option has been
213 			 * set and the argument hasn't been specified) and
214 			 * return a token corresponding to this string.
215 			 */
216 			if (i < 0 || (i >= yypcb->pcb_sargc &&
217 			    !(yypcb->pcb_cflags & DTRACE_C_DEFARG))) {
218 				xyerror(D_MACRO_UNDEF, "macro argument %s is "
219 				    "not defined\n", yytext);
220 			}
221 
222 			if (i < yypcb->pcb_sargc) {
223 				v = yypcb->pcb_sargv[i]; /* get val from pcb */
224 				yypcb->pcb_sflagv[i] |= DT_IDFLG_REF;
225 			}
226 
227 			/*
228 			 * If the macro text is not a valid integer or ident,
229 			 * then we treat it as a string.  The string may be
230 			 * optionally enclosed in quotes, which we strip.
231 			 */
232 			if (strbadidnum(v)) {
233 				size_t len = strlen(v);
234 
235 				if (len != 1 && *v == '"' && v[len - 1] == '"')
236 					yylval.l_str = strndup(v + 1, len - 2);
237 				else
238 					yylval.l_str = strndup(v, len);
239 
240 				if (yylval.l_str == NULL)
241 					longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
242 
243 				(void) stresc2chr(yylval.l_str);
244 				return (DT_TOK_STRING);
245 			}
246 
247 			/*
248 			 * If the macro text is not a string an begins with a
249 			 * digit or a +/- sign, process it as an integer token.
250 			 */
251 			if (isdigit(v[0]) || v[0] == '-' || v[0] == '+') {
252 				if (isdigit(v[0]))
253 					yyintprefix = 0;
254 				else
255 					yyintprefix = *v++;
256 
257 				errno = 0;
258 				yylval.l_int = strtoull(v, &p, 0);
259 				(void) strncpy(yyintsuffix, p,
260 				    sizeof (yyintsuffix));
261 				yyintdecimal = *v != '0';
262 
263 				if (errno == ERANGE) {
264 					xyerror(D_MACRO_OFLOW, "macro argument"
265 					    " %s constant %s results in integer"
266 					    " overflow\n", yytext, v);
267 				}
268 
269 				return (DT_TOK_INT);
270 			}
271 
272 			return (id_or_type(v));
273 		}
274 
275 <S0>"$$"{RGX_IDENT} {
276 			dt_ident_t *idp = dt_idhash_lookup(
277 			    yypcb->pcb_hdl->dt_macros, yytext + 2);
278 
279 			char s[16]; /* enough for UINT_MAX + \0 */
280 
281 			if (idp == NULL) {
282 				xyerror(D_MACRO_UNDEF, "macro variable %s "
283 				    "is not defined\n", yytext);
284 			}
285 
286 			/*
287 			 * For the moment, all current macro variables are of
288 			 * type id_t (refer to dtrace_update() for details).
289 			 */
290 			(void) snprintf(s, sizeof (s), "%u", idp->di_id);
291 			if ((yylval.l_str = strdup(s)) == NULL)
292 				longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
293 
294 			return (DT_TOK_STRING);
295 		}
296 
297 <S0>"$"{RGX_IDENT} {
298 			dt_ident_t *idp = dt_idhash_lookup(
299 			    yypcb->pcb_hdl->dt_macros, yytext + 1);
300 
301 			if (idp == NULL) {
302 				xyerror(D_MACRO_UNDEF, "macro variable %s "
303 				    "is not defined\n", yytext);
304 			}
305 
306 			/*
307 			 * For the moment, all current macro variables are of
308 			 * type id_t (refer to dtrace_update() for details).
309 			 */
310 			yylval.l_int = (intmax_t)(int)idp->di_id;
311 			yyintprefix = 0;
312 			yyintsuffix[0] = '\0';
313 			yyintdecimal = 1;
314 
315 			return (DT_TOK_INT);
316 		}
317 
318 <S0>{RGX_IDENT}	{
319 			return (id_or_type(yytext));
320 		}
321 
322 <S0>{RGX_AGG}	{
323 			if ((yylval.l_str = strdup(yytext)) == NULL)
324 				longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
325 			return (DT_TOK_AGG);
326 		}
327 
328 <S0>"@"		{
329 			if ((yylval.l_str = strdup("@_")) == NULL)
330 				longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
331 			return (DT_TOK_AGG);
332 		}
333 
334 <S0>{RGX_INT}	|
335 <S2>{RGX_INT}	|
336 <S3>{RGX_INT}	{
337 			char *p;
338 
339 			errno = 0;
340 			yylval.l_int = strtoull(yytext, &p, 0);
341 			yyintprefix = 0;
342 			(void) strncpy(yyintsuffix, p, sizeof (yyintsuffix));
343 			yyintdecimal = yytext[0] != '0';
344 
345 			if (errno == ERANGE) {
346 				xyerror(D_INT_OFLOW, "constant %s results in "
347 				    "integer overflow\n", yytext);
348 			}
349 
350 			if (*p != '\0' && strchr("uUlL", *p) == NULL) {
351 				xyerror(D_INT_DIGIT, "constant %s contains "
352 				    "invalid digit %c\n", yytext, *p);
353 			}
354 
355 			if ((YYSTATE) != S3)
356 				return (DT_TOK_INT);
357 
358 			yypragma = dt_node_link(yypragma,
359 			    dt_node_int(yylval.l_int));
360 		}
361 
362 <S0>{RGX_FP}	yyerror("floating-point constants are not permitted\n");
363 
364 <S0>\"{RGX_STR}$ |
365 <S3>\"{RGX_STR}$ xyerror(D_STR_NL, "newline encountered in string literal");
366 
367 <S0>\"{RGX_STR}\" |
368 <S3>\"{RGX_STR}\" {
369 			/*
370 			 * Quoted string -- convert C escape sequences and
371 			 * return the string as a token.
372 			 */
373 			yylval.l_str = strndup(yytext + 1, yyleng - 2);
374 
375 			if (yylval.l_str == NULL)
376 				longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
377 
378 			(void) stresc2chr(yylval.l_str);
379 			if ((YYSTATE) != S3)
380 				return (DT_TOK_STRING);
381 
382 			yypragma = dt_node_link(yypragma,
383 			    dt_node_string(yylval.l_str));
384 		}
385 
386 <S0>'{RGX_CHR}$	xyerror(D_CHR_NL, "newline encountered in character constant");
387 
388 <S0>'{RGX_CHR}'	{
389 			char *s, *p, *q;
390 			size_t nbytes;
391 
392 			/*
393 			 * Character constant -- convert C escape sequences and
394 			 * return the character as an integer immediate value.
395 			 */
396 			if (yyleng == 2)
397 				xyerror(D_CHR_NULL, "empty character constant");
398 
399 			s = yytext + 1;
400 			yytext[yyleng - 1] = '\0';
401 			nbytes = stresc2chr(s);
402 			yylval.l_int = 0;
403 			yyintprefix = 0;
404 			yyintsuffix[0] = '\0';
405 			yyintdecimal = 1;
406 
407 			if (nbytes > sizeof (yylval.l_int)) {
408 				xyerror(D_CHR_OFLOW, "character constant is "
409 				    "too long");
410 			}
411 #ifdef _LITTLE_ENDIAN
412 			p = ((char *)&yylval.l_int) + nbytes - 1;
413 			for (q = s; nbytes != 0; nbytes--)
414 				*p-- = *q++;
415 #else
416 			bcopy(s, ((char *)&yylval.l_int) +
417 			    sizeof (yylval.l_int) - nbytes, nbytes);
418 #endif
419 			return (DT_TOK_INT);
420 		}
421 
422 <S0>"/*"	|
423 <S2>"/*"	{
424 			yypcb->pcb_cstate = (YYSTATE);
425 			BEGIN(S1);
426 		}
427 
428 <S0>{RGX_INTERP} |
429 <S2>{RGX_INTERP} ;	/* discard any #! lines */
430 
431 <S0>{RGX_CTL}	|
432 <S2>{RGX_CTL}	|
433 <S4>{RGX_CTL}	{
434 			assert(yypragma == NULL);
435 			yypcb->pcb_cstate = (YYSTATE);
436 			BEGIN(S3);
437 		}
438 
439 <S4>.		;	/* discard */
440 <S4>"\n"	;	/* discard */
441 
442 <S0>"/"		{
443 			int c, tok;
444 
445 			/*
446 			 * The use of "/" as the predicate delimiter and as the
447 			 * integer division symbol requires special lookahead
448 			 * to avoid a shift/reduce conflict in the D grammar.
449 			 * We look ahead to the next non-whitespace character.
450 			 * If we encounter EOF, ";", "{", or "/", then this "/"
451 			 * closes the predicate and we return DT_TOK_EPRED.
452 			 * If we encounter anything else, it's DT_TOK_DIV.
453 			 */
454 			while ((c = input()) != 0) {
455 				if (strchr("\f\n\r\t\v ", c) == NULL)
456 					break;
457 			}
458 
459 			if (c == 0 || c == ';' || c == '{' || c == '/') {
460 				if (yypcb->pcb_parens != 0) {
461 					yyerror("closing ) expected in "
462 					    "predicate before /\n");
463 				}
464 				if (yypcb->pcb_brackets != 0) {
465 					yyerror("closing ] expected in "
466 					    "predicate before /\n");
467 				}
468 				tok = DT_TOK_EPRED;
469 			} else
470 				tok = DT_TOK_DIV;
471 
472 			unput(c);
473 			return (tok);
474 		}
475 
476 <S0>"("		{
477 			yypcb->pcb_parens++;
478 			return (DT_TOK_LPAR);
479 		}
480 
481 <S0>")"		{
482 			if (--yypcb->pcb_parens < 0)
483 				yyerror("extra ) in input stream\n");
484 			return (DT_TOK_RPAR);
485 		}
486 
487 <S0>"["		{
488 			yypcb->pcb_brackets++;
489 			return (DT_TOK_LBRAC);
490 		}
491 
492 <S0>"]"		{
493 			if (--yypcb->pcb_brackets < 0)
494 				yyerror("extra ] in input stream\n");
495 			return (DT_TOK_RBRAC);
496 		}
497 
498 <S0>"{"		|
499 <S2>"{"		{
500 			yypcb->pcb_braces++;
501 			return ('{');
502 		}
503 
504 <S0>"}"		{
505 			if (--yypcb->pcb_braces < 0)
506 				yyerror("extra } in input stream\n");
507 			return ('}');
508 		}
509 
510 <S0>"|"		return (DT_TOK_BOR);
511 <S0>"^"		return (DT_TOK_XOR);
512 <S0>"&"		return (DT_TOK_BAND);
513 <S0>"&&"	return (DT_TOK_LAND);
514 <S0>"^^"	return (DT_TOK_LXOR);
515 <S0>"||"	return (DT_TOK_LOR);
516 <S0>"=="	return (DT_TOK_EQU);
517 <S0>"!="	return (DT_TOK_NEQ);
518 <S0>"<"		return (DT_TOK_LT);
519 <S0>"<="	return (DT_TOK_LE);
520 <S0>">"		return (DT_TOK_GT);
521 <S0>">="	return (DT_TOK_GE);
522 <S0>"<<"	return (DT_TOK_LSH);
523 <S0>">>"	return (DT_TOK_RSH);
524 <S0>"+"		return (DT_TOK_ADD);
525 <S0>"-"		return (DT_TOK_SUB);
526 <S0>"*"		return (DT_TOK_MUL);
527 <S0>"%"		return (DT_TOK_MOD);
528 <S0>"~"		return (DT_TOK_BNEG);
529 <S0>"!"		return (DT_TOK_LNEG);
530 <S0>"?"		return (DT_TOK_QUESTION);
531 <S0>":"		return (DT_TOK_COLON);
532 <S0>"."		return (DT_TOK_DOT);
533 <S0>"->"	return (DT_TOK_PTR);
534 <S0>"="		return (DT_TOK_ASGN);
535 <S0>"+="	return (DT_TOK_ADD_EQ);
536 <S0>"-="	return (DT_TOK_SUB_EQ);
537 <S0>"*="	return (DT_TOK_MUL_EQ);
538 <S0>"/="	return (DT_TOK_DIV_EQ);
539 <S0>"%="	return (DT_TOK_MOD_EQ);
540 <S0>"&="	return (DT_TOK_AND_EQ);
541 <S0>"^="	return (DT_TOK_XOR_EQ);
542 <S0>"|="	return (DT_TOK_OR_EQ);
543 <S0>"<<="	return (DT_TOK_LSH_EQ);
544 <S0>">>="	return (DT_TOK_RSH_EQ);
545 <S0>"++"	return (DT_TOK_ADDADD);
546 <S0>"--"	return (DT_TOK_SUBSUB);
547 <S0>"..."	return (DT_TOK_ELLIPSIS);
548 <S0>","		return (DT_TOK_COMMA);
549 <S0>";"		return (';');
550 <S0>{RGX_WS}	; /* discard */
551 <S0>"\\"\n	; /* discard */
552 <S0>.		yyerror("syntax error near \"%c\"\n", yytext[0]);
553 
554 <S1>"/*"	yyerror("/* encountered inside a comment\n");
555 <S1>"*/"	BEGIN(yypcb->pcb_cstate);
556 <S1>.|\n	; /* discard */
557 
558 <S2>{RGX_PSPEC}	{
559 			/*
560 			 * S2 has an ambiguity because RGX_PSPEC includes '*'
561 			 * as a glob character and '*' also can be DT_TOK_STAR.
562 			 * Since lex always matches the longest token, this
563 			 * rule can be matched by an input string like "int*",
564 			 * which could begin a global variable declaration such
565 			 * as "int*x;" or could begin a RGX_PSPEC with globbing
566 			 * such as "int* { trace(timestamp); }".  If C_PSPEC is
567 			 * not set, we must resolve the ambiguity in favor of
568 			 * the type and perform lexer pushback if the fragment
569 			 * before '*' or entire fragment matches a type name.
570 			 * If C_PSPEC is set, we always return a PSPEC token.
571 			 * If C_PSPEC is off, the user can avoid ambiguity by
572 			 * including a ':' delimiter in the specifier, which
573 			 * they should be doing anyway to specify the provider.
574 			 */
575 			if (!(yypcb->pcb_cflags & DTRACE_C_PSPEC) &&
576 			    strchr(yytext, ':') == NULL) {
577 
578 				char *p = strchr(yytext, '*');
579 				char *q = yytext + yyleng - 1;
580 
581 				if (p != NULL && p > yytext)
582 					*p = '\0'; /* prune yytext */
583 
584 				if (dt_type_lookup(yytext, NULL) == 0) {
585 					yylval.l_str = strdup(yytext);
586 
587 					if (yylval.l_str == NULL) {
588 						longjmp(yypcb->pcb_jmpbuf,
589 						    EDT_NOMEM);
590 					}
591 
592 					if (p != NULL && p > yytext) {
593 						for (*p = '*'; q >= p; q--)
594 							unput(*q);
595 					}
596 
597 					yybegin(YYS_EXPR);
598 					return (DT_TOK_TNAME);
599 				}
600 
601 				if (p != NULL && p > yytext)
602 					*p = '*'; /* restore yytext */
603 			}
604 
605 			if ((yylval.l_str = strdup(yytext)) == NULL)
606 				longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
607 
608 			return (DT_TOK_PSPEC);
609 		}
610 
611 <S2>"/"		return (DT_TOK_DIV);
612 <S2>","		return (DT_TOK_COMMA);
613 
614 <S2>{RGX_WS}	; /* discard */
615 <S2>.		yyerror("syntax error near \"%c\"\n", yytext[0]);
616 
617 <S3>\n		{
618 			dt_pragma(yypragma);
619 			yypragma = NULL;
620 			BEGIN(yypcb->pcb_cstate);
621 		}
622 
623 <S3>[\f\t\v ]+	; /* discard */
624 
625 <S3>[^\f\n\t\v "]+ {
626 			dt_node_t *dnp;
627 
628 			if ((yylval.l_str = strdup(yytext)) == NULL)
629 				longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
630 
631 			/*
632 			 * We want to call dt_node_ident() here, but we can't
633 			 * because it will expand inlined identifiers, which we
634 			 * don't want to do from #pragma context in order to
635 			 * support pragmas that apply to the ident itself.  We
636 			 * call dt_node_string() and then reset dn_op instead.
637 			 */
638 			dnp = dt_node_string(yylval.l_str);
639 			dnp->dn_kind = DT_NODE_IDENT;
640 			dnp->dn_op = DT_TOK_IDENT;
641 			yypragma = dt_node_link(yypragma, dnp);
642 		}
643 
644 <S3>.		yyerror("syntax error near \"%c\"\n", yytext[0]);
645 
646 %%
647 
648 /*
649  * yybegin provides a wrapper for use from C code around the lex BEGIN() macro.
650  * We use two main states for lexing because probe descriptions use a syntax
651  * that is incompatible with the normal D tokens (e.g. names can contain "-").
652  * yybegin also handles the job of switching between two lists of dt_nodes
653  * as we allocate persistent definitions, like inlines, and transient nodes
654  * that will be freed once we are done parsing the current program file.
655  */
656 void
657 yybegin(yystate_t state)
658 {
659 #ifdef	YYDEBUG
660 	yydebug = _dtrace_debug;
661 #endif
662 	if (yypcb->pcb_yystate == state)
663 		return; /* nothing to do if we're in the state already */
664 
665 	if (yypcb->pcb_yystate == YYS_DEFINE) {
666 		yypcb->pcb_list = yypcb->pcb_hold;
667 		yypcb->pcb_hold = NULL;
668 	}
669 
670 	switch (state) {
671 	case YYS_CLAUSE:
672 		BEGIN(S2);
673 		break;
674 	case YYS_DEFINE:
675 		assert(yypcb->pcb_hold == NULL);
676 		yypcb->pcb_hold = yypcb->pcb_list;
677 		yypcb->pcb_list = NULL;
678 		/*FALLTHRU*/
679 	case YYS_EXPR:
680 		BEGIN(S0);
681 		break;
682 	case YYS_DONE:
683 		break;
684 	case YYS_CONTROL:
685 		BEGIN(S4);
686 		break;
687 	default:
688 		xyerror(D_UNKNOWN, "internal error -- bad yystate %d\n", state);
689 	}
690 
691 	yypcb->pcb_yystate = state;
692 }
693 
694 void
695 yyinit(dt_pcb_t *pcb)
696 {
697 	yypcb = pcb;
698 	yylineno = 1;
699 	yypragma = NULL;
700 	yysptr = yysbuf;
701 }
702 
703 /*
704  * Given a lexeme 's' (typically yytext), set yylval and return an appropriate
705  * token to the parser indicating either an identifier or a typedef name.
706  * User-defined global variables always take precedence over types, but we do
707  * use some heuristics because D programs can look at an ever-changing set of
708  * kernel types and also can implicitly instantiate variables by assignment,
709  * unlike in C.  The code here is ordered carefully as lookups are not cheap.
710  */
711 static int
712 id_or_type(const char *s)
713 {
714 	dtrace_hdl_t *dtp = yypcb->pcb_hdl;
715 	dt_decl_t *ddp = yypcb->pcb_dstack.ds_decl;
716 	int c0, c1, ttok = DT_TOK_TNAME;
717 	dt_ident_t *idp;
718 
719 	if ((s = yylval.l_str = strdup(s)) == NULL)
720 		longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
721 
722 	/*
723 	 * If the lexeme is a global variable or likely identifier or *not* a
724 	 * type_name, then it is an identifier token.
725 	 */
726 	if (dt_idstack_lookup(&yypcb->pcb_globals, s) != NULL ||
727 	    dt_idhash_lookup(yypcb->pcb_idents, s) != NULL ||
728 	    dt_type_lookup(s, NULL) != 0)
729 		return (DT_TOK_IDENT);
730 
731 	/*
732 	 * If we're in the midst of parsing a declaration and a type_specifier
733 	 * has already been shifted, then return DT_TOK_IDENT instead of TNAME.
734 	 * This semantic is necessary to permit valid ISO C code such as:
735 	 *
736 	 * typedef int foo;
737 	 * struct s { foo foo; };
738 	 *
739 	 * without causing shift/reduce conflicts in the direct_declarator part
740 	 * of the grammar.  The result is that we must check for conflicting
741 	 * redeclarations of the same identifier as part of dt_node_decl().
742 	 */
743 	if (ddp != NULL && ddp->dd_name != NULL)
744 		return (DT_TOK_IDENT);
745 
746 	/*
747 	 * If the lexeme is a type name and we are not in a program clause,
748 	 * then always interpret it as a type and return DT_TOK_TNAME.
749 	 */
750 	if ((YYSTATE) != S0)
751 		return (DT_TOK_TNAME);
752 
753 	/*
754 	 * If the lexeme matches a type name but is in a program clause, then
755 	 * it could be a type or it could be an undefined variable.  Peek at
756 	 * the next token to decide.  If we see ++, --, [, or =, we know there
757 	 * might be an assignment that is trying to create a global variable,
758 	 * so we optimistically return DT_TOK_IDENT.  There is no harm in being
759 	 * wrong: a type_name followed by ++, --, [, or = is a syntax error.
760 	 */
761 	while ((c0 = input()) != 0) {
762 		if (strchr("\f\n\r\t\v ", c0) == NULL)
763 			break;
764 	}
765 
766 	switch (c0) {
767 	case '+':
768 	case '-':
769 		if ((c1 = input()) == c0)
770 			ttok = DT_TOK_IDENT;
771 		unput(c1);
772 		break;
773 
774 	case '=':
775 		if ((c1 = input()) != c0)
776 			ttok = DT_TOK_IDENT;
777 		unput(c1);
778 		break;
779 	case '[':
780 		ttok = DT_TOK_IDENT;
781 		break;
782 	}
783 
784 	if (ttok == DT_TOK_IDENT) {
785 		idp = dt_idhash_insert(yypcb->pcb_idents, s, DT_IDENT_SCALAR, 0,
786 		    0, _dtrace_defattr, 0, &dt_idops_thaw, NULL, dtp->dt_gen);
787 
788 		if (idp == NULL)
789 			longjmp(yypcb->pcb_jmpbuf, EDT_NOMEM);
790 	}
791 
792 	unput(c0);
793 	return (ttok);
794 }
795 
796 static int
797 input(void)
798 {
799 	int c;
800 
801 	if (yysptr > yysbuf)
802 		c = *--yysptr;
803 	else if (yypcb->pcb_fileptr != NULL)
804 		c = fgetc(yypcb->pcb_fileptr);
805 	else if (yypcb->pcb_strptr < yypcb->pcb_string + yypcb->pcb_strlen)
806 		c = *yypcb->pcb_strptr++;
807 	else
808 		c = EOF;
809 
810 	if (c == '\n')
811 		yylineno++;
812 
813 	if (c != EOF)
814 		return (c);
815 
816 	if ((YYSTATE) == S1)
817 		yyerror("end-of-file encountered before matching */\n");
818 
819 	if ((YYSTATE) == S3)
820 		yyerror("end-of-file encountered before end of control line\n");
821 
822 	if (yypcb->pcb_fileptr != NULL && ferror(yypcb->pcb_fileptr))
823 		longjmp(yypcb->pcb_jmpbuf, EDT_FIO);
824 
825 	return (0); /* EOF */
826 }
827 
828 static void
829 unput(int c)
830 {
831 	if (c == '\n')
832 		yylineno--;
833 
834 	*yysptr++ = c;
835 	yytchar = c;
836 }
837