xref: /titanic_41/usr/src/cmd/sgs/m4/common/m4y_xpg4.y (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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) 1988 AT&T	*/
25 /*	  All Rights Reserved  	*/
26 
27 
28 /*
29  * Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
30  * Use is subject to license terms.
31  */
32 
33 %{
34 #pragma ident	"%Z%%M%	%I%	%E% SMI"
35 extern long	evalval;
36 #define	YYSTYPE	long
37 %}
38 
39 %term DIGITS
40 %left OROR
41 %left ANDAND
42 %left '|'
43 %left '^'
44 %left '&'
45 %nonassoc EQ NE
46 %nonassoc LE GE LT GT
47 %left LSHIFT RSHIFT
48 %left '+' '-'
49 %left '*' '/' '%'
50 %right POWER
51 %right '!' '~' UMINUS
52 %%
53 
54 s	: e	= { evalval = $1; }
55 	|	= { evalval = 0; }
56 	;
57 
58 e	: e OROR e	= { $$ = ($1 != 0 || $3 != 0) ? 1 : 0; }
59 	| e ANDAND e	= { $$ = ($1 != 0 && $3 != 0) ? 1 : 0; }
60 	| '!' e		= { $$ = $2 == 0; }
61 	| '~' e		= { $$ = ~$2; }
62 	| e EQ e	= { $$ = $1 == $3; }
63 	| e NE e	= { $$ = $1 != $3; }
64 	| e GT e	= { $$ = $1 > $3; }
65 	| e GE e	= { $$ = $1 >= $3; }
66 	| e LT e	= { $$ = $1 < $3; }
67 	| e LE e	= { $$ = $1 <= $3; }
68 	| e LSHIFT e	= { $$ = $1 << $3; }
69 	| e RSHIFT e	= { $$ = $1 >> $3; }
70 	| e '|' e	= { $$ = ($1 | $3); }
71 	| e '&' e	= { $$ = ($1 & $3); }
72 	| e '^' e	= { $$ = ($1 ^ $3); }
73 	| e '+' e	= { $$ = ($1 + $3); }
74 	| e '-' e	= { $$ = ($1 - $3); }
75 	| e '*' e	= { $$ = ($1 * $3); }
76 	| e '/' e	= { $$ = ($1 / $3); }
77 	| e '%' e	= { $$ = ($1 % $3); }
78 	| '(' e ')'	= { $$ = ($2); }
79 	| e POWER e	= { for ($$ = 1; $3-- > 0; $$ *= $1); }
80 	| '-' e %prec UMINUS	= { $$ = $2-1; $$ = -$2; }
81 	| '+' e %prec UMINUS	= { $$ = $2-1; $$ = $2; }
82 	| DIGITS	= { $$ = evalval; }
83 	;
84 
85 %%
86 
87 #include "m4.h"
88 extern wchar_t *pe;
89 
90 static int peek(char c, int r1, int r2);
91 static int peek3(char c1, int rc1, char c2, int rc2, int rc3);
92 
93 yylex() {
94 	while (*pe == ' ' || *pe == '\t' || *pe == '\n')
95 		pe++;
96 	switch (*pe) {
97 	case '\0':
98 	case '+':
99 	case '-':
100 	case '/':
101 	case '%':
102 	case '^':
103 	case '~':
104 	case '(':
105 	case ')':
106 		return (*pe++);
107 	case '*':
108 		return (peek('*', POWER, '*'));
109 	case '>':
110 		return (peek3('=', GE, '>', RSHIFT, GT));
111 	case '<':
112 		return (peek3('=', LE, '<', LSHIFT, LT));
113 	case '=':
114 		return (peek('=', EQ, EQ));
115 	case '|':
116 		return (peek('|', OROR, '|'));
117 	case '&':
118 		return (peek('&', ANDAND, '&'));
119 	case '!':
120 		return (peek('=', NE, '!'));
121 	default: {
122 		register	base;
123 
124 		evalval = 0;
125 
126 		if (*pe == '0') {
127 			if (*++pe == 'x' || *pe == 'X') {
128 				base = 16;
129 				++pe;
130 			} else
131 				base = 8;
132 		} else
133 			base = 10;
134 
135 		for (;;) {
136 			register	c, dig;
137 
138 			c = *pe;
139 
140 			if (is_digit(c))
141 				dig = c - '0';
142 			else if (c >= 'a' && c <= 'f')
143 				dig = c - 'a' + 10;
144 			else if (c >= 'A' && c <= 'F')
145 				dig = c - 'A' + 10;
146 			else
147 				break;
148 
149 			evalval = evalval*base + dig;
150 			++pe;
151 		}
152 		return (DIGITS);
153 	}
154 	}
155 }
156 
157 static int
158 peek(char c, int r1, int r2)
159 {
160 	if (*++pe != c)
161 		return (r2);
162 	++pe;
163 	return (r1);
164 }
165 
166 static int
167 peek3(char c1, int rc1, char c2, int rc2, int rc3)
168 {
169 	++pe;
170 	if (*pe == c1) {
171 		++pe;
172 		return (rc1);
173 	}
174 	if (*pe == c2) {
175 		++pe;
176 		return (rc2);
177 	}
178 	return (rc3);
179 }
180 
181 /*VARARGS*/
182 static void
183 yyerror()
184 {
185 	;
186 }
187