xref: /illumos-gate/usr/src/cmd/sgs/m4/common/m4y_xpg4.y (revision 2a8bcb4e)
17c478bd9Sstevel@tonic-gate %{
27c478bd9Sstevel@tonic-gate /*
37c478bd9Sstevel@tonic-gate  * CDDL HEADER START
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
67c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
77c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
87c478bd9Sstevel@tonic-gate  * with the License.
97c478bd9Sstevel@tonic-gate  *
107c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
117c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
127c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
137c478bd9Sstevel@tonic-gate  * and limitations under the License.
147c478bd9Sstevel@tonic-gate  *
157c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
167c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
177c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
187c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
197c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
207c478bd9Sstevel@tonic-gate  *
217c478bd9Sstevel@tonic-gate  * CDDL HEADER END
227c478bd9Sstevel@tonic-gate  */
237c478bd9Sstevel@tonic-gate %}
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*
26*9fb11590Smike_s  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
277c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
30*9fb11590Smike_s /*	Copyright (c) 1988 AT&T	*/
31*9fb11590Smike_s /*	  All Rights Reserved  	*/
32*9fb11590Smike_s 
337c478bd9Sstevel@tonic-gate %{
347c478bd9Sstevel@tonic-gate extern long	evalval;
357c478bd9Sstevel@tonic-gate #define	YYSTYPE	long
367c478bd9Sstevel@tonic-gate %}
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate %term DIGITS
397c478bd9Sstevel@tonic-gate %left OROR
407c478bd9Sstevel@tonic-gate %left ANDAND
417c478bd9Sstevel@tonic-gate %left '|'
427c478bd9Sstevel@tonic-gate %left '^'
437c478bd9Sstevel@tonic-gate %left '&'
447c478bd9Sstevel@tonic-gate %nonassoc EQ NE
457c478bd9Sstevel@tonic-gate %nonassoc LE GE LT GT
467c478bd9Sstevel@tonic-gate %left LSHIFT RSHIFT
477c478bd9Sstevel@tonic-gate %left '+' '-'
487c478bd9Sstevel@tonic-gate %left '*' '/' '%'
497c478bd9Sstevel@tonic-gate %right POWER
507c478bd9Sstevel@tonic-gate %right '!' '~' UMINUS
517c478bd9Sstevel@tonic-gate %%
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate s	: e	= { evalval = $1; }
547c478bd9Sstevel@tonic-gate 	|	= { evalval = 0; }
557c478bd9Sstevel@tonic-gate 	;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate e	: e OROR e	= { $$ = ($1 != 0 || $3 != 0) ? 1 : 0; }
587c478bd9Sstevel@tonic-gate 	| e ANDAND e	= { $$ = ($1 != 0 && $3 != 0) ? 1 : 0; }
597c478bd9Sstevel@tonic-gate 	| '!' e		= { $$ = $2 == 0; }
607c478bd9Sstevel@tonic-gate 	| '~' e		= { $$ = ~$2; }
617c478bd9Sstevel@tonic-gate 	| e EQ e	= { $$ = $1 == $3; }
627c478bd9Sstevel@tonic-gate 	| e NE e	= { $$ = $1 != $3; }
637c478bd9Sstevel@tonic-gate 	| e GT e	= { $$ = $1 > $3; }
647c478bd9Sstevel@tonic-gate 	| e GE e	= { $$ = $1 >= $3; }
657c478bd9Sstevel@tonic-gate 	| e LT e	= { $$ = $1 < $3; }
667c478bd9Sstevel@tonic-gate 	| e LE e	= { $$ = $1 <= $3; }
677c478bd9Sstevel@tonic-gate 	| e LSHIFT e	= { $$ = $1 << $3; }
687c478bd9Sstevel@tonic-gate 	| e RSHIFT e	= { $$ = $1 >> $3; }
697c478bd9Sstevel@tonic-gate 	| e '|' e	= { $$ = ($1 | $3); }
707c478bd9Sstevel@tonic-gate 	| e '&' e	= { $$ = ($1 & $3); }
717c478bd9Sstevel@tonic-gate 	| e '^' e	= { $$ = ($1 ^ $3); }
727c478bd9Sstevel@tonic-gate 	| e '+' e	= { $$ = ($1 + $3); }
737c478bd9Sstevel@tonic-gate 	| e '-' e	= { $$ = ($1 - $3); }
747c478bd9Sstevel@tonic-gate 	| e '*' e	= { $$ = ($1 * $3); }
757c478bd9Sstevel@tonic-gate 	| e '/' e	= { $$ = ($1 / $3); }
767c478bd9Sstevel@tonic-gate 	| e '%' e	= { $$ = ($1 % $3); }
777c478bd9Sstevel@tonic-gate 	| '(' e ')'	= { $$ = ($2); }
787c478bd9Sstevel@tonic-gate 	| e POWER e	= { for ($$ = 1; $3-- > 0; $$ *= $1); }
797c478bd9Sstevel@tonic-gate 	| '-' e %prec UMINUS	= { $$ = $2-1; $$ = -$2; }
807c478bd9Sstevel@tonic-gate 	| '+' e %prec UMINUS	= { $$ = $2-1; $$ = $2; }
817c478bd9Sstevel@tonic-gate 	| DIGITS	= { $$ = evalval; }
827c478bd9Sstevel@tonic-gate 	;
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate %%
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate #include "m4.h"
877c478bd9Sstevel@tonic-gate extern wchar_t *pe;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate static int peek(char c, int r1, int r2);
907c478bd9Sstevel@tonic-gate static int peek3(char c1, int rc1, char c2, int rc2, int rc3);
917c478bd9Sstevel@tonic-gate 
92*9fb11590Smike_s int
yylex(void)93*9fb11590Smike_s yylex(void)
94*9fb11590Smike_s {
957c478bd9Sstevel@tonic-gate 	while (*pe == ' ' || *pe == '\t' || *pe == '\n')
967c478bd9Sstevel@tonic-gate 		pe++;
977c478bd9Sstevel@tonic-gate 	switch (*pe) {
987c478bd9Sstevel@tonic-gate 	case '\0':
997c478bd9Sstevel@tonic-gate 	case '+':
1007c478bd9Sstevel@tonic-gate 	case '-':
1017c478bd9Sstevel@tonic-gate 	case '/':
1027c478bd9Sstevel@tonic-gate 	case '%':
1037c478bd9Sstevel@tonic-gate 	case '^':
1047c478bd9Sstevel@tonic-gate 	case '~':
1057c478bd9Sstevel@tonic-gate 	case '(':
1067c478bd9Sstevel@tonic-gate 	case ')':
1077c478bd9Sstevel@tonic-gate 		return (*pe++);
1087c478bd9Sstevel@tonic-gate 	case '*':
1097c478bd9Sstevel@tonic-gate 		return (peek('*', POWER, '*'));
1107c478bd9Sstevel@tonic-gate 	case '>':
1117c478bd9Sstevel@tonic-gate 		return (peek3('=', GE, '>', RSHIFT, GT));
1127c478bd9Sstevel@tonic-gate 	case '<':
1137c478bd9Sstevel@tonic-gate 		return (peek3('=', LE, '<', LSHIFT, LT));
1147c478bd9Sstevel@tonic-gate 	case '=':
1157c478bd9Sstevel@tonic-gate 		return (peek('=', EQ, EQ));
1167c478bd9Sstevel@tonic-gate 	case '|':
1177c478bd9Sstevel@tonic-gate 		return (peek('|', OROR, '|'));
1187c478bd9Sstevel@tonic-gate 	case '&':
1197c478bd9Sstevel@tonic-gate 		return (peek('&', ANDAND, '&'));
1207c478bd9Sstevel@tonic-gate 	case '!':
1217c478bd9Sstevel@tonic-gate 		return (peek('=', NE, '!'));
1227c478bd9Sstevel@tonic-gate 	default: {
123*9fb11590Smike_s 		int	base;
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 		evalval = 0;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 		if (*pe == '0') {
1287c478bd9Sstevel@tonic-gate 			if (*++pe == 'x' || *pe == 'X') {
1297c478bd9Sstevel@tonic-gate 				base = 16;
1307c478bd9Sstevel@tonic-gate 				++pe;
1317c478bd9Sstevel@tonic-gate 			} else
1327c478bd9Sstevel@tonic-gate 				base = 8;
1337c478bd9Sstevel@tonic-gate 		} else
1347c478bd9Sstevel@tonic-gate 			base = 10;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 		for (;;) {
137*9fb11590Smike_s 			int	c, dig;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 			c = *pe;
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 			if (is_digit(c))
1427c478bd9Sstevel@tonic-gate 				dig = c - '0';
1437c478bd9Sstevel@tonic-gate 			else if (c >= 'a' && c <= 'f')
1447c478bd9Sstevel@tonic-gate 				dig = c - 'a' + 10;
1457c478bd9Sstevel@tonic-gate 			else if (c >= 'A' && c <= 'F')
1467c478bd9Sstevel@tonic-gate 				dig = c - 'A' + 10;
1477c478bd9Sstevel@tonic-gate 			else
1487c478bd9Sstevel@tonic-gate 				break;
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 			evalval = evalval*base + dig;
1517c478bd9Sstevel@tonic-gate 			++pe;
1527c478bd9Sstevel@tonic-gate 		}
1537c478bd9Sstevel@tonic-gate 		return (DIGITS);
1547c478bd9Sstevel@tonic-gate 	}
1557c478bd9Sstevel@tonic-gate 	}
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate static int
peek(char c,int r1,int r2)1597c478bd9Sstevel@tonic-gate peek(char c, int r1, int r2)
1607c478bd9Sstevel@tonic-gate {
1617c478bd9Sstevel@tonic-gate 	if (*++pe != c)
1627c478bd9Sstevel@tonic-gate 		return (r2);
1637c478bd9Sstevel@tonic-gate 	++pe;
1647c478bd9Sstevel@tonic-gate 	return (r1);
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate static int
peek3(char c1,int rc1,char c2,int rc2,int rc3)1687c478bd9Sstevel@tonic-gate peek3(char c1, int rc1, char c2, int rc2, int rc3)
1697c478bd9Sstevel@tonic-gate {
1707c478bd9Sstevel@tonic-gate 	++pe;
1717c478bd9Sstevel@tonic-gate 	if (*pe == c1) {
1727c478bd9Sstevel@tonic-gate 		++pe;
1737c478bd9Sstevel@tonic-gate 		return (rc1);
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate 	if (*pe == c2) {
1767c478bd9Sstevel@tonic-gate 		++pe;
1777c478bd9Sstevel@tonic-gate 		return (rc2);
1787c478bd9Sstevel@tonic-gate 	}
1797c478bd9Sstevel@tonic-gate 	return (rc3);
1807c478bd9Sstevel@tonic-gate }
1817c478bd9Sstevel@tonic-gate 
182*9fb11590Smike_s /*ARGSUSED*/
1837c478bd9Sstevel@tonic-gate static void
yyerror(YYCONST char * msg)184*9fb11590Smike_s yyerror(YYCONST char *msg)
1857c478bd9Sstevel@tonic-gate {
1867c478bd9Sstevel@tonic-gate }
187