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