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 %right '!' '~' 45 %nonassoc GT GE LT LE NE EQ 46 %left '+' '-' 47 %left '*' '/' '%' 48 %right POWER 49 %right UMINUS 50 %% 51 52 s : e = { evalval = $1; } 53 | = { evalval = 0; } 54 ; 55 56 e : e OROR e = { $$ = ($1 != 0 || $3 != 0) ? 1 : 0; } 57 | e ANDAND e = { $$ = ($1 != 0 && $3 != 0) ? 1 : 0; } 58 | '!' e = { $$ = $2 == 0; } 59 | '~' e = { $$ = ~$2; } 60 | e EQ e = { $$ = $1 == $3; } 61 | e NE e = { $$ = $1 != $3; } 62 | e GT e = { $$ = $1 > $3; } 63 | e GE e = { $$ = $1 >= $3; } 64 | e LT e = { $$ = $1 < $3; } 65 | e LE e = { $$ = $1 <= $3; } 66 | e '|' e = { $$ = ($1 | $3); } 67 | e '&' e = { $$ = ($1 & $3); } 68 | e '^' e = { $$ = ($1 ^ $3); } 69 | e '+' 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 ')' = { $$ = ($2); } 75 | e POWER e = { for ($$ = 1; $3-- > 0; $$ *= $1); } 76 | '-' e %prec UMINUS = { $$ = $2-1; $$ = -$2; } 77 | '+' e %prec UMINUS = { $$ = $2-1; $$ = $2; } 78 | DIGITS = { $$ = evalval; } 79 ; 80 81 %% 82 83 #include "m4.h" 84 extern wchar_t *pe; 85 static int peek(int c, int r1, int r2); 86 87 yylex() { 88 while (*pe == ' ' || *pe == '\t' || *pe == '\n') 89 pe++; 90 switch (*pe) { 91 case '\0': 92 case '+': 93 case '-': 94 case '/': 95 case '%': 96 case '^': 97 case '~': 98 case '(': 99 case ')': 100 return (*pe++); 101 case '*': 102 return (peek('*', POWER, '*')); 103 case '>': 104 return (peek('=', GE, GT)); 105 case '<': 106 return (peek('=', LE, LT)); 107 case '=': 108 return (peek('=', EQ, EQ)); 109 case '|': 110 return (peek('|', OROR, '|')); 111 case '&': 112 return (peek('&', ANDAND, '&')); 113 case '!': 114 return (peek('=', NE, '!')); 115 default: { 116 register base; 117 118 evalval = 0; 119 120 if (*pe == '0') { 121 if (*++pe == 'x' || *pe == 'X') { 122 base = 16; 123 ++pe; 124 } else 125 base = 8; 126 } else 127 base = 10; 128 129 for (;;) { 130 register c, dig; 131 132 c = *pe; 133 134 if (is_digit(c)) 135 dig = c - '0'; 136 else if (c >= 'a' && c <= 'f') 137 dig = c - 'a' + 10; 138 else if (c >= 'A' && c <= 'F') 139 dig = c - 'A' + 10; 140 else 141 break; 142 143 evalval = evalval*base + dig; 144 ++pe; 145 } 146 return (DIGITS); 147 } 148 } 149 } 150 151 static int 152 peek(int c, int r1, int r2) 153 { 154 if (*++pe != c) 155 return (r2); 156 ++pe; 157 return (r1); 158 } 159 160 /*VARARGS*/ 161 static void 162 yyerror() 163 { 164 ; 165 } 166